예제 #1
0
 private BaseClientResponse createClientResponse(ClientRequest request, BrowserCache.Entry entry) {
   BaseClientResponse response = new BaseClientResponse(new CachedStreamFactory(entry));
   response.setStatus(200);
   response.setHeaders(entry.getHeaders());
   response.setProviderFactory(request.getProviderFactory());
   return response;
 }
  @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);
    }
  }