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/09/03 15:49:15 UTC

[3/5] git commit: MARMOTTA-150: implemented genid-based bnodes html renderization

MARMOTTA-150: implemented genid-based bnodes html renderization


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

Branch: refs/heads/develop
Commit: ed09ff2725191d18905b21024b9c882465c0377c
Parents: 6baff87
Author: Sergio Fernández <wi...@apache.org>
Authored: Tue Sep 3 15:09:01 2013 +0200
Committer: Sergio Fernández <wi...@apache.org>
Committed: Tue Sep 3 15:09:01 2013 +0200

----------------------------------------------------------------------
 .../marmotta/commons/http/ETagGenerator.java    | 24 +++++++--
 .../webservices/resource/MetaWebService.java    | 52 +++++++++++--------
 .../resource/ResourceWebService.java            | 54 +++++++++++++-------
 .../resource/ResourceWebServiceHelper.java      | 34 ++++++------
 4 files changed, 103 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ed09ff27/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
index d54a510..4d93807 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
@@ -18,13 +18,14 @@
 package org.apache.marmotta.commons.http;
 
 import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Resource;
 import org.openrdf.model.Statement;
 import org.openrdf.model.URI;
 import org.openrdf.repository.RepositoryConnection;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.RepositoryResult;
 
-
 import com.google.common.hash.HashFunction;
 import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
@@ -73,12 +74,25 @@ public class ETagGenerator {
         return hasher.hash().toString();
     }
     
