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;
* 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 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.
* @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;
}
/**
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);
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;
@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;
@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);
}
}
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);
}
}
- @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() {