You are viewing a plain text version of this content. The canonical link for it is here.
Posted to photark-commits@incubator.apache.org by av...@apache.org on 2010/02/12 17:49:37 UTC

svn commit: r909524 - in /incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark: services/ImageDisplayerImpl.java services/album/jcr/AlbumImpl.java upload/ArchiveFileExtractor.java upload/FileUploader.java upload/PhotoUploadServlet.java

Author: avd
Date: Fri Feb 12 17:49:37 2010
New Revision: 909524

URL: http://svn.apache.org/viewvc?rev=909524&view=rev
Log:
Adding ImageDisplayer component which is used for retrieved images from JCR and displaying images to front end.

Added:
    incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/ImageDisplayerImpl.java
Modified:
    incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/album/jcr/AlbumImpl.java
    incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/ArchiveFileExtractor.java
    incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/FileUploader.java
    incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/PhotoUploadServlet.java

Added: incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/ImageDisplayerImpl.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/ImageDisplayerImpl.java?rev=909524&view=auto
==============================================================================
--- incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/ImageDisplayerImpl.java (added)
+++ incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/ImageDisplayerImpl.java Fri Feb 12 17:49:37 2010
@@ -0,0 +1,86 @@
+/*
+ * 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.photark.services;
+
+import java.io.InputStream;
+
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.photark.services.gallery.jcr.JCRSession;
+import org.apache.tuscany.sca.data.collection.Entry;
+import org.apache.tuscany.sca.data.collection.NotFoundException;
+import org.oasisopen.sca.annotation.Init;
+
+public class ImageDisplayerImpl implements ImageDisplayer {
+	private Session session = JCRSession.getSession();
+
+	@Init
+	public void init() {
+	}
+
+	public ImageDisplayerImpl() {
+	}
+
+	public InputStream get(String key) throws NotFoundException {
+		String sub = StringUtils.substringAfter(key, "splayer/");
+		String stringArray[] = StringUtils.split(sub, "/");
+		String albumName = stringArray[0];
+		InputStream inStream = null;
+		try {
+			Node root = session.getRootNode();
+			Node albumNode = root.getNode(albumName);
+			String image = stringArray[1];
+			Node picNode = albumNode.getNode(image);
+			inStream = picNode.getProperty("imageContent").getStream();
+
+		} catch (PathNotFoundException e) {
+			e.printStackTrace();
+		} catch (RepositoryException e) {
+			e.printStackTrace();
+		}
+		return inStream;
+	}
+
+	public Entry<String, InputStream>[] getAll() {
+		return null;
+	}
+
+	public void delete(String key) {
+
+	}
+
+	public void put(String key, InputStream inputStream) {
+
+	}
+
+	public String post(String key, InputStream inputStream) {
+		System.out.println("insdie post:" + key);
+
+		return key;
+	}
+
+	public Entry<String, InputStream>[] query(String queryString) {
+		return null;
+	}
+}
\ No newline at end of file

Modified: incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/album/jcr/AlbumImpl.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/album/jcr/AlbumImpl.java?rev=909524&r1=909523&r2=909524&view=diff
==============================================================================
--- incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/album/jcr/AlbumImpl.java (original)
+++ incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/services/album/jcr/AlbumImpl.java Fri Feb 12 17:49:37 2010
@@ -20,6 +20,7 @@
 package org.apache.photark.services.album.jcr;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
@@ -81,12 +82,11 @@
                     		  if(!albumNode.hasNode(image))
                     		  {
                     			  Node picNode=albumNode.addNode(image);
-                    			  InputStream inFile = getClass().getClassLoader().getResourceAsStream(getLocation()+image);
-                    			//  picNode.setProperty("image", inFile );
+                    			  String imagePath = albumURL.getPath() + image;
+                    			  InputStream imageContent = new FileInputStream(new File(imagePath));
+                    			  picNode.setProperty("imageContent", imageContent );
                     			  picNode.setProperty("name", image);
                     			  picNode.setProperty("location", image);
-                    			  //image = getLocation() + image;
-                    			  //pictures.add(image);
                     		  }
                     	  }
                       }
@@ -183,6 +183,8 @@
 			Node root = session.getRootNode();
 			Node albumNode = root.getNode(name);
 			Node picNode = albumNode.addNode(picture.getName());
+			picture.getInputStream();
+			picNode.setProperty("imageContent", picture.getInputStream());
 			picNode.setProperty("name", picture.getName());
 			picNode.setProperty("location", picture.getName());
 			session.save();

Modified: incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/ArchiveFileExtractor.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/ArchiveFileExtractor.java?rev=909524&r1=909523&r2=909524&view=diff
==============================================================================
--- incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/ArchiveFileExtractor.java (original)
+++ incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/ArchiveFileExtractor.java Fri Feb 12 17:49:37 2010
@@ -19,53 +19,42 @@
 
 package org.apache.photark.upload;
 
-import java.io.File;
-import java.io.FileOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.io.IOUtils;
+import org.apache.photark.Picture;
+
 
 public class ArchiveFileExtractor 
 {
 	private String entryTypes[];
-	private String albumDir;
 		
-	public ArchiveFileExtractor(String albumDir, String [] entryTypes){
-		this.albumDir = albumDir;
+	public ArchiveFileExtractor(String [] entryTypes){
 		this.entryTypes = entryTypes;
 	}
 		
-	public List<String> extractArchive(InputStream inStream){
+	public List<Picture> extractArchive(InputStream inStream){
 		ArchiveStreamFactory streamFactory = new ArchiveStreamFactory();
-		List<String> pictures = new ArrayList<String>();
+		List<Picture> pictures = new ArrayList<Picture>();
 		try 
 		{
 			ArchiveInputStream archiveInputStream = streamFactory.createArchiveInputStream(inStream);
 			ArchiveEntry entry = null;
-			 byte[] buffer = new byte[2048];
 			while((entry = archiveInputStream.getNextEntry())!= null){
-				System.out.println("entry:"+entry.getName());
 				if(!entry.isDirectory() && isEntryTypeAllowd(entry)){
-					String outpath = albumDir + File.separator + entry.getName();
-					File file = new File(outpath);
-					FileOutputStream outputStream = null;
-					 try{
-						 outputStream = new FileOutputStream(file);
-					 	 int len = 0;
-					 	 while ((len = archiveInputStream.read(buffer)) > 0){
-					 		 outputStream.write(buffer, 0, len);
-					 	  }
-					 	pictures.add(entry.getName());
-					 }
-					finally{
-						if(outputStream!=null) outputStream.close();
-					}
+					byte buf[] = IOUtils.toByteArray(archiveInputStream);
+					InputStream inputStream = new ByteArrayInputStream(buf);
+					Picture picture = new Picture(entry.getName(), new Date(), inputStream);
+					pictures.add(picture);
 				}
 			}
 		} 
@@ -101,9 +90,11 @@
 	
 	
 	/**
-	 * Test wether the extension of given entry is allowed or not
-	 * @param entry
-	 * @return
+	 * Test weather the extension of given entry is allowed or not
+	 * 
+	 * @param entry ArchiveEntry
+	 * 
+	 * @return boolean
 	 */
 	private boolean isEntryTypeAllowd(ArchiveEntry entry){
 		String entryName = entry.getName();

Modified: incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/FileUploader.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/FileUploader.java?rev=909524&r1=909523&r2=909524&view=diff
==============================================================================
--- incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/FileUploader.java (original)
+++ incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/FileUploader.java Fri Feb 12 17:49:37 2010
@@ -19,54 +19,39 @@
 
 package org.apache.photark.upload;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.commons.compress.archivers.ArchiveException;
 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
-import org.apache.commons.io.IOUtils;
+import org.apache.photark.Picture;
 
 public class FileUploader {
 	
 	private String entryTypes [] = {".jpg",".jpeg",".png",".gif"}; 
 	
-	private String albumDir;
-	
-	public FileUploader(String albumDir){
-		this.albumDir = albumDir;
+	public FileUploader(){
 	}
 	
 	
-	public List<String> uploadFile(InputStream inStream, String fileName) throws IOException{
+	public List<Picture> uploadFile(InputStream inStream, String fileName) throws IOException{
 		
-		List<String> pictures = new ArrayList<String>();
+		List<Picture> pictures = new ArrayList<Picture>();
 		
 		if(isArchive(inStream)){
-			ArchiveFileExtractor archiveFileExtractor = new ArchiveFileExtractor(albumDir, entryTypes);
+			ArchiveFileExtractor archiveFileExtractor = new ArchiveFileExtractor(entryTypes);
 			pictures = archiveFileExtractor.extractArchive(inStream);
 		}
 		else{
 			// this is a picture file and not the archive file
-			uploadPicToAlbumDir(albumDir, fileName, inStream);
-			pictures.add(fileName);
+			Picture picture = new Picture(fileName, new Date(), inStream);
+			pictures.add(picture);
 		}
-		IOUtils.closeQuietly(inStream);
 		return pictures;
 	}
-	
-	
-	private void uploadPicToAlbumDir(String albumDir, String fileName ,InputStream inStream) throws IOException
-	{
-		String uploadPath = albumDir +  File.separator + fileName;
-		File file = new File(uploadPath);
-		FileOutputStream outStream = new FileOutputStream(file);
-		IOUtils.copy(inStream, outStream);
-		IOUtils.closeQuietly(outStream);
-	}
 
 	
 	/**

Modified: incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/PhotoUploadServlet.java
URL: http://svn.apache.org/viewvc/incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/PhotoUploadServlet.java?rev=909524&r1=909523&r2=909524&view=diff
==============================================================================
--- incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/PhotoUploadServlet.java (original)
+++ incubator/photark/trunk/photark-jcr/src/main/java/org/apache/photark/upload/PhotoUploadServlet.java Fri Feb 12 17:49:37 2010
@@ -20,15 +20,12 @@
 package org.apache.photark.upload;
 
 import java.io.BufferedInputStream;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
-import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -46,7 +43,6 @@
 {
 	private static final long serialVersionUID = 1L;
 	public static final long MAX_UPLOAD_ZIP_IN_MEGS = 30;
-	private String galleryDir = "gallery/";
 	
 	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
 		doPost(request, response);
@@ -59,9 +55,6 @@
 		if (!isMultipartContent) {
 			return;
 		}
-		
-		ServletContext context = request.getSession().getServletContext();
-		String realPath = context.getRealPath("");
 
 		FileItemFactory factory = new DiskFileItemFactory();
 		ServletFileUpload upload = new ServletFileUpload(factory);
@@ -90,20 +83,13 @@
 				if(!isFormField)
 				{	
 					String fileName = fileItem.getName();
-					String albumDir = realPath + File.separator + galleryDir + albumName;
-					
-					if(!isAlbumDirExits(albumDir)){
-						File f = new File(albumDir);
-						f.mkdir();
-					}
 					
 					InputStream inStream = fileItem.getInputStream();
 					
-					FileUploader uploader = new FileUploader(albumDir);
-					List<String> picsList = uploader.uploadFile(new BufferedInputStream(inStream), fileName);
+					FileUploader uploader = new FileUploader();
+					List<Picture> pictures = uploader.uploadFile(new BufferedInputStream(inStream), fileName);
 					
-					for(String pic :picsList){
-						Picture picture = new Picture(pic, new Date());
+					for(Picture picture :pictures){
 						addPictureToAlbum(albumName, picture);
 					}
 					sb.append("file=uploaded/" + fileName);
@@ -124,14 +110,6 @@
 		}
 	}
 	
-	private boolean isAlbumDirExits(String albumFolderPath){
-		File albumFolder = new File(albumFolderPath);
-		if(albumFolder.exists() && albumFolder.isDirectory()){
-			return true;
-		}
-		return false;
-	}
-	
 	
 	/**
 	 *