protected InputStream handleRequest(String url) {
    Request request = new RequestBuilder().setFollowRedirects(true).setUrl(url).build();

    try {
      final PipedOutputStream po = new PipedOutputStream();

      final PipedInputStream pi = new PipedInputStream(po);

      final BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(po);

      Future<Response> f = ahcProvider.getAsyncHttpClient().executeRequest(request, bdah);

      BodyDeferringInputStream result = new BodyDeferringInputStream(f, bdah, pi);

      if (200 == result.getAsapResponse().getStatusCode()) {
        return result;
      } else {
        result.close();

        return null;
      }
    } catch (IOException e) {
      getLogger().debug("Error retrieving lvo data", e);
    } catch (InterruptedException e) {
      getLogger().debug("Error retrieving lvo data", e);
    }

    return null;
  }
Пример #2
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);
    }
  }