예제 #1
0
  protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {
    ExternalIdentifierDAO identifierDAO = ExternalIdentifierDAOFactory.getInstance(context);

    Item item = null;
    Bitstream bitstream = null;

    String idString = request.getPathInfo();
    String filenameNoPath = null;
    String fullpath = null;
    String uri = null;

    // Parse URL
    if (idString != null) {
      // Remove leading slash
      if (idString.startsWith("/")) {
        idString = idString.substring(1);
      }

      // Get uri and full file path
      int slashIndex = idString.indexOf('/');
      if (slashIndex != -1) {
        slashIndex = idString.indexOf('/', slashIndex + 1);
        if (slashIndex != -1) {
          uri = idString.substring(0, slashIndex);
          fullpath =
              URLDecoder.decode(idString.substring(slashIndex + 1), Constants.DEFAULT_ENCODING);

          // Get filename with no path
          slashIndex = fullpath.indexOf('/');
          if (slashIndex != -1) {
            String[] pathComponents = fullpath.split("/");
            if (pathComponents.length <= maxDepthGuess + 1) {
              filenameNoPath = pathComponents[pathComponents.length - 1];
            }
          }
        }
      }
    }

    if (uri != null && fullpath != null) {
      // Find the item
      try {
        /*
         * If the original item doesn't have a persistent identifier
         * yet (because it's in the workflow) what we actually have is
         * a URL of the form: db-id/1234 where 1234 is the database ID
         * of the item.
         *
         * FIXME: This first part could be totally omitted now that we
         * have the dsi:x/y format of identification.
         */
        if (uri.startsWith("db-id")) {
          String dbIDString = uri.substring(uri.indexOf('/') + 1);
          int dbID = Integer.parseInt(dbIDString);
          item = ItemDAOFactory.getInstance(context).retrieve(dbID);
        } else {
          ExternalIdentifier identifier = identifierDAO.retrieve(uri);
          ObjectIdentifier oi = identifier.getObjectIdentifier();
          item = (Item) oi.getObject(context);
        }
      } catch (NumberFormatException nfe) {
        // Invalid ID - this will be dealt with below
      }
    }

    if (item != null) {
      // Try to find bitstream with exactly matching name + path
      bitstream = getItemBitstreamByName(item, fullpath);

      if (bitstream == null && filenameNoPath != null) {
        // No match with the full path, but we can try again with
        // only the filename
        bitstream = getItemBitstreamByName(item, filenameNoPath);
      }
    }

    // Did we get a bitstream?
    if (bitstream != null) {
      log.info(
          LogManager.getHeader(
              context, "view_html", "uri=" + uri + ",bitstream_id=" + bitstream.getID()));

      // Set the response MIME type
      response.setContentType(bitstream.getFormat().getMIMEType());

      // Response length
      response.setHeader("Content-Length", String.valueOf(bitstream.getSize()));

      // Pipe the bits
      InputStream is = bitstream.retrieve();

      Utils.bufferedCopy(is, response.getOutputStream());
      is.close();
      response.getOutputStream().flush();
    } else {
      // No bitstream - we got an invalid ID
      log.info(LogManager.getHeader(context, "view_html", "invalid_bitstream_id=" + idString));

      JSPManager.showInvalidIDError(request, response, idString, Constants.BITSTREAM);
    }
  }
