Authorized album is now working fully.
authorErik Brakkee <erik@brakkee.org>
Sun, 15 Sep 2013 21:26:20 +0000 (23:26 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 15 Sep 2013 21:26:20 +0000 (23:26 +0200)
src/main/java/org/wamblee/photos/model/PhotoEntry.java
src/main/java/org/wamblee/photos/model/authorization/AuthorizedAlbum.java
src/main/java/org/wamblee/photos/model/plumbing/AuthorizedPhotos.java
src/main/java/org/wamblee/photos/model/plumbing/Producer.java
src/main/java/org/wamblee/photos/wicket/HomePage.java

index b360eafe9235fa36c55375ad11e538635cd491d8..11c2f485eff10f2281731f3577ba8dcf1390d3c2 100644 (file)
  * 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.wamblee.photos.model;
 
+import java.io.Serializable;
+
 /**
- * Represents an entry inside a photo album. 
+ * Represents an entry inside a photo album.
  */
-public interface PhotoEntry extends Comparable<PhotoEntry> {
-       
-       /**
-        * Photo entry id. 
-        * @return Id of the entry. 
-        */
-       String getId(); 
-       
-       /**
-        * Gets the path of the given photo with respect to the 
-        * root album. The path will start with a "/".  
-        */
-       String getPath(); 
+public interface PhotoEntry extends Comparable<PhotoEntry>, Serializable {
+
+    /**
+     * Photo entry id.
+     * 
+     * @return Id of the entry.
+     */
+    String getId();
+
+    /**
+     * Gets the path of the given photo with respect to the root album. The path
+     * will start with a "/".
+     */
+    String getPath();
 }
index 96610a6c940a92e05967aed7f98bd40579179530..48fb47ace6be662593ceb477ad20e399278120aa 100644 (file)
@@ -21,12 +21,19 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Logger;
 
+import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
+import javax.servlet.http.HttpSession;
+
 import org.wamblee.cache.Cache;
 import org.wamblee.cache.CachedObject;
 import org.wamblee.photos.model.Album;
 import org.wamblee.photos.model.Path;
 import org.wamblee.photos.model.Photo;
 import org.wamblee.photos.model.PhotoEntry;
+import org.wamblee.photos.model.plumbing.AllPhotos;
+import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
+import org.wamblee.photos.model.plumbing.PhotoCache;
 import org.wamblee.security.authorization.AllOperation;
 import org.wamblee.security.authorization.AuthorizationService;
 import org.wamblee.security.authorization.DeleteOperation;
@@ -37,6 +44,8 @@ import org.wamblee.security.authorization.WriteOperation;
  * Decorator for an album providing defined behavior when used in a concurrent
  * setting.
  */
+@SessionScoped
+@AuthorizedPhotos
 public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
 
     private static final Logger LOGGER = Logger.getLogger(AuthorizedAlbum.class
@@ -46,7 +55,12 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
 
     private CachedObject<String, ArrayList<PhotoEntry>> _authorizedEntries;
 
-    private String _sessionId;
+    private HttpSession _session;
+
+    protected AuthorizedAlbum() {
+        super(null);
+        // for CDI
+    }
 
     /**
      * Constructs concurrent album as a decorator for an album implementation.
@@ -54,18 +68,21 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
      * @param aAlbum
      *            Album to decorate.
      */
-    public AuthorizedAlbum(Album aAlbum, AuthorizationService aService,
-        Cache aCache, String aSessionId) {
+    @Inject
+    public AuthorizedAlbum(@AllPhotos Album aAlbum,
+        AuthorizationService aService,
+        @PhotoCache Cache<String, ArrayList<PhotoEntry>> aCache,
+        HttpSession aSession) {
         super(aAlbum);
         _authorizer = aService;
         _authorizedEntries = new CachedObject<String, ArrayList<PhotoEntry>>(
-            aCache, aSessionId + "/" + aAlbum.getPath(),
+            aCache, aSession.getId() + "/" + aAlbum.getPath(),
             new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
                 public ArrayList<PhotoEntry> getObject(String aObjectKey) {
                     return AuthorizedAlbum.this.compute();
                 }
             });
-        _sessionId = aSessionId;
+        _session = aSession;
     }
 
     /**
@@ -105,7 +122,7 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
             return (T) new AuthorizedPhoto((Photo) aEntry);
         } else if (aEntry instanceof Album) {
             return (T) new AuthorizedAlbum((Album) aEntry, _authorizer,
-                _authorizedEntries.getCache(), _sessionId);
+                _authorizedEntries.getCache(), _session);
         } else {
             throw new IllegalArgumentException(
                 "Entry is neither a photo nor an album: " + aEntry);
index 935a79ef98372cfaff5dacc882b56b33bfb9afd7..5a1ceeb295e6a841c3cb4742c58561805a8400dc 100644 (file)
@@ -25,7 +25,7 @@ import javax.inject.Qualifier;
 
 @Qualifier
 @Retention(RUNTIME)
-@Target({ METHOD, PARAMETER, FIELD })
+@Target({ TYPE, METHOD, PARAMETER, FIELD })
 public @interface AuthorizedPhotos {
     // Empty.
 }
index 4e5b1da7b6a2a1540c3fb9c11bab938993f0dd09..a3d9c2a74f75525050cfc36887f674e36e4287b1 100644 (file)
@@ -38,7 +38,6 @@ import org.wamblee.io.InputResource;
 import org.wamblee.photos.concurrent.ConcurrentAlbum;
 import org.wamblee.photos.model.Album;
 import org.wamblee.photos.model.PhotoEntry;
-import org.wamblee.photos.model.authorization.AuthorizedAlbum;
 import org.wamblee.photos.model.filesystem.FileSystemAlbum;
 import org.wamblee.photos.security.PageAuthorizationRule;
 import org.wamblee.photos.security.PhotoAuthorizationRule;
@@ -104,6 +103,14 @@ public class Producer {
     @AllPhotos
     private Album allPhotos;
 
+    @Inject
+    @UserCache
+    private Cache<String, User> userCache;
+
+    @Inject
+    @PhotoCache
+    private Cache<String, ArrayList<PhotoEntry>> photoCache;
+
     private Configuration getCOnfiguration() {
         LOGGER.info("Initializing configuration");
         Configuration config;
@@ -122,32 +129,51 @@ public class Producer {
     @ApplicationScoped
     public UserAdministration getUserAdmin() {
         LOGGER.info("Initializing user administration");
+        NameValidator passwordvalidator = new RegexpNameValidator(".{5,}",
+            "INVALID_PASSWORD", "Password must have at least 5 characters");
+        MessageDigester passwordEncoder = new Md5HexMessageDigester();
+        UserSet userset = new JpaUserSet(userCache, passwordvalidator,
+            passwordEncoder, entityManager);
+        GroupSet groupset = new JpaGroupSet(entityManager);
+        NameValidator uservalidator = new RegexpNameValidator(
+            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
+            "User name must consist of alphanumeric characters only");
+        NameValidator groupvalidator = new RegexpNameValidator(
+            "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
+            "Group name must consist of alphanumeric characters only");
+
+        UserAdministration admin = new UserAdministrationImpl(userset,
+            groupset, uservalidator, groupvalidator);
+        UserAdminInitializer initializer = new UserAdminInitializer(admin,
+            new String[] { "erik", "admin" }, new String[] { "users",
+                "administrators" }, new String[] { "abc123", "abc123" });
+        return admin;
+    }
+
+    @Produces
+    @ApplicationScoped
+    @UserCache
+    public Cache<String, User> getUserCache() {
         try {
-            NameValidator passwordvalidator = new RegexpNameValidator(".{5,}",
-                "INVALID_PASSWORD", "Password must have at least 5 characters");
             InputResource cacheConfig = new ClassPathResource(
                 "META-INF/ehcache.xml");
-            Cache<String, User> userCache = new EhCache(cacheConfig, "users");
-            MessageDigester passwordEncoder = new Md5HexMessageDigester();
-            UserSet userset = new JpaUserSet(userCache, passwordvalidator,
-                passwordEncoder, entityManager);
-            GroupSet groupset = new JpaGroupSet(entityManager);
-            NameValidator uservalidator = new RegexpNameValidator(
-                "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_USERNAME",
-                "User name must consist of alphanumeric characters only");
-            NameValidator groupvalidator = new RegexpNameValidator(
-                "[a-zA-Z]+[a-zA-Z0-9]*", "INVALID_GROUPNAME",
-                "Group name must consist of alphanumeric characters only");
-
-            UserAdministration admin = new UserAdministrationImpl(userset,
-                groupset, uservalidator, groupvalidator);
-            UserAdminInitializer initializer = new UserAdminInitializer(admin,
-                new String[] { "erik", "admin" }, new String[] { "users",
-                    "administrators" }, new String[] { "abc123", "abc123" });
-            return admin;
+            return new EhCache(cacheConfig, "users");
         } catch (IOException e) {
-            throw new RuntimeException(
-                "Could not initialize user administration", e);
+            throw new RuntimeException("Could not create user cache", e);
+        }
+    }
+
+    @Produces
+    @ApplicationScoped
+    @PhotoCache
+    public Cache<String, ArrayList<PhotoEntry>> getPhotoCache() {
+        try {
+            InputResource cacheConfig = new ClassPathResource(
+                "META-INF/ehcache.xml");
+            return new EhCache<String, ArrayList<PhotoEntry>>(cacheConfig,
+                "photos");
+        } catch (IOException e) {
+            throw new RuntimeException("Could not create photo cache", e);
         }
     }
 
@@ -200,10 +226,6 @@ public class Producer {
 
         try {
             File dir = new File(getCOnfiguration().getPath());
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
-            Cache<String, ArrayList<PhotoEntry>> photoCache = new EhCache<String, ArrayList<PhotoEntry>>(
-                cacheConfig, "photos");
             Album fileSystemAlbum = new FileSystemAlbum(dir, "/", photoCache);
             Album concurrentAlbum = new ConcurrentAlbum(fileSystemAlbum);
 
@@ -213,26 +235,6 @@ public class Producer {
         }
     }
 
-    @Produces
-    @SessionScoped
-    @AuthorizedPhotos
-    public Album getAuthorizedAlbum() {
-        LOGGER.info("Initializing authorized photos for current session");
-        try {
-            InputResource cacheConfig = new ClassPathResource(
-                "META-INF/ehcache.xml");
-            Cache<String, User> userCache = new EhCache(cacheConfig, "users");
-            Cache authorizedPhotoCache = new EhCache(cacheConfig, "photos");
-
-            AuthorizedAlbum album = new AuthorizedAlbum(allPhotos,
-                authorizationService, authorizedPhotoCache, session.getId());
-            return album;
-        } catch (IOException e) {
-            throw new RuntimeException("Problem initializing authorized album",
-                e);
-        }
-    }
-
     @Produces
     @SessionScoped
     public User getUser() {
index ccb667034e39cf7bd512dfaada84396f83b280c0..acc2877089ee1cf209e25cfceea83048fc5cb473 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.wicket.markup.html.basic.Label;
 import org.wamblee.photos.model.Album;
 import org.wamblee.photos.model.PhotoEntry;
 import org.wamblee.photos.model.plumbing.AllPhotos;
+import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
 import org.wamblee.security.authentication.User;
 import org.wamblee.security.authentication.UserAdministration;
 
@@ -44,9 +45,9 @@ public class HomePage extends BasePage {
     @AllPhotos
     private Album album;
 
-    // @Inject
-    // @AuthorizedPhotos
-    // private Album authorized;
+    @Inject
+    @AuthorizedPhotos
+    private Album authorized;
 
     /**
      * Constructor that is invoked when page is invoked without a session.
@@ -73,13 +74,11 @@ public class HomePage extends BasePage {
                 entry.getPath());
         }
 
-        /*
         System.out.println("Authorized Entries: " + authorized.size());
         for (int i = 0; i < authorized.size(); i++) {
             PhotoEntry entry = authorized.getEntry(i);
             System.out.println("Entry " + i + " " + entry.getId() + " " +
                 entry.getPath());
         }
-        */
     }
 }