Ejemplo n.º 1
0
  /**
   * Do get.
   *
   * @param req the req
   * @param resp the resp
   * @throws ServletException the servlet exception
   * @throws IOException Signals that an I/O exception has occurred.
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    // Get the URL request and prepare it to obtain the maven metadata
    // information
    String url = req.getRequestURI();
    String maven_url = ""; // $NON-NLS-1$
    if (url.contains(URL_CONTEXT_STR)) {
      maven_url = url.substring(url.indexOf(URL_CONTEXT_STR) + URL_CONTEXT_STR.length());
    } else {
      maven_url = url;
    }

    if (maven_url.startsWith("/")) { // $NON-NLS-1$
      maven_url = maven_url.substring(1);
    }

    // Builder class that converts the url into a Maven MetaData Object
    MavenMetaData metadata = MavenMetaDataBuilder.build(maven_url);

    // If it is possible to detect a maven metadata information and it is
    // found an artifact information
    if (metadata.isArtifact()) {

      // Here we have the gav info. So let's go to Sramp to
      // obtain the InputStream with the info
      MavenArtifactWrapper artifact = null;
      try {
        artifact = service.getArtifactContent(metadata);
        if (artifact != null) {
          resp.setContentLength(artifact.getContentLength());
          resp.addHeader(
              "Content-Disposition", //$NON-NLS-1$
              "attachment; filename=" + artifact.getFileName()); // $NON-NLS-1$
          resp.setContentType(artifact.getContentType());
          IOUtils.copy(artifact.getContent(), resp.getOutputStream());
        } else {
          listItemsResponse(req, resp, maven_url);
        }
      } catch (MavenRepositoryException e) {
        logger.info(
            Messages.i18n.format(
                "maven.servlet.artifact.content.get.exception", //$NON-NLS-1$
                metadata.getGroupId(),
                metadata.getArtifactId(),
                metadata.getVersion(),
                metadata.getFileName()));
        // Send a 500 error if there's an exception
        resp.sendError(500);
      } finally {
        if (artifact != null) {
          IOUtils.closeQuietly(artifact.getContent());
        }
      }

    } else {
      // In case the metadata information is not an artifact, then the
      // maven url is listed
      listItemsResponse(req, resp, maven_url);
    }
  }