1 18 package org.apache.roller.webservices.adminapi; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import javax.servlet.http.HttpServletRequest ; 27 import org.jdom.Document; 28 import org.jdom.JDOMException; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.model.UserManager; 31 import org.apache.roller.pojos.PermissionsData; 32 import org.apache.roller.pojos.UserData; 33 import org.apache.roller.pojos.WebsiteData; 34 import org.apache.roller.util.cache.CacheManager; 35 import org.apache.roller.webservices.adminapi.sdk.Entry; 36 import org.apache.roller.webservices.adminapi.sdk.EntrySet; 37 import org.apache.roller.webservices.adminapi.sdk.MemberEntry; 38 import org.apache.roller.webservices.adminapi.sdk.MemberEntrySet; 39 import org.apache.roller.webservices.adminapi.sdk.MissingElementException; 40 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException; 41 42 45 class RollerMemberHandler extends Handler { 46 static class MemberURI extends URI { 47 private String username; 48 private String handle; 49 50 public MemberURI(HttpServletRequest req) throws BadRequestException { 51 super(req); 52 String entryId = getEntryId(); 53 if (entryId == null) { 54 username = null; 55 handle = null; 56 } else { 57 String [] entryIds = entryId.split("/"); 58 if (entryIds == null || entryIds.length == 0) { 59 throw new BadRequestException("ERROR: Invalid path info: " + req.getPathInfo()); 60 } 61 handle = entryIds[0]; 62 if (entryIds.length > 1) { 63 username = entryIds[1]; 64 } 65 } 66 } 67 68 public boolean hasUsername() { 69 return getUsername() != null; 70 } 71 72 public String getUsername() { 73 return username; 74 } 75 76 private void setUsername(String username) { 77 this.username = username; 78 } 79 80 public String getHandle() { 81 return handle; 82 } 83 84 private void setHandle(String handle) { 85 this.handle = handle; 86 } 87 } 88 89 private URI memberUri; 90 91 public RollerMemberHandler(HttpServletRequest request) throws HandlerException { 92 super(request); 93 memberUri = new MemberURI(request); 94 } 95 96 protected EntrySet getEntrySet(Document d) throws MissingElementException, UnexpectedRootElementException { 97 return new MemberEntrySet(d, getUrlPrefix()); 98 } 99 100 protected URI getUri() { 101 return memberUri; 102 } 103 104 public EntrySet processGet() throws HandlerException { 105 if (getUri().isCollection()) { 106 return getCollection(); 107 } else if (getUri().isEntry()) { 108 return getEntry(); 109 } else { 110 throw new BadRequestException("ERROR: Unknown GET URI type"); 111 } 112 } 113 114 public EntrySet processPost(Reader r) throws HandlerException { 115 if (getUri().isCollection()) { 116 return postCollection(r); 117 } else { 118 throw new BadRequestException("ERROR: Unknown POST URI type"); 119 } 120 } 121 122 public EntrySet processPut(Reader r) throws HandlerException { 123 if (getUri().isCollection()) { 124 return putCollection(r); 125 } else if (getUri().isEntry()) { 126 return putEntry(r); 127 } else { 128 throw new BadRequestException("ERROR: Unknown PUT URI type"); 129 } 130 } 131 132 public EntrySet processDelete() throws HandlerException { 133 if (getUri().isEntry()) { 134 return deleteEntry(); 135 } else { 136 throw new BadRequestException("ERROR: Unknown DELETE URI type"); 137 } 138 } 139 140 private EntrySet getCollection() throws HandlerException { 141 try { 143 List users = getRoller().getUserManager().getUsers(0, -1); 144 List perms = new ArrayList (); 145 for (Iterator i = users.iterator(); i.hasNext(); ) { 146 UserData user = (UserData)i.next(); 147 List permissions = getRoller().getUserManager().getAllPermissions(user); 148 for (Iterator j = permissions.iterator(); j.hasNext(); ) { 149 PermissionsData pd = (PermissionsData)j.next(); 150 perms.add(pd); 151 } 152 } 153 EntrySet es = toMemberEntrySet((PermissionsData[])perms.toArray(new PermissionsData[0])); 154 return es; 155 } catch (RollerException re) { 156 throw new InternalException("ERROR: Could not get member collection", re); 157 } 158 } 159 160 private EntrySet getEntry() throws HandlerException { 161 MemberURI muri = (MemberURI)getUri(); 162 String handle = muri.getHandle(); 163 String username = muri.getUsername(); 164 165 try { 166 List perms; 167 if (username == null) { 168 WebsiteData wd = getRoller().getUserManager().getWebsiteByHandle(handle); 170 if (wd == null) { 171 throw new NotFoundException("ERROR: Unknown weblog handle: " + handle); 172 } 173 perms = getRoller().getUserManager().getAllPermissions(wd); 174 } else { 175 WebsiteData wd = getRoller().getUserManager().getWebsiteByHandle(handle); 177 if (wd == null) { 178 throw new NotFoundException("ERROR: Unknown weblog handle: " + handle); 179 } 180 UserData ud = getRoller().getUserManager().getUserByUserName(username); 181 if (ud == null) { 182 throw new NotFoundException("ERROR: Unknown user name: " + username); 183 } 184 PermissionsData pd = getRoller().getUserManager().getPermissions(wd, ud); 185 if (pd == null) { 186 throw new NotFoundException("ERROR: Could not get permissions for user name: " + username + ", handle: " + handle); 187 } 188 perms = Collections.singletonList(pd); 189 } 190 191 EntrySet es = toMemberEntrySet((PermissionsData[])perms.toArray(new PermissionsData[0])); 192 return es; 193 } catch (RollerException re) { 194 throw new InternalException("ERROR: Could not get entry for handle: " + handle + ", username: " + username, re); 195 } 196 } 197 198 private EntrySet postCollection(Reader r) throws HandlerException { 199 EntrySet c = getEntrySet(r); 200 if (c.isEmpty()) { 201 throw new BadRequestException("ERROR: No entries"); 202 } 203 c = createMembers((MemberEntrySet)c); 204 205 return c; 206 } 207 208 private EntrySet putCollection(Reader r) throws HandlerException { 209 EntrySet c = getEntrySet(r); 210 if (c.isEmpty()) { 211 throw new BadRequestException("ERROR: No entries"); 212 } 213 c = updateMembers((MemberEntrySet)c); 214 215 return c; 216 } 217 218 private EntrySet putEntry(Reader r) throws HandlerException { 219 EntrySet c = getEntrySet(r); 220 if (c.isEmpty()) { 221 throw new BadRequestException("ERROR: No entries"); 222 } 223 if (c.getEntries().length > 1) { 224 throw new BadRequestException("ERROR: Cannot put >1 entries per request"); 225 } 226 227 MemberEntry entry = (MemberEntry)c.getEntries()[0]; 230 231 MemberURI muri = (MemberURI)getUri(); 232 233 String handle = muri.getHandle(); 237 if (entry.getHandle() == null) { 238 entry.setHandle(handle); 239 } else if (!entry.getHandle().equals(handle)) { 240 throw new BadRequestException("ERROR: URI and entry handle do not match"); 241 } 242 243 String username = muri.getUsername(); 248 if (entry.getName() == null) { 249 if (username == null) { 250 throw new BadRequestException("ERROR: No user name in URI or entry"); 251 } 252 entry.setName(username); 253 } else if (username != null && !entry.getName().equals(username)) { 254 throw new BadRequestException("ERROR: URI and entry user name do not match"); 255 } 256 257 c = updateMembers((MemberEntrySet)c); 258 259 return c; 260 } 261 262 private MemberEntrySet createMembers(MemberEntrySet c) throws HandlerException { 263 try { 264 UserManager mgr = getRoller().getUserManager(); 265 266 List permissionsDatas= new ArrayList (); 267 for (int i = 0; i < c.getEntries().length; i++) { 268 MemberEntry entry = (MemberEntry)c.getEntries()[i]; 269 PermissionsData pd = toPermissionsData(entry); 270 mgr.savePermissions(pd); 271 getRoller().flush(); 272 CacheManager.invalidate(pd.getUser()); 273 CacheManager.invalidate(pd.getWebsite()); 274 permissionsDatas.add(pd); 275 } 276 return toMemberEntrySet((PermissionsData[])permissionsDatas.toArray(new PermissionsData[0])); 277 } catch (RollerException re) { 278 throw new InternalException("ERROR: Could not create members", re); 279 } 280 } 281 282 private PermissionsData toPermissionsData(MemberEntry entry) throws HandlerException { 283 try { 284 UserManager mgr = getRoller().getUserManager(); 285 UserData ud = mgr.getUserByUserName(entry.getName()); 286 WebsiteData wd = mgr.getWebsiteByHandle(entry.getHandle()); 287 PermissionsData pd = new PermissionsData(); 288 pd.setUser(ud); 289 pd.setWebsite(wd); 290 pd.setPermissionMask(stringToMask(entry.getPermission())); 291 pd.setPending(false); 292 293 return pd; 294 } catch (RollerException re) { 295 throw new InternalException("ERROR: Could not convert to permissions data", re); 296 } 297 } 298 299 private PermissionsData getPermissionsData(MemberEntry entry) throws HandlerException { 300 return getPermissionsData(entry.getHandle(), entry.getName()); 301 } 302 303 private PermissionsData getPermissionsData(String handle, String username) throws HandlerException { 304 try { 305 UserManager mgr = getRoller().getUserManager(); 306 UserData ud = mgr.getUserByUserName(username); 307 WebsiteData wd = mgr.getWebsiteByHandle(handle); 308 PermissionsData pd = mgr.getPermissions(wd, ud); 309 310 return pd; 311 } catch (RollerException re) { 312 throw new InternalException("ERROR: Could not get permissions data", re); 313 } 314 } 315 316 private MemberEntrySet updateMembers(MemberEntrySet c) throws HandlerException { 317 List permissionsDatas= new ArrayList (); 318 for (int i = 0; i < c.getEntries().length; i++) { 319 MemberEntry entry = (MemberEntry)c.getEntries()[i]; 320 PermissionsData pd = getPermissionsData(entry); 321 if (pd == null) { 322 throw new NotFoundException("ERROR: Permissions do not exist for weblog handle: " + entry.getHandle() + ", user name: " + entry.getName()); 323 } 324 updatePermissionsData(pd, entry); 325 permissionsDatas.add(pd); 326 } 327 return toMemberEntrySet((PermissionsData[])permissionsDatas.toArray(new PermissionsData[0])); 328 } 329 330 331 private void updatePermissionsData(PermissionsData pd, MemberEntry entry) throws HandlerException { 332 334 if (entry.getPermission() != null) { 335 pd.setPermissionMask(stringToMask(entry.getPermission())); 336 } 337 338 try { 339 UserData ud = getRoller().getUserManager().getUserByUserName(entry.getName()); 340 WebsiteData wd = getRoller().getUserManager().getWebsiteByHandle(entry.getHandle()); 341 342 UserManager mgr = getRoller().getUserManager(); 343 mgr.savePermissions(pd); 344 getRoller().flush(); 345 CacheManager.invalidate(ud); 346 CacheManager.invalidate(wd); 347 } catch (RollerException re) { 348 throw new InternalException("ERROR: Could not update permissions data", re); 349 } 350 } 351 352 private EntrySet deleteEntry() throws HandlerException { 353 MemberURI muri = (MemberURI)getUri(); 354 355 String handle = muri.getHandle(); 356 String username = muri.getUsername(); 357 358 if (username == null) { 359 throw new BadRequestException("ERROR: No user name supplied in URI"); 360 } 361 362 try { 363 PermissionsData pd = getPermissionsData(handle, username); 364 PermissionsData[] pds; 365 if (pd == null) { 366 throw new NotFoundException("ERROR: Permissions do not exist for weblog handle: " + handle + ", user name: " + username); 367 } 368 pds = new PermissionsData[] { pd }; 369 370 UserManager mgr = getRoller().getUserManager(); 371 mgr.removePermissions(pd); 372 getRoller().flush(); 373 374 UserData ud = getRoller().getUserManager().getUserByUserName(username); 375 CacheManager.invalidate(ud); 376 WebsiteData wd = getRoller().getUserManager().getWebsiteByHandle(handle); 377 CacheManager.invalidate(wd); 378 379 EntrySet es = toMemberEntrySet(pds); 380 return es; 381 } catch (RollerException re) { 382 throw new InternalException("ERROR: Could not delete entry", re); 383 } 384 } 385 386 private MemberEntry toMemberEntry(PermissionsData pd) { 387 if (pd == null) { 388 throw new NullPointerException ("ERROR: Null permission data not allowed"); 389 } 390 MemberEntry me = new MemberEntry(pd.getWebsite().getHandle(), pd.getUser().getUserName(), getUrlPrefix()); 391 me.setPermission(maskToString(pd.getPermissionMask())); 392 393 return me; 394 } 395 private MemberEntrySet toMemberEntrySet(PermissionsData[] pds) { 396 if (pds == null) { 397 throw new NullPointerException ("ERROR: Null permission data not allowed"); 398 } 399 400 List entries = new ArrayList (); 401 for (int i = 0; i < pds.length; i++) { 402 PermissionsData pd = pds[i]; 403 Entry entry = toMemberEntry(pd); 404 entries.add(entry); 405 } 406 MemberEntrySet mes = new MemberEntrySet(getUrlPrefix()); 407 mes.setEntries((Entry[])entries.toArray(new Entry[0])); 408 409 return mes; 410 } 411 412 private static String maskToString(short mask) { 413 if (mask == PermissionsData.ADMIN) { 414 return MemberEntry.Permissions.ADMIN; 415 } 416 if (mask == PermissionsData.AUTHOR) { 417 return MemberEntry.Permissions.AUTHOR; 418 } 419 if (mask == PermissionsData.LIMITED) { 420 return MemberEntry.Permissions.LIMITED; 421 } 422 return null; 423 } 424 425 426 private static short stringToMask(String s) { 427 if (s == null) { 428 throw new NullPointerException ("ERROR: Null string not allowed"); 429 } 430 if (s.equalsIgnoreCase(MemberEntry.Permissions.ADMIN)) { 431 return PermissionsData.ADMIN; 432 } 433 if (s.equalsIgnoreCase(MemberEntry.Permissions.AUTHOR)) { 434 return PermissionsData.AUTHOR; 435 } 436 if (s.equalsIgnoreCase(MemberEntry.Permissions.LIMITED)) { 437 return PermissionsData.LIMITED; 438 } 439 return 0; 440 } 441 } 442 443 | Popular Tags |