Example #1
0
  /**
   * Upload artifact.
   *
   * @param req the req
   * @param response the response
   * @throws ServletException the servlet exception
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void uploadArtifact(HttpServletRequest req, HttpServletResponse response)
      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);
    }

    // Extract the relevant content from the POST'd form
    Map<String, String> responseMap = new HashMap<String, String>();

    InputStream content = null;
    // Parse the request
    content = req.getInputStream();

    // Builder class that converts the url into a Maven MetaData Object
    MavenMetaData metadata = MavenMetaDataBuilder.build(maven_url);
    try {
      if (metadata.isArtifact()) {
        if (SNAPSHOT_ALLOWED || !metadata.isSnapshotVersion()) {
          String uuid = service.uploadArtifact(metadata, content);
          responseMap.put("uuid", uuid); // $NON-NLS-1$
        } else {
          response.sendError(
              HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
              Messages.i18n.format("maven.servlet.put.snapshot.not.allowed")); // $NON-NLS-1$
        }

      } else {
        response.sendError(
            HttpServletResponse.SC_BAD_REQUEST,
            Messages.i18n.format("maven.servlet.put.url.without.artifact")); // $NON-NLS-1$
      }

    } catch (Throwable e) {
      logger.error(
          Messages.i18n.format("maven.servlet.artifact.content.put.exception"), e); // $NON-NLS-1$
      response.sendError(
          HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
          Messages.i18n.format("maven.servlet.put.exception")); // $NON-NLS-1$
    } finally {
      if (content != null) {
        IOUtils.closeQuietly(content);
      }
    }
  }
Example #2
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);
    }
  }