From be2dfde47261118e3f67f1c100bb935bc0a1b581 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 15 Sep 2013 23:26:20 +0200 Subject: [PATCH] Authorized album is now working fully. --- .../org/wamblee/photos/model/PhotoEntry.java | 33 ++++--- .../model/authorization/AuthorizedAlbum.java | 29 ++++-- .../model/plumbing/AuthorizedPhotos.java | 2 +- .../photos/model/plumbing/Producer.java | 96 ++++++++++--------- .../org/wamblee/photos/wicket/HomePage.java | 9 +- 5 files changed, 95 insertions(+), 74 deletions(-) diff --git a/src/main/java/org/wamblee/photos/model/PhotoEntry.java b/src/main/java/org/wamblee/photos/model/PhotoEntry.java index b360eaf..11c2f48 100644 --- a/src/main/java/org/wamblee/photos/model/PhotoEntry.java +++ b/src/main/java/org/wamblee/photos/model/PhotoEntry.java @@ -12,23 +12,26 @@ * 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 { - - /** - * 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, 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(); } diff --git a/src/main/java/org/wamblee/photos/model/authorization/AuthorizedAlbum.java b/src/main/java/org/wamblee/photos/model/authorization/AuthorizedAlbum.java index 96610a6..48fb47a 100644 --- a/src/main/java/org/wamblee/photos/model/authorization/AuthorizedAlbum.java +++ b/src/main/java/org/wamblee/photos/model/authorization/AuthorizedAlbum.java @@ -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> _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> aCache, + HttpSession aSession) { super(aAlbum); _authorizer = aService; _authorizedEntries = new CachedObject>( - aCache, aSessionId + "/" + aAlbum.getPath(), + aCache, aSession.getId() + "/" + aAlbum.getPath(), new CachedObject.Computation>() { public ArrayList 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); diff --git a/src/main/java/org/wamblee/photos/model/plumbing/AuthorizedPhotos.java b/src/main/java/org/wamblee/photos/model/plumbing/AuthorizedPhotos.java index 935a79e..5a1ceeb 100644 --- a/src/main/java/org/wamblee/photos/model/plumbing/AuthorizedPhotos.java +++ b/src/main/java/org/wamblee/photos/model/plumbing/AuthorizedPhotos.java @@ -25,7 +25,7 @@ import javax.inject.Qualifier; @Qualifier @Retention(RUNTIME) -@Target({ METHOD, PARAMETER, FIELD }) +@Target({ TYPE, METHOD, PARAMETER, FIELD }) public @interface AuthorizedPhotos { // Empty. } diff --git a/src/main/java/org/wamblee/photos/model/plumbing/Producer.java b/src/main/java/org/wamblee/photos/model/plumbing/Producer.java index 4e5b1da..a3d9c2a 100644 --- a/src/main/java/org/wamblee/photos/model/plumbing/Producer.java +++ b/src/main/java/org/wamblee/photos/model/plumbing/Producer.java @@ -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 userCache; + + @Inject + @PhotoCache + private Cache> 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 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 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> getPhotoCache() { + try { + InputResource cacheConfig = new ClassPathResource( + "META-INF/ehcache.xml"); + return new EhCache>(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> photoCache = new EhCache>( - 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 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() { diff --git a/src/main/java/org/wamblee/photos/wicket/HomePage.java b/src/main/java/org/wamblee/photos/wicket/HomePage.java index ccb6670..acc2877 100644 --- a/src/main/java/org/wamblee/photos/wicket/HomePage.java +++ b/src/main/java/org/wamblee/photos/wicket/HomePage.java @@ -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()); } - */ } } -- 2.31.1