Author: mir
Date: Wed Jun 30 12:55:12 2010
New Revision: 959293

URL: http://svn.apache.org/viewvc?rev=959293&view=rev
Log:
CLEREZZA-248: merged issue with trunk, because the feature is needed.

Removed:
    incubator/clerezza/issues/CLEREZZA-248/
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/
   (props changed)
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/
   (props changed)
    
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
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java

Propchange: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jun 30 12:55:12 2010
@@ -2,6 +2,7 @@
 
/incubator/clerezza/issues/CLEREZZA-113/org.apache.clerezza.platform.content:908544-911013
 
/incubator/clerezza/issues/CLEREZZA-119/org.apache.clerezza.platform.content:917505-924213
 
/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.platform.content:917938-918563
+/incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content:958551-959023
 
/incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content:891178-892849
 
/incubator/clerezza/issues/CLEREZZA-65/org.apache.clerezza.platform.content:897875-897920
 
/incubator/clerezza/issues/CLEREZZA-70/org.apache.clerezza.platform.content:897917-903631

Propchange: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jun 30 12:55:12 2010
@@ -1,2 +1,3 @@
 
/incubator/clerezza/issues/CLEREZZA-126/org.apache.clerezza.platform.content.representations:910212-916002
+/incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content.representations:958551-959023
 
/incubator/clerezza/issues/CLEREZZA-38/org.apache.clerezza.platform.content.representations:893127-895998

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=959293&r1=959292&r2=959293&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
 Wed Jun 30 12:55:12 2010
@@ -22,9 +22,11 @@ import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.util.concurrent.locks.Lock;
 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 +36,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 +50,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 +74,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,28 +148,59 @@ 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 = 
InfoDiscobit.createInstance(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);
+               Lock writeLock = node.writeLock();
+               writeLock.lock();
+               try {
+                       node.addProperty(DISCOBITS.thumbnail, thumbnailUri);
+                       return thumbnailUri;
+               } finally {
+                       writeLock.unlock();
                }
        }
 

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=959293&r1=959292&r2=959293&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
 Wed Jun 30 12:55:12 2010
@@ -25,6 +25,7 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.locks.Lock;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.QueryParam;
@@ -72,10 +73,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 +101,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 +113,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);
@@ -125,11 +130,23 @@ public class ThumbnailService implements
                if (thumbnailUri != null) {
                        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;
@@ -186,16 +203,22 @@ public class ThumbnailService implements
                }
                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,
-                                       cgProvider.getContentGraph());
-                       int thumbnailPixels = 
getSurfaceSizeIfFitting(thumbnailNode, width, height);
-                       if (thumbnailPixels > pixels) {
-                               resultThumbnailUri = thumbnailUri;
-                               pixels = thumbnailPixels;
+               Lock readLock = infoBitNode.readLock();
+               readLock.lock();
+               try {
+                       Iterator<Resource> thumbnails = 
infoBitNode.getObjects(DISCOBITS.thumbnail);
+                       while (thumbnails.hasNext()) {
+                               UriRef thumbnailUri = (UriRef) 
thumbnails.next();
+                               GraphNode thumbnailNode = new 
GraphNode(thumbnailUri,
+                                               cgProvider.getContentGraph());
+                               int thumbnailPixels = 
getSurfaceSizeIfFitting(thumbnailNode, width, height);
+                               if (thumbnailPixels > pixels) {
+                                       resultThumbnailUri = thumbnailUri;
+                                       pixels = thumbnailPixels;
+                               }
                        }
+               } finally {
+                       readLock.unlock();
                }
                return resultThumbnailUri;
        }
@@ -208,7 +231,7 @@ public class ThumbnailService implements
                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");
+                       log.warn(infoBitNode.getNode() + " doesn't have 
exif:width and exif:height");
                        return -1;
                }
                Integer thumbnailWidth = 
LiteralFactory.getInstance().createObject(
@@ -225,14 +248,20 @@ public class ThumbnailService implements
         * 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;
+               Lock readLock = infoBitNode.readLock();
+               readLock.lock();
+               try {
+                       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;
+                       }
+               } finally {
+                       readLock.unlock();
                }
        }
 

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java?rev=959293&r1=959292&r2=959293&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
 Wed Jun 30 12:55:12 2010
