MARMOTTA-449: added also prototype code for returning LDP-BR
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/84409f9b Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/84409f9b Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/84409f9b Branch: refs/heads/develop Commit: 84409f9b820bf95d415f96363d74c19785c4e212 Parents: f59d008 Author: Sergio Fernández <[email protected]> Authored: Sun Mar 2 13:08:05 2014 +0100 Committer: Sergio Fernández <[email protected]> Committed: Sun Mar 2 13:08:05 2014 +0100 ---------------------------------------------------------------------- .../platform/ldp/api/LdpBinaryStoreService.java | 5 +++-- .../marmotta/platform/ldp/api/LdpService.java | 4 ++++ .../ldp/services/LdpBinaryStoreServiceImpl.java | 16 +++++++++++++--- .../platform/ldp/services/LdpServiceImpl.java | 18 ++++++++++++++++++ .../platform/ldp/webservices/LdpWebService.java | 16 +++++++++++++--- 5 files changed, 51 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/84409f9b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpBinaryStoreService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpBinaryStoreService.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpBinaryStoreService.java index 8e5d67d..34ec302 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpBinaryStoreService.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpBinaryStoreService.java @@ -19,6 +19,7 @@ package org.apache.marmotta.platform.ldp.api; import org.openrdf.model.URI; +import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; @@ -33,8 +34,8 @@ public interface LdpBinaryStoreService { boolean store(URI resource, InputStream stream); - InputStream read(String resource); + InputStream read(String resource) throws IOException; - InputStream read(URI resource); + InputStream read(URI resource) throws IOException; } http://git-wip-us.apache.org/repos/asf/marmotta/blob/84409f9b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java index 2d90cb4..7978273 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java @@ -59,6 +59,10 @@ public interface LdpService { void exportResource(RepositoryConnection connection, URI resource, OutputStream output, RDFFormat format) throws RepositoryException, RDFHandlerException; + void exportResource(RepositoryConnection connection, String resource, OutputStream out) throws RepositoryException, IOException; + + void exportResource(RepositoryConnection connection, URI resource, OutputStream out) throws RepositoryException, IOException; + EntityTag generateETag(RepositoryConnection connection, String uri) throws RepositoryException; EntityTag generateETag(RepositoryConnection connection, URI uri) throws RepositoryException; http://git-wip-us.apache.org/repos/asf/marmotta/blob/84409f9b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpBinaryStoreServiceImpl.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpBinaryStoreServiceImpl.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpBinaryStoreServiceImpl.java index 7476336..20741b1 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpBinaryStoreServiceImpl.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpBinaryStoreServiceImpl.java @@ -96,12 +96,22 @@ public class LdpBinaryStoreServiceImpl implements LdpBinaryStoreService { } @Override - public InputStream read(String resource) { - return null; + public InputStream read(String resource) throws IOException { + try { + File file = getFile(resource); + if (!file.exists()) { + throw new IOException("File " + file.getAbsolutePath() + " not found"); + } else { + return new FileInputStream(file); + } + } catch (URISyntaxException e) { + log.error("Error reading resource {}: {}", resource, e.getMessage()); + return null; + } } @Override - public InputStream read(URI resource) { + public InputStream read(URI resource) throws IOException { return read(resource.stringValue()); } http://git-wip-us.apache.org/repos/asf/marmotta/blob/84409f9b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java index 70f1beb..8d1547c 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java @@ -20,6 +20,7 @@ package org.apache.marmotta.platform.ldp.services; import info.aduna.iteration.FilterIteration; import info.aduna.iteration.Iterations; import info.aduna.iteration.UnionIteration; +import org.apache.commons.io.IOUtils; import org.apache.marmotta.commons.vocabulary.DCTERMS; import org.apache.marmotta.commons.vocabulary.LDP; import org.apache.marmotta.platform.core.api.config.ConfigurationService; @@ -127,6 +128,23 @@ public class LdpServiceImpl implements LdpService { } @Override + public void exportResource(RepositoryConnection connection, String resource, OutputStream out) throws RepositoryException, IOException { + //TODO: check (resource, dct:format, type) + InputStream in = binaryStore.read(resource); + if (in != null) { + IOUtils.copy(in, out); + } else { + throw new IOException("Cannot read reosurce " + resource); + } + + } + + @Override + public void exportResource(RepositoryConnection connection, URI resource, OutputStream out) throws RepositoryException, IOException { + exportResource(connection, resource.stringValue(), out); + } + + @Override public boolean addResource(RepositoryConnection connection, String container, String resource, MediaType type, InputStream stream) throws RepositoryException, IOException, RDFParseException { return addResource(connection, buildURI(container), buildURI(resource), type, stream); } http://git-wip-us.apache.org/repos/asf/marmotta/blob/84409f9b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java index e176e21..d9a7f1d 100644 --- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java +++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java @@ -114,9 +114,19 @@ public class LdpWebService { final RDFFormat format = Rio.getWriterFormatForMIMEType(type.toString(), RDFFormat.TURTLE); if (format == null) { - log.warn("GET to <{}> with unknown accept {}", resource, type); - final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_IMPLEMENTED, resource); - con.rollback(); + log.warn("GET to <{}> with non-RDF format {}, so looking for a LDP-BR", resource, type); + final StreamingOutput entity = new StreamingOutput() { + @Override + public void write(OutputStream out) throws IOException, WebApplicationException { + try { + ldpService.exportResource(con, resource, out); + } catch (RepositoryException | IOException e) { + throw new WebApplicationException(e, createResponse(Response.status(Response.Status.INTERNAL_SERVER_ERROR)).entity(e).build()); + } + } + }; + final Response.ResponseBuilder resp = createResponse(con, Response.Status.OK, resource).entity(entity).type(format.getDefaultMIMEType()); + con.commit(); return resp; } else { // Deliver all triples with <subject> as subject.
