just before adding authorization service.
[photos] / src / main / java / org / wamblee / photos / model / authorization / AuthorizedAlbum.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.model.authorization;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.List;
22
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;
35
36 /**
37  * Decorator for an album providing defined behavior when used in a concurrent
38  * setting.
39  */
40 public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
41
42     private static final Logger LOGGER = Logger
43             .getLogger(AuthorizedAlbum.class);
44
45     private AuthorizationService _authorizer;
46
47     private CachedObject<String, ArrayList<PhotoEntry>> _authorizedEntries;
48
49     private String _sessionId;
50
51     /**
52      * Constructs concurrent album as a decorator for an album implementation.
53      * 
54      * @param aAlbum
55      *            Album to decorate.
56      */
57     public AuthorizedAlbum(Album aAlbum, AuthorizationService aService,
58             Cache aCache, String aSessionId) {
59         super(aAlbum);
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();
66                     }
67                 });
68         _sessionId = aSessionId;
69     }
70
71     /**
72      * Computes the cache of photo entries to which read access is allowed.
73      * 
74      * @return Photo entries to which read access is allowed.
75      */
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
83                 // automatically.
84             }
85         }
86         return result;
87     }
88
89     private Album decorated() {
90         return (Album) getEntry();
91     }
92
93     /**
94      * Creates a decorate for the photo entry to make it safe for concurrent
95      * access.
96      * 
97      * @param aEntry
98      *            Entry to decorate
99      * @return Decorated photo.
100      */
101     private <T extends PhotoEntry> T decorate(T aEntry) {
102         if (aEntry == null) {
103             return 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);
109         } else {
110             throw new IllegalArgumentException(
111                     "Entry is neither a photo nor an album: " + aEntry);
112         }
113     }
114
115     /*
116      * (non-Javadoc)
117      * 
118      * @see org.wamblee.photos.model.Album#getEntry(java.lang.String)
119      */
120     public PhotoEntry getEntry(String aPath) {
121         return getEntry(new Path(aPath));
122     }
123
124     /*
125      * (non-Javadoc)
126      * 
127      * @see org.wamblee.photos.model.Album#getEntry(org.wamblee.photos.model.Path)
128      */
129     public PhotoEntry getEntry(Path aPath) {
130         if (aPath.isRoot()) {
131             return this;
132         }
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()) {
139                     return entry;
140                 } else {
141                     if (!(entry instanceof Album)) {
142                         throw new IllegalArgumentException(getPath() + " "
143                                 + aPath);
144                     }
145                     return ((Album) entry).getEntry(remainder);
146                 }
147             }
148         }
149         return null;
150     }
151
152     /*
153      * (non-Javadoc)
154      * 
155      * @see org.wamblee.photos.model.Album#getEntry(int)
156      */
157     public PhotoEntry getEntry(int aIndex) {
158         return _authorizedEntries.get().get(aIndex);
159     }
160
161     /*
162      * (non-Javadoc)
163      * 
164      * @see org.wamblee.photos.model.Album#size()
165      */
166     public int size() {
167         return _authorizedEntries.get().size();
168     }
169
170     /*
171      * (non-Javadoc)
172      * 
173      * @see org.wamblee.photos.model.Album#addImage(java.lang.String,
174      *      java.io.InputStream)
175      */
176     public void addImage(String aId, InputStream aImage) throws IOException {
177         _authorizer.check(this, new WriteOperation());
178         _authorizedEntries.invalidate();
179         decorated().addImage(aId, aImage);
180     }
181
182     /*
183      * (non-Javadoc)
184      * 
185      * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String)
186      */
187     public void addAlbum(String aId) throws IOException {
188         _authorizer.check(this, new WriteOperation());
189         _authorizedEntries.invalidate();
190         decorated().addAlbum(aId);
191     }
192
193     /*
194      * (non-Javadoc)
195      * 
196      * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String)
197      */
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);
204     }
205
206     public Photo findPhotoBefore(String aId) {
207         Photo entry = decorated().findPhotoBefore(aId);
208         while (entry != null
209                 && !_authorizer.isAllowed(entry, new AllOperation())) {
210             entry = decorated().findPhotoBefore(entry.getId());
211         }
212         return decorate(entry);
213     }
214
215     public Photo findPhotoAfter(String aId) {
216         Photo entry = decorated().findPhotoAfter(aId);
217         while (entry != null
218                 && !_authorizer.isAllowed(entry, new AllOperation())) {
219             entry = decorated().findPhotoAfter(entry.getId());
220         }
221         return decorate(entry);
222     }
223
224     /*
225      * (non-Javadoc)
226      * 
227      * @see java.lang.Object#toString()
228      */
229     @Override
230     public String toString() {
231         return "AuthorizedAlbum(path = '" + decorated().getPath() + "')";
232     }
233 }