예제 #2
0
  protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {
    ExternalIdentifierDAO identifierDAO = ExternalIdentifierDAOFactory.getInstance(context);

    // Obtain information from request
    String uri = request.getParameter("uri");
    ExternalIdentifier identifier = identifierDAO.retrieve(uri);
    ObjectIdentifier oi = identifier.getObjectIdentifier();

    // Lookup Item title & collection
    Item item = null;
    String link = "";
    String title = null;
    String collName = null;
    if (identifier != null) {
      item = (Item) oi.getObject(context);
      link = item.getIdentifier().getURL().toString();
      request.setAttribute("link", link);

      if (item != null) {
        DCValue[] titleDC = item.getDC("title", null, Item.ANY);
        if (titleDC != null || titleDC.length > 0) {
          title = titleDC[0].value;
        }
        Collection[] colls = item.getCollections();
        collName = colls[0].getMetadata("name");
      }
    } else {
      String path = request.getPathInfo();
      log.info(LogManager.getHeader(context, "invalid_id", "path=" + path));
      JSPManager.showInvalidIDError(request, response, path, -1);
      return;
    }
    if (title == null) {
      title = "";
    }
    if (collName == null) {
      collName = "";
    }
    request.setAttribute("suggest.title", title);

    // User email from context
    EPerson currentUser = context.getCurrentUser();
    String authEmail = null;
    String userName = null;

    if (currentUser != null) {
      authEmail = currentUser.getEmail();
      userName = currentUser.getFullName();
    }

    if (request.getParameter("submit") != null) {
      String recipAddr = request.getParameter("recip_email");
      // the only required field is recipient email address
      if (recipAddr == null || recipAddr.equals("")) {
        log.info(LogManager.getHeader(context, "show_suggest_form", "problem=true"));
        request.setAttribute("suggest.problem", new Boolean(true));
        JSPManager.showJSP(request, response, "/suggest/suggest.jsp");
        return;
      }
      String recipName = request.getParameter("recip_name");
      if (recipName == null || "".equals(recipName)) {
        try {
          recipName =
              I18nUtil.getMessage("org.dspace.app.webui.servlet.SuggestServlet.recipient", context);
        } catch (MissingResourceException e) {
          log.warn(
              LogManager.getHeader(
                  context,
                  "show_suggest_form",
                  "Missing Resource: org.dspace.app.webui.servlet.SuggestServlet.sender"));
          recipName = "colleague";
        }
      }
      String senderName = request.getParameter("sender_name");
      if (senderName == null || "".equals(senderName)) {
        // use userName if available
        if (userName != null) {
          senderName = userName;
        } else {
          try {
            senderName =
                I18nUtil.getMessage("org.dspace.app.webui.servlet.SuggestServlet.sender", context);
          } catch (MissingResourceException e) {
            log.warn(
                LogManager.getHeader(
                    context,
                    "show_suggest_form",
                    "Missing Resource: org.dspace.app.webui.servlet.SuggestServlet.sender"));
            senderName = "A DSpace User";
          }
        }
      }
      String senderAddr = request.getParameter("sender_email");
      if (senderAddr == null || "".equals(senderAddr)) {
        // use authEmail if available
        if (authEmail != null) {
          senderAddr = authEmail;
        }
      }
      String itemUri = identifier.getURI().toString();
      String message = request.getParameter("message");
      String siteName = ConfigurationManager.getProperty("dspace.name");

      // All data is there, send the email
      try {
        Email email =
            ConfigurationManager.getEmail(
                I18nUtil.getEmailFilename(context.getCurrentLocale(), "suggest"));
        email.addRecipient(recipAddr); // recipient address
        email.addArgument(recipName); // 1st arg - recipient name
        email.addArgument(senderName); // 2nd arg - sender name
        email.addArgument(siteName); // 3rd arg - repository name
        email.addArgument(title); // 4th arg - item title
        email.addArgument(itemUri); // 5th arg - item identifier URI
        email.addArgument(link); // 6th arg - item local URL
        email.addArgument(collName); // 7th arg - collection name
        email.addArgument(message); // 8th arg - user comments

        // Set sender's address as 'reply-to' address if supplied
        if (senderAddr != null && !"".equals(senderAddr)) {
          email.setReplyTo(senderAddr);
        }

        // Only actually send the email if feature is enabled
        if (ConfigurationManager.getBooleanProperty("webui.suggest.enable", false)) {
          email.send();
        } else {
          throw new MessagingException(
              "Suggest item email not sent - webui.suggest.enable = false");
        }

        log.info(LogManager.getHeader(context, "sent_suggest", "from=" + senderAddr));

        JSPManager.showJSP(request, response, "/suggest/suggest_ok.jsp");
      } catch (MessagingException me) {
        log.warn(LogManager.getHeader(context, "error_mailing_suggest", ""), me);
        JSPManager.showInternalError(request, response);
      }
    } else {
      // Display suggest form
      log.info(LogManager.getHeader(context, "show_suggest_form", "problem=false"));
      request.setAttribute("authenticated.email", authEmail);
      request.setAttribute("eperson.name", userName);
      JSPManager.showJSP(request, response, "/suggest/suggest.jsp"); // asd
    }
  }