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 lr...@apache.org on 2011/06/04 06:57:58 UTC
svn commit: r1131332 - in
/incubator/photark/branches/photark-rest/photark-filesystem/src:
main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java
test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java
Author: lresende
Date: Sat Jun 4 06:57:57 2011
New Revision: 1131332
URL: http://svn.apache.org/viewvc?rev=1131332&view=rev
Log:
Tidying up file system photo stream provider
Modified:
incubator/photark/branches/photark-rest/photark-filesystem/src/main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java
incubator/photark/branches/photark-rest/photark-filesystem/src/test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java
Modified: incubator/photark/branches/photark-rest/photark-filesystem/src/main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-filesystem/src/main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java?rev=1131332&r1=1131331&r2=1131332&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark-filesystem/src/main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java (original)
+++ incubator/photark/branches/photark-rest/photark-filesystem/src/main/java/org/apache/photark/providers/filesystem/FileSystemPhotoStreamProvider.java Sat Jun 4 06:57:57 2011
@@ -20,9 +20,9 @@
package org.apache.photark.providers.filesystem;
import java.io.File;
-
import java.io.FilenameFilter;
import java.io.IOException;
+import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@@ -33,7 +33,6 @@ import org.apache.photark.Image;
import org.apache.photark.providers.PhotoStreamProvider;
import org.apache.photark.services.PhotarkRuntimeException;
import org.apache.photark.subscription.SubscriptionConfig;
-import org.apache.photark.subscription.SubscriptionConfig;
import org.oasisopen.sca.annotation.Property;
public class FileSystemPhotoStreamProvider implements PhotoStreamProvider {
@@ -43,6 +42,10 @@ public class FileSystemPhotoStreamProvid
private String galleryRoot;
+ public FileSystemPhotoStreamProvider() {
+ this.galleryRoot = "";
+ }
+
public FileSystemPhotoStreamProvider(@Property(name="galleryRoot") String galleryRoot) {
this.galleryRoot = galleryRoot;
}
@@ -69,6 +72,7 @@ public class FileSystemPhotoStreamProvid
Image image = new Image();
image.setId(pictureName);
image.setName(pictureName);
+ image.setTitle(pictureName);
image.setLocation("http://localhost:8085/gallery/" + config.getName() + "/" + pictureName);
images.add(image);
@@ -77,78 +81,45 @@ public class FileSystemPhotoStreamProvid
return images;
}
- public Image getImage(SubscriptionConfig config, String Id) throws PhotarkRuntimeException {
- Image image = null;
- File albumFolder = null;
- try {
- albumFolder = getAlbumFolder(config);
- } catch (IOException e) {
- if(logger.isLoggable(Level.WARNING)) {
- logger.log(Level.WARNING, "Error retrieving photo stream from '" + config.getUrl() + "' :" + e.getMessage(), e);
- }
- throw new PhotarkRuntimeException("Error retrieving photo stream from '" + config.getUrl() + "' :" + e.getMessage(), e);
- }
+ private File getAlbumFolder(SubscriptionConfig config) throws IOException {
- String[] listPictures = albumFolder.list(new ImageFilter(".jpg"));
- for(String pictureName : listPictures) {
- if(pictureName.equalsIgnoreCase(Id)) {
- image = new Image();
- image.setId(pictureName);
- image.setName(pictureName);
- image.setLocation("http://localhost:8085/gallery/" + config.getName() + "/" + pictureName);
- }
- }
+ String albumFolderName = config.getUrl();
- if(image == null) {
- if(logger.isLoggable(Level.WARNING)) {
- logger.log(Level.WARNING, "Error retrieving photo stream from '" + config.getUrl() + "'");
- }
- throw new PhotarkRuntimeException("Error retrieving photo stream from '" + config.getUrl() + "'");
+ if(galleryRoot != null && galleryRoot.isEmpty() == false) {
+ albumFolderName = galleryRoot + File.pathSeparator + albumFolderName;
}
- return image;
- }
-
- public String addImage(SubscriptionConfig album, Image image) throws PhotarkRuntimeException {
- throw new java.lang.UnsupportedOperationException("Operation not supported in album subscriptions");
- }
-
- public void updateImage(SubscriptionConfig album, Image image) throws PhotarkRuntimeException {
- throw new java.lang.UnsupportedOperationException("Operation not supported in album subscriptions");
- }
-
- public void deleteImage(SubscriptionConfig album, String Id) throws PhotarkRuntimeException {
- throw new java.lang.UnsupportedOperationException("Operation not supported in album subscriptions");
- }
+ try {
+ URL albumFolderURL = null;
- private File getAlbumFolder(SubscriptionConfig config) throws IOException {
+ URI albumFolderURI = new URI(config.getUrl());
+ if (albumFolderURI.isAbsolute()) {
+ if ("file".equals(albumFolderURI.getScheme())) {
+ albumFolderURL = albumFolderURI.toURL();
+ }
+ }
- String albumFolderName = galleryRoot + File.pathSeparator + config.getName();
- try {
- URL albumFolderURL = this.getClass().getClassLoader().getResource(config.getName());
- if(albumFolderURL == null) {
+ if (albumFolderURL == null) {
+ albumFolderURL = this.getClass().getClassLoader().getResource(config.getName());
+ }
+ if (albumFolderURL == null) {
// Accomodate for J2EE classpath that starts in WEB-INF\classes
albumFolderURL = this.getClass().getClassLoader().getResource("../../" + albumFolderName);
}
- if(albumFolderURL == null) {
+ if (albumFolderURL == null) {
// Workaroud for Google apps Engine
- String albumDir = System.getProperty("user.dir") + "/" + albumFolderName;
+ String albumDir = System.getProperty("user.dir") + "/" + albumFolderName;
albumFolderURL = new java.net.URL("file://" + albumDir);
}
- if(albumFolderURL != null) {
+ if (albumFolderURL != null) {
File albumFolder = new File(albumFolderURL.toURI());
if (albumFolder.isDirectory() && albumFolder.exists()) {
java.io.File[] albums = albumFolder.listFiles();
- for(File albumFile : albums) {
- if(! albumFile.getName().startsWith(".")) {
- if(albumFile.isDirectory() && albumFile.exists()) {
- return albumFolder;
- }
- }
- }
+ return albumFolder;
}
+
}
} catch (Exception e) {
throw new IOException("Error retrieving photo stream from '" + config.getUrl() + "' :" + e.getMessage(), e);
Modified: incubator/photark/branches/photark-rest/photark-filesystem/src/test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-filesystem/src/test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java?rev=1131332&r1=1131331&r2=1131332&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark-filesystem/src/test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java (original)
+++ incubator/photark/branches/photark-rest/photark-filesystem/src/test/java/org/apache/photark/album/filesystem/FileSystemProviderTestCase.java Sat Jun 4 06:57:57 2011
@@ -19,141 +19,54 @@
package org.apache.photark.album.filesystem;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.io.File;
+import java.net.URL;
import java.util.List;
-import junit.framework.Assert;
-
-import org.apache.tuscany.sca.node.Contribution;
-import org.apache.tuscany.sca.node.ContributionLocationHelper;
-import org.apache.tuscany.sca.node.Node;
-import org.apache.tuscany.sca.node.NodeFactory;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.junit.AfterClass;
+import org.apache.photark.Image;
+import org.apache.photark.providers.filesystem.FileSystemPhotoStreamProvider;
+import org.apache.photark.subscription.SubscriptionConfig;
import org.junit.BeforeClass;
import org.junit.Test;
-import com.meterware.httpunit.GetMethodWebRequest;
-import com.meterware.httpunit.PostMethodWebRequest;
-import com.meterware.httpunit.WebConversation;
-import com.meterware.httpunit.WebRequest;
-import com.meterware.httpunit.WebResponse;
-
/**
* Verify various operations for the FileSystem Gallery
*
* @version $Rev$ $Date$
*/
public class FileSystemProviderTestCase {
- private static final String GALLERY_SERVICE_URL = "http://localhost:8085/gallery";
+ private static final String GALLERY_ROOT = "gallery-root";
+ private static final String ALBUM_1 = GALLERY_ROOT + File.separatorChar + "album-1";
+ private static final String ALBUM_2 = GALLERY_ROOT + File.separatorChar + "album-2";
- private static Node node;
+ private static FileSystemPhotoStreamProvider fileSystemPhotoStreamProvider;
@BeforeClass
public static void BeforeClass() {
- try {
- String contribution = ContributionLocationHelper.getContributionLocation("gallery.composite");
- node = NodeFactory.newInstance().createNode("gallery.composite", new Contribution("gallery", contribution));
- node.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
+ fileSystemPhotoStreamProvider = new FileSystemPhotoStreamProvider();
}
- @AfterClass
- public static void AfterClass() {
- node.stop();
- }
@Test
- public void testGetAlbums() throws IOException, JSONException {
- String[] albums = readAlbums();
+ public void testDiscoverAlbums() throws Exception {
+ List<Image> images = fileSystemPhotoStreamProvider.getImages(createSubcriptionConfig(ALBUM_1));
- Assert.assertNotNull(albums);
- }
-
- @Test
- public void testAddAlbums() throws IOException, JSONException {
- String[] albums = readAlbums();
- int albumSize = albums.length;
-
- addAlbum();
-
- albums = readAlbums();
-
- Assert.assertEquals(albumSize + 1, albums.length);
- }
-
- @Test
- public void testRemoveAlbums() throws IOException, JSONException {
- WebConversation wc = new WebConversation();
- WebRequest request = new GetMethodWebRequest(GALLERY_SERVICE_URL + "/" + getLastAlbumName());
- ((GetMethodWebRequest) request).setMethod("DELETE");
- WebResponse response = wc.getResource(request);
-
- Assert.assertEquals(200, response.getResponseCode());
- }
-
-
- private void addAlbum() throws IOException, JSONException {
- JSONObject jsonAlbum = new JSONObject();
- jsonAlbum.put("name", getNewAlbumName());
- jsonAlbum.put("location", "http://localhost:8080/gallery/album/Boston");
- jsonAlbum.put("description", "Some description goes here");
-
- WebConversation wc = new WebConversation();
- WebRequest request = new PostMethodWebRequest(GALLERY_SERVICE_URL, new ByteArrayInputStream(jsonAlbum.toString().getBytes("UTF-8")),"application/json");
- request.setHeaderField("Content-Type", "application/json");
- WebResponse response = wc.getResource(request);
-
- Assert.assertEquals(204, response.getResponseCode());
- }
-
- private String[] readAlbums() throws IOException, JSONException {
- WebConversation wc = new WebConversation();
- WebRequest request = new GetMethodWebRequest(GALLERY_SERVICE_URL);
- WebResponse response = wc.getResource(request);
-
- JSONObject jsonResponse = new JSONObject(response.getText());
-
- //for debug purposes
- System.out.println(">>>" + jsonResponse.toString());
-
- JSONArray albums = (org.json.JSONArray) jsonResponse.get("albums");
- List<String> albumNames = new ArrayList<String>();
- for(int pos=0; pos<albums.length(); pos++) {
- JSONObject album = (JSONObject) albums.get(pos);
- albumNames.add( album.getString("name"));
+ for(Image image : images) {
+ System.out.println(">>>>>>>>>>>>>");
+ System.out.println(">> ID : " + image.getId());
+ System.out.println(">> Title : " + image.getTitle());
+ System.out.println(">> Location : " + image.getLocation());
}
-
- String[] albumNameArray = new String[albumNames.size()];
- albumNames.toArray(albumNameArray);
- Arrays.sort(albumNameArray, String.CASE_INSENSITIVE_ORDER);
-
- return albumNameArray;
}
- private String getNewAlbumName() throws IOException, JSONException {
- String[] albums = readAlbums();
- String album = null;
- if (albums.length == 0) {
- album = "album-0";
- } else {
- album = albums[albums.length -1 ];
- String[] tokens = album.split("-");
- album = tokens[0] + "-" + (Integer.parseInt(tokens[1]) + 1);
- }
- return album;
- }
+ private static SubscriptionConfig createSubcriptionConfig(String albumName) {
+ URL albumURL = FileSystemProviderTestCase.class.getClassLoader().getResource(albumName);
- private String getLastAlbumName() throws IOException, JSONException {
- String[] albums = readAlbums();
- String album = albums[albums.length -1];
+ SubscriptionConfig album = new SubscriptionConfig();
+ album.setId("24662369");
+ album.setName("Album 1");
+ album.setType(fileSystemPhotoStreamProvider.getProviderType());
+ album.setUrl(albumURL.toString());
return album;
}