Author: reto
Date: Tue Jan 12 13:05:45 2010
New Revision: 898310
URL: http://svn.apache.org/viewvc?rev=898310&view=rev
Log:
CLEREZZA-64: returning uri to image if it fits into thumbneil spec
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java?rev=898310&r1=898309&r2=898310&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.webapp.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java
Tue Jan 12 13:05:45 2010
@@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/
-
package org.apache.clerezza.platform.content.representations.core;
import java.net.URI;
@@ -24,11 +23,13 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.clerezza.platform.config.PlatformConfig;
import
org.apache.clerezza.platform.content.representations.ontologies.REPRESENTATIONS;
import org.apache.clerezza.platform.graphprovider.content.ContentGraphProvider;
+import org.apache.clerezza.rdf.core.Literal;
import org.apache.clerezza.rdf.core.LiteralFactory;
import org.apache.clerezza.rdf.core.NonLiteral;
import org.apache.clerezza.rdf.core.Resource;
@@ -43,6 +44,8 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This JAX-RS resource provides a method to retrieve the uri to
@@ -58,11 +61,10 @@
@Reference
ContentGraphProvider cgProvider;
-
@Reference
PlatformConfig config;
-
private UriRef defaultIconUri;
+ private static final Logger log =
LoggerFactory.getLogger(ThumbnailService.class);
protected void activate(ComponentContext context) {
String baseUri = config.getDefaultBaseUri().getUnicodeString();
@@ -87,7 +89,7 @@
private void addMediaTypeIcon(UriRef icon, String type) {
cgProvider.getContentGraph().add(
- new TripleImpl(icon, REPRESENTATIONS.isIconFor ,
+ new TripleImpl(icon, REPRESENTATIONS.isIconFor,
LiteralFactory.getInstance().createTypedLiteral(type)));
}
@@ -108,7 +110,11 @@
@GET
public Response getThumbnailUri(@QueryParam("uri") UriRef infoBitUri,
@QueryParam("width") Integer width,
- @QueryParam("height") Integer height) {
+ @QueryParam("height") Integer height) {
+ if ((width == null) && (height == null)) {
+ throw new WebApplicationException(new
IllegalArgumentException("height and/or width must be specified"),
+ Response.Status.BAD_REQUEST);
+ }
if (width == null) {
width = Integer.MAX_VALUE;
}
@@ -116,49 +122,79 @@
height = Integer.MAX_VALUE;
}
GraphNode infoBitNode = new GraphNode(infoBitUri,
cgProvider.getContentGraph());
- UriRef thumbnailUri = getThumbnailUri(infoBitNode, height,
width);
+ UriRef thumbnailUri = getThumbnailUri(infoBitNode, width,
height);
if (thumbnailUri != null) {
return Response.seeOther(
URI.create((thumbnailUri).getUnicodeString())).build();
- }
+ }
Iterator<Resource> mediaTypes =
infoBitNode.getObjects(DISCOBITS.mediaType);
if (mediaTypes.hasNext()) {
- GraphNode mediaType = new GraphNode(mediaTypes.next(),
+ GraphNode mediaType = new GraphNode(mediaTypes.next(),
cgProvider.getContentGraph());
-
+
Iterator<NonLiteral> icons =
mediaType.getSubjects(REPRESENTATIONS.isIconFor);
if (icons.hasNext()) {
return Response.seeOther(
-
URI.create(((UriRef)icons.next()).getUnicodeString())).build();
+ URI.create(((UriRef)
icons.next()).getUnicodeString())).build();
}
}
return
Response.seeOther(URI.create(defaultIconUri.getUnicodeString())).build();
}
- private UriRef getThumbnailUri(GraphNode infoBitNode, Integer height,
- Integer width) {
+ private UriRef getThumbnailUri(GraphNode infoBitNode,
+ Integer width, Integer height) {
+ if (isFittingImage(infoBitNode, width, height)) {
+ return (UriRef) infoBitNode.getNode();
+ }
UriRef resultThumbnailUri = null;
int pixels = 0;
Iterator<Resource> thumbnails =
infoBitNode.getObjects(DISCOBITS.thumbnail);
while (thumbnails.hasNext()) {
UriRef thumbnailUri = (UriRef) thumbnails.next();
- GraphNode thumbnailNode = new GraphNode(thumbnailUri,
+ GraphNode thumbnailNode = new GraphNode(thumbnailUri,
cgProvider.getContentGraph());
- Iterator<Resource> exifWidths =
thumbnailNode.getObjects(EXIF.width);
- Iterator<Resource> exifHeights =
thumbnailNode.getObjects(EXIF.height);
- if (!exifWidths.hasNext() || !exifHeights.hasNext()) {
- continue;
- }
- Integer thumbnailWidth =
LiteralFactory.getInstance().createObject(
- Integer.class, (TypedLiteral)
exifWidths.next());
- Integer thumbnailHeight =
LiteralFactory.getInstance().createObject(
- Integer.class, (TypedLiteral)
exifHeights.next());
- int thumbnailPixels = thumbnailWidth * thumbnailHeight;
- if (thumbnailHeight <= height && thumbnailWidth <=
width && thumbnailPixels > pixels) {
+ int thumbnailPixels =
getSurfaceSizeIfFitting(thumbnailNode, width, height);
+ if (thumbnailPixels > pixels) {
resultThumbnailUri = thumbnailUri;
pixels = thumbnailPixels;
}
}
return resultThumbnailUri;
}
-}
\ No newline at end of file
+
+ /**
+ * returns the surface in pixel if the image fits withing width and
height,
+ * or -1 if it doesn't fit
+ */
+ private int getSurfaceSizeIfFitting(GraphNode infoBitNode, Integer
width, Integer height) {
+ Iterator<Resource> exifWidths =
infoBitNode.getObjects(EXIF.width);
+ Iterator<Resource> exifHeights =
infoBitNode.getObjects(EXIF.height);
+ if (!exifWidths.hasNext() || !exifHeights.hasNext()) {
+ log.warn(infoBitNode.getNode() + " doesn't have
exif:width and exif:heigh");
+ return -1;
+ }
+ Integer thumbnailWidth =
LiteralFactory.getInstance().createObject(
+ Integer.class, (TypedLiteral)
exifWidths.next());
+ Integer thumbnailHeight =
LiteralFactory.getInstance().createObject(
+ Integer.class, (TypedLiteral)
exifHeights.next());
+ if (thumbnailHeight <= height && thumbnailWidth <= width) {
+ return thumbnailWidth * thumbnailHeight;
+ }
+ return -1;
+ }
+
+ /**
+ * returns true if infoBitNode is an image and fits
+ */
+ private boolean isFittingImage(GraphNode infoBitNode, Integer width,
Integer height) {
+ final Iterator<Literal> mediaTypesIter =
infoBitNode.getLiterals(DISCOBITS.mediaType);
+ if (!mediaTypesIter.hasNext()) {
+ return false;
+ }
+ if (mediaTypesIter.next().getLexicalForm().startsWith("image"))
{
+ return getSurfaceSizeIfFitting(infoBitNode, width,
height) > -1;
+ } else {
+ return false;
+ }
+ }
+}