예제 #1
0
  @Override
  public AbstractStorageItem retrieveItem(
      ProxyRepository repository, ResourceStoreRequest request, String baseUrl)
      throws ItemNotFoundException, RemoteStorageException {
    final URL remoteURL = getAbsoluteUrlFromBase(baseUrl, request.getRequestPath());

    final String itemUrl = remoteURL.toString();

    final AsyncHttpClient client = getClient(repository);

    try {

      BodyDeferringInputStream ris = AHCUtils.fetchContent(client, itemUrl);

      // this blocks until response headers arrived
      Response response = ris.getAsapResponse();

      // expected: 200 OK
      validateResponse(repository, request, "GET", itemUrl, response, 200);

      long length = AHCUtils.getContentLength(response, -1);

      long lastModified = AHCUtils.getLastModified(response, System.currentTimeMillis());

      // non-reusable simplest content locator, the ris InputStream is ready to be consumed
      PreparedContentLocator contentLocator =
          new PreparedContentLocator(ris, response.getContentType());

      DefaultStorageFileItem result =
          new DefaultStorageFileItem(
              repository, request, true /* canRead */, true /* canWrite */, contentLocator);

      result.setLength(length);

      result.setModified(lastModified);

      result.setCreated(result.getModified());

      result.setRemoteUrl(itemUrl);

      result.getItemContext().setParentContext(request.getRequestContext());

      return result;
    } catch (ItemNotFoundException e) {
      throw e;
    } catch (RemoteStorageException e) {
      throw e;
    } catch (Exception e) {
      throw new RemoteStorageException(e);
    }
  }
예제 #2
0
  @Override
  public AbstractStorageItem retrieveItem(
      final ProxyRepository repository, final ResourceStoreRequest request, final String baseUrl)
      throws ItemNotFoundException, RemoteStorageException {
    final URL remoteURL =
        appendQueryString(getAbsoluteUrlFromBase(baseUrl, request.getRequestPath()), repository);

    final HttpGet method = new HttpGet(remoteURL.toExternalForm());

    final HttpResponse httpResponse = executeRequest(repository, request, method);

    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

      if (method.getURI().getPath().endsWith("/")) {
        // this is a collection and not a file!
        // httpClient will follow redirections, and the getPath()
        // _should_
        // give us URL with ending "/"
        release(httpResponse);
        throw new ItemNotFoundException(
            "The remoteURL we got to looks like is a collection, and Nexus cannot fetch collections over plain HTTP (remoteUrl=\""
                + remoteURL.toString()
                + "\")",
            request,
            repository);
      }

      InputStream is;
      try {
        is = httpResponse.getEntity().getContent();

        String mimeType = EntityUtils.getContentMimeType(httpResponse.getEntity());
        if (mimeType == null) {
          mimeType =
              getMimeSupport()
                  .guessMimeTypeFromPath(repository.getMimeRulesSource(), request.getRequestPath());
        }

        final DefaultStorageFileItem httpItem =
            new DefaultStorageFileItem(
                repository, request, CAN_READ, CAN_WRITE, new PreparedContentLocator(is, mimeType));

        if (httpResponse.getEntity().getContentLength() != -1) {
          httpItem.setLength(httpResponse.getEntity().getContentLength());
        }
        httpItem.setRemoteUrl(remoteURL.toString());
        httpItem.setModified(makeDateFromHeader(httpResponse.getFirstHeader("last-modified")));
        httpItem.setCreated(httpItem.getModified());
        httpItem.getItemContext().putAll(request.getRequestContext());

        return httpItem;
      } catch (IOException ex) {
        release(httpResponse);
        throw new RemoteStorageException(
            "IO Error during response stream handling [repositoryId=\""
                + repository.getId()
                + "\", requestPath=\""
                + request.getRequestPath()
                + "\", remoteUrl=\""
                + remoteURL.toString()
                + "\"]!",
            ex);
      } catch (RuntimeException ex) {
        release(httpResponse);
        throw ex;
      }
    } else {
      release(httpResponse);
      if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        throw new ItemNotFoundException(
            "The remoteURL we requested does not exists on remote server (remoteUrl=\""
                + remoteURL.toString()
                + "\")",
            request,
            repository);
      } else {
        throw new RemoteStorageException(
            "The method execution returned result code "
                + httpResponse.getStatusLine().getStatusCode()
                + ". [repositoryId=\""
                + repository.getId()
                + "\", requestPath=\""
                + request.getRequestPath()
                + "\", remoteUrl=\""
                + remoteURL.toString()
                + "\"]");
      }
    }
  }