marmotta-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wik...@apache.org
Subject [27/50] git commit: MARMOTTA-527: avoid re-using URIs on resource creation (POST) added 410 status code for deleted resources
Date Fri, 19 Sep 2014 07:30:53 GMT
MARMOTTA-527: avoid re-using URIs on resource creation (POST)
added 410 status code for deleted resources


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

Branch: refs/heads/develop
Commit: 7e8b71388257a4a851c76bdc81931297e7ad1803
Parents: df08c53
Author: Jakob Frank <jakob@apache.org>
Authored: Wed Sep 10 09:57:38 2014 +0200
Committer: Jakob Frank <jakob@apache.org>
Committed: Wed Sep 10 10:00:43 2014 +0200

----------------------------------------------------------------------
 .../marmotta/platform/ldp/api/LdpService.java   | 32 ++++++++++++
 .../platform/ldp/services/LdpServiceImpl.java   | 22 +++++---
 .../platform/ldp/webservices/LdpWebService.java | 53 ++++++++++++++------
 .../ldp/webservices/LdpWebServiceTest.java      |  6 +--
 4 files changed, 88 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/7e8b7138/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 46fd6ff..f2ab2e0 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
@@ -96,10 +96,42 @@ public interface LdpService {
 
     UriBuilder getResourceUriBuilder(UriInfo uriInfo);
 
+    /**
+     * Check if the specified resource already exists.
+     * @param connection the repository connection
+     * @param resource the resource to test
+     * @return true if it exists
+     * @throws RepositoryException
+     */
     boolean exists(RepositoryConnection connection, String resource) throws RepositoryException;
 
+    /**
+     * Check if the specified resource already exists.
+     * @param connection the repository connection
+     * @param resource the resource to test
+     * @return true if it exists
+     * @throws RepositoryException
+     */
     boolean exists(RepositoryConnection connection, URI resource) throws RepositoryException;
 
+    /**
+     * Check if the specified resource would be a re-used URI.
+     * @param connection the repository connection
+     * @param resource the resource to test
+     * @return true if it had existed
+     * @throws RepositoryException
+     */
+    boolean isReusedURI(RepositoryConnection connection, String resource) throws RepositoryException;
+
+    /**
+     * Check if the specified resource would be a re-used URI.
+     * @param connection the repository connection
+     * @param resource the resource to test
+     * @return true if it had existed
+     * @throws RepositoryException
+     */
+    boolean isReusedURI(RepositoryConnection connection, URI resource) throws RepositoryException;
+
     boolean hasType(RepositoryConnection connection, URI resource, URI type) throws RepositoryException;
 
     /**

http://git-wip-us.apache.org/repos/asf/marmotta/blob/7e8b7138/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 a001220..a336c6b 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
@@ -80,11 +80,12 @@ public class LdpServiceImpl implements LdpService {
     @Inject
     private LdpBinaryStoreService binaryStore;
 
-    private final URI ldpContext, ldpInteractionModelProperty;
+    private final URI ldpContext, ldpInteractionModelProperty, ldpUsed;
 
     public LdpServiceImpl() {
         ldpContext = ValueFactoryImpl.getInstance().createURI(LDP.NAMESPACE);
         ldpInteractionModelProperty = ValueFactoryImpl.getInstance().createURI(LDP.NAMESPACE,
"interactionModel");
+        ldpUsed = ValueFactoryImpl.getInstance().createURI(LDP.NAMESPACE, "used");
     }
 
     @Override
@@ -97,6 +98,7 @@ public class LdpServiceImpl implements LdpService {
             connection.add(root, RDF.TYPE, LDP.RDFSource, ldpContext);
             connection.add(root, RDF.TYPE, LDP.Container, ldpContext);
             connection.add(root, RDF.TYPE, LDP.BasicContainer, ldpContext);
+            connection.add(root, ldpInteractionModelProperty, InteractionModel.LDPC.getUri(),
ldpContext);
             connection.add(root, DCTERMS.created, now, ldpContext);
             connection.add(root, DCTERMS.modified, now, ldpContext);
         }
@@ -140,6 +142,16 @@ public class LdpServiceImpl implements LdpService {
     }
 
     @Override
+    public boolean isReusedURI(RepositoryConnection connection, String resource) throws RepositoryException
{
+        return isReusedURI(connection, buildURI(resource));
+    }
+
+    @Override
+    public boolean isReusedURI(RepositoryConnection connection, URI resource) throws RepositoryException
{
+        return connection.hasStatement(ldpContext, ldpUsed, resource, true, ldpContext);
+    }
+
+    @Override
     public boolean hasType(RepositoryConnection connection, URI resource, URI type) throws
RepositoryException {
         return connection.hasStatement(resource, RDF.TYPE, type, true, ldpContext);
     }
@@ -589,13 +601,7 @@ public class LdpServiceImpl implements LdpService {
         connection.clear(resource);
 
         // Sec. 5.2.3.11: LDP servers that allow member creation via POST should not re-use
URIs.
-        /* FIXME: the following statement makes problems:
-         *    including this line breakes true deletions of resources (fails our unit-tests)
-         *    removing this line allows re-use of uris and thus fails the ldp-testsuite
-         *   --> maybe use a different namespace? Add a 'deleted' marker and send 410
'Gone' responses?
-         */
-        //connection.add(resource, RDF.TYPE, LDP.Resource, ldpContext);
-        // TODO: keep the track if was there work, but is a good idea?
+        connection.add(ldpContext, ldpUsed, resource, ldpContext);
 
         return true;
     }

