Working upload of photos (individual and zip)
[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 import java.util.logging.Logger;
23
24 import javax.enterprise.context.SessionScoped;
25 import javax.inject.Inject;
26 import javax.servlet.http.HttpSession;
27
28 import org.wamblee.cache.Cache;
29 import org.wamblee.cache.CachedObject;
30 import org.wamblee.photos.model.Album;
31 import org.wamblee.photos.model.Path;
32 import org.wamblee.photos.model.Photo;
33 import org.wamblee.photos.model.PhotoEntry;
34 import org.wamblee.photos.model.plumbing.AllPhotos;
35 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
36 import org.wamblee.photos.model.plumbing.PhotoCache;
37 import org.wamblee.security.authorization.AllOperation;
38 import org.wamblee.security.authorization.AuthorizationService;
39 import org.wamblee.security.authorization.DeleteOperation;
40 import org.wamblee.security.authorization.ReadOperation;
41 import org.wamblee.security.authorization.WriteOperation;
42
43 /**
44  * Decorator for an album providing defined behavior when used in a concurrent
45  * setting.
46  */
47 @SessionScoped
48 @AuthorizedPhotos
49 public class AuthorizedAlbum extends AuthorizedPhotoEntry implements Album {
50
51     private static final Logger LOGGER = Logger.getLogger(AuthorizedAlbum.class.getName());
52
53     private AuthorizationService _authorizer;
54
55     private CachedObject<String, ArrayList<PhotoEntry>> _authorizedEntries;
56
57     private HttpSession _session;
58
59     /**
60      * Constructs concurrent album as a decorator for an album implementation.
61      *
62      * @param aAlbum Album to decorate.
63      */
64     @Inject
65     public AuthorizedAlbum(@AllPhotos Album aAlbum, AuthorizationService aService,
66             @PhotoCache Cache<String, ArrayList<PhotoEntry>> aCache, HttpSession aSession) {
67         super(aAlbum);
68         _authorizer = aService;
69         _authorizedEntries = new CachedObject<String, ArrayList<PhotoEntry>>(aCache,
70                 "session:" + aSession.getId() + "/" + aAlbum.getPath(),
71                 new CachedObject.Computation<String, ArrayList<PhotoEntry>>() {
72                     public ArrayList<PhotoEntry> getObject(String aObjectKey) {
73                         return AuthorizedAlbum.this.compute();
74                     }
75                 });
76         _session = aSession;
77     }
78
79     /**
80      * Computes the cache of photo entries to which read access is allowed.
81      *
82      * @return Photo entries to which read access is allowed.
83      */
84     private synchronized ArrayList<PhotoEntry> compute() {
85         LOGGER.info("Refreshing cache " + getPath());
86         ArrayList<PhotoEntry> result = new ArrayList<PhotoEntry>();
87         for (int i = 0; i < decorated().size(); i++) {
88             PhotoEntry entry = decorated().getEntry(i);
89             if (_authorizer.isAllowed(entry, new ReadOperation())) {
90                 result.add(decorate(entry)); // subscription will take place
91                 // automatically.
92             }
93         }
94         if (result == null) {
95             throw new RuntimeException("Result is null");
96         }
97         return result;
98     }
99
100     private Album decorated() {
101         return (Album) getEntry();
102     }
103
104     /**
105      * Creates a decorate for the photo entry to make it safe for concurrent
106      * access.
107      *
108      * @param aEntry Entry to decorate
109      * @return Decorated photo.
110      */
111     private <T extends PhotoEntry> T decorate(T aEntry) {
112         if (aEntry == null) {
113             return null;
114         } else if (aEntry instanceof Photo) {
115             return (T) new AuthorizedPhoto((Photo) aEntry);
116         } else if (aEntry instanceof Album) {
117             return (T) new AuthorizedAlbum((Album) aEntry, _authorizer, _authorizedEntries.getCache(), _session);
118         } else {
119             throw new IllegalArgumentException("Entry is neither a photo nor an album: " + aEntry);
120         }
121     }
122
123     /*
124      * (non-Javadoc)
125      * 
126      * @see org.wamblee.photos.model.Album#getEntry(java.lang.String)
127      */
128     public PhotoEntry getEntry(String aPath) {
129         return getEntry(new Path(aPath));
130     }
131
132     /*
133      * (non-Javadoc)
134      * 
135      * @see org.wamblee.photos.model.Album#getEntry(org.wamblee.photos.model.Path)
136      */
137     public PhotoEntry getEntry(Path aPath) {
138         if (aPath.isRoot()) {
139             return this;
140         }
141         List<PhotoEntry> cache = _authorizedEntries.get();
142         String id = aPath.getPart(0);
143         Path remainder = aPath.remainder();
144         for (PhotoEntry entry : cache) {
145             if (entry.getId().equals(id)) {
146                 if (remainder.isRoot()) {
147                     return entry;
148                 } else {
149                     if (!(entry instanceof Album)) {
150                         throw new IllegalArgumentException(getPath() + " " +
151                                 aPath);
152                     }
153                     return ((Album) entry).getEntry(remainder);
154                 }
155             }
156         }
157         return null;
158     }
159
160     /*
161      * (non-Javadoc)
162      * 
163      * @see org.wamblee.photos.model.Album#getEntry(int)
164      */
165     public PhotoEntry getEntry(int aIndex) {
166         return _authorizedEntries.get().get(aIndex);
167     }
168
169     /*
170      * (non-Javadoc)
171      * 
172      * @see org.wamblee.photos.model.Album#size()
173      */
174     public int size() {
175         return _authorizedEntries.get().size();
176     }
177
178     /*
179      * (non-Javadoc)
180      * 
181      * @see org.wamblee.photos.model.Album#addImage(java.lang.String,
182      *      java.io.InputStream)
183      */
184     public void addImage(String aId, InputStream aImage) throws IOException {
185         _authorizer.check(this, new WriteOperation());
186         int oldsize = _authorizedEntries.get().size();
187         _authorizedEntries.invalidate();
188         decorated().addImage(aId, aImage);
189         int newsize = _authorizedEntries.get().size();
190         if (newsize != oldsize + 1) {
191             throw new RuntimeException("cache was not refreshed property");
192         }
193     }
194
195     /*
196      * (non-Javadoc)
197      * 
198      * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String)
199      */
200     public void addAlbum(String aId) throws IOException {
201         _authorizer.check(this, new WriteOperation());
202         _authorizedEntries.invalidate();
203         decorated().addAlbum(aId);
204     }
205
206     /*
207      * (non-Javadoc)
208      * 
209      * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String)
210      */
211     public void removeEntry(String aId) throws IOException {
212         // Check whether deletion is allowed.
213         PhotoEntry entry = _authorizer.check(decorated().getEntry("/" + aId), new DeleteOperation());
214         _authorizedEntries.invalidate();
215         decorated().removeEntry(aId);
216     }
217
218     public Photo findPhotoBefore(String aId) {
219         Photo entry = decorated().findPhotoBefore(aId);
220         while (entry != null && !_authorizer.isAllowed(entry, new AllOperation())) {
221             entry = decorated().findPhotoBefore(entry.getId());
222         }
223         return decorate(entry);
224     }
225
226     public Photo findPhotoAfter(String aId) {
227         Photo entry = decorated().findPhotoAfter(aId);
228         while (entry != null && !_authorizer.isAllowed(entry, new AllOperation())) {
229             entry = decorated().findPhotoAfter(entry.getId());
230         }
231         return decorate(entry);
232     }
233
234     /*
235      * (non-Javadoc)
236      * 
237      * @see java.lang.Object#toString()
238      */
239     @Override
240     public String toString() {
241         return "AuthorizedAlbum(path = '" + decorated().getPath() + "')";
242     }
243 }