Author: mir
Date: Tue Dec 15 17:30:45 2009
New Revision: 890896

URL: http://svn.apache.org/viewvc?rev=890896&view=rev
Log:
CLEREZZA-25: implemented ThumbnailGenerator

Added:
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/pom.xml
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/
    
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/ThumbnailGenerator.java

Added: 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/pom.xml?rev=890896&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/pom.xml
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/pom.xml
 Tue Dec 15 17:30:45 2009
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?><project 
xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <artifactId>org.apache.clerezza.parent</artifactId>
+               <groupId>org.apache.clerezza</groupId>
+               <version>0.2-SNAPSHOT</version>
+       </parent>
+       <groupId>org.apache.clerezza</groupId>
+       <artifactId>org.apache.clerezza.platform.content.thumbnails</artifactId>
+       <version>0.1-SNAPSHOT</version>
+       <packaging>bundle</packaging>
+       <name>Clerezza - Platform Content Thumbnails</name>
+       <description>Provides a Meta Data Generator that generates thumbnails 
for data of
+               various meta types</description>
+       <dependencies>
+               <dependency>
+                       <groupId>org.osgi</groupId>
+                       <artifactId>org.osgi.core</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.osgi</groupId>
+                       <artifactId>org.osgi.compendium</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.core</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       <artifactId>org.apache.clerezza.rdf.utils</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       
<artifactId>org.apache.clerezza.platform.content</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.clerezza</groupId>
+                       
<artifactId>org.apache.clerezza.utils.imageprocessing</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.felix</groupId>
+                       
<artifactId>org.apache.felix.scr.annotations</artifactId>
+               </dependency>
+       </dependencies>
+</project>

Added: 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/ThumbnailGenerator.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/ThumbnailGenerator.java?rev=890896&view=auto
==============================================================================
--- 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/ThumbnailGenerator.java
 (added)
+++ 
incubator/clerezza/issues/CLEREZZA-25/org.apache.clerezza.platform.content.thumbnails/src/main/java/org/apache/clerezza/platform/content/thumbnails/ThumbnailGenerator.java
 Tue Dec 15 17:30:45 2009
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.platform.content.thumbnails;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.ws.rs.core.MediaType;
+import org.apache.clerezza.platform.content.DiscobitsHandler;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.utils.imageprocessing.ImageProcessor;
+import org.apache.clerezza.rdf.ontologies.DISCOBITS;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.platform.content.MetaDataGenerator;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * This class provides a method that generates a thumbnail for specified data
+ * if the specified media type is supported. The supported media types are 
"image/*".
+ * The generated thumbnail is added as a property to the specified
+ * <code>GraphNode</code>. The property URI is
+ * "http://discobits.org/ontology#thumbnail";.
+ *
+ * @author mir
+ */
+...@component
+...@service(MetaDataGenerator.class)
+public class ThumbnailGenerator implements MetaDataGenerator {
+
+       @Property(intValue = 50, description = "Specifies the width of the 
thumbnail.")
+       private static final String THUMBNAIL_WIDTH = "thumbnailWidth";
+       @Property(intValue = 50, description = "Specifies the height of the 
thumbnail.")
+       private static final String THUMBNAIL_HEIGHT = "thumbnailHeight";
+       @Reference
+       private ImageProcessor imageProcessor;
+       private int widthThumbnail;
+       private int heightThumbnail;
+
+       private ServiceTracker discobitTracker;
+
+       protected void activate(ComponentContext context) {
+
+               widthThumbnail = (Integer) 
context.getProperties().get(THUMBNAIL_WIDTH);
+               heightThumbnail = (Integer) 
context.getProperties().get(THUMBNAIL_HEIGHT);
+
+               discobitTracker = new ServiceTracker(context.getBundleContext(),
+                               DiscobitsHandler.class.getName(), null);
+               new Thread() {
+                       @Override
+                       public void run() {
+                               discobitTracker.open();
+                       }
+               }.start();
+       }
+
+       protected void deactivate(ComponentContext context) {
+               discobitTracker.close();
+               discobitTracker = null;
+       }
+
+       @Override
+       public void generate(GraphNode node, byte[] data, MediaType mediaType) {
+               if (node.getSubjects(DISCOBITS.thumbnail).hasNext()) {
+                       return;
+               }
+               if (mediaType.getType().startsWith("image")) {
+                       try {
+                               BufferedImage buffImage = ImageIO.read(new 
ByteArrayInputStream(data));
+                               BufferedImage thumbnail = 
imageProcessor.makeAThumbnail(buffImage, widthThumbnail, heightThumbnail);
+                               byte[] thumbnailBytes = 
bufferedImage2ByteArray(thumbnail, mediaType);
+                               DiscobitsHandler contentHandler = 
(DiscobitsHandler)discobitTracker.getService();
+                               UriRef thumbnailUri = 
createThumbnailUri((UriRef) node.getNode());
+                               node.addProperty(DISCOBITS.thumbnail, 
thumbnailUri);
+                               contentHandler.put(thumbnailUri, mediaType, 
thumbnailBytes);
+                       } catch (IOException ex) {
+                               return;
+                       }
+               }
+       }
+
+       private byte[] bufferedImage2ByteArray(BufferedImage image,
+                       MediaType mediaType) throws IOException {
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ImageIO.write(image, mediaType.getSubtype(), baos);
+               byte[] bytes = baos.toByteArray();
+               baos.flush();
+               return bytes;
+       }
+
+       private UriRef createThumbnailUri(UriRef uriRef) {
+               String thumbnail = "_thumbnail";
+               String oldUri = uriRef.getUnicodeString();
+               String newUri;
+               int lastIndexOfDot = oldUri.lastIndexOf(".");
+               if (lastIndexOfDot >= (oldUri.length() - 4)) {
+                       String firstPart = oldUri.substring(0, lastIndexOfDot);
+                       String lastPart = oldUri.substring(lastIndexOfDot, 
oldUri.length());
+                       newUri = firstPart + thumbnail + lastPart;
+               } else {
+                       newUri = oldUri + thumbnail;
+               }
+               return new UriRef(newUri);
+       }
+}


Reply via email to