Author: mir
Date: Mon Jun 28 12:10:44 2010
New Revision: 958553

URL: http://svn.apache.org/viewvc?rev=958553&view=rev
Log:
CLEREZZA-248: missing thumbnails are now created on the fly, if user has 
permission 

Added:
    incubator/clerezza/issues/CLEREZZA-248/
      - copied from r958550, incubator/clerezza/issues/CLEREZZA-CLEREZZA-248/
    incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content/
      - copied from r958550, 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/
    
incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content.representations/
      - copied from r958550, 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content.representations/
Removed:
    incubator/clerezza/issues/CLEREZZA-CLEREZZA-248/
Modified:
    
incubator/clerezza/issues/CLEREZZA-248/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/issues/CLEREZZA-248/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/issues/CLEREZZA-248/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java

Modified: 
incubator/clerezza/issues/CLEREZZA-248/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/issues/CLEREZZA-248/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=958553&r1=958550&r2=958553&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-248/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/issues/CLEREZZA-248/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
 Mon Jun 28 12:10:44 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,55 @@ 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);
+               node.addProperty(DISCOBITS.thumbnail, thumbnailUri);
+               return thumbnailUri;
+       }
+
        private byte[] bufferedImage2ByteArray(BufferedImage image,
                        MediaType mediaType) throws IOException {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

Modified: 
incubator/clerezza/issues/CLEREZZA-248/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/issues/CLEREZZA-248/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=958553&r1=958550&r2=958553&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-248/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/issues/CLEREZZA-248/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
 Mon Jun 28 12:10:44 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;

Modified: 
incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java?rev=958553&r1=958550&r2=958553&view=diff
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
 (original)
+++ 
incubator/clerezza/issues/CLEREZZA-248/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/InfoDiscobit.java
 Mon Jun 28 12:10:44 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