1 18 package org.apache.roller.webservices.atomprotocol; 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.InputStream ; 23 import java.sql.Timestamp ; 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 import java.util.Collections ; 30 import javax.activation.MimetypesFileTypeMap ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 34 import org.apache.commons.codec.binary.Base64; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.apache.roller.model.FileManager; 38 import org.apache.roller.model.Roller; 39 import org.apache.roller.model.RollerFactory; 40 import org.apache.roller.pojos.UserData; 41 import org.apache.roller.pojos.PermissionsData; 42 import org.apache.roller.pojos.WeblogCategoryData; 43 import org.apache.roller.pojos.WeblogEntryData; 44 import org.apache.roller.pojos.WebsiteData; 45 import org.apache.roller.ui.core.RollerContext; 46 import org.apache.roller.util.RollerMessages; 47 import org.apache.roller.util.Utilities; 48 import org.apache.roller.util.WSSEUtilities; 49 50 import com.sun.syndication.feed.atom.Content; 51 import com.sun.syndication.feed.atom.Category; 52 import com.sun.syndication.feed.atom.Entry; 53 import com.sun.syndication.feed.atom.Feed; 54 import com.sun.syndication.feed.atom.Link; 55 import com.sun.syndication.feed.atom.Person; 56 import java.io.IOException ; 57 import java.text.SimpleDateFormat ; 58 import java.util.Comparator ; 59 import java.util.Map ; 60 import java.util.SortedSet ; 61 import java.util.TreeSet ; 62 import javax.activation.FileTypeMap ; 63 import org.apache.commons.lang.StringUtils; 64 import org.apache.roller.RollerException; 65 import org.apache.roller.config.RollerConfig; 66 import org.apache.roller.config.RollerRuntimeConfig; 67 import org.apache.roller.model.WeblogManager; 68 import org.apache.roller.pojos.RollerPropertyData; 69 import org.apache.roller.util.URLUtilities; 70 import org.apache.roller.util.cache.CacheManager; 71 72 110 public class RollerAtomHandler implements AtomHandler { 111 private HttpServletRequest mRequest; 112 private Roller mRoller; 113 private RollerContext mRollerContext; 114 private UserData user; 115 private int mMaxEntries = 20; 116 119 private static Log mLogger = 120 LogFactory.getFactory().getInstance(RollerAtomHandler.class); 121 122 124 129 public RollerAtomHandler(HttpServletRequest request) { 130 mRequest = request; 131 mRoller = RollerFactory.getRoller(); 132 mRollerContext = RollerContext.getRollerContext(); 133 134 String userName = authenticateBASIC(request); 137 if (userName != null) { 138 try { 139 this.user = mRoller.getUserManager().getUserByUserName(userName); 140 } catch (Exception neverHappen) { 141 mLogger.debug("ERROR: getting user", neverHappen); 142 } 143 } 144 } 145 146 149 public String getAuthenticatedUsername() { 150 String ret = null; 151 if (this.user != null) { 152 ret = user.getUserName(); 153 } 154 return ret; 155 } 156 157 159 163 public AtomService getIntrospection() throws AtomException { 164 AtomService service = new AtomService(); 165 List perms = null; 166 try { 167 perms = mRoller.getUserManager().getAllPermissions(user); 168 169 } catch (RollerException re) { 170 throw new AtomException("ERROR: getting user's weblogs", re); 171 } 172 String accept = null; 173 try { 174 accept = getAcceptedContentTypeRange(); 175 } catch (RollerException re) { 176 throw new AtomException("ERROR: getting site's accept range", re); 177 } 178 if (perms != null) { 179 for (Iterator iter=perms.iterator(); iter.hasNext();) { 180 PermissionsData perm = (PermissionsData)iter.next(); 181 String handle = perm.getWebsite().getHandle(); 182 AtomService.Workspace workspace = new AtomService.Workspace(); 183 workspace.setTitle(Utilities.removeHTML(perm.getWebsite().getName())); 184 service.addWorkspace(workspace); 185 186 AtomService.Collection entryCol = new AtomService.Collection(); 187 entryCol.setTitle("Weblog Entries"); 188 entryCol.setAccept("entry"); 189 entryCol.setHref(URLUtilities.getAtomProtocolURL(true)+"/"+handle+"/entries"); 190 try { 191 AtomService.Categories cats = new AtomService.Categories(); 192 cats.setFixed(true); 193 cats.setScheme(URLUtilities.getWeblogURL(perm.getWebsite(), null, true)); 194 List rollerCats = mRoller.getWeblogManager().getWeblogCategories(perm.getWebsite(), false); 195 for (Iterator it = rollerCats.iterator(); it.hasNext();) { 196 WeblogCategoryData rollerCat = (WeblogCategoryData)it.next(); 197 AtomService.Category cat = new AtomService.Category(); 198 cat.setTerm(rollerCat.getPath()); 199 cat.setLabel(rollerCat.getName()); 200 cats.addCategory(cat); 201 } 202 entryCol.addCategories(cats); 203 } catch (Exception e) { 204 throw new AtomException("ERROR fetching weblog categories"); 205 } 206 workspace.addCollection(entryCol); 207 208 AtomService.Collection uploadCol = new AtomService.Collection(); 209 uploadCol.setTitle("Media Files"); 210 uploadCol.setAccept(accept); 211 uploadCol.setHref(URLUtilities.getAtomProtocolURL(true)+"/"+handle+"/resources"); 212 workspace.addCollection(uploadCol); 213 } 214 } 215 return service; 216 } 217 218 222 private String getAcceptedContentTypeRange() throws RollerException { 223 StringBuffer sb = new StringBuffer (); 224 Roller roller = RollerFactory.getRoller(); 225 Map config = roller.getPropertiesManager().getProperties(); 226 String allows = ((RollerPropertyData)config.get("uploads.types.allowed")).getValue(); 227 String [] rules = StringUtils.split(StringUtils.deleteWhitespace(allows), ","); 228 for (int i=0; i<rules.length; i++) { 229 if (rules[i].indexOf("/") == -1) continue; 230 if (sb.length() != 0) { 231 sb.append(","); 232 } 233 sb.append(rules[i]); 234 } 235 return sb.toString(); 236 } 237 238 240 250 public Feed getCollection(String [] pathInfo) throws AtomException { 251 int start = 0; 252 if (pathInfo.length > 2) { 253 try { 254 String s = pathInfo[2].trim(); 255 start = Integer.parseInt(s); 256 } catch (Throwable t) { 257 mLogger.warn("Unparsable range: " + pathInfo[2]); 258 } 259 } 260 if (pathInfo.length > 0 && pathInfo[1].equals("entries")) { 261 return getCollectionOfEntries(pathInfo, start, mMaxEntries); 262 } else if (pathInfo.length > 0 && pathInfo[1].equals("resources")) { 263 return getCollectionOfResources(pathInfo, start, mMaxEntries); 264 } 265 throw new AtomNotFoundException("ERROR: cannot find collection specified"); 266 } 267 268 271 public Feed getCollectionOfEntries( 272 String [] pathInfo, int start, int max) throws AtomException { 273 try { 274 String handle = pathInfo[0]; 275 String absUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 276 WebsiteData website = 277 mRoller.getUserManager().getWebsiteByHandle(handle); 278 if (website == null) { 279 throw new AtomNotFoundException("ERROR: cannot find specified weblog"); 280 } 281 List entries = null; 282 if (canView(website)) { 283 entries = mRoller.getWeblogManager().getWeblogEntries( 284 website, null, null, null, null, null, "updateTime", null, start, max + 1); Feed feed = new Feed(); 295 feed.setId(URLUtilities.getAtomProtocolURL(true) 296 +"/"+website.getHandle() + "/entries/" + start); 297 feed.setTitle(website.getName()); 298 299 Link link = new Link(); 300 link.setHref(absUrl + "/" + website.getHandle()); 301 link.setRel("alternate"); 302 link.setType("text/html"); 303 feed.setAlternateLinks(Collections.singletonList(link)); 304 305 List atomEntries = new ArrayList (); 306 int count = 0; 307 for (Iterator iter = entries.iterator(); iter.hasNext() && count < mMaxEntries; count++) { 308 WeblogEntryData rollerEntry = (WeblogEntryData)iter.next(); 309 Entry entry = createAtomEntry(rollerEntry); 310 atomEntries.add(entry); 311 if (count == 0) { 312 feed.setUpdated(entry.getUpdated()); 314 } 315 } 316 List links = new ArrayList (); 317 if (entries.size() > max) { int nextOffset = start + max; 319 String url = URLUtilities.getAtomProtocolURL(true)+"/" 320 + website.getHandle() + "/entries/" + nextOffset; 321 Link nextLink = new Link(); 322 nextLink.setRel("next"); 323 nextLink.setHref(url); 324 links.add(nextLink); 325 } 326 if (start > 0) { int prevOffset = start > max ? start - max : 0; 328 String url = URLUtilities.getAtomProtocolURL(true)+"/" 329 +website.getHandle() + "/entries/" + prevOffset; 330 Link prevLink = new Link(); 331 prevLink.setRel("previous"); 332 prevLink.setHref(url); 333 links.add(prevLink); 334 } 335 if (links.size() > 0) feed.setOtherLinks(links); 336 feed.setEntries(atomEntries); 338 return feed; 339 } 340 throw new AtomNotAuthorizedException("ERROR: not authorized to access website"); 341 342 } catch (RollerException re) { 343 throw new AtomException("ERROR: getting entry collection"); 344 } 345 } 346 347 350 public Feed getCollectionOfResources( 351 String [] pathInfo, int start, int max) throws AtomException { 352 try { 353 String handle = pathInfo[0]; 354 String absUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 355 WebsiteData website = 356 mRoller.getUserManager().getWebsiteByHandle(handle); 357 if (website == null) { 358 throw new AtomNotFoundException( 359 "ERROR: cannot find specified weblog"); 360 } 361 FileManager fmgr = mRoller.getFileManager(); 362 File [] files = fmgr.getFiles(website.getHandle()); 363 364 if (canView(website)) { 365 Feed feed = new Feed(); 366 feed.setId(URLUtilities.getAtomProtocolURL(true) 367 +"/"+website.getHandle() + "/entries/" + start); 368 feed.setTitle(website.getName()); 369 370 Link link = new Link(); 371 link.setHref(absUrl + "/" + website.getHandle()); 372 link.setRel("alternate"); 373 link.setType("text/html"); 374 feed.setAlternateLinks(Collections.singletonList(link)); 375 376 SortedSet sortedSet = new TreeSet (new Comparator () { 377 public int compare(Object o1, Object o2) { 378 File f1 = (File )o1; 379 File f2 = (File )o2; 380 if (f1.lastModified() < f2.lastModified()) return 1; 381 else if (f1.lastModified() == f2.lastModified()) return 0; 382 else return -1; 383 } 384 public boolean equals(Object obj) { 385 return false; 386 } 387 }); 388 List atomEntries = new ArrayList (); 389 if (files != null && start < files.length) { 390 for (int i=0; i<files.length; i++) { 391 sortedSet.add(files[i]); 392 } 393 } 394 int count = 0; 395 File [] sortedArray = (File [])sortedSet.toArray(new File [sortedSet.size()]); 396 for (int i=start; i<(start + max) && i<(sortedArray.length); i++) { 397 Entry entry = createAtomResourceEntry(website, sortedArray[i]); 398 atomEntries.add(entry); 399 if (count == 0) { 400 feed.setUpdated(entry.getUpdated()); 402 } 403 count++; 404 } 405 if (start + count < files.length) { int nextOffset = start + max; 407 String url = URLUtilities.getAtomProtocolURL(true) 408 +"/"+ website.getHandle() + "/resources/" + nextOffset; 409 Link nextLink = new Link(); 410 nextLink.setRel("next"); 411 nextLink.setHref(url); 412 List next = new ArrayList (); 413 next.add(nextLink); 414 feed.setOtherLinks(next); 415 } 416 if (start > 0) { int prevOffset = start > max ? start - max : 0; 418 String url = URLUtilities.getAtomProtocolURL(true) 419 +"/"+website.getHandle() + "/resources/" + prevOffset; 420 Link prevLink = new Link(); 421 prevLink.setRel("previous"); 422 prevLink.setHref(url); 423 List prev = new ArrayList (); 424 prev.add(prevLink); 425 feed.setOtherLinks(prev); 426 } 427 feed.setEntries(atomEntries); 428 return feed; 429 } 430 throw new AtomNotAuthorizedException( 431 "ERROR: not authorized to access website"); 432 433 } catch (RollerException re) { 434 throw new AtomException("ERROR: getting resource collection"); 435 } 436 } 437 438 440 443 public Entry postEntry(String [] pathInfo, Entry entry) throws AtomException { 444 try { 445 String handle = pathInfo[0]; 447 WebsiteData website = 448 mRoller.getUserManager().getWebsiteByHandle(handle); 449 if (website == null) { 450 throw new AtomNotFoundException("ERROR: cannot find specified weblog"); 451 } 452 if (canEdit(website)) { 453 WeblogManager mgr = mRoller.getWeblogManager(); 455 WeblogEntryData rollerEntry = createRollerEntry(website, entry); 456 rollerEntry.setCreator(this.user); 457 mgr.saveWeblogEntry(rollerEntry); 458 mRoller.flush(); 459 460 try { Thread.sleep(1000); } catch (Exception ignored) {} 463 464 CacheManager.invalidate(website); 465 if (rollerEntry.isPublished()) { 466 mRoller.getIndexManager().addEntryReIndexOperation(rollerEntry); 467 } 468 return createAtomEntry(rollerEntry); 469 } 470 throw new AtomNotAuthorizedException( 471 "ERROR: not authorized to access website"); 472 473 } catch (RollerException re) { 474 throw new AtomException("ERROR: posting entry"); 475 } 476 } 477 478 481 public Entry getEntry(String [] pathInfo) throws AtomException { 482 try { 483 if (pathInfo.length == 3) { 485 if (pathInfo[1].equals("entry")) { 486 WeblogEntryData entry = 487 mRoller.getWeblogManager().getWeblogEntry(pathInfo[2]); 488 if (entry == null) { 489 throw new AtomNotFoundException( 490 "ERROR: cannot find specified entry/resource"); 491 } 492 if (!canView(entry)) { 493 throw new AtomNotAuthorizedException( 494 "ERROR: not authorized to view entry"); 495 } else { 496 return createAtomEntry(entry); 497 } 498 } else if (pathInfo[1].equals("resource") && pathInfo[2].endsWith(".media-link")) { 499 String fileName = 500 pathInfo[2].substring(0, pathInfo[2].length() - ".media-link".length()); 501 String handle = pathInfo[0]; 502 WebsiteData website = 503 mRoller.getUserManager().getWebsiteByHandle(handle); 504 String uploadPath = 505 RollerFactory.getRoller().getFileManager().getUploadUrl(); 506 File resource = 507 new File (uploadPath + File.separator + fileName); 508 return createAtomResourceEntry(website, resource); 509 } 510 } 511 throw new AtomNotFoundException( 512 "ERROR: cannot find specified entry/resource"); 513 } catch (RollerException re) { 514 throw new AtomException("ERROR: getting entry"); 515 } 516 } 517 518 521 public Entry putEntry(String [] pathInfo, Entry entry) throws AtomException { 522 try { 523 if (pathInfo.length == 3) { 525 WeblogEntryData rollerEntry = 526 mRoller.getWeblogManager().getWeblogEntry(pathInfo[2]); 527 if (rollerEntry == null) { 528 throw new AtomNotFoundException( 529 "ERROR: cannot find specified entry/resource"); 530 } 531 if (canEdit(rollerEntry)) { 532 WeblogManager mgr = mRoller.getWeblogManager(); 533 534 WeblogEntryData rawUpdate = createRollerEntry(rollerEntry.getWebsite(), entry); 535 rollerEntry.setPubTime(rawUpdate.getPubTime()); 536 rollerEntry.setUpdateTime(rawUpdate.getUpdateTime()); 537 rollerEntry.setText(rawUpdate.getText()); 538 rollerEntry.setStatus(rawUpdate.getStatus()); 539 rollerEntry.setCategory(rawUpdate.getCategory()); 540 rollerEntry.setTitle(rawUpdate.getTitle()); 541 542 mgr.saveWeblogEntry(rollerEntry); 543 mRoller.flush(); 544 545 CacheManager.invalidate(rollerEntry.getWebsite()); 546 if (rollerEntry.isPublished()) { 547 mRoller.getIndexManager().addEntryReIndexOperation(rollerEntry); 548 } 549 return createAtomEntry(rollerEntry); 550 } 551 throw new AtomNotAuthorizedException("ERROR not authorized to update entry"); 552 } 553 throw new AtomNotFoundException("ERROR: cannot find specified entry/resource"); 554 555 } catch (RollerException re) { 556 throw new AtomException("ERROR: updating entry"); 557 } 558 } 559 560 563 public void deleteEntry(String [] pathInfo) throws AtomException { 564 try { 565 if (pathInfo.length == 3) { 567 if (pathInfo[1].equals("entry")) { 568 WeblogEntryData rollerEntry = mRoller.getWeblogManager().getWeblogEntry(pathInfo[2]); 569 if (rollerEntry == null) { 570 throw new AtomNotFoundException("ERROR: cannot find specified entry/resource"); 571 } 572 if (canEdit(rollerEntry)) { 573 WeblogManager mgr = mRoller.getWeblogManager(); 574 mgr.removeWeblogEntry(rollerEntry); 575 mRoller.flush(); 576 CacheManager.invalidate(rollerEntry.getWebsite()); 577 mRoller.getIndexManager().removeEntryIndexOperation(rollerEntry); 578 return; 579 } 580 } else if (pathInfo[1].equals("resource")) { 581 String handle = pathInfo[0]; 582 WebsiteData website = mRoller.getUserManager().getWebsiteByHandle(handle); 583 if (website == null) { 584 throw new AtomNotFoundException("ERROR: cannot find specified weblog"); 585 } 586 if (canEdit(website) && pathInfo.length > 1) { 587 try { 588 String fileName = pathInfo[2]; 589 if (pathInfo[2].endsWith(".media-link")) { 590 fileName = fileName.substring(0, pathInfo[2].length() - ".media-link".length()); 591 } 592 FileManager fmgr = mRoller.getFileManager(); 593 fmgr.deleteFile(website.getHandle(), fileName); 594 } catch (Exception e) { 595 String msg = "ERROR in atom.deleteResource"; 596 mLogger.error(msg,e); 597 throw new AtomException(msg); 598 } 599 return; 600 } 601 } 602 throw new AtomNotAuthorizedException("ERROR not authorized to delete entry"); 603 } 604 throw new AtomNotFoundException("ERROR: cannot find specified entry/resource"); 605 606 } catch (RollerException re) { 607 throw new AtomException("ERROR: deleting entry"); 608 } 609 } 610 611 613 619 public Entry postMedia(String [] pathInfo, 620 String title, String slug, String contentType, InputStream is) 621 throws AtomException { 622 try { 623 File tempFile = null; 625 RollerMessages msgs = new RollerMessages(); 626 String handle = pathInfo[0]; 627 WebsiteData website = 628 mRoller.getUserManager().getWebsiteByHandle(handle); 629 if (canEdit(website) && pathInfo.length > 1) { 630 String fileName = createFileName(website, (slug != null) ? slug : title, contentType); 632 try { 633 FileManager fmgr = mRoller.getFileManager(); 634 tempFile = File.createTempFile(fileName, "tmp"); 635 FileOutputStream fos = new FileOutputStream (tempFile); 636 Utilities.copyInputToOutput(is, fos); 637 fos.close(); 638 639 if (fmgr.canSave(website.getHandle(), fileName, contentType, tempFile.length(), msgs)) { 641 FileInputStream fis = new FileInputStream (tempFile); 643 fmgr.saveFile(website.getHandle(), fileName, contentType, tempFile.length(), fis); 644 fis.close(); 645 646 File resource = new File (fmgr.getUploadDir() + File.separator + fileName); 647 return createAtomResourceEntry(website, resource); 648 } 649 650 } catch (IOException e) { 651 String msg = "ERROR reading posted file"; 652 mLogger.error(msg,e); 653 throw new AtomException(msg, e); 654 } finally { 655 if (tempFile != null) tempFile.delete(); 656 } 657 } 658 throw new AtomException("File upload denied because:" + msgs.toString()); 660 661 } catch (RollerException re) { 662 throw new AtomException("ERROR: posting media"); 663 } 664 } 665 666 695 private String createFileName(WebsiteData weblog, String title, String contentType) { 696 697 if (weblog == null) throw new IllegalArgumentException ("weblog cannot be null"); 698 if (contentType == null) throw new IllegalArgumentException ("contentType cannot be null"); 699 700 String fileName = null; 701 702 String [] typeTokens = contentType.split("/"); 707 String ext = typeTokens[1]; 708 709 if (title != null && !title.trim().equals("")) { 710 String base = Utilities.replaceNonAlphanumeric(title, ' '); 712 StringTokenizer toker = new StringTokenizer (base); 713 String tmp = null; 714 int count = 0; 715 while (toker.hasMoreTokens() && count < 5) { 716 String s = toker.nextToken(); 717 s = s.toLowerCase(); 718 tmp = (tmp == null) ? s : tmp + "_" + s; 719 count++; 720 } 721 fileName = tmp + "." + ext; 722 723 } else { 724 SimpleDateFormat sdf = new SimpleDateFormat (); 727 sdf.applyPattern("yyyyMMddHHSS"); 728 fileName = weblog.getHandle()+"-"+sdf.format(new Date ())+"."+ext; 729 } 730 731 return fileName; 732 } 733 734 735 739 public Entry putMedia(String [] pathInfo, 740 String contentType, InputStream is) throws AtomException { 741 if (pathInfo.length > 2) { 742 String name = pathInfo[2]; 743 return postMedia(pathInfo, name, name, contentType, is); 744 } 745 throw new AtomException("ERROR: bad pathInfo"); 746 } 747 748 750 753 public boolean isIntrospectionURI(String [] pathInfo) { 754 if (pathInfo.length==0) return true; 755 return false; 756 } 757 758 761 public boolean isEntryURI(String [] pathInfo) { 762 if (pathInfo.length > 2 && pathInfo[1].equals("entry")) return true; 763 if (pathInfo.length > 2 && pathInfo[1].equals("resource")) return true; 764 return false; 765 } 766 767 770 public boolean isMediaEditURI(String [] pathInfo) { 771 if (pathInfo.length > 1 && pathInfo[1].equals("resource")) return true; 772 return false; 773 } 774 775 778 public boolean isCategoryURI(String [] pathInfo) { 779 if (pathInfo.length > 1 && pathInfo[1].equals("category")) return true; 780 return false; 781 } 782 783 786 public boolean isCollectionURI(String [] pathInfo) { 787 if (pathInfo.length > 1 && pathInfo[1].equals("entries")) return true; 788 if (pathInfo.length > 1 && pathInfo[1].equals("resources")) return true; 789 if (pathInfo.length > 1 && pathInfo[1].equals("categories")) return true; 790 return false; 791 } 792 793 795 798 private boolean canEdit(WeblogEntryData entry) { 799 try { 800 return entry.hasWritePermissions(this.user); 801 } catch (Exception e) { 802 mLogger.error("ERROR: checking website.canSave()"); 803 } 804 return false; 805 } 806 807 810 private boolean canEdit(WebsiteData website) { 811 try { 812 return website.hasUserPermissions(this.user, PermissionsData.AUTHOR); 813 } catch (Exception e) { 814 mLogger.error("ERROR: checking website.hasUserPermissions()"); 815 } 816 return false; 817 } 818 819 822 private boolean canView(WeblogEntryData entry) { 823 return canEdit(entry); 824 } 825 826 829 private boolean canView(WebsiteData website) { 830 return canEdit(website); 831 } 832 833 835 839 protected String authenticateWSSE(HttpServletRequest request) { 840 String wsseHeader = request.getHeader("X-WSSE"); 841 if (wsseHeader == null) return null; 842 843 String ret = null; 844 String userName = null; 845 String created = null; 846 String nonce = null; 847 String passwordDigest = null; 848 String [] tokens = wsseHeader.split(","); 849 for (int i = 0; i < tokens.length; i++) { 850 int index = tokens[i].indexOf('='); 851 if (index != -1) { 852 String key = tokens[i].substring(0, index).trim(); 853 String value = tokens[i].substring(index + 1).trim(); 854 value = value.replaceAll("\"", ""); 855 if (key.startsWith("UsernameToken")) { 856 userName = value; 857 } else if (key.equalsIgnoreCase("nonce")) { 858 nonce = value; 859 } else if (key.equalsIgnoreCase("passworddigest")) { 860 passwordDigest = value; 861 } else if (key.equalsIgnoreCase("created")) { 862 created = value; 863 } 864 } 865 } 866 String digest = null; 867 try { 868 UserData user = mRoller.getUserManager().getUserByUserName(userName); 869 digest = WSSEUtilities.generateDigest( 870 WSSEUtilities.base64Decode(nonce), 871 created.getBytes("UTF-8"), 872 user.getPassword().getBytes("UTF-8")); 873 if (digest.equals(passwordDigest)) { 874 ret = userName; 875 } 876 } catch (Exception e) { 877 mLogger.error("ERROR in wsseAuthenticataion: " + e.getMessage(), e); 878 } 879 return ret; 880 } 881 882 885 public String authenticateBASIC(HttpServletRequest request) { 886 boolean valid = false; 887 String userID = null; 888 String password = null; 889 try { 890 String authHeader = request.getHeader("Authorization"); 891 if (authHeader != null) { 892 StringTokenizer st = new StringTokenizer (authHeader); 893 if (st.hasMoreTokens()) { 894 String basic = st.nextToken(); 895 if (basic.equalsIgnoreCase("Basic")) { 896 String credentials = st.nextToken(); 897 String userPass = new String (Base64.decodeBase64(credentials.getBytes())); 898 int p = userPass.indexOf(":"); 899 if (p != -1) { 900 userID = userPass.substring(0, p); 901 UserData user = mRoller.getUserManager().getUserByUserName(userID); 902 boolean enabled = user.getEnabled().booleanValue(); 903 if (enabled) { 904 RollerContext rollerContext = 906 RollerContext.getRollerContext(); 907 String encrypted = 908 RollerConfig.getProperty("passwds.encryption.enabled"); 909 password = userPass.substring(p+1); 910 if ("true".equalsIgnoreCase(encrypted)) { 911 password = Utilities.encodePassword(password, 912 RollerConfig.getProperty("passwds.encryption.algorithm")); 913 } 914 valid = user.getPassword().equals(password); 915 } 916 } 917 } 918 } 919 } 920 } catch (Exception e) { 921 mLogger.debug(e); 922 } 923 if (valid) return userID; 924 return null; 925 } 926 927 929 934 private Entry createAtomEntry(WeblogEntryData entry) { 935 Entry atomEntry = new Entry(); 936 Content content = new Content(); 937 content.setType(Content.HTML); 938 content.setValue(entry.getText()); 939 List contents = new ArrayList (); 940 contents.add(content); 941 942 String absUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 943 atomEntry.setId( absUrl + entry.getPermaLink()); 944 atomEntry.setTitle( entry.getTitle()); 945 atomEntry.setContents( contents); 946 atomEntry.setPublished( entry.getPubTime()); 947 atomEntry.setUpdated( entry.getUpdateTime()); 948 949 UserData creator = entry.getCreator(); 950 Person author = new Person(); 951 author.setName( creator.getUserName()); 952 author.setEmail( creator.getEmailAddress()); 953 atomEntry.setAuthors( Collections.singletonList(author)); 954 955 List categories = new ArrayList (); 956 Category atomCat = new Category(); 957 atomCat.setTerm(entry.getCategory().getPath()); 958 categories.add(atomCat); 959 atomEntry.setCategories(categories); 960 961 Link altlink = new Link(); 962 altlink.setRel("alternate"); 963 altlink.setHref(absUrl + entry.getPermaLink()); 964 List altlinks = new ArrayList (); 965 altlinks.add(altlink); 966 atomEntry.setAlternateLinks(altlinks); 967 968 Link editlink = new Link(); 969 editlink.setRel("edit"); 970 editlink.setHref( 971 URLUtilities.getAtomProtocolURL(true) 972 +"/"+entry.getWebsite().getHandle() + "/entry/" + entry.getId()); 973 List otherlinks = new ArrayList (); 974 otherlinks.add(editlink); 975 atomEntry.setOtherLinks(otherlinks); 976 977 List modules = new ArrayList (); 978 PubControlModule pubControl = new PubControlModuleImpl(); 979 pubControl.setDraft( 980 !WeblogEntryData.PUBLISHED.equals(entry.getStatus())); 981 modules.add(pubControl); 982 atomEntry.setModules(modules); 983 984 return atomEntry; 985 } 986 987 private Entry createAtomResourceEntry(WebsiteData website, File file) { 988 String absUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 989 String editURI = 990 URLUtilities.getAtomProtocolURL(true)+"/"+website.getHandle() 991 + "/resource/" + file.getName() + ".media-link"; 992 String editMediaURI = 993 URLUtilities.getAtomProtocolURL(true)+"/"+ website.getHandle() 994 + "/resource/" + file.getName(); 995 String viewURI = absUrl 996 + "/resources/" + website.getHandle() 997 + "/" + file.getName(); 998 999 FileTypeMap map = FileTypeMap.getDefaultFileTypeMap(); 1000 if (map instanceof MimetypesFileTypeMap ) { 1002 try { 1003 ((MimetypesFileTypeMap )map).addMimeTypes("image/png png PNG"); 1004 } catch (Exception ignored) {} 1005 } 1006 String contentType = map.getContentType(file); 1007 1008 Entry entry = new Entry(); 1009 entry.setId(editMediaURI); 1010 entry.setTitle(file.getName()); 1011 entry.setUpdated(new Date (file.lastModified())); 1012 1013 List otherlinks = new ArrayList (); 1014 entry.setOtherLinks(otherlinks); 1015 Link editlink = new Link(); 1016 editlink.setRel("edit"); 1017 editlink.setHref(editURI); 1018 otherlinks.add(editlink); 1019 Link editMedialink = new Link(); 1020 editMedialink.setRel("edit-media"); 1021 editMedialink.setHref(editMediaURI); 1022 otherlinks.add(editMedialink); 1023 1024 Content content = new Content(); 1025 content.setSrc(viewURI); 1026 content.setType(contentType); 1027 List contents = new ArrayList (); 1028 contents.add(content); 1029 entry.setContents(contents); 1030 1031 return entry; 1032 } 1033 1034 1037 private WeblogEntryData createRollerEntry(WebsiteData website, Entry entry) 1038 throws RollerException { 1039 1040 Timestamp current = new Timestamp (System.currentTimeMillis()); 1041 Timestamp pubTime = current; 1042 Timestamp updateTime = current; 1043 if (entry.getPublished() != null) { 1044 pubTime = new Timestamp ( entry.getPublished().getTime() ); 1045 } 1046 if (entry.getUpdated() != null) { 1047 updateTime = new Timestamp ( entry.getUpdated().getTime() ); 1048 } 1049 WeblogEntryData rollerEntry = new WeblogEntryData(); 1050 rollerEntry.setTitle(entry.getTitle()); 1051 if (entry.getContents() != null && entry.getContents().size() > 0) { 1052 Content content = (Content)entry.getContents().get(0); 1053 rollerEntry.setText(content.getValue()); 1054 } 1055 rollerEntry.setPubTime(pubTime); 1056 rollerEntry.setUpdateTime(updateTime); 1057 rollerEntry.setWebsite(website); 1058 1059 PubControlModule control = 1060 (PubControlModule)entry.getModule("http://purl.org/atom/app#"); 1061 if (control!=null && control.getDraft()) { 1062 rollerEntry.setStatus(WeblogEntryData.DRAFT); 1063 } else { 1064 rollerEntry.setStatus(WeblogEntryData.PUBLISHED); 1065 } 1066 1067 List categories = entry.getCategories(); 1070 if (categories != null && categories.size() > 0) { 1071 for (int i=0; i<categories.size(); i++) { 1072 Category cat = (Category)categories.get(i); 1073 String catString = cat.getTerm() != null ? cat.getTerm() : cat.getLabel(); 1075 if (catString != null) { 1076 WeblogCategoryData rollerCat = 1077 mRoller.getWeblogManager().getWeblogCategoryByPath( 1078 website, catString); 1079 if (rollerCat != null) { 1080 rollerEntry.setCategory(rollerCat); 1082 break; 1083 } 1084 } 1085 } 1086 } 1087 if (rollerEntry.getCategory() == null) { 1088 rollerEntry.setCategory(website.getBloggerCategory()); 1090 } 1091 return rollerEntry; 1092 } 1093 1094} 1095 | Popular Tags |