1 16 package org.outerj.daisy.books.store.impl; 17 18 import org.outerj.daisy.books.store.*; 19 import org.outerj.daisy.repository.Repository; 20 import org.apache.cocoon.matching.helpers.WildcardHelper; 21 import org.outerx.daisy.x10Bookstoremeta.ResourcePropertiesDocument; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.URI ; 28 import java.util.HashMap ; 29 30 public class UserBookInstance implements BookInstance { 31 private CommonBookInstance delegate; 32 private Repository repository; 33 private boolean locked = false; 34 35 38 UserBookInstance(CommonBookInstance delegate, Repository repository, boolean locked) { 39 this.delegate = delegate; 40 this.repository = repository; 41 this.locked = locked; 42 } 43 44 File getDirectory() { 45 return delegate.getDirectory(); 46 } 47 48 private boolean isLockedBySomeoneElse() { 49 if (!locked) { 50 return delegate.isLocked(); 51 } else { 52 return false; 53 } 54 } 55 56 private boolean isLockedByMe() { 57 return locked; 58 } 59 60 private AclResult checkCanRead() { 61 if (isLockedByMe()) 62 return new AclResult(true, true); 63 if (isLockedBySomeoneElse()) 64 throw new BookStoreException("This book instance is current locked for updating. Try again later."); 65 AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds()); 66 if (!aclResult.canRead()) 67 throw new BookStoreAccessDeniedException(); 68 return aclResult; 69 } 70 71 private void checkCanRead(String path) { 72 AclResult result = checkCanRead(); 73 if (!result.canManage()) { 74 if (path.charAt(0) != '/') 77 path = "/" + path; 78 boolean ok = false; 79 for (int i = 0; i < PUBLIC_RESOURCES_PATTERNS.length; i++) { 80 HashMap matchMap = new HashMap (); 81 if (WildcardHelper.match(matchMap, path, PUBLIC_RESOURCES_PATTERNS[i])) { 82 ok = true; 83 break; 84 } 85 } 86 if (!ok) 87 throw new BookStoreAccessDeniedException("Access denied to " + path); 88 } 89 } 90 91 private void checkCanWrite() { 92 if (!isLockedByMe()) 93 throw new BookStoreException("A lock is required to update a book instance."); 94 AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds()); 95 if (!aclResult.canRead()) 96 throw new BookStoreAccessDeniedException(); 97 } 98 99 public void lock() { 100 if (isLockedByMe() || isLockedBySomeoneElse()) { 101 throw new BookStoreException("This book instance is alredy locked."); 102 } 103 104 AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds()); 105 if (!aclResult.canManage()) 106 throw new BookStoreAccessDeniedException("You are not allowed to take a lock on the book instance \"" + getName() + "\"."); 107 108 File lockFile = new File (delegate.getDirectory(), CommonBookInstance.LOCK_FILE_NAME); 109 try { 110 if (lockFile.createNewFile()) { 111 locked = true; 112 } else { 113 throw new BookStoreException("This book instance is already locked."); 114 } 115 } catch (IOException e) { 116 throw new BookStoreException("IO Exception while trying to take a lock on a book instance.", e); 117 } 118 } 119 120 public void unlock() { 121 if (!isLockedByMe()) { 122 throw new BookStoreException("You do not have a lock on this book instance."); 123 } 124 125 if (locked) { 126 File lockFile = new File (delegate.getDirectory(), CommonBookInstance.LOCK_FILE_NAME); 127 boolean success = lockFile.delete(); 128 if (!success) 129 throw new BookStoreException("Could not remove lock on book instance."); 130 locked = false; 131 } 132 } 133 134 public String getName() { 135 return delegate.getName(); 136 } 137 138 private static final int[][] PUBLIC_RESOURCES_PATTERNS = new int[][] { 139 WildcardHelper.compilePattern("/data/resources/**"), 140 WildcardHelper.compilePattern("/publications/*/output/**") 141 }; 142 143 public InputStream getResource(String path) { 144 if (path == null || path.length() == 0) 145 throw new IllegalArgumentException ("path argument can not be null or empty"); 146 checkCanRead(path); 147 return delegate.getResource(path); 148 } 149 150 public ResourcePropertiesDocument getResourceProperties(String path) { 151 if (path == null || path.length() == 0) 152 throw new IllegalArgumentException ("path argument can not be null or empty"); 153 checkCanRead(path); 154 return delegate.getResourceProperties(path); 155 } 156 157 public void storeResource(String path, InputStream is) { 158 checkCanWrite(); 159 delegate.storeResource(path, is); 160 } 161 162 public void storeResourceProperties(String path, ResourcePropertiesDocument resourcePropertiesDocument) { 163 checkCanWrite(); 164 delegate.storeResourceProperties(path, resourcePropertiesDocument); 165 } 166 167 public OutputStream getResourceOutputStream(String path) throws IOException { 168 checkCanWrite(); 169 return delegate.getResourceOutputStream(path); 170 } 171 172 public boolean rename(String path, String newName) { 173 checkCanWrite(); 174 return delegate.rename(path, newName); 175 } 176 177 public boolean exists(String path) { 178 checkCanRead(path); 179 return delegate.exists(path); 180 } 181 182 public long getLastModified(String path) { 183 checkCanRead(path); 184 return delegate.getLastModified(path); 185 } 186 187 public long getContentLength(String path) { 188 checkCanRead(path); 189 return delegate.getContentLength(path); 190 } 191 192 public BookAcl getAcl() { 193 checkCanRead(); 194 return delegate.getAcl(); 195 } 196 197 public void setAcl(BookAcl bookAcl) { 198 checkCanWrite(); 199 delegate.setAcl(bookAcl); 200 } 201 202 public boolean canManage() { 203 AclResult aclResult = BookAclEvaluator.evaluate(delegate.getAcl(), repository.getUserId(), repository.getActiveRoleIds()); 204 return aclResult.canManage(); 205 } 206 207 public URI getResourceURI(String path) { 208 checkCanRead(path); 209 return delegate.getResourceURI(path); 210 } 211 212 public PublicationsInfo getPublicationsInfo() { 213 checkCanRead(); 214 return delegate.getPublicationsInfo(); 215 } 216 217 public void addPublication(PublicationInfo publicationInfo) { 218 checkCanWrite(); 219 delegate.addPublication(publicationInfo); 220 } 221 222 public void setPublications(PublicationsInfo publicationsInfo) { 223 checkCanWrite(); 224 delegate.setPublications(publicationsInfo); 225 } 226 227 public BookInstanceMetaData getMetaData() { 228 checkCanRead(); 229 return delegate.getMetaData(); 230 } 231 232 public void setMetaData(BookInstanceMetaData metaData) { 233 checkCanWrite(); 234 delegate.setMetaData(metaData); 235 } 236 237 public String [] getDescendantPaths(String path) { 238 checkCanRead(); 239 return delegate.getDescendantPaths(path); 240 } 241 } 242 | Popular Tags |