Ejemplo n.º 1
0
  protected DSpaceObject resolveHandle(String handle) {
    DSpaceObject dso = null;
    try {
      dso = handleService.resolveToObject(this.context, handle);
    } catch (SQLException ex) {
      log.error(ex);
      System.err.println(
          "A problem with the database connection " + "occurred. Canceled pending actions.");
      System.err.println(ex.getMessage());
      ex.printStackTrace(System.err);
      System.exit(1);
    } catch (IllegalStateException ex) {
      log.error(ex);
      System.err.println("Cannot recognize identifier '" + handle + "', skipping.");
      return null;
    }
    if (dso == null) {
      System.err.println("Cannot resolve identifier '" + handle + "', skipping.");
      log.debug("Couldn't resolve identifier '" + handle + "', dso was null.");
      return null;
    }
    if (dso.getType() != Constants.SITE
        && dso.getType() != Constants.COMMUNITY
        && dso.getType() != Constants.COLLECTION
        && dso.getType() != Constants.ITEM) {
      System.err.println(
          contentServiceFactory.getDSpaceObjectService(dso).getTypeText(dso)
              + " are currently not "
              + "supported as independent entities. Bundles and Bitstreams "
              + "should be processed as part of their item.");
      return null;
    }

    return dso;
  }
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // we expect a path in the form /resource/<prefix>/<suffix>.
    String pathInfo = request.getPathInfo();

    log.debug("Pathinfo: " + pathInfo);
    if (StringUtils.isEmpty(pathInfo) || StringUtils.countMatches(pathInfo, "/") < 2) {
      log.debug("Path does not contain the expected number of slashes.");
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // remove trailing slash of the path info and split it.
    String[] path = request.getPathInfo().substring(1).split("/");

    String handle = path[0] + "/" + path[1];
    String dspaceURL = (new DSpace()).getConfigurationService().getProperty("dspace.url");

    // Prepare content negotiation
    int requestedMimeType = Negotiator.negotiate(request.getHeader(ACCEPT_HEADER_NAME));

    Context context = null;
    DSpaceObject dso = null;
    try {
      context = new Context(Context.READ_ONLY);
      dso = handleService.resolveToObject(context, handle);
    } catch (SQLException ex) {
      log.error("SQLException: " + ex.getMessage(), ex);
      context.abort();
      // probably a problem with the db connection => send Service Unavailable
      response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
      return;
    } catch (IllegalStateException ex) {
      log.error(
          "Cannot resolve handle " + handle + ". IllegalStateException:" + ex.getMessage(), ex);
      context.abort();
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }
    if (dso == null) {
      log.info("Cannot resolve handle '" + handle + "' to dso. => 404");
      context.abort();
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    // close the context and send forward.
    context.abort();
    Negotiator.sendRedirect(response, handle, "", requestedMimeType, true);
  }