-    public static String getWeakETag(RepositoryConnection conn, String uri) throws RepositoryException {
-        URI resource = ResourceUtils.getUriResource(conn, uri);
-        return getWeakETag(conn, resource);
+    public static String getWeakETag(RepositoryConnection conn, String resource) throws RepositoryException {
+        if (resource.startsWith("http://")) {
+        	return getWeakETag(conn, ResourceUtils.getUriResource(conn, resource));
+        } else {
+        	return getWeakETag(conn, ResourceUtils.getAnonResource(conn, resource));
+        }
     }   
     
-    public static String getWeakETag(RepositoryConnection conn, URI resource) throws RepositoryException {
+//    public static String getWeakETag(RepositoryConnection conn, Resource resource) throws RepositoryException {
+//    	if (resource instanceof URI) {
+//    		return getWeakETag(conn, (URI)resource);
+//    	} else if (resource instanceof BNode) {
+//    		return getWeakETag(conn, (BNode)resource);
+//    	} else {
+//    		return null;
+//    	}
+//    }
+    
+    public static String getWeakETag(RepositoryConnection conn, Resource resource) throws RepositoryException {
     	if (resource == null) return "";
     	
         Hasher hasher = buildHasher();

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ed09ff27/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 cae9e4c..885dcb3 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
@@ -40,6 +40,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.StreamingOutput;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.marmotta.commons.collections.CollectionUtils;
 import org.apache.marmotta.commons.http.ETagGenerator;
 import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
@@ -112,8 +113,14 @@ 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);
+    public Response getMetaRemote(@QueryParam("uri") String uri, @QueryParam("genid") String genid, @PathParam("mimetype") String mimetype) throws UnsupportedEncodingException {
+    	if (StringUtils.isNotBlank(uri)) {
+    		return getMeta(URLDecoder.decode(uri, "utf-8"), mimetype);
+    	} else if (StringUtils.isNotBlank(genid)) {
+    		return getMeta(URLDecoder.decode(genid, "utf-8"), mimetype);
+    	} else {
+    		return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.BAD_REQUEST, "Invalid Request", configurationService, templatingService);
+    	}
     }
 
     /**
@@ -240,19 +247,24 @@ public class MetaWebService {
         return deleteMetaRemote(uri);
     }
 
-    private Response getMeta(String uri, String mimetype) throws UnsupportedEncodingException {
+    private Response getMeta(String resource, String mimetype) throws UnsupportedEncodingException {
         try {
             RepositoryConnection conn = sesameService.getConnection();
-            
-            if (!ResourceUtils.existsResource(conn, uri)) {
-            	return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
-            }
 
             try {
                 conn.begin();
-                // FIXME String appendix = uuid == null ? "?uri=" + URLEncoder.encode(uri, "utf-8") :
-                // "/" + uuid;
-                URI resource = conn.getValueFactory().createURI(uri);
+                
+                Resource r = null;
+            	if (resource.startsWith("http://")) {
+                    r = ResourceUtils.getUriResource(conn, resource);
+            	} else {
+            		r = ResourceUtils.getAnonResource(conn, resource);
+            	}
+            	
+                if (r == null || !ResourceUtils.isUsed(conn, r)) {
+                	return ResourceWebServiceHelper.buildErrorPage(resource, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
+                }
+            	
                 // create parser
                 final RDFFormat serializer = kiWiIOService.getSerializer(mimetype);
                 if (serializer == null) {
@@ -261,9 +273,9 @@ public class MetaWebService {
                     return response;
                 }
 
-                if(resource != null) {
+                if(r != null) {
 
-                    final Resource subject = resource;
+                    final Resource subject = r;
 
                     StreamingOutput entity = new StreamingOutput() {
                         @Override
@@ -289,15 +301,15 @@ public class MetaWebService {
                     };
 
                     // build response
-                    Response response = Response.ok(entity).lastModified(KiWiSesameUtil.lastModified(resource, conn)).build();
-                    response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, resource) + "\"");
+                    Response response = Response.ok(entity).lastModified(KiWiSesameUtil.lastModified(r, conn)).build();
+                    response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, r) + "\"");
                     
                     if (!mimetype.contains("html")) { // then create a proper filename
 	                    String[] components;
-	                    if (uri.contains("#")) {
-	                    	components = uri.split("#");	                    	
+	                    if (resource.contains("#")) {
+	                    	components = resource.split("#");	                    	
 	                    } else {
-	                    	components = uri.split("/");
+	                    	components = resource.split("/");
 	                    }
 	                    final String fileName = components[components.length-1] + "." + serializer.getDefaultFileExtension();   
 	                    response.getMetadata().add("Content-Disposition", "attachment; filename=\""+fileName+"\"");
@@ -314,7 +326,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, contentService.getContentType(resource), configurationService);
+                    String contentLink = ResourceWebServiceHelper.buildContentLink(r, contentService.getContentType(r), configurationService);
                     if(!"".equals(contentLink)) {
                         links.add(contentLink);
                     }
@@ -324,7 +336,7 @@ public class MetaWebService {
                     }
                     return response;
                 } else {
-                    return Response.status(Response.Status.NOT_FOUND).entity("resource with URI "+uri+" does not exist").build();
+                    return Response.status(Response.Status.NOT_FOUND).entity("resource with URI "+resource+" does not exist").build();
                 }
             } finally {
                 if (conn.isOpen()) {
@@ -333,7 +345,7 @@ public class MetaWebService {
                 }
             }
         } catch (RepositoryException e) {
-            return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService, templatingService);
+            return ResourceWebServiceHelper.buildErrorPage(resource, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService, templatingService);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ed09ff27/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 6ea5c89..d3de5a1 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
@@ -336,37 +336,53 @@ public class ResourceWebService {
      *                 (content and meta)
      */
     @GET
-    public Response getRemote(@QueryParam("uri") String uri, @QueryParam("format") String format, @HeaderParam("Accept") String types)
+    public Response getRemote(@QueryParam("uri") String uri, @QueryParam("genid") String genid, @QueryParam("format") String format, @HeaderParam("Accept") String types)
             throws UnsupportedEncodingException {
         try {
-            if (uri != null) {
+            if (StringUtils.isNotBlank(uri)) {
                 if (format != null && StringUtils.isNotBlank(format)) {
                     types = format;
                 }
                 //TODO: add 'If-None-Match' support, sending a '304 Not Modified' when the ETag matches
                 return get(uri, types);
-            } else
-                return Response.status(400).entity("uri may not be null").build();
+            } else if (StringUtils.isNotBlank(genid)) {
+                if (format != null && StringUtils.isNotBlank(format)) {
+                    types = format;
+                }
+                //TODO: add 'If-None-Match' support, sending a '304 Not Modified' when the ETag matches
+                return get(genid, types);
+            } else {
+                return Response.status(400).entity("resource not identified").build();
+            }
         } catch (URISyntaxException e) {
             return Response.serverError().entity(e.getMessage()).build();
         }
     }
 
-    private Response get(String uri, String types) throws URISyntaxException, UnsupportedEncodingException {
+    private Response get(String resource, String types) throws URISyntaxException, UnsupportedEncodingException {
         try {
 
             RepositoryConnection conn = sesameService.getConnection();
             try {
                 conn.begin();
-                URI resource;
-                try {
-                	resource = ResourceUtils.getUriResource(conn, uri);
-                } catch (Exception e) {
-                	log.error("Error retrieving the URI <{}>: {}", uri, e.getMessage());
-                	log.debug("So redirecting directly to it...");
-                	return Response.seeOther(new java.net.URI(uri)).build();
-                }
-                if (resource == null) return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
+                Resource r;
+            	if (resource.startsWith("http://")) {
+                    try {
+                    	r = ResourceUtils.getUriResource(conn, resource);
+                    } catch (Exception e) {
+                    	log.error("Error retrieving the resource <{}>: {}", resource, e.getMessage());
+                    	log.debug("So redirecting directly to it...");
+                    	return Response.seeOther(new java.net.URI(resource)).build();
+                    }
+            	} else {
+            		try {
+            			r = ResourceUtils.getAnonResource(conn, resource);
+	                } catch (Exception e) {
+	                	log.error("Error retrieving the blank node <{}>: {}", resource, e.getMessage());
+	                	return Response.status(Status.NOT_FOUND).entity("blank node id "  + resource + " not found").build();
+	                }
+            	}
+                if (r == null) return ResourceWebServiceHelper.buildErrorPage(resource, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
                 // FIXME String appendix = uuid == null ? "?uri=" + URLEncoder.encode(uri, "utf-8") :
                 // "/" + uuid;
 
@@ -374,7 +390,7 @@ public class ResourceWebService {
                 for(ContentType t : offeredTypes) {
                 	t.setParameter("rel", "meta");
                 }
-                String contentmime = contentService.getContentType(resource);
+                String contentmime = contentService.getContentType(r);
                 if(contentmime != null) {
                 	ContentType tContent = LMFHttpUtils.parseContentType(contentmime);
                 	tContent.setParameter("rel", "content");
@@ -391,9 +407,9 @@ public class ResourceWebService {
                 log.debug("identified best type: {}",bestType);
 
                 if(bestType != null) {
-                    Response response = buildGetResponse(resource, bestType);
-                    response.getMetadata().add("Last-Modified", KiWiSesameUtil.lastModified(resource, conn));
-                    response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, resource) + "\"");
+                    Response response = buildGetResponse(r, bestType);
+                    response.getMetadata().add("Last-Modified", KiWiSesameUtil.lastModified(r, conn));
+                    response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, r) + "\"");
                     return response;
                 } else {
                     return build406(acceptedTypes, offeredTypes);
@@ -608,7 +624,7 @@ public class ResourceWebService {
         return deleteResourceRemote(uri);
     }
 
-    private Response buildGetResponse(URI resource, ContentType type) {
+    private Response buildGetResponse(Resource resource, ContentType type) {
         try {
 
             return Response

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ed09ff27/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 1dd21cf..15abd70 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
@@ -20,10 +20,12 @@ package org.apache.marmotta.platform.core.webservices.resource;
 import org.apache.marmotta.platform.core.api.config.ConfigurationService;
 import org.apache.marmotta.platform.core.api.templating.TemplatingService;
 import org.apache.marmotta.commons.http.ContentType;
+import org.openrdf.model.Resource;
 import org.openrdf.model.URI;
 
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
+
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.HashMap;
@@ -78,7 +80,7 @@ public class ResourceWebServiceHelper {
         return buildContentLink(resource, mime, configurationService);
     }
 
-    public static String buildContentLink(URI resource, String mime, ConfigurationService configurationService) {
+    public static String buildContentLink(Resource resource, String mime, ConfigurationService configurationService) {
         //TODO: test if there is content
         StringBuffer b = new StringBuffer();
         if (mime != null) {
@@ -124,25 +126,23 @@ public class ResourceWebServiceHelper {
                 cType.getMime(), configurationService);
     }
 
-    public static String buildResourceLink(URI resource, String rel, String mime, ConfigurationService configurationService) {
-        final String src = configurationService.getServerUri(), base = configurationService
-                .getBaseUri();
-
-        if (src.equals(base)
-                && resource.stringValue().startsWith(base + ConfigurationService.RESOURCE_PATH + "/")) {
+    public static String buildResourceLink(Resource resource, String rel, String mime, ConfigurationService configurationService) {
+        final String src = configurationService.getServerUri();
+        final String base = configurationService.getBaseUri();
+        if (src.equals(base) && resource.stringValue().startsWith(base + ConfigurationService.RESOURCE_PATH + "/")) {
             final String uuid;
-            uuid = resource.stringValue().substring(
-                    (base + "resource/").length());
+            uuid = resource.stringValue().substring((base + "resource/").length());
             return String.format("%s%s/%s/%s", base, rel, mime, uuid);
-
         } else {
-            try {
-                return String.format("%s%s/%s?uri=%s", src, rel, mime,
-                        URLEncoder.encode(resource.stringValue(), ResourceWebService.CHARSET));
-            } catch (UnsupportedEncodingException e) {
-                return String.format("%s%s/%s?uri=%s", src, rel, mime,
-                        resource.stringValue());
-            }
+	    	if (resource instanceof URI) {
+	            try {  
+	                return String.format("%s%s/%s?uri=%s", src, rel, mime, URLEncoder.encode(resource.stringValue(), ResourceWebService.CHARSET));
+	            } catch (UnsupportedEncodingException e) {
+	                return String.format("%s%s/%s?uri=%s", src, rel, mime, resource.stringValue());
+	            }                    
+	    	} else {
+	    		return String.format("%s%s/%s?genid=%s", src, rel, mime, resource.stringValue());
+	    	}      
         }
     }