You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ja...@apache.org on 2014/02/25 20:50:35 UTC
[1/6] git commit: MARMOTTA-440: implemented HTTP PATCH operation
Repository: marmotta
Updated Branches:
refs/heads/ldp e66e59d75 -> 370a49b68
MARMOTTA-440: implemented HTTP PATCH operation
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/05e03798
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/05e03798
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/05e03798
Branch: refs/heads/ldp
Commit: 05e03798f53469d58bc5543de974b4b1a41d4e98
Parents: e66e59d
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 17:57:42 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 18:01:10 2014 +0100
----------------------------------------------------------------------
.../marmotta/platform/ldp/api/LdpService.java | 4 ++
.../InvalidModificationException.java | 40 +++++++++++++++++
.../patch/InvalidPatchDocumentException.java | 40 +++++++++++++++++
.../platform/ldp/patch/RdfPatchUtil.java | 25 ++++++++---
.../platform/ldp/services/LdpServiceImpl.java | 36 +++++++++++++++
.../platform/ldp/webservices/LdpWebService.java | 46 +++++++++++++++++---
.../platform/ldp/patch/RdfPatchUtilTest.java | 19 ++++++++
.../ldp/webservices/LdpWebServiceTest.java | 6 +--
8 files changed, 199 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/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 887aa8c..3429599 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
@@ -17,6 +17,9 @@
*/
package org.apache.marmotta.platform.ldp.api;
+import org.apache.marmotta.platform.ldp.exceptions.InvalidModificationException;
+import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
+import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryException;
@@ -62,4 +65,5 @@ public interface LdpService {
boolean deleteResource(String resource) throws RepositoryException;
+ void patchResource(String resource, InputStream patchData) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException;
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
new file mode 100644
index 0000000..32e1bde
--- /dev/null
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.ldp.exceptions;
+
+/**
+ * Created by jakob on 2/25/14.
+ */
+public class InvalidModificationException extends Exception {
+ public InvalidModificationException() {
+ super();
+ }
+
+ public InvalidModificationException(String message) {
+ super(message);
+ }
+
+ public InvalidModificationException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvalidModificationException(Throwable cause) {
+ super(cause);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
new file mode 100644
index 0000000..7e5a873
--- /dev/null
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.ldp.patch;
+
+/**
+ * Created by jakob on 2/25/14.
+ */
+public class InvalidPatchDocumentException extends Exception {
+
+ public InvalidPatchDocumentException() {
+ super();
+ }
+
+ public InvalidPatchDocumentException(String message) {
+ super(message);
+ }
+
+ public InvalidPatchDocumentException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvalidPatchDocumentException(Throwable cause) {
+ super(cause);
+ }
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
index be43762..8be7652 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
@@ -18,6 +18,7 @@
package org.apache.marmotta.platform.ldp.patch;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.marmotta.platform.ldp.patch.model.PatchLine;
import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
@@ -39,21 +40,21 @@ import java.util.List;
*/
public class RdfPatchUtil {
- public static void applyPatch(Repository repository, String patch, Resource... contexts) throws RepositoryException, ParseException {
+ public static void applyPatch(Repository repository, String patch, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(repository, getPatch(patch), contexts);
}
- public static void applyPatch(Repository repository, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException {
+ public static void applyPatch(Repository repository, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(repository, getPatch(patchSource), contexts);
}
- public static void applyPatch(RepositoryConnection con, String patch, Resource... contexts) throws RepositoryException, ParseException {
+ public static void applyPatch(RepositoryConnection con, String patch, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(con, getPatch(patch), contexts);
}
- public static void applyPatch(RepositoryConnection con, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException {
+ public static void applyPatch(RepositoryConnection con, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(con, getPatch(patchSource), contexts);
}
- public static void applyPatch(Repository repository, List<PatchLine> patch, Resource... contexts) throws RepositoryException {
+ public static void applyPatch(Repository repository, List<PatchLine> patch, Resource... contexts) throws RepositoryException, InvalidPatchDocumentException {
RepositoryConnection con = repository.getConnection();
try {
con.begin();
@@ -67,7 +68,7 @@ public class RdfPatchUtil {
}
}
- public static void applyPatch(RepositoryConnection con, List<PatchLine> patch, Resource... contexts) throws RepositoryException {
+ public static void applyPatch(RepositoryConnection con, List<PatchLine> patch, Resource... contexts) throws RepositoryException, InvalidPatchDocumentException {
Resource subject = null;
URI predicate = null;
Value object = null;
@@ -78,6 +79,18 @@ public class RdfPatchUtil {
predicate = statement.getPredicate()!=null?statement.getPredicate():predicate;
object = statement.getObject()!=null?statement.getObject():object;
+ if (subject == null || predicate == null || object == null) {
+ if (subject == null) {
+ throw new InvalidPatchDocumentException("Cannot resolve 'R' - subject was never set");
+ }
+ if (predicate == null) {
+ throw new InvalidPatchDocumentException("Cannot resolve 'R' - predicate was never set");
+ }
+ if (object == null) {
+ throw new InvalidPatchDocumentException("Cannot resolve 'R' - object was never set");
+ }
+ }
+
switch (patchLine.getOperator()) {
case ADD:
con.add(subject, predicate, object, contexts);
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/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 b49f9d2..030d930 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
@@ -23,6 +23,12 @@ import org.apache.marmotta.commons.vocabulary.LDP;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.platform.ldp.api.LdpService;
+import org.apache.marmotta.platform.ldp.exceptions.InvalidModificationException;
+import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
+import org.apache.marmotta.platform.ldp.patch.RdfPatchUtil;
+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.RdfPatchParser;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
import org.openrdf.model.Literal;
import org.openrdf.model.Statement;
@@ -235,6 +241,36 @@ public class LdpServiceImpl implements LdpService {
}
@Override
+ public void patchResource(String resource, InputStream patchData) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException {
+ final RepositoryConnection conn = sesameService.getConnection();
+ final URI rUri = conn.getValueFactory().createURI(resource);
+ final URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
+
+ final Literal now = conn.getValueFactory().createLiteral(new Date());
+
+
+ log.trace("parsing patch");
+ List<PatchLine> patch = new RdfPatchParser(patchData).parsePatch();
+
+ // we are allowed to restrict the patch contents (Sec. ???)
+ log.trace("checking for invalid patch statements");
+ for (PatchLine patchLine : patch) {
+ if (LDP.contains.equals(patchLine.getStatement().getPredicate())) {
+ throw new InvalidModificationException("must not change <" + LDP.contains.stringValue() + "> via PATCH");
+ }
+ }
+
+ log.debug("patching <{}> ({} changes)", resource, patch.size());
+
+ RdfPatchUtil.applyPatch(conn, patch, rUri);
+
+ log.trace("update resource meta");
+ conn.remove(rUri, DCTERMS.modified, null, ldpContext);
+ conn.add(rUri, DCTERMS.modified, now, ldpContext);
+
+ }
+
+ @Override
public boolean deleteResource(String resource) throws RepositoryException {
return deleteResource(buildURI(resource));
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/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 9fe07e6..9163b67 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
@@ -21,6 +21,9 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.marmotta.commons.vocabulary.LDP;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.ldp.api.LdpService;
+import org.apache.marmotta.platform.ldp.exceptions.InvalidModificationException;
+import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
+import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
import org.apache.marmotta.platform.ldp.util.EntityTagUtils;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
import org.openrdf.model.Statement;
@@ -163,7 +166,7 @@ public class LdpWebService {
@PUT
public Response PUT(@Context UriInfo uriInfo, @Context Request request,
@HeaderParam(HttpHeaders.IF_MATCH) EntityTag eTag,
- InputStream postBody, @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type)
+ @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type, InputStream postBody)
throws RepositoryException {
/*
* Handle PUT (Sec. 5.5, Sec. 6.5)
@@ -215,13 +218,42 @@ public class LdpWebService {
}
@PATCH
- public Response PATCH(@Context UriInfo uriInfo, InputStream postBody, @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type) {
+ 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);
+ log.debug("PATCH to <{}>", resource);
+
+ if (!ldpService.exists(resource)) {
+ return createResponse(Response.Status.NOT_FOUND, uriInfo).build();
+ }
+
+ if (eTag != null) {
+ // check ETag if present
+ log.trace("Checking If-Match: {}", eTag);
+ EntityTag hasTag = ldpService.generateETag(resource);
+ if (!EntityTagUtils.equals(eTag, hasTag)) {
+ log.trace("If-Match header did not match, expected {}", hasTag);
+ return createResponse(Response.Status.PRECONDITION_FAILED, uriInfo).build();
+ }
+ }
+
// Check for the supported mime-type
if (!type.toString().equals(APPLICATION_RDF_PATCH)) {
- return createResponse(Response.Status.BAD_REQUEST, uriInfo).entity("Unknown Content-Type: " + type + "\n").build();
- };
+ log.trace("Incompatible Content-Type for PATCH: {}", type);
+ return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, uriInfo).entity("Unknown Content-Type: " + type + "\n").build();
+ }
+
+ try {
+ ldpService.patchResource(resource, postBody);
+ } catch (ParseException | InvalidPatchDocumentException e) {
+ return createResponse(Response.Status.BAD_REQUEST, uriInfo).entity(e.getMessage() + "\n").build();
+ } catch (InvalidModificationException e) {
+ return createResponse(422, uriInfo).entity(e.getMessage() + "\n").build();
+ }
+
+ return createResponse(Response.Status.NO_CONTENT, uriInfo).build();
- return Response.status(Response.Status.NOT_IMPLEMENTED).build();
}
@OPTIONS
@@ -237,13 +269,13 @@ public class LdpWebService {
Response.ResponseBuilder builder = createResponse(Response.Status.OK, uriInfo);
// Sec. 5.9.2
- builder.allow("GET", "HEAD", "POST", "OPTIONS");
+ builder.allow("GET", "HEAD", "POST", "PATCH", "OPTIONS");
// Sec. 6.4.14 / Sec. 8.1
// builder.header("Accept-Post", "text/turtle, */*");
builder.header("Accept-Post", "text/turtle");
- // TODO: Sec. 5.8.2
+ // Sec. 5.8.2
builder.header("Accept-Patch", APPLICATION_RDF_PATCH);
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
index ba55337..6094968 100644
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
@@ -18,6 +18,7 @@
package org.apache.marmotta.platform.ldp.patch;
import org.apache.marmotta.commons.vocabulary.FOAF;
+import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -28,6 +29,7 @@ import org.openrdf.model.impl.LiteralImpl;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.sail.memory.MemoryStore;
@@ -95,4 +97,21 @@ public class RdfPatchUtilTest {
con.close();
}
}
+
+ @Test(expected = InvalidPatchDocumentException.class)
+ public void testInvalidPatchDocumentException() throws RepositoryException, ParseException, InvalidPatchDocumentException {
+ RepositoryConnection con = repository.getConnection();
+ try {
+ final String invalidPatch = "A <http://example/foo> R <http://example/bar> .";
+
+ RdfPatchUtil.applyPatch(con, invalidPatch);
+
+ Assert.fail("applyPatch should throw an InvalidPatchDocumentException");
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/05e03798/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 947d594..3e0a3d6 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
@@ -60,10 +60,8 @@ public class LdpWebServiceTest {
}
@Test
- public void test() {
- // Nothing is implemented so far...
- Assert.assertEquals(501, RestAssured.get("/ldp").statusCode());
- Assert.assertEquals(501, RestAssured.get("/ldp/foo/bar/and/some/more").statusCode());
+ public void testCRUD() {
+
}
@AfterClass
[6/6] git commit: MARMOTTA-440: Refactored LdpWebService and
LdpService(Impl) to have only one transaction (RepositoryConnection) per
request.
Posted by ja...@apache.org.
MARMOTTA-440: Refactored LdpWebService and LdpService(Impl) to have only one transaction (RepositoryConnection) per request.
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/370a49b6
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/370a49b6
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/370a49b6
Branch: refs/heads/ldp
Commit: 370a49b68e5493e15c375ba11582d6de9f192f6a
Parents: 7ce3f83
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 20:47:42 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 20:47:42 2014 +0100
----------------------------------------------------------------------
.../marmotta/platform/ldp/api/LdpService.java | 34 +-
.../platform/ldp/services/LdpServiceImpl.java | 280 ++++++-------
.../platform/ldp/webservices/LdpWebService.java | 407 +++++++++++++------
.../platform/ldp/patch/RdfPatchUtilTest.java | 4 +-
.../patch/parser/RdfPatchParserImplTest.java | 91 +++++
.../ldp/patch/parser/RdfPatchParserTest.java | 89 ----
6 files changed, 522 insertions(+), 383 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/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 3429599..bd85565 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
@@ -22,6 +22,7 @@ import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
+import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
@@ -32,6 +33,7 @@ import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Date;
import java.util.List;
/**
@@ -41,29 +43,35 @@ import java.util.List;
*/
public interface LdpService {
- boolean exists(String resource) throws RepositoryException;
+ boolean exists(RepositoryConnection connection, String resource) throws RepositoryException;
- boolean exists(URI resource) throws RepositoryException;
+ boolean exists(RepositoryConnection connection, URI resource) throws RepositoryException;
- boolean addResource(InputStream stream, MediaType type, String container, String resource) throws RepositoryException, IOException, RDFParseException;
+ boolean addResource(RepositoryConnection connection, String container, String resource, MediaType type, InputStream stream) throws RepositoryException, IOException, RDFParseException;
- boolean addResource(InputStream stream, MediaType type, URI container, URI resource) throws RepositoryException, IOException, RDFParseException;
+ boolean addResource(RepositoryConnection connection, URI container, URI resource, MediaType type, InputStream stream) throws RepositoryException, IOException, RDFParseException;
- List<Statement> getStatements(String resource) throws RepositoryException;
+ List<Statement> getStatements(RepositoryConnection connection, String resource) throws RepositoryException;
- List<Statement> getStatements(URI resource) throws RepositoryException;
+ List<Statement> getStatements(RepositoryConnection conn1, URI resource) throws RepositoryException;
- void exportResource(OutputStream output, String resource, RDFFormat format) throws RepositoryException, RDFHandlerException;
+ void exportResource(RepositoryConnection connection, String resource, OutputStream output, RDFFormat format) throws RepositoryException, RDFHandlerException;
- void exportResource(OutputStream output, URI resouce, RDFFormat format) throws RepositoryException, RDFHandlerException;
+ void exportResource(RepositoryConnection connection, URI resource, OutputStream output, RDFFormat format) throws RepositoryException, RDFHandlerException;
- EntityTag generateETag(String uri) throws RepositoryException;
+ EntityTag generateETag(RepositoryConnection connection, String uri) throws RepositoryException;
- EntityTag generateETag(URI uri) throws RepositoryException;
+ EntityTag generateETag(RepositoryConnection connection, URI uri) throws RepositoryException;
- boolean deleteResource(URI resource) throws RepositoryException;
+ boolean deleteResource(RepositoryConnection connection, URI resource) throws RepositoryException;
- boolean deleteResource(String resource) throws RepositoryException;
+ void patchResource(RepositoryConnection connection, URI uri, InputStream patchData, boolean strict) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException;
- void patchResource(String resource, InputStream patchData) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException;
+ boolean deleteResource(RepositoryConnection connection, String resource) throws RepositoryException;
+
+ Date getLastModified(RepositoryConnection connection, String resource) throws RepositoryException;
+
+ Date getLastModified(RepositoryConnection connection, URI uri) throws RepositoryException;
+
+ void patchResource(RepositoryConnection connection, String resource, InputStream patchData, boolean strict) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException;
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/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 421f088..1d99956 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
@@ -17,6 +17,7 @@
*/
package org.apache.marmotta.platform.ldp.services;
+import info.aduna.iteration.Iterations;
import info.aduna.iteration.UnionIteration;
import org.apache.marmotta.commons.vocabulary.DCTERMS;
import org.apache.marmotta.commons.vocabulary.LDP;
@@ -28,7 +29,6 @@ import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
import org.apache.marmotta.platform.ldp.patch.RdfPatchUtil;
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.RdfPatchParser;
import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParserImpl;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
import org.openrdf.model.Literal;
@@ -68,158 +68,122 @@ public class LdpServiceImpl implements LdpService {
@Inject
private ConfigurationService configurationService;
- @Inject
- private SesameService sesameService;
-
private URI buildURI(String resource) {
return ValueFactoryImpl.getInstance().createURI(resource);
}
@Override
- public boolean exists(String resource) throws RepositoryException {
- return exists(buildURI(resource));
+ public boolean exists(RepositoryConnection connection, String resource) throws RepositoryException {
+ return exists(connection, buildURI(resource));
}
@Override
- public boolean exists(URI resource) throws RepositoryException {
- final RepositoryConnection conn = sesameService.getConnection();
- try {
- conn.begin();
- final URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
- return conn.hasStatement(resource, RDF.TYPE, null, true, ldpContext);
- } finally {
- conn.commit();
- conn.close();
- }
+ public boolean exists(RepositoryConnection connection, URI resource) throws RepositoryException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ return connection.hasStatement(resource, RDF.TYPE, null, true, ldpContext);
}
@Override
- public List<Statement> getStatements(String resource) throws RepositoryException {
- return getStatements(buildURI(resource));
+ public List<Statement> getStatements(RepositoryConnection connection, String resource) throws RepositoryException {
+ return getStatements(connection, buildURI(resource));
}
@Override
- public List<Statement> getStatements(URI resource) throws RepositoryException {
- final RepositoryConnection conn = sesameService.getConnection();
- try {
- URI ldp = conn.getValueFactory().createURI(LDP.NAMESPACE);
- return conn.getStatements(resource, RDF.TYPE, null, false, ldp).asList(); //FIXME
- } finally {
- conn.close();
- }
+ public List<Statement> getStatements(RepositoryConnection connection, URI resource) throws RepositoryException {
+ final URI ldp = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ return Iterations.asList(connection.getStatements(resource, RDF.TYPE, null, false, ldp)); //FIXME
}
@Override
- public void exportResource(OutputStream output, String resource, RDFFormat format) throws RepositoryException, RDFHandlerException {
- exportResource(output, buildURI(resource), format);
+ public void exportResource(RepositoryConnection connection, String resource, OutputStream output, RDFFormat format) throws RepositoryException, RDFHandlerException {
+ exportResource(connection, buildURI(resource), output, format);
}
@Override
- public void exportResource(OutputStream output, URI resource, RDFFormat format) throws RepositoryException, RDFHandlerException {
- RepositoryConnection conn = sesameService.getConnection();
+ public void exportResource(RepositoryConnection connection, URI resource, OutputStream output, RDFFormat format) throws RepositoryException, RDFHandlerException {
+ URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ // TODO: this should be a little more sophisticated...
+ // TODO: non-membership triples flag / Prefer-header
+ RDFWriter writer = Rio.createWriter(format, output);
+ UnionIteration<Statement, RepositoryException> union = new UnionIteration<>(
+ connection.getStatements(null, null, null, false, resource),
+ connection.getStatements(resource, null, null, false, ldpContext)
+ );
try {
- conn.begin();
- URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
- // TODO: this should be a little more sophisticated...
- // TODO: non-membership triples flag / Prefer-header
- RDFWriter writer = Rio.createWriter(format, output);
- UnionIteration<Statement, RepositoryException> union = new UnionIteration<>(
- conn.getStatements(null, null, null, false, resource),
- conn.getStatements(resource, null, null, false, ldpContext)
- );
- try {
- LdpWebServiceUtils.exportIteration(writer, resource, union);
- } finally {
- union.close();
- }
- conn.commit();
+ LdpWebServiceUtils.exportIteration(writer, resource, union);
} finally {
- conn.close();
+ union.close();
}
}
@Override
- public boolean addResource(InputStream stream, MediaType type, String container, String resource) throws RepositoryException, IOException, RDFParseException {
- return addResource(stream, type, buildURI(container), buildURI(resource));
+ 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);
}
@Override
- public boolean addResource(InputStream stream, MediaType type, URI container, URI resource) throws RepositoryException, IOException, RDFParseException {
- final RepositoryConnection conn = sesameService.getConnection();
- try {
- final URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
- conn.begin();
-
- log.trace("Checking possible name clash for new resource <{}>", resource.stringValue());
- if (conn.hasStatement(resource, null, null, false, ldpContext)) {
- int i = 0;
- final String base = resource.stringValue();
- do {
- final String candidate = base + "-" + (++i);
- log.trace("<{}> already exists, trying <{}>", resource.stringValue(), candidate);
- resource = conn.getValueFactory().createURI(candidate);
- } while (conn.hasStatement(resource, null, null, false, ldpContext));
- log.debug("resolved name clash, new resource will be <{}>", resource.stringValue());
- } else {
- log.debug("no name clash for <{}>", resource);
- }
+ public boolean addResource(RepositoryConnection connection, URI container, URI resource, MediaType type, InputStream stream) throws RepositoryException, IOException, RDFParseException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+
+ // FIXME: this should be done in LdpWebService
+ log.trace("Checking possible name clash for new resource <{}>", resource.stringValue());
+ if (connection.hasStatement(resource, null, null, false, ldpContext)) {
+ int i = 0;
+ final String base = resource.stringValue();
+ do {
+ final String candidate = base + "-" + (++i);
+ log.trace("<{}> already exists, trying <{}>", resource.stringValue(), candidate);
+ resource = connection.getValueFactory().createURI(candidate);
+ } while (connection.hasStatement(resource, null, null, false, ldpContext));
+ log.debug("resolved name clash, new resource will be <{}>", resource.stringValue());
+ } else {
+ log.debug("no name clash for <{}>", resource);
+ }
- log.debug("POST to <{}> will create new LDP-R <{}>", container, resource);
+ log.debug("POST to <{}> will create new LDP-R <{}>", container, resource);
- // Add container triples (Sec. 6.4.3)
- // container and meta triples!
+ // Add container triples (Sec. 6.4.3)
+ // container and meta triples!
- Literal now = conn.getValueFactory().createLiteral(new Date());
+ Literal now = connection.getValueFactory().createLiteral(new Date());
- conn.add(container, RDF.TYPE, LDP.BasicContainer, ldpContext);
- conn.add(container, LDP.contains, resource, ldpContext);
- conn.remove(container, DCTERMS.modified, null, ldpContext);
- conn.add(container, DCTERMS.modified, now, ldpContext);
+ connection.add(container, RDF.TYPE, LDP.BasicContainer, ldpContext);
+ connection.add(container, LDP.contains, resource, ldpContext);
+ connection.remove(container, DCTERMS.modified, null, ldpContext);
+ connection.add(container, DCTERMS.modified, now, ldpContext);
- conn.add(resource, RDF.TYPE, LDP.Resource, ldpContext);
- conn.add(resource, DCTERMS.created, now, ldpContext);
- conn.add(resource, DCTERMS.modified, now, ldpContext);
+ connection.add(resource, RDF.TYPE, LDP.Resource, ldpContext);
+ connection.add(resource, DCTERMS.created, now, ldpContext);
+ connection.add(resource, DCTERMS.modified, now, ldpContext);
- // LDP-BC for now!
- conn.commit();
+ // TODO: No LDP-BC for now!
- // Add the bodyContent
- log.trace("Content ({}) for new resource <{}>", type, resource);
- final RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(type.toString(), RDFFormat.TURTLE);
- if (rdfFormat == null) {
- log.debug("POST creates new LDP-BR with type {}", type);
- log.warn("LDP-BR not (yet) supported!");
- throw new UnsupportedRDFormatException("No available parser for " + type.toString());
- } else {
- log.debug("POST creates new LDP-RR, data provided as {}", rdfFormat.getName());
- conn.begin();
+ // Add the bodyContent
+ log.trace("Content ({}) for new resource <{}>", type, resource);
+ final RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(type.toString(), RDFFormat.TURTLE);
+ if (rdfFormat == null) {
+ log.debug("POST creates new LDP-BR with type {}", type);
+ log.warn("LDP-BR not (yet) supported!");
+ throw new UnsupportedRDFormatException("No available parser for " + type.toString());
+ } else {
+ log.debug("POST creates new LDP-RR, data provided as {}", rdfFormat.getName());
- // FIXME: We are (are we?) allowed to filter out server-managed properties here
- conn.add(stream, resource.stringValue(), rdfFormat, resource);
+ // FIXME: We are (are we?) allowed to filter out server-managed properties here
+ connection.add(stream, resource.stringValue(), rdfFormat, resource);
- conn.commit();
- return true;
- }
- } catch (final Throwable t) {
- if (conn.isActive()) {
- conn.rollback();
- }
- throw t;
- } finally {
- conn.close();
+ return true;
}
}
@Override
- public EntityTag generateETag(String resource) throws RepositoryException {
- return generateETag(buildURI(resource));
+ public EntityTag generateETag(RepositoryConnection connection, String resource) throws RepositoryException {
+ return generateETag(connection, buildURI(resource));
}
@Override
- public EntityTag generateETag(URI uri) throws RepositoryException {
- final RepositoryConnection conn = sesameService.getConnection();
- final URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
- final RepositoryResult<Statement> stmts = conn.getStatements(uri, DCTERMS.modified, null, true, ldpContext);
+ public EntityTag generateETag(RepositoryConnection connection, URI uri) throws RepositoryException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ final RepositoryResult<Statement> stmts = connection.getStatements(uri, DCTERMS.modified, null, true, ldpContext);
try {
// TODO: ETag is the last-modified date (explicitly managed) thus only weak.
Date latest = null;
@@ -243,12 +207,42 @@ public class LdpServiceImpl implements LdpService {
}
@Override
- public void patchResource(String resource, InputStream patchData) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException {
- final RepositoryConnection conn = sesameService.getConnection();
- final URI rUri = conn.getValueFactory().createURI(resource);
- final URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
+ public Date getLastModified(RepositoryConnection connection, String resource) throws RepositoryException {
+ return getLastModified(connection, buildURI(resource));
+ }
+
+ @Override
+ public Date getLastModified(RepositoryConnection connection, URI uri) throws RepositoryException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ final RepositoryResult<Statement> stmts = connection.getStatements(uri, DCTERMS.modified, null, true, ldpContext);
+ try {
+ // TODO: ETag is the last-modified date (explicitly managed) thus only weak.
+ Date latest = null;
+ while (stmts.hasNext()) {
+ Value o = stmts.next().getObject();
+ if (o instanceof Literal) {
+ Date d = ((Literal)o).calendarValue().toGregorianCalendar().getTime();
+ if (latest == null || d.after(latest)) {
+ latest = d;
+ }
+ }
+ }
+ return latest;
+ } finally {
+ stmts.close();
+ }
+ }
+
+ @Override
+ public void patchResource(RepositoryConnection connection, String resource, InputStream patchData, boolean strict) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException {
+ patchResource(connection, buildURI(resource), patchData, strict);
+ }
- final Literal now = conn.getValueFactory().createLiteral(new Date());
+ @Override
+ public void patchResource(RepositoryConnection connection, URI uri, InputStream patchData, boolean strict) throws RepositoryException, ParseException, InvalidModificationException, InvalidPatchDocumentException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+
+ final Literal now = connection.getValueFactory().createLiteral(new Date());
log.trace("parsing patch");
@@ -262,55 +256,45 @@ public class LdpServiceImpl implements LdpService {
}
}
- log.debug("patching <{}> ({} changes)", resource, patch.size());
+ log.debug("patching <{}> ({} changes)", uri.stringValue(), patch.size());
- RdfPatchUtil.applyPatch(conn, patch, rUri);
+ RdfPatchUtil.applyPatch(connection, patch, uri);
log.trace("update resource meta");
- conn.remove(rUri, DCTERMS.modified, null, ldpContext);
- conn.add(rUri, DCTERMS.modified, now, ldpContext);
+ connection.remove(uri, DCTERMS.modified, null, ldpContext);
+ connection.add(uri, DCTERMS.modified, now, ldpContext);
}
@Override
- public boolean deleteResource(String resource) throws RepositoryException {
- return deleteResource(buildURI(resource));
+ public boolean deleteResource(RepositoryConnection connection, String resource) throws RepositoryException {
+ return deleteResource(connection, buildURI(resource));
}
@Override
- public boolean deleteResource(URI resource) throws RepositoryException {
- final RepositoryConnection conn = sesameService.getConnection();
- try {
- conn.begin();
-
- log.warn("NOT CHECKING EXISTENCE OF <{}>", resource);
-
- URI ldpContext = conn.getValueFactory().createURI(LDP.NAMESPACE);
- Literal now = conn.getValueFactory().createLiteral(new Date());
+ public boolean deleteResource(RepositoryConnection connection, URI resource) throws RepositoryException {
+ final URI ldpContext = connection.getValueFactory().createURI(LDP.NAMESPACE);
+ final Literal now = connection.getValueFactory().createLiteral(new Date());
- // Delete corresponding containment and membership triples (Sec. 6.6.1)
- RepositoryResult<Statement> stmts = conn.getStatements(null, LDP.member, resource, false, ldpContext);
- try {
- while (stmts.hasNext()) {
- Statement st = stmts.next();
- conn.remove(st.getSubject(), DCTERMS.modified, null);
- conn.add(st.getSubject(), DCTERMS.modified, now);
- conn.remove(st);
- }
- } finally {
- stmts.close();
+ // Delete corresponding containment and membership triples (Sec. 6.6.1)
+ RepositoryResult<Statement> stmts = connection.getStatements(null, LDP.member, resource, false, ldpContext);
+ try {
+ while (stmts.hasNext()) {
+ Statement st = stmts.next();
+ connection.remove(st.getSubject(), DCTERMS.modified, null);
+ connection.add(st.getSubject(), DCTERMS.modified, now);
+ connection.remove(st);
}
- // Delete the resource meta
- conn.remove(resource, null, null, ldpContext);
-
- // Delete the resource data
- conn.clear(resource);
-
- conn.commit();
- return true;
} finally {
- conn.close();
+ stmts.close();
}
+ // Delete the resource meta
+ connection.remove(resource, null, null, ldpContext);
+
+ // Delete the resource data
+ connection.clear(resource);
+
+ return true;
}
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/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 9ac0211..25febcb 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
@@ -20,6 +20,7 @@ package org.apache.marmotta.platform.ldp.webservices;
import org.apache.commons.lang3.StringUtils;
import org.apache.marmotta.commons.vocabulary.LDP;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.platform.ldp.api.LdpService;
import org.apache.marmotta.platform.ldp.exceptions.InvalidModificationException;
import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
@@ -27,9 +28,11 @@ import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParser;
import org.apache.marmotta.platform.ldp.util.EntityTagUtils;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
+import org.jboss.resteasy.spi.NoLogWebApplicationException;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
+import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.*;
import org.slf4j.Logger;
@@ -69,6 +72,9 @@ public class LdpWebService {
@Inject
private LdpService ldpService;
+ @Inject
+ private SesameService sesameService;
+
@PostConstruct
protected void initialize() {
// TODO: basic initialisation
@@ -93,33 +99,59 @@ public class LdpWebService {
}
private Response.ResponseBuilder buildGetResponse(final String resource, Request r, MediaType type) throws RepositoryException {
+ final RepositoryConnection con = sesameService.getConnection();
+ try {
+ con.begin();
- if (!ldpService.exists(resource)) {
- return Response.status(Response.Status.NOT_FOUND);
- }
-
- // TODO: Maybe this is a LDP-BR?
- // TODO: Proper content negotiation
+ if (!ldpService.exists(con, resource)) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+ con.rollback();
+ return resp;
+ }
- final RDFFormat format = Rio.getWriterFormatForMIMEType(type.toString(), RDFFormat.TURTLE);
- if (format == null) {
- log.warn("GET to <{}> with unknown accept {}", resource, type);
- return createResponse(Response.Status.NOT_IMPLEMENTED, resource);
- } else {
- // Deliver all triples with <subject> as subject.
- final StreamingOutput entity = new StreamingOutput() {
- @Override
- public void write(OutputStream output) throws IOException, WebApplicationException {
- try {
- ldpService.exportResource(output, resource, format);
- } catch (RDFHandlerException e) {
- throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).build());
- } catch (RepositoryException e) {
- throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).build());
+ // TODO: Maybe this is a LDP-BR?
+ // TODO: Proper content negotiation
+
+ 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();
+ return resp;
+ } else {
+ // Deliver all triples with <subject> as subject.
+ final StreamingOutput entity = new StreamingOutput() {
+ @Override
+ public void write(OutputStream output) throws IOException, WebApplicationException {
+ try {
+ final RepositoryConnection outputCon = sesameService.getConnection();;
+ try {
+ outputCon.begin();
+ ldpService.exportResource(outputCon, resource, output, format);
+ outputCon.commit();
+ } catch (RDFHandlerException e) {
+ outputCon.rollback();
+ throw new NoLogWebApplicationException(e, createResponse(Response.status(Response.Status.INTERNAL_SERVER_ERROR)).entity(e.getMessage()).build());
+ } catch (final Throwable t) {
+ outputCon.rollback();
+ throw t;
+ } finally {
+ outputCon.close();
+ }
+ } catch (RepositoryException e) {
+ throw new WebApplicationException(e, createResponse(Response.status(Response.Status.INTERNAL_SERVER_ERROR)).entity(e).build());
+ }
}
- }
- };
- return createResponse(Response.Status.OK, resource).entity(entity).type(format.getDefaultMIMEType());
+ };
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.OK, resource).entity(entity).type(format.getDefaultMIMEType());
+ con.commit();
+ return resp;
+ }
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
}
}
@@ -136,82 +168,131 @@ public class LdpWebService {
final String container = getResourceUri(uriInfo);
log.debug("POST to LDPC <{}>", container);
- // TODO: Check if resource (container) exists
- log.warn("NOT CHECKING EXISTENCE OF <{}>", container);
-
- final String localName;
- if (StringUtils.isBlank(slug)) {
- /* Sec. 6.4.9) */
- localName = UUID.randomUUID().toString();
- } else {
- // Honor client wishes from Slug-header (Sec. 6.4.11)
- // http://www.ietf.org/rfc/rfc5023.txt
- log.trace("Slug-Header is '{}'", slug);
- localName = LdpWebServiceUtils.urify(slug);
- log.trace("Slug-Header urified: {}", localName);
- }
-
- String newResource = uriInfo.getRequestUriBuilder().path(localName).build().toString();
+ final RepositoryConnection con = sesameService.getConnection();
try {
- ldpService.addResource(postBody, type, container, newResource);
- return createResponse(Response.Status.CREATED, container).location(java.net.URI.create(newResource)).build();
- } catch (IOException | RDFParseException e) {
- return createResponse(Response.Status.BAD_REQUEST, container).entity(e.getClass().getSimpleName() + ": "+ e.getMessage()).build();
- } catch (UnsupportedRDFormatException e) {
- return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, container).entity(e).build();
+ con.begin();
+ // TODO: Check if resource (container) exists
+ log.warn("NOT CHECKING EXISTENCE OF <{}>", container);
+
+ final String localName;
+ if (StringUtils.isBlank(slug)) {
+ /* Sec. 6.4.9) */
+ localName = UUID.randomUUID().toString();
+ } else {
+ // Honor client wishes from Slug-header (Sec. 6.4.11)
+ // http://www.ietf.org/rfc/rfc5023.txt
+ log.trace("Slug-Header is '{}'", slug);
+ localName = LdpWebServiceUtils.urify(slug);
+ log.trace("Slug-Header urified: {}", localName);
+ }
+
+ String newResource = uriInfo.getRequestUriBuilder().path(localName).build().toString();
+ try {
+ ldpService.addResource(con, container, newResource, type, postBody);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.CREATED, container).location(java.net.URI.create(newResource));
+ con.commit();
+ return resp.build();
+ } catch (IOException | RDFParseException e) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.BAD_REQUEST, container).entity(e.getClass().getSimpleName() + ": " + e.getMessage());
+ con.rollback();
+ return resp.build();
+ } catch (UnsupportedRDFormatException e) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.UNSUPPORTED_MEDIA_TYPE, container).entity(e);
+ con.rollback();
+ return resp.build();
+ }
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
}
}
+ /**
+ * Handle PUT (Sec. 5.5, Sec. 6.5)
+ */
@PUT
public Response PUT(@Context UriInfo uriInfo, @Context Request request,
@HeaderParam(HttpHeaders.IF_MATCH) EntityTag eTag,
@HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType type, InputStream postBody)
throws RepositoryException {
- /*
- * Handle PUT (Sec. 5.5, Sec. 6.5)
- */
final String resource = getResourceUri(uriInfo);
log.debug("PUT to <{}>", resource);
- log.warn("NOT CHECKING EXISTENCE OF <{}>", resource);
+ final RepositoryConnection con = sesameService.getConnection();
+ try {
+ con.begin();
- if (eTag == null) {
- // check for If-Match header (ETag) -> 428 Precondition Required (Sec. 5.5.3)
- log.trace("No If-Match header, but that's a MUST");
- return createResponse(428, resource).build();
- } else {
- // check ETag -> 412 Precondition Failed (Sec. 5.5.3)
- log.trace("Checking If-Match: {}", eTag);
- EntityTag hasTag = ldpService.generateETag(resource);
- if (!EntityTagUtils.equals(eTag, hasTag)) {
- log.trace("If-Match header did not match, expected {}", hasTag);
- return createResponse(Response.Status.PRECONDITION_FAILED, resource).build();
+ if (!ldpService.exists(con, resource)) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+ con.rollback();
+ return resp.build();
}
- }
- /*
- * TODO: PUT implementation
- *
- * clients should not be allowed to update LDPC-membership triples -> 409 Conflict (Sec. 6.5.1)
- *
- * if the target resource exists, replace ALL data of the target.
- */
- return createResponse(Response.Status.NOT_IMPLEMENTED, resource).build();
- }
+ if (eTag == null) {
+ // check for If-Match header (ETag) -> 428 Precondition Required (Sec. 5.5.3)
+ log.trace("No If-Match header, but that's a MUST");
+ final Response.ResponseBuilder resp = createResponse(con, 428, resource);
+ con.rollback();
+ return resp.build();
+ } else {
+ // check ETag -> 412 Precondition Failed (Sec. 5.5.3)
+ log.trace("Checking If-Match: {}", eTag);
+ EntityTag hasTag = ldpService.generateETag(con, resource);
+ if (!EntityTagUtils.equals(eTag, hasTag)) {
+ log.trace("If-Match header did not match, expected {}", hasTag);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.PRECONDITION_FAILED, resource);
+ con.rollback();
+ return resp.build();
+ }
+ }
+ /*
+ * TODO: PUT implementation
+ *
+ * clients should not be allowed to update LDPC-membership triples -> 409 Conflict (Sec. 6.5.1)
+ *
+ * if the target resource exists, replace ALL data of the target.
+ */
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_IMPLEMENTED, resource);
+ con.rollback();
+ return resp.build();
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ }
+ }
+ /**
+ * Handle delete (Sec. 5.6, Sec. 6.6)
+ */
@DELETE
- public Response DELETE(@Context UriInfo uriInfo) {
- /*
- * Handle delete (Sec. 5.6, Sec. 6.6)
- */
+ public Response DELETE(@Context UriInfo uriInfo) throws RepositoryException {
final String resource = getResourceUri(uriInfo);
log.debug("DELETE to <{}>", resource);
+
+ final RepositoryConnection con = sesameService.getConnection();
try {
- ldpService.deleteResource(resource);
- return createResponse(Response.Status.NO_CONTENT, resource).build();
- } catch (RepositoryException e) {
+ con.begin();
+
+ if (!ldpService.exists(con, resource)) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+ con.rollback();
+ return resp.build();
+ }
+
+ ldpService.deleteResource(con, resource);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NO_CONTENT, resource);
+ con.commit();
+ return resp.build();
+ } catch (final Throwable e) {
log.error("Error deleting LDP-R: {}: {}", resource, e.getMessage());
- return createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).entity("Error deleting LDP-R: " + e.getMessage()).build();
+ con.rollback();
+ throw e;
+ } finally {
+ con.close();
}
}
@@ -223,85 +304,126 @@ public class LdpWebService {
final String resource = getResourceUri(uriInfo);
log.debug("PATCH to <{}>", resource);
- if (!ldpService.exists(resource)) {
- return createResponse(Response.Status.NOT_FOUND, resource).build();
- }
+ final RepositoryConnection con = sesameService.getConnection();
+ try {
+ con.begin();
- if (eTag != null) {
- // check ETag if present
- log.trace("Checking If-Match: {}", eTag);
- EntityTag hasTag = ldpService.generateETag(resource);
- if (!EntityTagUtils.equals(eTag, hasTag)) {
- log.trace("If-Match header did not match, expected {}", hasTag);
- return createResponse(Response.Status.PRECONDITION_FAILED, resource).build();
+ if (!ldpService.exists(con, resource)) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NOT_FOUND, resource);
+ con.rollback();
+ return resp.build();
}
- }
- // Check for the supported mime-type
- if (!type.toString().equals(RdfPatchParser.MIME_TYPE)) {
- log.trace("Incompatible Content-Type for PATCH: {}", type);
- return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, resource).entity("Unknown Content-Type: " + type + "\n").build();
- }
+ if (eTag != null) {
+ // check ETag if present
+ log.trace("Checking If-Match: {}", eTag);
+ EntityTag hasTag = ldpService.generateETag(con, resource);
+ if (!EntityTagUtils.equals(eTag, hasTag)) {
+ log.trace("If-Match header did not match, expected {}", hasTag);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.PRECONDITION_FAILED, resource);
+ con.rollback();
+ return resp.build();
+ }
+ }
- try {
- ldpService.patchResource(resource, postBody);
- } catch (ParseException | InvalidPatchDocumentException e) {
- return createResponse(Response.Status.BAD_REQUEST, resource).entity(e.getMessage() + "\n").build();
- } catch (InvalidModificationException e) {
- return createResponse(422, resource).entity(e.getMessage() + "\n").build();
- }
+ // Check for the supported mime-type
+ if (!type.toString().equals(RdfPatchParser.MIME_TYPE)) {
+ log.trace("Incompatible Content-Type for PATCH: {}", type);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.UNSUPPORTED_MEDIA_TYPE, resource).entity("Unknown Content-Type: " + type + "\n");
+ con.rollback();
+ return resp.build();
+ }
- return createResponse(Response.Status.NO_CONTENT, resource).build();
+ try {
+ ldpService.patchResource(con, resource, postBody, false);
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.NO_CONTENT, resource);
+ con.commit();
+ return resp.build();
+ } catch (ParseException | InvalidPatchDocumentException e) {
+ final Response.ResponseBuilder resp = createResponse(con, Response.Status.BAD_REQUEST, resource).entity(e.getMessage() + "\n");
+ con.rollback();
+ return resp.build();
+ } catch (InvalidModificationException e) {
+ final Response.ResponseBuilder resp = createResponse(con, 422, resource).entity(e.getMessage() + "\n");
+ con.rollback();
+ return resp.build();
+ }
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ }
}
+ /**
+ * Handle OPTIONS (Sec. 5.9, Sec. 6.9)
+ */
@OPTIONS
- public Response OPTIONS(@Context final UriInfo uriInfo) {
- /*
- * Handle OPTIONS (Sec. 5.9, Sec. 6.9)
- */
+ public Response OPTIONS(@Context final UriInfo uriInfo) throws RepositoryException {
final String resource = getResourceUri(uriInfo);
log.debug("OPTIONS to <{}>", resource);
- log.warn("NOT CHECKING EXISTENCE OF <{}>", resource);
+ final RepositoryConnection con = sesameService.getConnection();
+ try {
+ con.begin();
+
+ log.warn("NOT CHECKING EXISTENCE OF <{}>", resource);
- Response.ResponseBuilder builder = createResponse(Response.Status.OK, resource);
+ Response.ResponseBuilder builder = createResponse(con, Response.Status.OK, resource);
- // Sec. 5.9.2
- builder.allow("GET", "HEAD", "POST", "PATCH", "OPTIONS");
+ // Sec. 5.9.2
+ builder.allow("GET", "HEAD", "POST", "PATCH", "OPTIONS");
- // Sec. 6.4.14 / Sec. 8.1
- // builder.header("Accept-Post", "text/turtle, */*");
- builder.header("Accept-Post", "text/turtle");
+ // Sec. 6.4.14 / Sec. 8.1
+ // builder.header("Accept-Post", "text/turtle, */*");
+ builder.header("Accept-Post", "text/turtle");
- // Sec. 5.8.2
- builder.header("Accept-Patch", RdfPatchParser.MIME_TYPE);
+ // Sec. 5.8.2
+ builder.header("Accept-Patch", RdfPatchParser.MIME_TYPE);
- // TODO: Sec. 6.9.1
- //builder.link(resource, "meta");
+ // TODO: Sec. 6.9.1
+ //builder.link(resource, "meta");
+ con.commit();
+ return builder.build();
+ } catch (final Throwable t) {
+ con.rollback();
+ throw t;
+ } finally {
+ con.close();
+ }
- return builder.build();
}
- protected Response.ResponseBuilder createResponse(int status, String resource) {
- return createResponse(Response.status(status), resource);
+ /**
+ * Add all the default headers specified in LDP to the Response
+ *
+ * @param connection
+ * @param status the status code
+ * @param resource the uri/url of the resouce
+ * @return the provided ResponseBuilder for chaining
+ */
+ protected Response.ResponseBuilder createResponse(RepositoryConnection connection, int status, String resource) throws RepositoryException {
+ return createResponse(connection, Response.status(status), resource);
}
/**
* Add all the default headers specified in LDP to the Response
+ *
+ * @param connection
* @param rb the ResponseBuilder
* @param resource the uri/url of the resouce
* @return the provided ResponseBuilder for chaining
*/
- protected Response.ResponseBuilder createResponse(Response.ResponseBuilder rb, String resource) {
+ protected Response.ResponseBuilder createResponse(RepositoryConnection connection, Response.ResponseBuilder rb, String resource) throws RepositoryException {
+ createResponse(rb);
- // Link rel='describedby' (Sec. 5.2.11)
- rb.link("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby");
-
- try {
- List<Statement> statements = ldpService.getStatements(resource);
+ if (ldpService.exists(connection, resource)) {
+ // Link rel='type' (Sec. 5.2.8, 6.2.8)
+ List<Statement> statements = ldpService.getStatements(connection, resource);
for (Statement stmt : statements) {
Value o = stmt.getObject();
if (o instanceof URI && o.stringValue().startsWith(LDP.NAMESPACE)) {
@@ -310,16 +432,37 @@ public class LdpWebService {
}
// ETag (Sec. 5.2.7)
- rb.tag(ldpService.generateETag(resource));
- } catch (RepositoryException e) {
- log.error("Could not set ldp-response headers", e);
+ rb.tag(ldpService.generateETag(connection, resource));
+
+ // Last modified date
+ rb.lastModified(ldpService.getLastModified(connection, resource));
}
return new RB(rb);
}
- protected Response.ResponseBuilder createResponse(Response.Status status, String resource) {
- return createResponse(status.getStatusCode(), resource);
+ /**
+ * Add the non-resource related headers specified in LDP to the provided ResponseBuilder
+ * @param rb the ResponseBuilder to decorate
+ * @return the updated ResponseBuilder for chaining
+ */
+ private Response.ResponseBuilder createResponse(Response.ResponseBuilder rb) {
+ // Link rel='describedby' (Sec. 5.2.11)
+ rb.link("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby");
+
+ return rb;
+ }
+
+ /**
+ * Add all the default headers specified in LDP to the Response
+ *
+ * @param connection
+ * @param status the StatusCode
+ * @param resource the uri/url of the resouce
+ * @return the provided ResponseBuilder for chaining
+ */
+ protected Response.ResponseBuilder createResponse(RepositoryConnection connection, Response.Status status, String resource) throws RepositoryException {
+ return createResponse(connection, status.getStatusCode(), resource);
}
protected String getResourceUri(UriInfo uriInfo) {
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
index 6094968..80a42c3 100644
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtilTest.java
@@ -35,7 +35,9 @@ import org.openrdf.rio.RDFFormat;
import org.openrdf.sail.memory.MemoryStore;
/**
- * Created by jakob on 2/25/14.
+ * Testing RdfPatchUtil
+ *
+ * @author Jakob Frank
*/
public class RdfPatchUtilTest {
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserImplTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserImplTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserImplTest.java
new file mode 100644
index 0000000..bb3ad45
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserImplTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.ldp.patch.parser;
+
+import org.apache.marmotta.commons.vocabulary.FOAF;
+import org.apache.marmotta.platform.ldp.patch.model.PatchLine;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.openrdf.model.*;
+import org.openrdf.model.impl.LiteralImpl;
+import org.openrdf.model.impl.URIImpl;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Testing the RdfPatchParserImpl
+ *
+ * @author Jakob Frank
+ */
+public class RdfPatchParserImplTest {
+
+
+ private RdfPatchParserImpl parser;
+ private URI alice, bob, charlie;
+ private Literal lcBob, ucBob;
+
+ @Before
+ public void setUp() {
+ parser = new RdfPatchParserImpl(this.getClass().getResourceAsStream("/illustrative.rdfp"));
+
+ alice = new URIImpl("http://example/alice");
+ bob = new URIImpl("http://example/bob");
+ charlie = new URIImpl("http://example/charlie");
+
+ lcBob = new LiteralImpl("bob");
+ ucBob = new LiteralImpl("Bob");
+ }
+
+ @After
+ public void tearDown() {
+ parser = null;
+ }
+
+
+ @Test
+ public void testParsing() throws ParseException {
+ List<PatchLine> patchLines = parser.parsePatch();
+
+ Iterator<PatchLine> it = patchLines.iterator();
+
+ Assert.assertTrue(it.hasNext());
+ checkPatchLine(it.next(), PatchLine.Operator.DELETE, bob, FOAF.name, lcBob);
+
+ Assert.assertTrue(it.hasNext());
+ checkPatchLine(it.next(), PatchLine.Operator.ADD, bob, FOAF.name, ucBob);
+
+ Assert.assertTrue(it.hasNext());
+ checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.knows, alice);
+
+ Assert.assertTrue(it.hasNext());
+ checkPatchLine(it.next(), PatchLine.Operator.DELETE, null, null, charlie);
+ }
+
+ private void checkPatchLine(PatchLine line, PatchLine.Operator operator, Resource subejct, URI predicate, Value object) {
+ Assert.assertEquals("Wrong patch operation", operator, line.getOperator());
+
+ Statement statement = line.getStatement();
+ Assert.assertEquals("Wrong subject", subejct, statement.getSubject());
+ Assert.assertEquals("Wrong predicate", predicate, statement.getPredicate());
+ Assert.assertEquals("Wrong object", object, statement.getObject());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/370a49b6/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
deleted file mode 100644
index b543061..0000000
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.marmotta.platform.ldp.patch.parser;
-
-import org.apache.marmotta.commons.vocabulary.FOAF;
-import org.apache.marmotta.platform.ldp.patch.model.PatchLine;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.openrdf.model.*;
-import org.openrdf.model.impl.LiteralImpl;
-import org.openrdf.model.impl.URIImpl;
-
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Created by jakob on 2/24/14.
- */
-public class RdfPatchParserTest {
-
-
- private RdfPatchParser parser;
- private URI alice, bob, charlie;
- private Literal lcBob, ucBob;
-
- @Before
- public void setUp() {
- parser = new RdfPatchParser(this.getClass().getResourceAsStream("/illustrative.rdfp"));
-
- alice = new URIImpl("http://example/alice");
- bob = new URIImpl("http://example/bob");
- charlie = new URIImpl("http://example/charlie");
-
- lcBob = new LiteralImpl("bob");
- ucBob = new LiteralImpl("Bob");
- }
-
- @After
- public void tearDown() {
- parser = null;
- }
-
-
- @Test
- public void testParsing() throws ParseException {
- List<PatchLine> patchLines = parser.parsePatch();
-
- Iterator<PatchLine> it = patchLines.iterator();
-
- Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.DELETE, bob, FOAF.name, lcBob);
-
- Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.ADD, bob, FOAF.name, ucBob);
-
- Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.knows, alice);
-
- Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.DELETE, null, null, charlie);
- }
-
- private void checkPatchLine(PatchLine line, PatchLine.Operator operator, Resource subejct, URI predicate, Value object) {
- Assert.assertEquals("Wrong patch operation", operator, line.getOperator());
-
- Statement statement = line.getStatement();
- Assert.assertEquals("Wrong subject", subejct, statement.getSubject());
- Assert.assertEquals("Wrong predicate", predicate, statement.getPredicate());
- Assert.assertEquals("Wrong object", object, statement.getObject());
- }
-
-}
[5/6] git commit: MARMOTTA-440: Handle UnsupportedMediaType for HTTP
POST
Posted by ja...@apache.org.
MARMOTTA-440: Handle UnsupportedMediaType for HTTP POST
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/7ce3f833
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/7ce3f833
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/7ce3f833
Branch: refs/heads/ldp
Commit: 7ce3f83361f3ef6408e53c74f69bde052d47361c
Parents: 30302e3
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 19:26:58 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 19:26:58 2014 +0100
----------------------------------------------------------------------
.../marmotta/platform/ldp/webservices/LdpWebService.java | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/7ce3f833/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 f702aea..9ac0211 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
@@ -31,10 +31,7 @@ import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.repository.RepositoryException;
-import org.openrdf.rio.RDFFormat;
-import org.openrdf.rio.RDFHandlerException;
-import org.openrdf.rio.RDFParseException;
-import org.openrdf.rio.Rio;
+import org.openrdf.rio.*;
import org.slf4j.Logger;
import javax.annotation.PostConstruct;
@@ -160,6 +157,8 @@ public class LdpWebService {
return createResponse(Response.Status.CREATED, container).location(java.net.URI.create(newResource)).build();
} catch (IOException | RDFParseException e) {
return createResponse(Response.Status.BAD_REQUEST, container).entity(e.getClass().getSimpleName() + ": "+ e.getMessage()).build();
+ } catch (UnsupportedRDFormatException e) {
+ return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, container).entity(e).build();
}
}
[3/6] git commit: MARMOTTA-440: Some minor refactoring and javadoc
Posted by ja...@apache.org.
MARMOTTA-440: Some minor refactoring and javadoc
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/679ac561
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/679ac561
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/679ac561
Branch: refs/heads/ldp
Commit: 679ac561fde85a139d2ac60411798b518a237cd5
Parents: ca0d8c0
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 19:11:12 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 19:11:12 2014 +0100
----------------------------------------------------------------------
.../InvalidModificationException.java | 7 +-
.../patch/InvalidPatchDocumentException.java | 3 +-
.../platform/ldp/patch/RdfPatchUtil.java | 73 ++++++++++++++++----
.../platform/ldp/patch/model/PatchLine.java | 15 ++--
.../ldp/patch/model/WildcardStatement.java | 3 +-
.../ldp/patch/parser/RdfPatchParser.java | 47 +++++++++++++
.../platform/ldp/services/LdpServiceImpl.java | 4 +-
.../platform/ldp/util/LdpWebServiceUtils.java | 12 +++-
.../platform/ldp/webservices/LdpWebService.java | 7 +-
.../platform/ldp/webservices/PATCH.java | 18 ++++-
.../marmotta-ldp/src/main/javacc/rdf-patch.jj | 6 +-
.../ldp/patch/parser/RdfPatchParserTest.java | 4 +-
12 files changed, 164 insertions(+), 35 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
index 32e1bde..e529ae8 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/exceptions/InvalidModificationException.java
@@ -18,9 +18,14 @@
package org.apache.marmotta.platform.ldp.exceptions;
/**
- * Created by jakob on 2/25/14.
+ * Exception to be thrown if a client operation tries to modify server-controlled data, and the operation should not be ignored.
+ *
+ * @author Jakob Frank
+ *
+ * @see <a href="https://dvcs.w3.org/hg/ldpwg/raw-file/default/ldp.html#ldpr-put-servermanagedprops">LDP Spec, Sec. 5.5.2</a>
*/
public class InvalidModificationException extends Exception {
+
public InvalidModificationException() {
super();
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
index 7e5a873..a5128a7 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/InvalidPatchDocumentException.java
@@ -18,7 +18,8 @@
package org.apache.marmotta.platform.ldp.patch;
/**
- * Created by jakob on 2/25/14.
+ * Exception thrown if a PatchDocument (i.e. a List of {@link org.apache.marmotta.platform.ldp.patch.model.PatchLine}s)
+ * cannot be evauated, e.g. because a wildcard ({@code R}) was never defined before.
*/
public class InvalidPatchDocumentException extends Exception {
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
index 8be7652..e4d073b 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/RdfPatchUtil.java
@@ -18,11 +18,10 @@
package org.apache.marmotta.platform.ldp.patch;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.ObjectUtils;
-import org.apache.commons.lang3.StringUtils;
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.RdfPatchParser;
+import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParserImpl;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
@@ -36,24 +35,67 @@ import java.io.InputStream;
import java.util.List;
/**
- * Created by jakob on 2/25/14.
+ * RdfPatchUtil - Util-Class to apply rdf-patches on a {@link Repository} or {@link org.openrdf.repository.RepositoryConnection}
+ *
+ * @author Jakob Frank
*/
public class RdfPatchUtil {
+ /**
+ * Apply the provided patch to the repository
+ * @param repository the {@link org.openrdf.repository.Repository} to patch
+ * @param patch the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws ParseException if the patch could not be parsed
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
public static void applyPatch(Repository repository, String patch, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(repository, getPatch(patch), contexts);
}
+ /**
+ * Apply the provided patch to the repository
+ * @param repository the {@link org.openrdf.repository.Repository} to patch
+ * @param patchSource the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws ParseException if the patch could not be parsed
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
public static void applyPatch(Repository repository, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
applyPatch(repository, getPatch(patchSource), contexts);
}
- public static void applyPatch(RepositoryConnection con, String patch, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
- applyPatch(con, getPatch(patch), contexts);
+
+ /**
+ * Apply the provided patch to the repository
+ * @param connection the {@link org.openrdf.repository.RepositoryConnection} to patch
+ * @param patch the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws ParseException if the patch could not be parsed
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
+ public static void applyPatch(RepositoryConnection connection, String patch, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
+ applyPatch(connection, getPatch(patch), contexts);
}
- public static void applyPatch(RepositoryConnection con, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
- applyPatch(con, getPatch(patchSource), contexts);
+
+ /**
+ * Apply the provided patch to the repository
+ * @param connection the {@link org.openrdf.repository.RepositoryConnection} to patch
+ * @param patchSource the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws ParseException if the patch could not be parsed
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
+ public static void applyPatch(RepositoryConnection connection, InputStream patchSource, Resource... contexts) throws RepositoryException, ParseException, InvalidPatchDocumentException {
+ applyPatch(connection, getPatch(patchSource), contexts);
}
+ /**
+ * Apply the provided patch to the repository
+ * @param repository the {@link org.openrdf.repository.Repository} to patch
+ * @param patch the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
public static void applyPatch(Repository repository, List<PatchLine> patch, Resource... contexts) throws RepositoryException, InvalidPatchDocumentException {
RepositoryConnection con = repository.getConnection();
try {
@@ -68,7 +110,14 @@ public class RdfPatchUtil {
}
}
- public static void applyPatch(RepositoryConnection con, List<PatchLine> patch, Resource... contexts) throws RepositoryException, InvalidPatchDocumentException {
+ /**
+ * Apply the provided patch to the repository
+ * @param connection the {@link org.openrdf.repository.RepositoryConnection} to patch
+ * @param patch the patch to apply
+ * @param contexts restrict changes to these contexts (leave empty to apply to <em>all</em> contexts)
+ * @throws InvalidPatchDocumentException if the patch is invalid
+ */
+ public static void applyPatch(RepositoryConnection connection, List<PatchLine> patch, Resource... contexts) throws RepositoryException, InvalidPatchDocumentException {
Resource subject = null;
URI predicate = null;
Value object = null;
@@ -93,10 +142,10 @@ public class RdfPatchUtil {
switch (patchLine.getOperator()) {
case ADD:
- con.add(subject, predicate, object, contexts);
+ connection.add(subject, predicate, object, contexts);
break;
- case DEL:
- con.remove(subject, predicate, object, contexts);
+ case DELETE:
+ connection.remove(subject, predicate, object, contexts);
break;
default:
throw new IllegalArgumentException("Unknown patch operation: " + patchLine.getOperator());
@@ -105,7 +154,7 @@ public class RdfPatchUtil {
}
private static List<PatchLine> getPatch(InputStream is) throws ParseException {
- RdfPatchParser parser = new RdfPatchParser(is);
+ RdfPatchParser parser = new RdfPatchParserImpl(is);
return parser.parsePatch();
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java
index 4e0da28..83c6651 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/PatchLine.java
@@ -20,19 +20,25 @@ package org.apache.marmotta.platform.ldp.patch.model;
import org.openrdf.model.Statement;
/**
- * Created by jakob on 2/24/14.
+ * A single PatchLine, i.e. a operation of a Patch.
+ * Can be either a {@code ADD} od {@code DELETE}
*/
public class PatchLine {
public enum Operator {
- DEL("D"),
- ADD("A");
+ ADD("A"),
+ DELETE("D");
+
private final String cmd;
- Operator(String cmd) {
+ private Operator(String cmd) {
this.cmd = cmd;
}
+ public String getCommand() {
+ return cmd;
+ }
+
public static Operator fromCommand(String cmd) {
for (Operator op: values()) {
if (op.cmd.equalsIgnoreCase(cmd)) {
@@ -48,7 +54,6 @@ public class PatchLine {
private final Statement statement;
public PatchLine(Operator operator, Statement statement) {
-
this.operator = operator;
this.statement = statement;
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java
index a243d34..4180a23 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/model/WildcardStatement.java
@@ -24,7 +24,8 @@ import org.openrdf.model.URI;
import org.openrdf.model.Value;
/**
- * Created by jakob on 2/25/14.
+ * The Statement in a RdfPatch.
+ * {@code null}-Values represent {@code R}epeat from the <a href="http://afs.github.io/rdf-patch/#rdf-patch-details">Spec</a>.
*/
public class WildcardStatement implements Statement {
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParser.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParser.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParser.java
new file mode 100644
index 0000000..d78d7c5
--- /dev/null
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParser.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.marmotta.platform.ldp.patch.parser;
+
+import org.apache.marmotta.platform.ldp.patch.model.PatchLine;
+
+import java.util.List;
+
+/**
+ * Parser for the {@code application/rdf-patch} format.
+ *
+ * @see <a href="http://afs.github.io/rdf-patch/">http://afs.github.io/rdf-patch/</a>
+ */
+public interface RdfPatchParser {
+
+ /**
+ * Mime-Type: {@value}
+ */
+ public static final String MIME_TYPE = "application/rdf-patch";
+
+ /**
+ * Default File extension for rdf-patch: {@value}
+ */
+ public static final String FILE_EXTENSION = "rdfp";
+
+ /**
+ * Parse the rdf-patch
+ * @return a List of {@link org.apache.marmotta.platform.ldp.patch.model.PatchLine}s
+ * @throws ParseException if the patch could not be parsed.
+ */
+ public List<PatchLine> parsePatch() throws ParseException;
+}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/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 030d930..421f088 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
@@ -29,6 +29,7 @@ import org.apache.marmotta.platform.ldp.patch.RdfPatchUtil;
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.RdfPatchParser;
+import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParserImpl;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
import org.openrdf.model.Literal;
import org.openrdf.model.Statement;
@@ -57,6 +58,7 @@ import java.util.List;
* LDP Service default implementation
*
* @author Sergio Fernández
+ * @author Jakob Frank
*/
@ApplicationScoped
public class LdpServiceImpl implements LdpService {
@@ -250,7 +252,7 @@ public class LdpServiceImpl implements LdpService {
log.trace("parsing patch");
- List<PatchLine> patch = new RdfPatchParser(patchData).parsePatch();
+ List<PatchLine> patch = new RdfPatchParserImpl(patchData).parsePatch();
// we are allowed to restrict the patch contents (Sec. ???)
log.trace("checking for invalid patch statements");
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java
index a537538..1116879 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpWebServiceUtils.java
@@ -31,14 +31,14 @@ import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
/**
- * Created by jakob on 2/18/14.
+ * Various Util-Methods for the {@link org.apache.marmotta.platform.ldp.webservices.LdpWebService}.
*/
public class LdpWebServiceUtils {
/**
* Urify the Slug: header value, i.e. replace all non-url chars with a single dash.
*
- * @param slugHeaderValue
+ * @param slugHeaderValue the client-provided Slug-header
* @return the slugHeaderValue "urified"
*/
public static String urify(String slugHeaderValue) {
@@ -47,6 +47,14 @@ public class LdpWebServiceUtils {
.replaceAll("[^\\w]+", "-");
}
+ /**
+ * LDP-Style to serialize a resource.
+ * @param writer the writer to serialize to
+ * @param subject the resource to serialize
+ * @param iteration the Iteration containing the data
+ * @throws RDFHandlerException
+ * @throws RepositoryException
+ */
public static void exportIteration(RDFWriter writer, URI subject, CloseableIteration<Statement, RepositoryException> iteration) throws RDFHandlerException, RepositoryException {
writer.startRDF();
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/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 9163b67..32994db 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
@@ -24,6 +24,7 @@ import org.apache.marmotta.platform.ldp.api.LdpService;
import org.apache.marmotta.platform.ldp.exceptions.InvalidModificationException;
import org.apache.marmotta.platform.ldp.patch.InvalidPatchDocumentException;
import org.apache.marmotta.platform.ldp.patch.parser.ParseException;
+import org.apache.marmotta.platform.ldp.patch.parser.RdfPatchParser;
import org.apache.marmotta.platform.ldp.util.EntityTagUtils;
import org.apache.marmotta.platform.ldp.util.LdpWebServiceUtils;
import org.openrdf.model.Statement;
@@ -63,8 +64,6 @@ public class LdpWebService {
public static final String PATH = "ldp"; //FIXME: imho this should be root '/' (jakob)
- public static final String APPLICATION_RDF_PATCH = "application/rdf-patch";
-
private Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());
@Inject
@@ -239,7 +238,7 @@ public class LdpWebService {
}
// Check for the supported mime-type
- if (!type.toString().equals(APPLICATION_RDF_PATCH)) {
+ if (!type.toString().equals(RdfPatchParser.MIME_TYPE)) {
log.trace("Incompatible Content-Type for PATCH: {}", type);
return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, uriInfo).entity("Unknown Content-Type: " + type + "\n").build();
}
@@ -276,7 +275,7 @@ public class LdpWebService {
builder.header("Accept-Post", "text/turtle");
// Sec. 5.8.2
- builder.header("Accept-Patch", APPLICATION_RDF_PATCH);
+ builder.header("Accept-Patch", RdfPatchParser.MIME_TYPE);
// TODO: Sec. 6.9.1
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java
index d9b65b0..4d8a5b8 100644
--- a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java
+++ b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/PATCH.java
@@ -17,8 +17,20 @@
*/
package org.apache.marmotta.platform.ldp.webservices;
-@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD})
-@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
-@javax.ws.rs.HttpMethod("PATCH")
+import javax.ws.rs.HttpMethod;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that the annotated method responds to HTTP PATCH requests.
+ *
+ * @author Jakob Frank (jakob@apache.org)
+ * @see HttpMethod
+ */
+@Target({java.lang.annotation.ElementType.METHOD})
+@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@HttpMethod("PATCH")
+@Documented
public @interface PATCH {
}
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj b/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj
index 93047cf..33b41fc 100644
--- a/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj
+++ b/platform/marmotta-ldp/src/main/javacc/rdf-patch.jj
@@ -26,7 +26,7 @@
//DEBUG_LOOKAHEAD=true;
}
-PARSER_BEGIN(RdfPatchParser)
+PARSER_BEGIN(RdfPatchParserImpl)
package org.apache.marmotta.platform.ldp.patch.parser;
import org.openrdf.model.*;
@@ -39,7 +39,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-public class RdfPatchParser {
+public class RdfPatchParserImpl implements RdfPatchParser {
private HashMap<String, String> namespaces = new HashMap<String, String>();
@@ -82,7 +82,7 @@ public class RdfPatchParser {
}
}
-PARSER_END(RdfPatchParser)
+PARSER_END(RdfPatchParserImpl)
SKIP : {
" "
http://git-wip-us.apache.org/repos/asf/marmotta/blob/679ac561/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
index 6ebcc58..b543061 100644
--- a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
+++ b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/patch/parser/RdfPatchParserTest.java
@@ -65,7 +65,7 @@ public class RdfPatchParserTest {
Iterator<PatchLine> it = patchLines.iterator();
Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.DEL, bob, FOAF.name, lcBob);
+ checkPatchLine(it.next(), PatchLine.Operator.DELETE, bob, FOAF.name, lcBob);
Assert.assertTrue(it.hasNext());
checkPatchLine(it.next(), PatchLine.Operator.ADD, bob, FOAF.name, ucBob);
@@ -74,7 +74,7 @@ public class RdfPatchParserTest {
checkPatchLine(it.next(), PatchLine.Operator.ADD, null, FOAF.knows, alice);
Assert.assertTrue(it.hasNext());
- checkPatchLine(it.next(), PatchLine.Operator.DEL, null, null, charlie);
+ checkPatchLine(it.next(), PatchLine.Operator.DELETE, null, null, charlie);
}
private void checkPatchLine(PatchLine line, PatchLine.Operator operator, Resource subejct, URI predicate, Value object) {
[2/6] git commit: MARMOTTA-440: Updated LDP-WebService tests (but
they do not work...)
Posted by ja...@apache.org.
MARMOTTA-440: Updated LDP-WebService tests (but they do not work...)
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/ca0d8c0a
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/ca0d8c0a
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/ca0d8c0a
Branch: refs/heads/ldp
Commit: ca0d8c0aa824960205e0000d05def76fab93cada
Parents: 05e0379
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 18:33:41 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 18:33:41 2014 +0100
----------------------------------------------------------------------
.../ldp/webservices/LdpWebServiceTest.java | 53 +++++++++++++++++++-
.../marmotta-ldp/src/test/resources/test.ttl | 26 ++++++++++
2 files changed, 77 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/ca0d8c0a/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 3e0a3d6..9016443 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
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
+import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.apache.marmotta.platform.core.api.importer.ImportService;
import org.apache.marmotta.platform.core.api.triplestore.ContextService;
@@ -30,6 +31,7 @@ import org.apache.marmotta.platform.core.test.base.JettyMarmotta;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.openrdf.rio.RDFFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,11 +49,14 @@ public class LdpWebServiceTest {
private static JettyMarmotta marmotta;
+ private static String testResourceTTL;
+
@BeforeClass
- public static void setUp() throws MarmottaImportException, URISyntaxException {
+ public static void setUp() throws MarmottaImportException, URISyntaxException, IOException {
marmotta = new JettyMarmotta("/marmotta", LdpWebService.class);
//TODO: initialization
+ testResourceTTL = IOUtils.toString(LdpWebServiceTest.class.getResourceAsStream("/test.ttl"), "utf8");
RestAssured.baseURI = "http://localhost";
RestAssured.port = marmotta.getPort();
@@ -61,7 +66,51 @@ public class LdpWebServiceTest {
@Test
public void testCRUD() {
-
+ // The container
+ final String container = "/lpd/test/container1";
+ final String newResourceUri = container + "/resource1";
+
+ RestAssured.expect().statusCode(404).get(container);
+
+ // Create
+ RestAssured.given()
+ .header("Slug", "resource1")
+ .body(testResourceTTL.getBytes())
+ .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+ .expect()
+ .statusCode(201)
+ .header("Location", RestAssured.baseURI + newResourceUri)
+ .post(container);
+
+ // now the container exists
+ log.info("200 - container");
+ RestAssured.given()
+ .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .expect()
+ .statusCode(200)
+ .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+ .get(container);
+
+ // also the new resource exists
+ RestAssured.given()
+ .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .expect()
+ .statusCode(200)
+ .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+ .get(newResourceUri);
+
+ // delete
+ RestAssured.expect()
+ .statusCode(204)
+ .delete(newResourceUri);
+
+ // now the new resource does not exist.
+ RestAssured.given()
+ .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+ .expect()
+ .statusCode(404)
+ .get(newResourceUri);
+
}
@AfterClass
http://git-wip-us.apache.org/repos/asf/marmotta/blob/ca0d8c0a/platform/marmotta-ldp/src/test/resources/test.ttl
----------------------------------------------------------------------
diff --git a/platform/marmotta-ldp/src/test/resources/test.ttl b/platform/marmotta-ldp/src/test/resources/test.ttl
new file mode 100644
index 0000000..bf70c35
--- /dev/null
+++ b/platform/marmotta-ldp/src/test/resources/test.ttl
@@ -0,0 +1,26 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
+@prefix dct: <http://purl.org/dc/terms/>.
+@prefix skos: <http://www.w3.org/2004/02/skos/core#>.
+@prefix ldp: <http://www.w3.org/ns/ldp#>.
+
+<> a <http://example.com/Example> ;
+ dct:title "Example"@en , "Beispiel"@de ;
+ dct:description "Just an example"@en .
[4/6] git commit: MARMOTTA-440: getResourceUri should be called only
once per request
Posted by ja...@apache.org.
MARMOTTA-440: getResourceUri should be called only once per request
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/30302e30
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/30302e30
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/30302e30
Branch: refs/heads/ldp
Commit: 30302e30579d7a15f71fef60b4fa88cfebe4b9d9
Parents: 679ac56
Author: Jakob Frank <ja...@apache.org>
Authored: Tue Feb 25 19:21:09 2014 +0100
Committer: Jakob Frank <ja...@apache.org>
Committed: Tue Feb 25 19:21:09 2014 +0100
----------------------------------------------------------------------
.../platform/ldp/webservices/LdpWebService.java | 72 ++++++++++----------
1 file changed, 36 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/30302e30/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 32994db..f702aea 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
@@ -79,24 +79,25 @@ public class LdpWebService {
@GET
public Response GET(@Context final UriInfo uriInfo, @Context Request r, @HeaderParam(HttpHeaders.ACCEPT) MediaType type) throws RepositoryException {
+ final String resource = getResourceUri(uriInfo);
if (log.isDebugEnabled()) {
- log.debug("GET to LDPR <{}>", getResourceUri(uriInfo));
+ log.debug("GET to LDPR <{}>", resource);
}
- return buildResourceResponse(uriInfo, r, type).build();
+ 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);
if (log.isDebugEnabled()) {
- log.debug("HEAD to LDPR <{}>", getResourceUri(uriInfo));
+ log.debug("HEAD to LDPR <{}>", resource);
}
- return buildResourceResponse(uriInfo, r, type).entity(null).build();
+ return buildGetResponse(resource, r, type).entity(null).build();
}
- private Response.ResponseBuilder buildResourceResponse(final UriInfo uriInfo, Request r, MediaType type) throws RepositoryException {
- final String subject = getResourceUri(uriInfo);
+ private Response.ResponseBuilder buildGetResponse(final String resource, Request r, MediaType type) throws RepositoryException {
- if (!ldpService.exists(getResourceUri(uriInfo))) {
+ if (!ldpService.exists(resource)) {
return Response.status(Response.Status.NOT_FOUND);
}
@@ -105,23 +106,23 @@ public class LdpWebService {
final RDFFormat format = Rio.getWriterFormatForMIMEType(type.toString(), RDFFormat.TURTLE);
if (format == null) {
- log.warn("GET to <{}> with unknown accept {}", subject, type);
- return createResponse(Response.Status.NOT_IMPLEMENTED, uriInfo);
+ log.warn("GET to <{}> with unknown accept {}", resource, type);
+ return createResponse(Response.Status.NOT_IMPLEMENTED, resource);
} else {
// Deliver all triples with <subject> as subject.
final StreamingOutput entity = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
try {
- ldpService.exportResource(output, subject, format);
+ ldpService.exportResource(output, resource, format);
} catch (RDFHandlerException e) {
- throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, uriInfo).build());
+ throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).build());
} catch (RepositoryException e) {
- throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, uriInfo).build());
+ throw new WebApplicationException(e, createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).build());
}
}
};
- return createResponse(Response.Status.OK, uriInfo).entity(entity).type(format.getDefaultMIMEType());
+ return createResponse(Response.Status.OK, resource).entity(entity).type(format.getDefaultMIMEType());
}
}
@@ -156,9 +157,9 @@ public class LdpWebService {
String newResource = uriInfo.getRequestUriBuilder().path(localName).build().toString();
try {
ldpService.addResource(postBody, type, container, newResource);
- return createResponse(Response.Status.CREATED, uriInfo).location(java.net.URI.create(newResource)).build();
+ return createResponse(Response.Status.CREATED, container).location(java.net.URI.create(newResource)).build();
} catch (IOException | RDFParseException e) {
- return createResponse(Response.Status.BAD_REQUEST, uriInfo).entity(e.getClass().getSimpleName() + ": "+ e.getMessage()).build();
+ return createResponse(Response.Status.BAD_REQUEST, container).entity(e.getClass().getSimpleName() + ": "+ e.getMessage()).build();
}
}
@@ -178,14 +179,14 @@ public class LdpWebService {
if (eTag == null) {
// check for If-Match header (ETag) -> 428 Precondition Required (Sec. 5.5.3)
log.trace("No If-Match header, but that's a MUST");
- return createResponse(428, uriInfo).build();
+ return createResponse(428, resource).build();
} else {
// check ETag -> 412 Precondition Failed (Sec. 5.5.3)
log.trace("Checking If-Match: {}", eTag);
EntityTag hasTag = ldpService.generateETag(resource);
if (!EntityTagUtils.equals(eTag, hasTag)) {
log.trace("If-Match header did not match, expected {}", hasTag);
- return createResponse(Response.Status.PRECONDITION_FAILED, uriInfo).build();
+ return createResponse(Response.Status.PRECONDITION_FAILED, resource).build();
}
}
@@ -196,7 +197,7 @@ public class LdpWebService {
*
* if the target resource exists, replace ALL data of the target.
*/
- return createResponse(Response.Status.NOT_IMPLEMENTED, uriInfo).build();
+ return createResponse(Response.Status.NOT_IMPLEMENTED, resource).build();
}
@DELETE
@@ -208,10 +209,10 @@ public class LdpWebService {
log.debug("DELETE to <{}>", resource);
try {
ldpService.deleteResource(resource);
- return createResponse(Response.Status.NO_CONTENT, uriInfo).build();
+ return createResponse(Response.Status.NO_CONTENT, resource).build();
} catch (RepositoryException e) {
log.error("Error deleting LDP-R: {}: {}", resource, e.getMessage());
- return createResponse(Response.Status.INTERNAL_SERVER_ERROR, uriInfo).entity("Error deleting LDP-R: " + e.getMessage()).build();
+ return createResponse(Response.Status.INTERNAL_SERVER_ERROR, resource).entity("Error deleting LDP-R: " + e.getMessage()).build();
}
}
@@ -224,7 +225,7 @@ public class LdpWebService {
log.debug("PATCH to <{}>", resource);
if (!ldpService.exists(resource)) {
- return createResponse(Response.Status.NOT_FOUND, uriInfo).build();
+ return createResponse(Response.Status.NOT_FOUND, resource).build();
}
if (eTag != null) {
@@ -233,25 +234,25 @@ public class LdpWebService {
EntityTag hasTag = ldpService.generateETag(resource);
if (!EntityTagUtils.equals(eTag, hasTag)) {
log.trace("If-Match header did not match, expected {}", hasTag);
- return createResponse(Response.Status.PRECONDITION_FAILED, uriInfo).build();
+ return createResponse(Response.Status.PRECONDITION_FAILED, resource).build();
}
}
// Check for the supported mime-type
if (!type.toString().equals(RdfPatchParser.MIME_TYPE)) {
log.trace("Incompatible Content-Type for PATCH: {}", type);
- return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, uriInfo).entity("Unknown Content-Type: " + type + "\n").build();
+ return createResponse(Response.Status.UNSUPPORTED_MEDIA_TYPE, resource).entity("Unknown Content-Type: " + type + "\n").build();
}
try {
ldpService.patchResource(resource, postBody);
} catch (ParseException | InvalidPatchDocumentException e) {
- return createResponse(Response.Status.BAD_REQUEST, uriInfo).entity(e.getMessage() + "\n").build();
+ return createResponse(Response.Status.BAD_REQUEST, resource).entity(e.getMessage() + "\n").build();
} catch (InvalidModificationException e) {
- return createResponse(422, uriInfo).entity(e.getMessage() + "\n").build();
+ return createResponse(422, resource).entity(e.getMessage() + "\n").build();
}
- return createResponse(Response.Status.NO_CONTENT, uriInfo).build();
+ return createResponse(Response.Status.NO_CONTENT, resource).build();
}
@@ -265,7 +266,7 @@ public class LdpWebService {
log.warn("NOT CHECKING EXISTENCE OF <{}>", resource);
- Response.ResponseBuilder builder = createResponse(Response.Status.OK, uriInfo);
+ Response.ResponseBuilder builder = createResponse(Response.Status.OK, resource);
// Sec. 5.9.2
builder.allow("GET", "HEAD", "POST", "PATCH", "OPTIONS");
@@ -285,24 +286,23 @@ public class LdpWebService {
return builder.build();
}
- protected Response.ResponseBuilder createResponse(int status, UriInfo uriInfo) {
- return createResponse(Response.status(status), uriInfo);
+ protected Response.ResponseBuilder createResponse(int status, String resource) {
+ return createResponse(Response.status(status), resource);
}
/**
* Add all the default headers specified in LDP to the Response
* @param rb the ResponseBuilder
- * @param uriInfo the uri-info to build the resource uri
+ * @param resource the uri/url of the resouce
* @return the provided ResponseBuilder for chaining
*/
- protected Response.ResponseBuilder createResponse(Response.ResponseBuilder rb, UriInfo uriInfo) {
+ protected Response.ResponseBuilder createResponse(Response.ResponseBuilder rb, String resource) {
// Link rel='describedby' (Sec. 5.2.11)
rb.link("http://wiki.apache.org/marmotta/LDPImplementationReport", "describedby");
- final String rUri = getResourceUri(uriInfo);
try {
- List<Statement> statements = ldpService.getStatements(rUri);
+ List<Statement> statements = ldpService.getStatements(resource);
for (Statement stmt : statements) {
Value o = stmt.getObject();
if (o instanceof URI && o.stringValue().startsWith(LDP.NAMESPACE)) {
@@ -311,7 +311,7 @@ public class LdpWebService {
}
// ETag (Sec. 5.2.7)
- rb.tag(ldpService.generateETag(rUri));
+ rb.tag(ldpService.generateETag(resource));
} catch (RepositoryException e) {
log.error("Could not set ldp-response headers", e);
}
@@ -319,8 +319,8 @@ public class LdpWebService {
return new RB(rb);
}
- protected Response.ResponseBuilder createResponse(Response.Status status, UriInfo uriInfo) {
- return createResponse(status.getStatusCode(), uriInfo);
+ protected Response.ResponseBuilder createResponse(Response.Status status, String resource) {
+ return createResponse(status.getStatusCode(), resource);
}
protected String getResourceUri(UriInfo uriInfo) {