http://git-wip-us.apache.org/repos/asf/marmotta/blob/7e8b7138/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 4969c63..a6448f6 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
@@ -164,7 +164,12 @@ public class LdpWebService {
             log.trace("Checking existence of {}", resource);
             if (!ldpService.exists(conn, resource)) {
                 log.debug("{} does not exist", resource);
-                final Response.ResponseBuilder resp = createResponse(conn, Response.Status.NOT_FOUND,
resource);
+                final Response.ResponseBuilder resp;
+                if (ldpService.isReusedURI(conn, resource)) {
+                    resp = createResponse(conn, Response.Status.GONE, resource);
+                } else {
+                    resp = createResponse(conn, Response.Status.NOT_FOUND, resource);
+                }
                 conn.rollback();
                 return resp;
             } else {
@@ -327,6 +332,19 @@ public class LdpWebService {
         try {
             conn.begin();
 
+            if (!ldpService.exists(conn, container)) {
+                final Response.ResponseBuilder resp;
+                if (ldpService.isReusedURI(conn, container)) {
+                    log.debug("<{}> has been deleted, can't POST to it!", container);
+                    resp = createResponse(conn, Response.Status.GONE, container);
+                } else {
+                    log.debug("<{}> does not exists, can't POST to it!", container);
+                    resp = createResponse(conn, Response.Status.NOT_FOUND, container);
+                }
+                conn.rollback();
+                return resp.build();
+            }
+
             // Check that the target container supports the LDPC Interaction Model
             final LdpService.InteractionModel containerModel = ldpService.getInteractionModel(conn,
container);
             if (containerModel != LdpService.InteractionModel.LDPC) {
@@ -361,14 +379,14 @@ public class LdpWebService {
             }
 
             log.trace("Checking possible name clash for new resource <{}>", newResource);
-            if (ldpService.exists(conn, newResource)) {
+            if (ldpService.exists(conn, newResource) || ldpService.isReusedURI(conn, newResource))
{
                 int i = 0;
                 final String base = newResource;
                 do {
                     final String candidate = base + "-" + (++i);
                     log.trace("<{}> already exists, trying <{}>", newResource,
candidate);
                     newResource = candidate;
-                } while (ldpService.exists(conn, newResource));
+                } while (ldpService.exists(conn, newResource) || ldpService.isReusedURI(conn,
newResource));
                 log.debug("resolved name clash, new resource will be <{}>", newResource);
             } else {
                 log.debug("no name clash for <{}>", newResource);
@@ -426,7 +444,7 @@ public class LdpWebService {
                         @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type, InputStream
postBody)
             throws RepositoryException, IOException, InvalidModificationException, RDFParseException,
IncompatibleResourceTypeException, URISyntaxException {
         final String resource = ldpService.getResourceUri(uriInfo);
-        log.error("PUT to <{}>", resource);
+        log.debug("PUT to <{}>", resource);
 
         final RepositoryConnection conn = sesameService.getConnection();
         try {
@@ -461,6 +479,11 @@ public class LdpWebService {
                 resp = createResponse(conn, Response.Status.OK, resource);
                 conn.commit();
                 return resp.build();
+            } else if (ldpService.isReusedURI(conn, resource)) {
+                log.debug("<{}> has beed deleted, we should not re-use the URI!", resource);
+                resp = createResponse(conn, Response.Status.GONE, resource);
+                conn.commit();
+                return resp.build();
             } else {
                 log.debug("<{}> does not exist, so this is a CREATE", resource);
                 //LDP servers may allow resource creation using PUT (Sec. 4.2.4.6)
@@ -486,14 +509,6 @@ public class LdpWebService {
                     conn.commit();
                     return response.entity(e.getMessage()).build();
                 }
-                /*
-                URI uri = conn.getValueFactory().createURI(resource);
-                newResource = ldpService.addResource(conn, LdpUtils.getContainer(uri), uri,
LdpService.InteractionModel.LDPR, mimeType, postBody);
-                log.info("PUT on <{}> created new resource", newResource);
-                resp = createResponse(conn, Response.Status.CREATED, newResource).location(java.net.URI.create(newResource));
-                conn.commit();
-                return resp.build();
-                */
             }
         } catch (IOException | RDFParseException e) {
             final Response.ResponseBuilder resp = createResponse(conn, Response.Status.BAD_REQUEST,
resource).entity(e.getClass().getSimpleName() + ": " + e.getMessage());
@@ -523,7 +538,12 @@ public class LdpWebService {
             con.begin();
 
             if (!ldpService.exists(con, resource)) {
-                final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND,
resource);
+                final Response.ResponseBuilder resp;
+                if (ldpService.isReusedURI(con, resource)) {
+                    resp = createResponse(con, Response.Status.GONE, resource);
+                } else {
+                    resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+                }
                 con.rollback();
                 return resp.build();
             }
@@ -554,7 +574,12 @@ public class LdpWebService {
             con.begin();
 
             if (!ldpService.exists(con, resource)) {
-                final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND,
resource);
+                final Response.ResponseBuilder resp;
+                if (ldpService.isReusedURI(con, resource)) {
+                    resp = createResponse(con, Response.Status.GONE, resource);
+                } else {
+                    resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+                }
                 con.rollback();
                 return resp.build();
             }

http://git-wip-us.apache.org/repos/asf/marmotta/blob/7e8b7138/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
index 418346f..7033669 100644
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
@@ -154,12 +154,12 @@ public class LdpWebServiceTest {
                 .header("Last-Modified", HeaderMatchers.headerNotPresent())
             .delete(newResource);
 
-        // now the new resource does not exist.
+        // now the new resource does not exist any more.
         RestAssured
             .given()
                 .header("Accept", mimeType)
             .expect()
-                .statusCode(404)
+                .statusCode(410)
             .get(newResource);
 
     }
@@ -489,7 +489,7 @@ public class LdpWebServiceTest {
         // now the new resource does not exist.
         RestAssured
             .expect()
-                .statusCode(404)
+                .statusCode(410)
             .get(newResource);
 
     }


Mime
View raw message