@AuthorizedPhotos
public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
- private static final Logger LOGGER = Logger.getLogger(AuthorizedAlbum.class
- .getName());
+ private static final Logger LOGGER = Logger.getLogger(AuthorizedAlbum.class.getName());
private AuthorizationService _authorizer;
private HttpSession _session;
- protected AuthorizedAlbum() {
- super(null);
- // for CDI
- }
-
/**
* Constructs concurrent album as a decorator for an album implementation.
- *
- * @param aAlbum
- * Album to decorate.
+ *
+ * @param aAlbum Album to decorate.
*/
@Inject
- public AuthorizedAlbum(@AllPhotos Album aAlbum,
- AuthorizationService aService,
- @PhotoCache Cache<String, ArrayList<PhotoEntry>> aCache,
- HttpSession aSession) {
+ 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, aSession.getId() + "/" + aAlbum.getPath(),
- new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
- public ArrayList<PhotoEntry> getObject(String aObjectKey) {
- return AuthorizedAlbum.this.compute();
- }
- });
+ _authorizedEntries = new CachedObject<String, ArrayList<PhotoEntry>>(aCache,
+ "session:" + aSession.getId() + "/" + aAlbum.getPath(),
+ new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
+ public ArrayList<PhotoEntry> getObject(String aObjectKey) {
+ return AuthorizedAlbum.this.compute();
+ }
+ });
_session = aSession;
}
/**
* Computes the cache of photo entries to which read access is allowed.
- *
+ *
* @return Photo entries to which read access is allowed.
*/
private synchronized ArrayList<PhotoEntry> compute() {
// automatically.
}
}
+ if (result == null) {
+ throw new RuntimeException("Result is null");
+ }
return result;
}
/**
* Creates a decorate for the photo entry to make it safe for concurrent
* access.
- *
- * @param aEntry
- * Entry to decorate
+ *
+ * @param aEntry Entry to decorate
* @return Decorated photo.
*/
private <T extends PhotoEntry> T decorate(T aEntry) {
} else if (aEntry instanceof Photo) {
return (T) new AuthorizedPhoto((Photo) aEntry);
} else if (aEntry instanceof Album) {
- return (T) new AuthorizedAlbum((Album) aEntry, _authorizer,
- _authorizedEntries.getCache(), _session);
+ return (T) new AuthorizedAlbum((Album) aEntry, _authorizer, _authorizedEntries.getCache(), _session);
} else {
- throw new IllegalArgumentException(
- "Entry is neither a photo nor an album: " + aEntry);
+ throw new IllegalArgumentException("Entry is neither a photo nor an album: " + aEntry);
}
}
} else {
if (!(entry instanceof Album)) {
throw new IllegalArgumentException(getPath() + " " +
- aPath);
+ aPath);
}
return ((Album) entry).getEntry(remainder);
}
*/
public void addImage(String aId, InputStream aImage) throws IOException {
_authorizer.check(this, new WriteOperation());
+ int oldsize = _authorizedEntries.get().size();
_authorizedEntries.invalidate();
decorated().addImage(aId, aImage);
+ int newsize = _authorizedEntries.get().size();
+ if (newsize != oldsize + 1) {
+ throw new RuntimeException("cache was not refreshed property");
+ }
}
/*
*/
public void removeEntry(String aId) throws IOException {
// Check whether deletion is allowed.
- PhotoEntry entry = _authorizer.check(decorated().getEntry("/" + aId),
- new DeleteOperation());
+ PhotoEntry entry = _authorizer.check(decorated().getEntry("/" + aId), 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);
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);