Пример #1
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()
                + "\"]");
      }
    }
  }