You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by wi...@apache.org on 2013/04/08 18:12:28 UTC
[3/3] git commit: some fixes in the ContentWebService - redirect for
requests without mimetype
some fixes in the ContentWebService
- redirect for requests without mimetype
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/ae3f3056
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/ae3f3056
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/ae3f3056
Branch: refs/heads/master
Commit: ae3f30562b4b8d646c19312e247345bd6829707a
Parents: db0de75
Author: Jakob Frank <ja...@apache.org>
Authored: Wed Apr 3 15:40:00 2013 +0200
Committer: Sergio Fernández <wi...@apache.org>
Committed: Mon Apr 8 18:11:24 2013 +0200
----------------------------------------------------------------------
.../webservices/resource/ContentWebService.java | 75 ++++++++++++++-
.../core/webservices/resource/MetaWebService.java | 10 +-
.../webservices/resource/ResourceWebService.java | 11 +-
.../resource/ResourceWebServiceHelper.java | 16 +++-
4 files changed, 95 insertions(+), 17 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae3f3056/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
index 5c8a8bf..bbc13ce 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
@@ -20,6 +20,7 @@ package org.apache.marmotta.platform.core.webservices.resource;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -102,7 +103,7 @@ public class ContentWebService {
@Path(ResourceWebService.MIME_PATTERN + ResourceWebService.UUID_PATTERN)
public Response getContentLocal(@PathParam("uuid") String uuid, @PathParam("mimetype") String mimetype, @HeaderParam("Range") String range) throws UnsupportedEncodingException {
String uri = configurationService.getBaseUri() + "resource/" + uuid;
- return getContent(uri, mimetype, uuid, range);
+ return getContent(uri, mimetype, range);
}
/**
@@ -127,8 +128,71 @@ public class ContentWebService {
@GET
@Path(ResourceWebService.MIME_PATTERN)
public Response getContentRemote(@QueryParam("uri") @NotNull String uri, @PathParam("mimetype") String mimetype, @HeaderParam("Range") String range) throws UnsupportedEncodingException {
- return getContent(URLDecoder.decode(uri, "utf-8"), mimetype, null, range);
+ return getContent(URLDecoder.decode(uri, "utf-8"), mimetype, range);
}
+
+ /**
+ * Creates a redirect depending of the stored mimeType for the requested uri.
+ * @param uri the resource requested
+ * @return a redirect
+ * @throws UnsupportedEncodingException
+ *
+ * @HTTP 3xx redirect to the requested content
+ * @HTTP 404 if the resource has no contetn
+ * @HTTP 500 Internal Error
+ */
+ @GET
+ public Response getContentRemote(@QueryParam("uri") @NotNull String uri) throws UnsupportedEncodingException {
+ try {
+ final RepositoryConnection conn = sesameService.getConnection();
+ try {
+ conn.begin();
+ URI resource = conn.getValueFactory().createURI(uri);
+ conn.commit();
+ final String mimeType = contentService.getContentType(resource);
+ if (mimeType != null) {
+ return Response
+ .status(configurationService.getIntConfiguration(
+ "linkeddata.redirect.status", 303))
+ .header("Vary", "Accept")
+ .header("Content-Type", mimeType + "; rel=content")
+ .location(
+ new java.net.URI(ResourceWebServiceHelper
+ .buildResourceLink(resource,
+ "content", mimeType,
+ configurationService)))
+ .build();
+
+ } else {
+ return Response.status(Status.NOT_FOUND).entity("No content for <"+resource.stringValue()+">").build();
+ }
+ } finally {
+ conn.close();
+ }
+ } catch (RepositoryException ex) {
+ return Response.serverError().entity(ex.getMessage()).build();
+ } catch (URISyntaxException ex) {
+ return Response.serverError().entity(ex.getMessage()).build();
+ }
+ }
+
+ /**
+ * Creates a redirect depending of the stored mimeType for the requested resource.
+ * @param uuid the local resource requested
+ * @return a redirect
+ * @throws UnsupportedEncodingException
+ *
+ * @HTTP 3xx redirect to the requested content
+ * @HTTP 404 if the resource has no content
+ * @HTTP 500 Internal Error
+ */
+ @GET
+ @Path(ResourceWebService.UUID_PATTERN)
+ public Response getContentLocal(@PathParam("uuid") String uuid) throws UnsupportedEncodingException {
+ String uri = configurationService.getBaseUri() + ConfigurationService.RESOURCE_PATH + "/" + uuid;
+ return getContentRemote(uri);
+ }
+
/**
* Sets content to a given locale resource
@@ -227,7 +291,7 @@ public class ContentWebService {
return deleteContentRemote(uri);
}
- private Response getContent(String uri, String mimetype, String uuid, String range) throws UnsupportedEncodingException {
+ private Response getContent(String uri, String mimetype, String range) throws UnsupportedEncodingException {
try {
// FIXME String appendix = uuid == null ? "?uri=" + URLEncoder.encode(uri, "utf-8") :
// "/" + uuid;
@@ -236,6 +300,9 @@ public class ContentWebService {
conn.begin();
URI resource = conn.getValueFactory().createURI(uri);
conn.commit();
+ if (mimetype == null) {
+ mimetype = contentService.getContentType(resource);
+ }
if (contentService.hasContent(resource, mimetype)) {
InputStream is = contentService.getContentStream(resource, mimetype);
@@ -279,7 +346,7 @@ public class ContentWebService {
}
// append data links
- String s = ResourceWebServiceHelper.buildMetaLinks(resource, uuid, kiWiIOService.getProducedTypes(), configurationService);
+ String s = ResourceWebServiceHelper.buildMetaLinks(resource, kiWiIOService.getProducedTypes(), configurationService);
if (s != null) {
response.getMetadata().add("Links", s);
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae3f3056/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
index e771d14..a09a9e2 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
@@ -21,7 +21,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
-import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -44,7 +43,6 @@ import javax.ws.rs.core.StreamingOutput;
import org.apache.marmotta.commons.collections.CollectionUtils;
import org.apache.marmotta.commons.http.ETagGenerator;
import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
-import org.apache.marmotta.commons.util.DateUtils;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.content.ContentService;
import org.apache.marmotta.platform.core.api.io.MarmottaIOService;
@@ -111,7 +109,7 @@ public class MetaWebService {
@GET
@Path(ResourceWebService.MIME_PATTERN)
public Response getMetaRemote(@QueryParam("uri") @NotNull String uri, @PathParam("mimetype") String mimetype) throws UnsupportedEncodingException {
- return getMeta(URLDecoder.decode(uri, "utf-8"), mimetype, null);
+ return getMeta(URLDecoder.decode(uri, "utf-8"), mimetype);
}
/**
@@ -136,7 +134,7 @@ public class MetaWebService {
@Path(ResourceWebService.MIME_PATTERN + ResourceWebService.UUID_PATTERN)
public Response getMetaLocal(@PathParam("uuid") String uuid, @PathParam("mimetype") String mimetype) throws UnsupportedEncodingException {
String uri = configurationService.getBaseUri() + "resource/" + uuid;
- return getMeta(uri, mimetype, uuid);
+ return getMeta(uri, mimetype);
}
/**
@@ -239,7 +237,7 @@ public class MetaWebService {
return deleteMetaRemote(uri);
}
- private Response getMeta(String uri, String mimetype, String uuid) throws UnsupportedEncodingException {
+ private Response getMeta(String uri, String mimetype) throws UnsupportedEncodingException {
try {
RepositoryConnection conn = sesameService.getConnection();
@@ -313,7 +311,7 @@ public class MetaWebService {
List<String> links = new LinkedList<String>();
// build the link to the human readable content of this resource (if it exists)
- String contentLink = ResourceWebServiceHelper.buildContentLink(resource, uuid, contentService.getContentType(resource), configurationService);
+ String contentLink = ResourceWebServiceHelper.buildContentLink(resource, contentService.getContentType(resource), configurationService);
if(!"".equals(contentLink)) {
links.add(contentLink);
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae3f3056/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
index 2987d56..415fd25 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
@@ -298,7 +298,7 @@ public class ResourceWebService {
public Response getLocal(@PathParam("uuid") String uuid, @HeaderParam("Accept") String types) throws UnsupportedEncodingException {
String uri = configurationService.getBaseUri() + "resource/" + uuid;
try {
- return get(uri, types, uuid);
+ return get(uri, types);
} catch (URISyntaxException e) {
return Response.serverError().entity(e.getMessage()).build();
}
@@ -332,7 +332,7 @@ public class ResourceWebService {
types = format;
}
//TODO: add 'If-None-Match' support, sending a '304 Not Modified' when the ETag matches
- return get(uri, types, null);
+ return get(uri, types);
} else
return Response.status(400).entity("uri may not be null").build();
} catch (URISyntaxException e) {
@@ -340,7 +340,7 @@ public class ResourceWebService {
}
}
- private Response get(String uri, String types, String uuid) throws URISyntaxException, UnsupportedEncodingException {
+ private Response get(String uri, String types) throws URISyntaxException, UnsupportedEncodingException {
try {
RepositoryConnection conn = sesameService.getConnection();
@@ -379,7 +379,7 @@ public class ResourceWebService {
log.debug("identified best type: {}",bestType);
if(bestType != null) {
- Response response = buildGetResponse(resource, uuid, bestType);
+ Response response = buildGetResponse(resource, bestType);
response.getMetadata().add("Last-Modified", KiWiSesameUtil.lastModified(resource, conn));
response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, resource) + "\"");
return response;
@@ -596,8 +596,7 @@ public class ResourceWebService {
return deleteResourceRemote(uri);
}
- private Response buildGetResponse(URI resource, String uuid,
- ContentType type) {
+ private Response buildGetResponse(URI resource, ContentType type) {
try {
return Response
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae3f3056/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
index cf23571..88bbddd 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
@@ -69,8 +69,15 @@ public class ResourceWebServiceHelper {
}
}
+ /**
+ * @deprecated Use {@link #buildContentLink(URI,String,ConfigurationService)} instead
+ */
public static String buildContentLink(URI resource, String uuid, String mime, ConfigurationService configurationService) {
- // test if there is content
+ return buildContentLink(resource, mime, configurationService);
+ }
+
+ public static String buildContentLink(URI resource, String mime, ConfigurationService configurationService) {
+ //TODO: test if there is content
StringBuffer b = new StringBuffer();
if (mime != null) {
b.append("<");
@@ -85,7 +92,14 @@ public class ResourceWebServiceHelper {
return b.toString();
}
+ /**
+ * @deprecated Use {@link #buildMetaLinks(URI,List<String>,ConfigurationService)} instead
+ */
public static String buildMetaLinks(URI resource, String uuid, List<String> datamimes, ConfigurationService configurationService) {
+ return buildMetaLinks(resource, datamimes, configurationService);
+ }
+
+ public static String buildMetaLinks(URI resource, List<String> datamimes, ConfigurationService configurationService) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < datamimes.size(); i++) {
b.append("<");