2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.model.authorization;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.apache.log4j.Logger;
24 import org.wamblee.cache.Cache;
25 import org.wamblee.cache.CachedObject;
26 import org.wamblee.photos.model.Album;
27 import org.wamblee.photos.model.Path;
28 import org.wamblee.photos.model.Photo;
29 import org.wamblee.photos.model.PhotoEntry;
30 import org.wamblee.security.authorization.AllOperation;
31 import org.wamblee.security.authorization.AuthorizationService;
32 import org.wamblee.security.authorization.DeleteOperation;
33 import org.wamblee.security.authorization.ReadOperation;
34 import org.wamblee.security.authorization.WriteOperation;
37 * Decorator for an album providing defined behavior when used in a concurrent
40 public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
42 private static final Logger LOGGER = Logger
43 .getLogger(AuthorizedAlbum.class);
45 private AuthorizationService _authorizer;
47 private CachedObject<String, ArrayList<PhotoEntry>> _authorizedEntries;
49 private String _sessionId;
52 * Constructs concurrent album as a decorator for an album implementation.
57 public AuthorizedAlbum(Album aAlbum, AuthorizationService aService,
58 Cache aCache, String aSessionId) {
60 _authorizer = aService;
61 _authorizedEntries = new CachedObject<String, ArrayList<PhotoEntry>>(
62 aCache, aSessionId + "/" + aAlbum.getPath(),
63 new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
64 public ArrayList<PhotoEntry> getObject(String aObjectKey) {
65 return AuthorizedAlbum.this.compute();
68 _sessionId = aSessionId;
72 * Computes the cache of photo entries to which read access is allowed.
74 * @return Photo entries to which read access is allowed.
76 private synchronized ArrayList<PhotoEntry> compute() {
77 LOGGER.info("Refreshing cache " + getPath());
78 ArrayList<PhotoEntry> result = new ArrayList<PhotoEntry>();
79 for (int i = 0; i < decorated().size(); i++) {
80 PhotoEntry entry = decorated().getEntry(i);
81 if (_authorizer.isAllowed(entry, new ReadOperation())) {
82 result.add(decorate(entry)); // subscription will take place
89 private Album decorated() {
90 return (Album) getEntry();
94 * Creates a decorate for the photo entry to make it safe for concurrent
99 * @return Decorated photo.
101 private <T extends PhotoEntry> T decorate(T aEntry) {
102 if (aEntry == null) {
104 } else if (aEntry instanceof Photo) {
105 return (T) new AuthorizedPhoto((Photo) aEntry);
106 } else if (aEntry instanceof Album) {
107 return (T) new AuthorizedAlbum((Album) aEntry, _authorizer,
108 _authorizedEntries.getCache(), _sessionId);
110 throw new IllegalArgumentException(
111 "Entry is neither a photo nor an album: " + aEntry);
118 * @see org.wamblee.photos.model.Album#getEntry(java.lang.String)
120 public PhotoEntry getEntry(String aPath) {
121 return getEntry(new Path(aPath));
127 * @see org.wamblee.photos.model.Album#getEntry(org.wamblee.photos.model.Path)
129 public PhotoEntry getEntry(Path aPath) {
130 if (aPath.isRoot()) {
133 List<PhotoEntry> cache = _authorizedEntries.get();
134 String id = aPath.getPart(0);
135 Path remainder = aPath.remainder();
136 for (PhotoEntry entry : cache) {
137 if (entry.getId().equals(id)) {
138 if (remainder.isRoot()) {
141 if (!(entry instanceof Album)) {
142 throw new IllegalArgumentException(getPath() + " "
145 return ((Album) entry).getEntry(remainder);
155 * @see org.wamblee.photos.model.Album#getEntry(int)
157 public PhotoEntry getEntry(int aIndex) {
158 return _authorizedEntries.get().get(aIndex);
164 * @see org.wamblee.photos.model.Album#size()
167 return _authorizedEntries.get().size();
173 * @see org.wamblee.photos.model.Album#addImage(java.lang.String,
174 * java.io.InputStream)
176 public void addImage(String aId, InputStream aImage) throws IOException {
177 _authorizer.check(this, new WriteOperation());
178 _authorizedEntries.invalidate();
179 decorated().addImage(aId, aImage);
185 * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String)
187 public void addAlbum(String aId) throws IOException {
188 _authorizer.check(this, new WriteOperation());
189 _authorizedEntries.invalidate();
190 decorated().addAlbum(aId);
196 * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String)
198 public void removeEntry(String aId) throws IOException {
199 // Check whether deletion is allowed.
200 PhotoEntry entry = _authorizer.check(decorated().getEntry("/" + aId),
201 new DeleteOperation());
202 _authorizedEntries.invalidate();
203 decorated().removeEntry(aId);
206 public Photo findPhotoBefore(String aId) {
207 Photo entry = decorated().findPhotoBefore(aId);
209 && !_authorizer.isAllowed(entry, new AllOperation())) {
210 entry = decorated().findPhotoBefore(entry.getId());
212 return decorate(entry);
215 public Photo findPhotoAfter(String aId) {
216 Photo entry = decorated().findPhotoAfter(aId);
218 && !_authorizer.isAllowed(entry, new AllOperation())) {
219 entry = decorated().findPhotoAfter(entry.getId());
221 return decorate(entry);
227 * @see java.lang.Object#toString()
230 public String toString() {
231 return "AuthorizedAlbum(path = '" + decorated().getPath() + "')";