private ClientRequest buildClientRequest() throws Exception {
   final ClientRequest request = mock(ClientRequest.class);
   final MultivaluedMap<String, String> headerMap = new MultivaluedMapImpl<String, String>();
   when(request.getHeaders()).thenReturn(headerMap);
   when(request.getUri()).thenReturn(URI);
   when(request.getHttpMethod()).thenReturn(HTTP_METHOD);
   return request;
 }
  @SuppressWarnings("unchecked")
  public ClientResponse execute(ClientRequest request) throws Exception {
    String uri = request.getUri();
    final HttpRequestBase httpMethod = createHttpMethod(uri, request.getHttpMethod());
    try {
      loadHttpMethod(request, httpMethod);

      final HttpResponse res = httpClient.execute(httpMethod, httpContext);

      BaseClientResponse response =
          new BaseClientResponse(
              new BaseClientResponseStreamFactory() {
                InputStream stream;

                public InputStream getInputStream() throws IOException {
                  if (stream == null) {
                    HttpEntity entity = res.getEntity();
                    if (entity == null) return null;
                    stream = new SelfExpandingBufferredInputStream(entity.getContent());
                  }
                  return stream;
                }

                public void performReleaseConnection() {
                  // Apache Client 4 is stupid, You have to get the InputStream and close it if
                  // there is an entity
                  // otherwise the connection is never released. There is, of course, no close()
                  // method on response
                  // to make this easier.
                  try {
                    if (stream != null) {
                      stream.close();
                    } else {
                      InputStream is = getInputStream();
                      if (is != null) {
                        is.close();
                      }
                    }
                  } catch (Exception ignore) {
                  }
                }
              },
              this);
      response.setAttributes(request.getAttributes());
      response.setStatus(res.getStatusLine().getStatusCode());
      response.setHeaders(extractHeaders(res));
      response.setProviderFactory(request.getProviderFactory());
      return response;
    } finally {
      cleanUpAfterExecute(httpMethod);
    }
  }
예제 #3
0
  public ClientResponse execute(ClientExecutionContext ctx) throws Exception {
    ClientRequest request = ctx.getRequest();
    if (!request.getHttpMethod().equals("GET")) {
      return ctx.proceed();
    }

    BrowserCache.Entry entry = getEntry(request);
    if (entry == null) {
      return cache(request, ctx.proceed());
    }

    if (entry.expired()) {
      cache.remove(request.getUri(), entry.getMediaType());
      BrowserCache.Header[] headers = entry.getValidationHeaders();
      for (BrowserCache.Header header : headers) {
        request.header(header.getName(), header.getValue());
      }
      return handleExpired(ctx, request, entry);
    }

    return createClientResponse(request, entry);
  }