You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by mi...@apache.org on 2010/06/29 18:27:03 UTC

svn commit: r959021 - /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations...

Author: mir
Date: Tue Jun 29 16:27:03 2010
New Revision: 959021

URL: http://svn.apache.org/viewvc?rev=959021&view=rev
Log:
CLEREZZA-248: added missing changes

Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/AlternativeRepresentationGenerator.java
    incubator/clerezza/trunk/org.apache.clerezza.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.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/AlternativeRepresentationGenerator.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/AlternativeRepresentationGenerator.java?rev=959021&r1=959020&r2=959021&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/AlternativeRepresentationGenerator.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/AlternativeRepresentationGenerator.java Tue Jun 29 16:27:03 2010
@@ -25,6 +25,7 @@ import java.io.IOException;
 import javax.imageio.ImageIO;
 import javax.ws.rs.core.MediaType;
 import org.apache.clerezza.platform.content.DiscobitsHandler;
+import org.apache.clerezza.platform.content.InfoDiscobit;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
@@ -34,6 +35,7 @@ import org.apache.clerezza.utils.imagepr
 import org.apache.clerezza.rdf.ontologies.DISCOBITS;
 import org.apache.clerezza.rdf.utils.GraphNode;
 import org.apache.clerezza.rdf.metadata.MetaDataGenerator;
+import org.apache.felix.scr.annotations.Services;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -47,7 +49,11 @@ import org.osgi.util.tracker.ServiceTrac
  * @author mir
  */
 @Component(metatype=true)
-@Service(MetaDataGenerator.class)
+@Services({
+	@Service(MetaDataGenerator.class),
+	@Service(AlternativeRepresentationGenerator.class)
+})
+
 public class AlternativeRepresentationGenerator implements MetaDataGenerator {
 
 	private static class Resolution {
@@ -67,6 +73,17 @@ public class AlternativeRepresentationGe
 			height = new Integer(widthAndHeight[1]);
 		}
 
+		/**
+		 * A Resolution with the specified width and height.
+		 *
+		 * @param width
+		 * @param height
+		 */
+		public Resolution(int width, int height) {
+			this.width = width;
+			this.height = height;
+		}
+
 		public int getHeight() {
 			return height;
 		}
@@ -130,31 +147,52 @@ public class AlternativeRepresentationGe
 			return;
 		}
 		if (mediaType.getType().startsWith("image")) {
-			try {
-				isAltRepresentation.set(Boolean.TRUE);
-				BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(data));
-				int imgWidth = buffImage.getWidth();
-				int imgHeigth = buffImage.getHeight();
-				for (Resolution resolution : resolutions) {
-					if (imgWidth > resolution.getWidth() ||
-							imgHeigth > resolution.getHeight()){
-						BufferedImage alternativeImage = imageProcessor.makeAThumbnail(
-								buffImage, resolution.getWidth(), resolution.getHeight());
-						byte[] alternativeImageBytes = bufferedImage2ByteArray(alternativeImage, mediaType);
-						DiscobitsHandler contentHandler = (DiscobitsHandler)discobitTracker.getService();
-						UriRef thumbnailUri = createThumbnailUri((UriRef) node.getNode(), alternativeImage);
-						contentHandler.put(thumbnailUri, mediaType, alternativeImageBytes);
-						node.addProperty(DISCOBITS.thumbnail, thumbnailUri);
-					}
+			generateAlternativeImages(data, mediaType, node);
+		}
+	}
+	
+	public UriRef generateAlternativeImage(GraphNode infoBitNode, int width, int height) {
+		try {
+			isAltRepresentation.set(Boolean.TRUE);
+			InfoDiscobit infoBit = new InfoDiscobit(infoBitNode);
+			BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(infoBit.getData()));
+			return generateAlternativeImage(buffImage, new Resolution(width, height), 
+					MediaType.valueOf(infoBit.getContentType()), infoBitNode);
+		} catch (IOException ex) {
+			throw new RuntimeException(ex);
+		} finally {
+			isAltRepresentation.set(Boolean.FALSE);
+		}
+	}
+
+	private void generateAlternativeImages(byte[] data, MediaType mediaType, GraphNode node) throws RuntimeException {
+		try {
+			isAltRepresentation.set(Boolean.TRUE);
+			BufferedImage buffImage = ImageIO.read(new ByteArrayInputStream(data));
+			int imgWidth = buffImage.getWidth();
+			int imgHeigth = buffImage.getHeight();
+			for (Resolution resolution : resolutions) {
+				if (imgWidth > resolution.getWidth() || imgHeigth > resolution.getHeight()) {
+					generateAlternativeImage( buffImage, resolution, mediaType, node);
 				}
-			} catch (IOException ex) {
-				throw new RuntimeException(ex);
-			} finally {
-				isAltRepresentation.set(Boolean.FALSE);
 			}
+		} catch (IOException ex) {
+			throw new RuntimeException(ex);
+		} finally {
+			isAltRepresentation.set(Boolean.FALSE);
 		}
 	}
 