@@ -16,67 +16,67 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.platform.content;
-
-import java.util.Iterator;
-import org.apache.clerezza.rdf.core.Literal;
-import org.apache.clerezza.rdf.core.LiteralFactory;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.TypedLiteral;
-
-import org.apache.clerezza.rdf.utils.GraphNode;
-import org.apache.clerezza.rdf.ontologies.DISCOBITS;
-import org.apache.clerezza.rdf.ontologies.RDF;
-
-/**
- * Represents an InfoDiscoBit
- *
- * @author reto
- */
-class InfoDiscobit {
-
-       private GraphNode infoBit;
-
-
-       /**
-        *
-        * @param infoBit
-        * @return an instance of InfoDiscobit or null if node is not an 
InfoDiscoBit
-        */
-       static InfoDiscobit createInstance(GraphNode node) {
-               Iterator<Resource> types = node.getObjects(RDF.type);
-               while(types.hasNext()) {
-                       if (types.next().equals(DISCOBITS.InfoDiscoBit)){
-                               return new InfoDiscobit(node);
-                       }
-               }
-               return null;
-       }
-
-       private InfoDiscobit(GraphNode infoBit) {
-               this.infoBit = infoBit;
-       }
-
-       public String getContentType() {
-               Iterator<Literal> mediaTypeLits = 
infoBit.getLiterals(DISCOBITS.mediaType);
-               if (mediaTypeLits.hasNext()) {
-                       return mediaTypeLits.next().getLexicalForm();
-               }
-               return null;
-       }
-       
-       public byte[] getData() {
-               byte[] result = null;
-               Iterator<Literal> mediaTypeLits = 
infoBit.getLiterals(DISCOBITS.infoBit);
-               if (mediaTypeLits.hasNext()) {
-                       final Literal literalValue = mediaTypeLits.next();
-                       if (literalValue instanceof TypedLiteral) {
-                               result = 
LiteralFactory.getInstance().createObject(
-                                               (new 
byte[0]).getClass(),(TypedLiteral)literalValue);
-                       }
-               }
-               
-               return result;  
-       };
-
-}
+package org.apache.clerezza.platform.content;
+
+import java.util.Iterator;
+import org.apache.clerezza.rdf.core.Literal;
+import org.apache.clerezza.rdf.core.LiteralFactory;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.rdf.ontologies.DISCOBITS;
+import org.apache.clerezza.rdf.ontologies.RDF;
+
+/**
+ * Represents an InfoDiscoBit
+ *
+ * @author reto
+ */
+public class InfoDiscobit {
+
+       private GraphNode infoBit;
+
+
+       /**
+        *
+        * @param infoBit
+        * @return an instance of InfoDiscobit or null if node is not an 
InfoDiscoBit
+        */
+       public static InfoDiscobit createInstance(GraphNode node) {
+               Iterator<Resource> types = node.getObjects(RDF.type);
+               while(types.hasNext()) {
+                       if (types.next().equals(DISCOBITS.InfoDiscoBit)){
+                               return new InfoDiscobit(node);
+                       }
+               }
+               return null;
+       }
+
+       InfoDiscobit(GraphNode infoBit) {
+               this.infoBit = infoBit;
+       }
+
+       public String getContentType() {
+               Iterator<Literal> mediaTypeLits = 
infoBit.getLiterals(DISCOBITS.mediaType);
+               if (mediaTypeLits.hasNext()) {
+                       return mediaTypeLits.next().getLexicalForm();
+               }
+               return null;
+       }
+       
+       public byte[] getData() {
+               byte[] result = null;
+               Iterator<Literal> mediaTypeLits = 
infoBit.getLiterals(DISCOBITS.infoBit);
+               if (mediaTypeLits.hasNext()) {
+                       final Literal literalValue = mediaTypeLits.next();
+                       if (literalValue instanceof TypedLiteral) {
+                               result = 
LiteralFactory.getInstance().createObject(
+                                               (new 
byte[0]).getClass(),(TypedLiteral)literalValue);
+                       }
+               }
+               
+               return result;  
+       };
+
+}


Reply via email to