/** * Create URI as string for a given handle and optional bitstream. URI is relative to top of DAV * hierarchy, but starts with '/'. * * @param handle handle of a DSpace object (Item, Collection, etc) * @param bsPid bitstream persistent identifier. * @return "absolute" URI from top of DAV hierarchy * @throws IOException Signals that an I/O exception has occurred. * @throws SQLException the SQL exception */ protected String makeURI(String handle, String bsPid) throws IOException, SQLException { try { ResolvableIdentifier dsi = IdentifierService.resolve(context, handle); DSpaceObject dso = (DSpaceObject) IdentifierService.getResource(context, dsi); if (dso == null) { return null; } return makeURI(dso, bsPid); } catch (IdentifierException e) { log.error("caught exception: ", e); throw new RuntimeException(e); } }
/** * Utility method obtain URLs for the most recent items * * @param context DSpace context * @param items the items to get URLs for * @return an array of URLs (in Strings) corresponding to those items */ private String[] getItemURLs(Context context, List items) throws SQLException { String[] urls = new String[items.size()]; for (int i = 0; i < items.size(); i++) { Item item = (Item) items.get(i); urls[i] = IdentifierService.getURL(item).toString(); } return urls; }
/** * Show a community home page, or deal with button press on home page * * @param context Context object * @param request the HTTP request * @param response the HTTP response * @param community the community */ private void communityHome( Context context, HttpServletRequest request, HttpServletResponse response, Community community) throws ServletException, IOException, SQLException { // Handle click on a browse or search button if (!handleButton(request, response, IdentifierService.getURL(community))) { // No button pressed, display community home page log.info( LogManager.getHeader(context, "view_community", "community_id=" + community.getID())); // Get the collections within the community Collection[] collections = (Collection[]) community.getCollections().toArray(); // get any subcommunities of the community Community[] subcommunities = (Community[]) community.getSubCommunities().toArray(); // perform any necessary pre-processing preProcessCommunityHome(context, request, response, community); // is the user a COMMUNITY_EDITOR? // if (community.canEditBoolean()) if (AuthorizeManager.canEdit(community, context)) { // set a variable to create an edit button request.setAttribute("editor_button", new Boolean(true)); } // can they add to this community? if (AuthorizeManager.authorizeActionBoolean(context, community, Constants.ADD)) { // set a variable to create an edit button request.setAttribute("add_button", new Boolean(true)); } // can they remove from this community? if (AuthorizeManager.authorizeActionBoolean(context, community, Constants.REMOVE)) { // set a variable to create an edit button request.setAttribute("remove_button", new Boolean(true)); } // Forward to community home page request.setAttribute("community", community); request.setAttribute("collections", collections); request.setAttribute("subcommunities", subcommunities); JSPManager.showJSP(request, response, "/community-home.jsp"); } }
/** * Show a collection home page, or deal with button press on home page * * @param context Context object * @param request the HTTP request * @param response the HTTP response * @param community the community * @param collection the collection */ private void collectionHome( Context context, HttpServletRequest request, HttpServletResponse response, Community community, Collection collection) throws ServletException, IOException, SQLException, AuthorizeException { // Handle click on a browse or search button if (!handleButton(request, response, IdentifierService.getURL(community))) { // Will need to know whether to commit to DB boolean updated = false; // No search or browse button pressed, check for if (request.getParameter("submit_subscribe") != null) { // Subscribe button pressed. // Only registered can subscribe, so redirect unless logged in. if (context.getCurrentUser() == null && !Authenticate.startAuthentication(context, request, response)) return; else { SubscriptionManager.subscribe(context, context.getCurrentUser(), collection); updated = true; } } else if (request.getParameter("submit_unsubscribe") != null) { SubscriptionManager.unsubscribe(context, context.getCurrentUser(), collection); updated = true; } // display collection home page log.info( LogManager.getHeader(context, "view_collection", "collection_id=" + collection.getID())); // perform any necessary pre-processing preProcessCollectionHome(context, request, response, collection); // Is the user logged in/subscribed? EPerson e = context.getCurrentUser(); boolean subscribed = false; if (e != null) { subscribed = SubscriptionManager.isSubscribed(context, e, collection); // is the user a COLLECTION_EDITOR? // if (collection.canEditBoolean()) if (AuthorizeManager.canEdit(collection, context)) { // set a variable to create an edit button request.setAttribute("editor_button", new Boolean(true)); } // can they admin this collection? if (AuthorizeManager.authorizeActionBoolean( context, collection, Constants.COLLECTION_ADMIN)) { request.setAttribute("admin_button", new Boolean(true)); // give them a button to manage submitter list // what group is the submitter? Group group = collection.getSubmitters(); if (group != null) { request.setAttribute("submitters", group); } } // can they submit to this collection? if (AuthorizeManager.authorizeActionBoolean(context, collection, Constants.ADD)) { request.setAttribute("can_submit_button", new Boolean(true)); } else { request.setAttribute("can_submit_button", new Boolean(false)); } } // Forward to collection home page request.setAttribute("collection", collection); request.setAttribute("community", community); request.setAttribute("logged.in", new Boolean(e != null)); request.setAttribute("subscribed", new Boolean(subscribed)); JSPManager.showJSP(request, response, "/collection-home.jsp"); if (updated) { context.complete(); } } }
/** * Send a redirect (302) response to client with DAV URL of the resource for this handle and/or * bitstream. Puts URL in the <code>Location:</code> header. * * @return URL in string form for desired DAV resource. * @throws IOException Signals that an I/O exception has occurred. * @throws SQLException the SQL exception * @throws DAVStatusException the DAV status exception * @throw IOException * @throw SQLException */ private void doRedirect() throws IOException, SQLException, DAVStatusException { try { DSpaceObject dso = null; String bsPid = null; /* * FIXME: (maybe?) NOTE: This is currently hard-wired to accomodate the * syntax of Handles, with "prefix/suffix" separated by the slash -- * that means the Handle probably takes up multiple path elements, * unless the client escaped the '/'. This code *might* need adjusting * if we allow other kinds of persistent identifiers for DSpace objects. */ int hdlStart = -1; if (this.pathElt.length > 2 && this.pathElt[1].equals("handle")) { hdlStart = 2; } else if (this.pathElt.length > 3 && this.pathElt[1].equals("bitstream-handle")) { bsPid = this.pathElt[2]; hdlStart = 3; } else { throw new DAVStatusException( HttpServletResponse.SC_BAD_REQUEST, "Unrecognized 'lookup' request format."); } String prefix = decodeHandle(this.pathElt[hdlStart]); String handle = null; // if "prefix" contains a slash, then it's the whole handle: if (prefix.indexOf("/") >= 0) { handle = prefix; log.debug("Lookup: resolving escaped handle \"" + handle + "\""); } else if (this.pathElt.length >= hdlStart + 2) { StringBuffer hdl = new StringBuffer(prefix); for (int i = hdlStart + 1; i < this.pathElt.length; ++i) { hdl.append("/"); hdl.append(this.pathElt[i]); } handle = hdl.toString(); log.debug("Lookup: resolving multielement handle \"" + handle + "\""); } else { throw new DAVStatusException( HttpServletResponse.SC_BAD_REQUEST, "Incomplete handle in lookup request."); } // did handle lookup fail? ResolvableIdentifier dsi = IdentifierService.resolve(context, handle); dso = (DSpaceObject) IdentifierService.getResource(context, dsi); if (dso == null) { throw new DAVStatusException( HttpServletResponse.SC_NOT_FOUND, "Cannot resolve handle \"" + handle + "\""); } // bitstream must exist too String location = makeLocation(dso, bsPid); if (location == null) { throw new DAVStatusException( HttpServletResponse.SC_NOT_FOUND, "Bitstream \"" + bsPid + "\" does not exist in \"" + handle + "\""); } // add query string -- unnecessary, but it helps naive clients that // use GET with "package" query arg to download an Item. String qs = this.request.getQueryString(); if (qs != null) { location += "?" + qs; } log.debug("Lookup returning redirect to: " + location); this.response.setHeader("Location", location); this.response.sendError( HttpServletResponse.SC_MOVED_TEMPORARILY, "These are not the droids you are looking for."); } catch (IdentifierException e) { log.error("caught exception: ", e); throw new RuntimeException(e); } }