1 40 package org.dspace.app.webui.servlet; 41 42 import java.io.IOException ; 43 import java.net.URLEncoder ; 44 import java.sql.SQLException ; 45 import java.util.List ; 46 47 import javax.servlet.ServletException ; 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.app.webui.util.Authenticate; 53 import org.dspace.app.webui.util.JSPManager; 54 import org.dspace.app.webui.util.UIUtil; 55 import org.dspace.authorize.AuthorizeException; 56 import org.dspace.authorize.AuthorizeManager; 57 import org.dspace.browse.Browse; 58 import org.dspace.browse.BrowseScope; 59 import org.dspace.content.Collection; 60 import org.dspace.content.Community; 61 import org.dspace.content.DCValue; 62 import org.dspace.content.DSpaceObject; 63 import org.dspace.content.Item; 64 import org.dspace.core.Constants; 65 import org.dspace.core.Context; 66 import org.dspace.core.LogManager; 67 import org.dspace.eperson.EPerson; 68 import org.dspace.eperson.Group; 69 import org.dspace.eperson.Subscribe; 70 import org.dspace.handle.HandleManager; 71 72 86 public class HandleServlet extends DSpaceServlet 87 { 88 89 private static Logger log = Logger.getLogger(DSpaceServlet.class); 90 91 protected void doDSGet(Context context, HttpServletRequest request, 92 HttpServletResponse response) throws ServletException , IOException , 93 SQLException , AuthorizeException 94 { 95 String handle = null; 96 String extraPathInfo = null; 97 DSpaceObject dso = null; 98 99 String path = request.getPathInfo(); 102 103 if (path != null) 104 { 105 path = path.substring(1); 107 108 try 109 { 110 int firstSlash = path.indexOf('/'); 112 int secondSlash = path.indexOf('/', firstSlash + 1); 113 114 if (secondSlash != -1) 115 { 116 handle = path.substring(0, secondSlash); 118 extraPathInfo = path.substring(secondSlash); 119 } 120 else 121 { 122 handle = path; 124 } 125 } 126 catch (NumberFormatException nfe) 127 { 128 } 130 } 131 132 if (handle != null) 134 { 135 dso = HandleManager.resolveToObject(context, handle); 136 } 137 138 if (dso == null) 139 { 140 log.info(LogManager 141 .getHeader(context, "invalid_id", "path=" + path)); 142 JSPManager.showInvalidIDError(request, response, path, -1); 143 144 return; 145 } 146 147 if (dso.getType() == Constants.ITEM) 149 { 150 Item item = (Item) dso; 151 152 response.setDateHeader("Last-Modified", item 153 .getLastModified().getTime()); 154 155 long modSince = request.getDateHeader("If-Modified-Since"); 157 158 if (modSince != -1 && item.getLastModified().getTime() < modSince) 159 { 160 response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 163 } 164 else 165 { 166 displayItem(context, request, response, item, handle); 168 } 169 } 170 else if (dso.getType() == Constants.COLLECTION) 171 { 172 Collection c = (Collection) dso; 173 174 request.setAttribute("dspace.collection", c); 176 177 183 Community[] parents = c.getCommunities(); 184 request.setAttribute("dspace.community", parents[0]); 185 186 190 request.setAttribute("dspace.communities", getParents(parents[0], 191 true)); 192 193 if ((extraPathInfo == null) || (extraPathInfo.equals("/"))) 195 { 196 collectionHome(context, request, response, parents[0], c); 197 } 198 else 199 { 200 request.getRequestDispatcher(extraPathInfo).forward(request, 202 response); 203 } 204 } 205 else if (dso.getType() == Constants.COMMUNITY) 206 { 207 Community c = (Community) dso; 208 209 request.setAttribute("dspace.community", c); 211 212 215 request.setAttribute("dspace.communities", getParents(c, false)); 216 217 if ((extraPathInfo == null) || (extraPathInfo.equals("/"))) 219 { 220 communityHome(context, request, response, c); 221 } 222 else 223 { 224 request.getRequestDispatcher(extraPathInfo).forward(request, 226 response); 227 } 228 } 229 else 230 { 231 log.info(LogManager.getHeader(context, 233 "Handle not an item, collection or community", "handle=" 234 + handle)); 235 JSPManager.showInvalidIDError(request, response, path, -1); 236 237 return; 238 } 239 } 240 241 255 private void displayItem(Context context, HttpServletRequest request, 256 HttpServletResponse response, Item item, String handle) 257 throws ServletException , IOException , SQLException , 258 AuthorizeException 259 { 260 if (item.isWithdrawn()) 262 { 263 JSPManager.showJSP(request, response, "/tombstone.jsp"); 264 265 return; 266 } 267 268 AuthorizeManager.authorizeAction(context, item, Constants.READ); 270 271 log 272 .info(LogManager.getHeader(context, "view_item", "handle=" 273 + handle)); 274 275 if (item.canEdit()) 277 { 278 request.setAttribute("admin_button", new Boolean (true)); 280 } 281 282 Collection[] collections = item.getCollections(); 284 285 request.setAttribute("dspace.collection", item.getOwningCollection()); 289 290 Community[] comms = item.getOwningCollection().getCommunities(); 291 request.setAttribute("dspace.community", comms[0]); 292 293 296 request.setAttribute("dspace.communities", getParents(comms[0], true)); 297 298 boolean displayAll = false; 300 String modeParam = request.getParameter("mode"); 301 302 if ((modeParam != null) && modeParam.equalsIgnoreCase("full")) 303 { 304 displayAll = true; 305 } 306 307 request.setAttribute("display.all", new Boolean (displayAll)); 309 request.setAttribute("item", item); 310 request.setAttribute("collections", collections); 311 JSPManager.showJSP(request, response, "/display-item.jsp"); 312 } 313 314 326 private void communityHome(Context context, HttpServletRequest request, 327 HttpServletResponse response, Community community) 328 throws ServletException , IOException , SQLException 329 { 330 if (!handleButton(request, response, community.getHandle())) 332 { 333 log.info(LogManager.getHeader(context, "view_community", 335 "community_id=" + community.getID())); 336 337 Collection[] collections = community.getCollections(); 339 340 Community[] subcommunities = community.getSubcommunities(); 342 343 BrowseScope scope = new BrowseScope(context); 345 scope.setScope(community); 346 scope.setTotal(5); 347 348 List items = Browse.getLastSubmitted(scope); 349 350 String [] itemTitles = getItemTitles(items); 352 String [] itemLinks = getItemURLs(context, items); 353 354 if (community.canEditBoolean()) 356 { 357 request.setAttribute("editor_button", new Boolean (true)); 359 } 360 361 if (AuthorizeManager.authorizeActionBoolean(context, community, 363 Constants.ADD)) 364 { 365 request.setAttribute("add_button", new Boolean (true)); 367 } 368 369 if (AuthorizeManager.authorizeActionBoolean(context, community, 371 Constants.REMOVE)) 372 { 373 request.setAttribute("remove_button", new Boolean (true)); 375 } 376 377 request.setAttribute("last.submitted.titles", itemTitles); 379 request.setAttribute("last.submitted.urls", itemLinks); 380 request.setAttribute("community", community); 381 request.setAttribute("collections", collections); 382 request.setAttribute("subcommunities", subcommunities); 383 JSPManager.showJSP(request, response, "/community-home.jsp"); 384 } 385 } 386 387 401 private void collectionHome(Context context, HttpServletRequest request, 402 HttpServletResponse response, Community community, 403 Collection collection) throws ServletException , IOException , 404 SQLException , AuthorizeException 405 { 406 if (!handleButton(request, response, collection.getHandle())) 408 { 409 boolean updated = false; 411 412 if (request.getParameter("submit_subscribe") != null) 414 { 415 if (context.getCurrentUser() == null && 418 !Authenticate 419 .startAuthentication(context, request, response)) 420 421 return; 422 else 423 { 424 Subscribe.subscribe(context, context.getCurrentUser(), 425 collection); 426 updated = true; 427 } 428 } 429 else if (request.getParameter("submit_unsubscribe") != null) 430 { 431 Subscribe.unsubscribe(context, context.getCurrentUser(), 432 collection); 433 updated = true; 434 } 435 436 log.info(LogManager.getHeader(context, "view_collection", 438 "collection_id=" + collection.getID())); 439 440 BrowseScope scope = new BrowseScope(context); 442 scope.setScope(collection); 443 scope.setTotal(5); 444 445 List items = Browse.getLastSubmitted(scope); 446 447 String [] itemTitles = getItemTitles(items); 449 String [] itemLinks = getItemURLs(context, items); 450 451 EPerson e = context.getCurrentUser(); 453 boolean subscribed = false; 454 455 if (e != null) 456 { 457 subscribed = Subscribe.isSubscribed(context, e, collection); 458 459 if (collection.canEditBoolean()) 461 { 462 request.setAttribute("editor_button", new Boolean (true)); 464 } 465 466 if (AuthorizeManager.authorizeActionBoolean(context, 468 collection, Constants.COLLECTION_ADMIN)) 469 { 470 request.setAttribute("admin_button", new Boolean (true)); 471 472 Group group = collection.getSubmitters(); 475 476 if (group != null) 477 { 478 request.setAttribute("submitters", group); 479 } 480 } 481 482 if (AuthorizeManager.authorizeActionBoolean(context, 484 collection, Constants.ADD)) 485 { 486 request 487 .setAttribute("can_submit_button", 488 new Boolean (true)); 489 490 } 491 else 492 { 493 request.setAttribute("can_submit_button", 494 new Boolean (false)); 495 } 496 } 497 498 request.setAttribute("last.submitted.titles", itemTitles); 500 request.setAttribute("last.submitted.urls", itemLinks); 501 request.setAttribute("collection", collection); 502 request.setAttribute("community", community); 503 request.setAttribute("logged.in", new Boolean (e != null)); 504 request.setAttribute("subscribed", new Boolean (subscribed)); 505 JSPManager.showJSP(request, response, "/collection-home.jsp"); 506 507 if (updated) 508 { 509 context.complete(); 510 } 511 } 512 } 513 514 528 private boolean handleButton(HttpServletRequest request, 529 HttpServletResponse response, String handle) throws IOException 530 { 531 String button = UIUtil.getSubmitButton(request, ""); 532 String location = request.getParameter("location"); 533 String prefix = "/"; 534 String url = null; 535 536 if (location == null) 537 { 538 return false; 539 } 540 541 546 if (!location.equals("/")) 547 { 548 prefix = "/handle/" + location + "/"; 549 } 550 551 if (button.equals("submit_titles")) 552 { 553 url = request.getContextPath() + prefix + "browse-title"; 555 } 556 else if (button.equals("submit_authors")) 557 { 558 url = request.getContextPath() + prefix + "browse-author"; 560 } 561 else if (button.equals("submit_subjects")) 562 { 563 url = request.getContextPath() + prefix + "browse-subject"; 565 } 566 else if (button.equals("submit_dates")) 567 { 568 url = request.getContextPath() + prefix + "browse-date"; 570 } 571 else if (button.equals("submit_search") 572 || (request.getParameter("query") != null)) 573 { 574 579 url = request.getContextPath() 580 + prefix 581 + "simple-search?query=" 582 + URLEncoder.encode(request.getParameter("query"), 583 Constants.DEFAULT_ENCODING); 584 } 585 586 if (url != null) 588 { 589 response.sendRedirect(response.encodeRedirectURL(url)); 590 591 return true; 592 } 593 594 return false; 595 } 596 597 604 private String [] getItemTitles(List items) 605 { 606 String [] titles = new String [items.size()]; 607 608 for (int i = 0; i < items.size(); i++) 609 { 610 Item item = (Item) items.get(i); 611 612 DCValue[] titlesForThis = item.getDC("title", null, Item.ANY); 614 615 if (titlesForThis.length == 0) 617 { 618 titles[i] = null; 620 } 621 else 622 { 623 titles[i] = titlesForThis[0].value; 625 } 626 } 627 628 return titles; 629 } 630 631 640 private String [] getItemURLs(Context context, List items) 641 throws SQLException 642 { 643 String [] urls = new String [items.size()]; 644 645 for (int i = 0; i < items.size(); i++) 646 { 647 Item item = (Item) items.get(i); 648 urls[i] = "/handle/" + item.getHandle(); 649 } 650 651 return urls; 652 } 653 654 661 private Community[] getParents(Community c, boolean include) 662 throws SQLException 663 { 664 Community[] parents = c.getAllParents(); 666 667 int revLength = include ? (parents.length + 1) : parents.length; 669 Community[] reversedParents = new Community[revLength]; 670 int index = parents.length - 1; 671 672 for (int i = 0; i < parents.length; i++) 673 { 674 reversedParents[i] = parents[index - i]; 675 } 676 677 if (include) 678 { 679 reversedParents[revLength - 1] = c; 680 } 681 682 return reversedParents; 683 } 684 } 685 | Popular Tags |