Authorized album is now working fully.
[photos] / src / main / java / org / wamblee / photos / model / authorization / AuthorizedAlbum.java
index 7406d9985f6e5e3308c8fab514a47c3514ecb82b..48fb47ace6be662593ceb477ad20e399278120aa 100644 (file)
@@ -19,14 +19,21 @@ import java.io.IOException;
 import java.io.InputStream;
 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.apache.log4j.Logger;
 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,16 +44,23 @@ 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);
+    private static final Logger LOGGER = Logger.getLogger(AuthorizedAlbum.class
+        .getName());
 
     private AuthorizationService _authorizer;
 
     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(),
-                new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
-                    public ArrayList<PhotoEntry> getObject(String aObjectKey) {
-                        return AuthorizedAlbum.this.compute();
-                    }
-                });
-        _sessionId = aSessionId;
+            aCache, aSession.getId() + "/" + aAlbum.getPath(),
+            new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
+                public ArrayList<PhotoEntry> getObject(String aObjectKey) {
+                    return AuthorizedAlbum.this.compute();
+                }
+            });
+        _session = aSession;
     }
 
     /**
@@ -105,10 +122,10 @@ 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);
+                "Entry is neither a photo nor an album: " + aEntry);
         }
     }
 
@@ -139,8 +156,8 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
                     return entry;
                 } else {
                     if (!(entry instanceof Album)) {
-                        throw new IllegalArgumentException(getPath() + " "
-                                + aPath);
+                        throw new IllegalArgumentException(getPath() + " " +
+                            aPath);
                     }
                     return ((Album) entry).getEntry(remainder);
                 }
@@ -198,15 +215,15 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
     public void removeEntry(String aId) throws IOException {
         // Check whether deletion is allowed.
         PhotoEntry entry = _authorizer.check(decorated().getEntry("/" + aId),
-                new DeleteOperation());
+            new DeleteOperation());
         _authorizedEntries.invalidate();
         decorated().removeEntry(aId);
     }
 
     public Photo findPhotoBefore(String aId) {
         Photo entry = decorated().findPhotoBefore(aId);
-        while (entry != null
-                && !_authorizer.isAllowed(entry, new AllOperation())) {
+        while (entry != null &&
+            !_authorizer.isAllowed(entry, new AllOperation())) {
             entry = decorated().findPhotoBefore(entry.getId());
         }
         return decorate(entry);
@@ -214,8 +231,8 @@ public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
 
     public Photo findPhotoAfter(String aId) {
         Photo entry = decorated().findPhotoAfter(aId);
-        while (entry != null
-                && !_authorizer.isAllowed(entry, new AllOperation())) {
+        while (entry != null &&
+            !_authorizer.isAllowed(entry, new AllOperation())) {
             entry = decorated().findPhotoAfter(entry.getId());
         }
         return decorate(entry);