marmotta-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wik...@apache.org
Subject [12/16] git commit: MARMOTTA-508: moved initialization to a ContainerRequestFilter
Date Mon, 21 Jul 2014 13:38:08 GMT
MARMOTTA-508: moved initialization to a ContainerRequestFilter


Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/5d0993b6
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/5d0993b6
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/5d0993b6

Branch: refs/heads/ldp
Commit: 5d0993b66ac29be167c8763f035acd7f35c9640a
Parents: a8781fa
Author: Sergio Fernández <wikier@apache.org>
Authored: Tue Jul 15 11:52:43 2014 +0200
Committer: Sergio Fernández <wikier@apache.org>
Committed: Tue Jul 15 11:52:43 2014 +0200

----------------------------------------------------------------------
 .../marmotta/platform/ldp/api/LdpService.java   | 17 ++++++-
 .../platform/ldp/services/LdpServiceImpl.java   | 43 ++++++++++++++++
 .../platform/ldp/webservices/LdpPreProcess.java | 53 ++++++++++++++++++++
 .../platform/ldp/webservices/LdpWebService.java | 36 ++++---------
 4 files changed, 121 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/5d0993b6/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 e148f0f..46fd6ff 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
@@ -33,6 +33,8 @@ import org.openrdf.rio.RDFParseException;
 
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.Link;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -46,6 +48,8 @@ import java.util.*;
  */
 public interface LdpService {
 
+    public static final Set<URI> SERVER_MANAGED_PROPERTIES = new HashSet<>(Arrays.asList(LDP.contains));
+
     public static enum InteractionModel {
         LDPR(LDP.Resource),
         LDPC(LDP.Container);
@@ -79,7 +83,18 @@ public interface LdpService {
 
     }
 
-    public static final Set<URI> SERVER_MANAGED_PROPERTIES = new HashSet<>(Arrays.asList(LDP.contains));
+    /**
+     * Initializes the root LDP Container
+     *
+     * @param connection repository connection
+     * @param root root container
+     * @throws RepositoryException
+     */
+    void init(RepositoryConnection connection, URI root) throws RepositoryException;
+
+    String getResourceUri(UriInfo uriInfo);
+
+    UriBuilder getResourceUriBuilder(UriInfo uriInfo);
 
     boolean exists(RepositoryConnection connection, String resource) throws RepositoryException;
 

http://git-wip-us.apache.org/repos/asf/marmotta/blob/5d0993b6/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 75053b1..5e88356 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
@@ -35,9 +35,11 @@ import org.apache.marmotta.platform.ldp.patch.model.PatchLine;
 import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
 import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParserImpl;
 import org.apache.marmotta.platform.ldp.util.LdpUtils;
+import org.apache.marmotta.platform.ldp.webservices.LdpWebService;
 import org.openrdf.model.*;
 import org.openrdf.model.impl.ValueFactoryImpl;
 import org.openrdf.model.vocabulary.RDF;
+import org.openrdf.model.vocabulary.RDFS;
 import org.openrdf.repository.RepositoryConnection;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.RepositoryResult;
@@ -51,6 +53,8 @@ import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.Link;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -83,6 +87,45 @@ public class LdpServiceImpl implements LdpService {
         ldpInteractionModelProperty = ValueFactoryImpl.getInstance().createURI(LDP.NAMESPACE,
"interactionModel");
     }
 
+    @Override
+    public void init(RepositoryConnection connection, URI root) throws RepositoryException
{
+        if (!exists(connection, root)) {
+            connection.add(root, RDF.TYPE, LDP.Resource, ldpContext);
+            connection.add(root, RDF.TYPE, LDP.RDFSource, ldpContext);
+            connection.add(root, RDF.TYPE, LDP.Container, ldpContext);
+            connection.add(root, RDF.TYPE, LDP.BasicContainer, ldpContext);
+
+            final ValueFactory valueFactory = connection.getValueFactory();
+            connection.add(root, RDFS.LABEL, valueFactory.createLiteral("Marmotta's LDP Root
Container"), ldpContext);
+            final Literal now = valueFactory.createLiteral(new Date());
+            connection.add(root, DCTERMS.created, now, ldpContext);
+            connection.add(root, DCTERMS.modified, now, ldpContext);
+        }
+    }
+
+    @Override
+    public String getResourceUri(UriInfo uriInfo) {
+        final UriBuilder uriBuilder = getResourceUriBuilder(uriInfo);
+        uriBuilder.path(uriInfo.getPathParameters().getFirst("local"));
+        // uriBuilder.path(uriInfo.getPath().replaceFirst("/$", ""));
+        String uri = uriBuilder.build().toString();
+        log.debug("Request URI: {}", uri);
+        return uri;
+    }
+
+    @Override
+    public UriBuilder getResourceUriBuilder(UriInfo uriInfo) {
+        final UriBuilder uriBuilder;
+        if (configurationService.getBooleanConfiguration("ldp.force_baseuri", false)) {
+            log.trace("UriBuilder is forced to configured baseuri <{}>", configurationService.getBaseUri());
+            uriBuilder = UriBuilder.fromUri(java.net.URI.create(configurationService.getBaseUri()));
+        } else {
+            uriBuilder = uriInfo.getBaseUriBuilder();
+        }
+        uriBuilder.path(LdpWebService.PATH);
+        return uriBuilder;
+    }
+
     private URI buildURI(String resource) {
         return ValueFactoryImpl.getInstance().createURI(resource);
     }

http://git-wip-us.apache.org/repos/asf/marmotta/blob/5d0993b6/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpPreProcess.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpPreProcess.java
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpPreProcess.java
new file mode 100644
index 0000000..342b9b7
--- /dev/null
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpPreProcess.java
@@ -0,0 +1,53 @@
+package org.apache.marmotta.platform.ldp.webservices;
+
+import org.apache.marmotta.platform.core.api.triplestore.SesameService;
+import org.apache.marmotta.platform.ldp.api.LdpService;
+import org.openrdf.model.URI;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.PreMatching;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+import java.io.IOException;
+
+/**
+ * Pre-process interceptor for LDP
+ *
+ * @author Sergio Fernández
+ */
+@PreMatching
+@ApplicationScoped
+public class LdpPreProcess implements ContainerRequestFilter {
+
+    @Inject
+    private LdpService ldpService;
+
+    @Inject
+    private SesameService sesameService;
+
+    @Override
+    public void filter(ContainerRequestContext context) throws IOException {
+        UriInfo uriInfo = context.getUriInfo();
+        UriBuilder resourceUriBuilder = ldpService.getResourceUriBuilder(uriInfo);
+        //TODO avoid the expensive operations once it's initialized
+        try {
+            final RepositoryConnection conn = sesameService.getConnection();
+            try {
+                conn.begin();
+                URI root = conn.getValueFactory().createURI(resourceUriBuilder.build().toString());
+                ldpService.init(conn, root);
+                conn.commit();
+            } finally {
+                conn.close();
+            }
+        } catch (RepositoryException e) {
+            e.printStackTrace();
+            throw new IOException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/marmotta/blob/5d0993b6/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 f4afbf9..02cd5a8 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
@@ -52,7 +52,6 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URISyntaxException;
 import java.util.List;
-import java.util.Set;
 import java.util.UUID;
 
 /**
@@ -67,7 +66,7 @@ import java.util.UUID;
 @Path(LdpWebService.PATH + "{local:.*}")
 public class LdpWebService {
 
-    public static final String PATH = "/ldp"; //FIXME: imho this should be root '/' (jakob)
+    public static final String PATH = "/ldp"; //TODO: at some point this will be root ('/')
in marmotta
     public static final String LDP_SERVER_CONSTRAINTS = "https://wiki.apache.org/marmotta/LDPImplementationReport/2014-03-11";
 
     private Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());
@@ -86,20 +85,20 @@ public class LdpWebService {
 
     @PostConstruct
     protected void initialize() {
-        // TODO: basic initialisation
+        // TODO: basic initialization
         log.info("Starting up LDP WebService Endpoint");
     }
 
     @GET
     public Response GET(@Context final UriInfo uriInfo, @Context Request r, @HeaderParam(HttpHeaders.ACCEPT)
MediaType type) throws RepositoryException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.debug("GET to LDPR <{}>", resource);
         return buildGetResponse(resource, r, type).build();
     }
 
     @HEAD
     public Response HEAD(@Context final UriInfo uriInfo, @Context Request r, @HeaderParam(HttpHeaders.ACCEPT)
MediaType type)  throws RepositoryException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.debug("HEAD to LDPR <{}>", resource);
         return buildGetResponse(resource, r, type).entity(null).build();
     }
@@ -209,7 +208,7 @@ public class LdpWebService {
                          InputStream postBody, @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType
type)
             throws RepositoryException {
 
-        final String container = getResourceUri(uriInfo);
+        final String container = ldpService.getResourceUri(uriInfo);
         log.debug("POST to LDPC <{}>", container);
 
         final RepositoryConnection conn = sesameService.getConnection();
@@ -304,7 +303,7 @@ public class LdpWebService {
                         @HeaderParam(HttpHeaders.IF_MATCH) EntityTag eTag,
                         @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type, InputStream
postBody)
             throws RepositoryException, IOException, InvalidModificationException, RDFParseException,
IncompatibleResourceTypeException, URISyntaxException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.error("PUT to <{}>", resource);
 
         final RepositoryConnection conn = sesameService.getConnection();
@@ -369,7 +368,7 @@ public class LdpWebService {
      */
     @DELETE
     public Response DELETE(@Context UriInfo uriInfo) throws RepositoryException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.debug("DELETE to <{}>", resource);
 
         final RepositoryConnection con = sesameService.getConnection();
@@ -400,7 +399,7 @@ public class LdpWebService {
     public Response PATCH(@Context UriInfo uriInfo,
                           @HeaderParam(HttpHeaders.IF_MATCH) EntityTag eTag,
                           @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type, InputStream
postBody) throws RepositoryException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.debug("PATCH to <{}>", resource);
 
         final RepositoryConnection con = sesameService.getConnection();
@@ -461,7 +460,7 @@ public class LdpWebService {
      */
     @OPTIONS
     public Response OPTIONS(@Context final UriInfo uriInfo) throws RepositoryException {
-        final String resource = getResourceUri(uriInfo);
+        final String resource = ldpService.getResourceUri(uriInfo);
         log.debug("OPTIONS to <{}>", resource);
 
         final RepositoryConnection con = sesameService.getConnection();
@@ -580,21 +579,4 @@ public class LdpWebService {
         return createResponse(connection, status.getStatusCode(), resource);
     }
 
-    protected String getResourceUri(UriInfo uriInfo) {
-        final UriBuilder uriBuilder;
-        if (configurationService.getBooleanConfiguration("ldp.force_baseuri", false)) {
-            log.trace("UriBuilder is forced to configured baseuri <{}>", configurationService.getBaseUri());
-            uriBuilder = UriBuilder.fromUri(java.net.URI.create(configurationService.getBaseUri()));
-        } else {
-            uriBuilder = uriInfo.getBaseUriBuilder();
-        }
-        uriBuilder.path(PATH);
-        uriBuilder.path(uriInfo.getPathParameters().getFirst("local"));
-//        uriBuilder.path(uriInfo.getPath().replaceFirst("/$", ""));
-
-        String uri = uriBuilder.build().toString();
-        log.debug("RequestUri: {}", uri);
-        return uri;
-    }
-
 }


Mime
View raw message