예제 #1
0
  private void importFile(File baseDir, String relPath)
      throws StorageException, UnsupportedStorageOperationException, IllegalOperationException {
    File source = new File(baseDir, relPath);

    ResourceStoreRequest request = new ResourceStoreRequest(relPath);

    DefaultStorageFileItem file =
        new DefaultStorageFileItem(
            this,
            request,
            source.canRead(),
            source.canWrite(),
            new FileContentLocator(source, getMimeUtil().getMimeType(source)));
    file.setModified(source.lastModified());
    file.setCreated(source.lastModified());
    file.setLength(source.length());

    storeItem(false, file);
  }
  @Test
  public void testRemoteReturnsErrorWith200StatusHeadersNotSet()
      throws ItemNotFoundException, IOException {

    String expectedContent = "my cool expected content";
    ErrorServlet.CONTENT = expectedContent;

    // remote request
    ResourceStoreRequest storeRequest = new ResourceStoreRequest("random/file.txt");
    DefaultStorageFileItem item =
        (DefaultStorageFileItem)
            remoteStorage.retrieveItem(aProxyRepository, storeRequest, this.baseUrl);

    // result should be HTML
    InputStream itemInputStrem = item.getInputStream();

    try {
      String content = IOUtil.toString(itemInputStrem);
      Assert.assertEquals(expectedContent, content);
    } finally {
      IOUtil.close(itemInputStrem);
    }
  }
예제 #3
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);
    }
  }
예제 #4
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()
                + "\"]");
      }
    }
  }
  @Override
  public AbstractStorageItem retrieveItem(
      final ProxyRepository repository, final ResourceStoreRequest request, final String baseUrl)
      throws ItemNotFoundException, RemoteStorageException {
    final URL remoteURL =
        appendQueryString(
            repository, request, getAbsoluteUrlFromBase(baseUrl, request.getRequestPath()));

    final String url = remoteURL.toExternalForm();
    if (remoteURL.getPath().endsWith("/")) {
      // NEXUS-5125 we do not want to fetch any collection
      // Even though it is unlikely that we actually see a request for a collection here,
      // requests for paths like this over the REST layer will be localOnly not trigger a remote
      // request.
      //
      // The usual case is that there is a request for a directory that is redirected to '/', see
      // below behavior
      // for SC_MOVED_*
      throw new RemoteItemNotFoundException(
          request, repository, "remoteIsCollection", remoteURL.toString());
    }

    final HttpGet method = new HttpGet(url);

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

    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      InputStream is;
      try {
        is =
            new Hc4InputStream(
                repository,
                new InterruptableInputStream(method, httpResponse.getEntity().getContent()));

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

        final long entityLength = httpResponse.getEntity().getContentLength();
        final DefaultStorageFileItem httpItem =
            new DefaultStorageFileItem(
                repository,
                request,
                CAN_READ,
                CAN_WRITE,
                new PreparedContentLocator(
                    is,
                    mimeType,
                    entityLength != -1 ? entityLength : ContentLocator.UNKNOWN_LENGTH));

        httpItem.setRemoteUrl(remoteURL.toString());
        httpItem.setModified(makeDateFromHeader(httpResponse.getFirstHeader("last-modified")));
        httpItem.setCreated(httpItem.getModified());

        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 RemoteItemNotFoundException(
            request, repository, "NotFound", remoteURL.toString());
      } else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
          || httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY) {
        // NEXUS-5125 unfollowed redirect means collection (path.endsWith("/"))
        // see also HttpClientUtil#configure
        throw new RemoteItemNotFoundException(
            request, repository, "redirected", remoteURL.toString());
      } else {
        throw new RemoteStorageException(
            "The method execution returned result code "
                + httpResponse.getStatusLine().getStatusCode()
                + " (expected 200). [repositoryId=\""
                + repository.getId()
                + "\", requestPath=\""
                + request.getRequestPath()
                + "\", remoteUrl=\""
                + remoteURL.toString()
                + "\"]");
      }
    }
  }