+	private UriRef generateAlternativeImage(BufferedImage buffImage, Resolution resolution, MediaType mediaType, GraphNode node) throws IOException {
+		BufferedImage alternativeImage = imageProcessor.makeAThumbnail(buffImage, resolution.getWidth(), resolution.getHeight());
+		byte[] alternativeImageBytes = bufferedImage2ByteArray(alternativeImage, mediaType);
+		DiscobitsHandler contentHandler = (DiscobitsHandler) discobitTracker.getService();
+		UriRef thumbnailUri = createThumbnailUri((UriRef) node.getNode(), alternativeImage);
+		contentHandler.put(thumbnailUri, mediaType, alternativeImageBytes);
+		node.addProperty(DISCOBITS.thumbnail, thumbnailUri);
+		return thumbnailUri;
+	}
+
 	private byte[] bufferedImage2ByteArray(BufferedImage image,
 			MediaType mediaType) throws IOException {
 		ByteArrayOutputStream baos = new ByteArrayOutputStream();

Modified: incubator/clerezza/trunk/org.apache.clerezza.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.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java?rev=959021&r1=959020&r2=959021&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.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.platform.content.representations/org.apache.clerezza.platform.content.representations.core/src/main/java/org/apache/clerezza/platform/content/representations/core/ThumbnailService.java Tue Jun 29 16:27:03 2010
@@ -72,10 +72,13 @@ public class ThumbnailService implements
 	ContentGraphProvider cgProvider;
 	@Reference
 	PlatformConfig config;
+	@Reference
+	AlternativeRepresentationGenerator altRepGen;
+
 	private static final Logger log = LoggerFactory.getLogger(ThumbnailService.class);
 	private BundleContext bundleContext;
 	private String STATICWEB_PATH = "/org/apache/clerezza/web/resources/style/staticweb/";
-	private String BASE_PATH = STATICWEB_PATH + "images/icons/mediatype/";
+	private String MEDIA_TYPE_BASE_PATH = STATICWEB_PATH + "images/icons/mediatype/";
 	private Bundle cachedStyleBundle = null;
 	private Map<MediaType, String> mediaTypeIconUriCache =
 			Collections.synchronizedMap(new HashMap<MediaType, String>());
@@ -97,9 +100,11 @@ public class ThumbnailService implements
 	 * maximum width and height can optionally be specified over the query parameters
 	 * "width" and "height". If more than one acceptable thumbnail is available
 	 * then the thumbnail uri of the thumbnail with the highest resolution
-	 * (width * height) is returned. If no thumbnail is available then the uri of
-	 * the icon representing the media type is returned. If also no media type
-	 * icon is available the uri to default icon is returned.
+	 * (width * height) is returned. If no thumbnail is available and the logged
+	 * in user has the write permission for the content graph, then an attempt is
+	 * made to create the thumbnail on the fly. If this fails or the write permission
+	 * is missing, then the uri of the icon representing the media type is returned.
+	 * If also no media type icon is available the uri to default icon is returned.
 	 * @param infoBitUri the uri of the infoDiscoBit of which the thumbnail uri should be returned
 	 * @param height the maximum height that the thumbnail has
 	 * @param width the maximum width that the thumbnail has
@@ -107,9 +112,8 @@ public class ThumbnailService implements
 	 */
 	@GET
 	public Response getThumbnailUri(@QueryParam("uri") UriRef infoBitUri,
-			@QueryParam("width") Integer width,
-			@QueryParam("height") Integer height,
-						@Context UriInfo uriInfo) {
+			@QueryParam("width") Integer width,	@QueryParam("height") Integer height,
+			@Context UriInfo uriInfo) {
 		if ((width == null) && (height == null)) {
 			throw new WebApplicationException(new IllegalArgumentException("height and/or width must be specified"),
 					Response.Status.BAD_REQUEST);
@@ -126,10 +130,23 @@ public class ThumbnailService implements
 			return Response.seeOther(
 					URI.create((thumbnailUri).getUnicodeString())).build();
 		}
+		
+		
 		Iterator<Resource> mediaTypes = infoBitNode.getObjects(DISCOBITS.mediaType);
 		if (mediaTypes.hasNext()) {
 			MediaType mediaType = MediaType.valueOf(LiteralFactory.getInstance().createObject(
 					String.class, (TypedLiteral) mediaTypes.next()));
+			// if the infoBit is an image, create a thumbnail on the fly.
+			if (mediaType.getType().startsWith("image")) {
+				try {
+					thumbnailUri = altRepGen.generateAlternativeImage(infoBitNode, width,
+							height);
+					return RedirectUtil.createSeeOtherResponse(thumbnailUri.getUnicodeString(), uriInfo);
+				} catch (Exception ex) {
+					// Was worth a try. eLets go on
+				}
+			}
+
 			String iconUri = mediaTypeIconUriCache.get(mediaType);
 			if (iconUri == null) {
 				iconUri = getMediaTypeIconUri(mediaType);
@@ -146,7 +163,7 @@ public class ThumbnailService implements
 		if (styleBundle == null) {
 			throw new RuntimeException("no style bundle found");
 		}
-		String path = BASE_PATH + mediaType.getType() + "/";
+		String path = MEDIA_TYPE_BASE_PATH + mediaType.getType() + "/";
 		Enumeration entries = styleBundle.findEntries(path,
 				mediaType.getSubtype() + ".*", false);
 		String iconUri = createIconUri(entries);
@@ -162,7 +179,7 @@ public class ThumbnailService implements
 	}
 
 	private String getDefaultIconUrl(Bundle bundle) {
-		Enumeration entries = bundle.findEntries(BASE_PATH, "any.*", false);
+		Enumeration entries = bundle.findEntries(MEDIA_TYPE_BASE_PATH, "any.*", false);
 		String iconUri = createIconUri(entries);
 		if (iconUri != null) {
 			return iconUri;