Exemplo n.º 1
0
  /**
   * Return a url to the DSpace object, either use the official handle for the item or build a url
   * based upon the current server.
   *
   * <p>If the dspaceobject is null then a local url to the repository is generated.
   *
   * @param request current servlet request
   * @param dso The object to reference, null if to the repository.
   * @return URL
   */
  protected String resolveURL(HttpServletRequest request, DSpaceObject dso) {
    // If no object given then just link to the whole repository,
    // since no offical handle exists so we have to use local resolution.
    if (dso == null) {
      if (baseURL == null) {
        if (request == null) {
          baseURL = ConfigurationManager.getProperty("dspace.url");
        } else {
          baseURL = (request.isSecure()) ? "https://" : "http://";
          baseURL += ConfigurationManager.getProperty("dspace.hostname");
          baseURL += ":" + request.getServerPort();
          baseURL += request.getContextPath();
        }
      }
      return baseURL;
    }

    // return a link to handle in repository
    else if (ConfigurationManager.getBooleanProperty("webui.feed.localresolve")) {
      return resolveURL(request, null) + "/handle/" + dso.getHandle();
    }

    // link to the Handle server or other persistent URL source
    else {
      return HandleServiceFactory.getInstance()
          .getHandleService()
          .getCanonicalForm(dso.getHandle());
    }
  }
Exemplo n.º 2
0
  public RDFizer() {
    this.stdout = false;
    this.verbose = false;
    this.dryrun = false;
    this.lang = "TURTLE";
    this.processed = new CopyOnWriteArraySet<UUID>();
    this.context = new Context(Context.READ_ONLY);

    this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
    this.contentServiceFactory = ContentServiceFactory.getInstance();
    this.communityService = contentServiceFactory.getCommunityService();
    this.itemService = contentServiceFactory.getItemService();
    this.handleService = HandleServiceFactory.getInstance().getHandleService();
    this.storage = RDFFactory.getInstance().getRDFStorage();
  }
/** @author Pascal-Nicolas Becker (dspace -at- pascal -hyphen- becker -dot- de) */
public class LocalURIRedirectionServlet extends HttpServlet {
  public static final String ACCEPT_HEADER_NAME = "Accept";

  private static final Logger log = Logger.getLogger(LocalURIRedirectionServlet.class);

  protected HandleService handleService = HandleServiceFactory.getInstance().getHandleService();

  /**
   * 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);
  }

  /**
   * Handles the HTTP <code>GET</code> method.
   *
   * @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 doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    processRequest(request, response);
  }

  /**
   * Handles the HTTP <code>POST</code> method.
   *
   * @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 doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    processRequest(request, response);
  }

  /**
   * Returns a short description of the servlet.
   *
   * @return a String containing servlet description
   */
  public String getServletInfo() {
    return "Ensures that URIs used in RDF can be dereferenced.";
  }
}