Пример #1
0
  protected BrowserCache.Entry getEntry(ClientRequest request) throws Exception {
    String uri = request.getUri();

    BrowserCache.Entry entry = null;
    String acceptHeader = request.getHeaders().getFirst(HttpHeaders.ACCEPT);
    if (acceptHeader != null) {
      List<WeightedMediaType> waccepts = new ArrayList<WeightedMediaType>();
      String[] split = acceptHeader.split(",");
      for (String accept : split) {
        waccepts.add(WeightedMediaType.valueOf(accept));
      }
      Collections.sort(waccepts);
      List<MediaType> accepts = new ArrayList<MediaType>();
      for (WeightedMediaType accept : waccepts) {
        accepts.add(new MediaType(accept.getType(), accept.getSubtype(), accept.getParameters()));
      }
      for (MediaType accept : accepts) {
        entry = cache.get(uri, accept);
        if (entry != null) return entry;
      }
    } else {
      return cache.getAny(uri);
    }

    return null;
  }
 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;
 }
Пример #3
0
  public ClientResponse updateOnNotModified(
      ClientRequest request, BrowserCache.Entry old, BaseClientResponse response) throws Exception {
    old.getHeaders().remove(HttpHeaders.CACHE_CONTROL);
    old.getHeaders().remove(HttpHeaders.EXPIRES);
    old.getHeaders().remove(HttpHeaders.LAST_MODIFIED);
    String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL);
    String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES);
    int expires = -1;

    if (cc != null) {
      CacheControl cacheControl = CacheControl.valueOf(cc);
      if (cacheControl.isNoCache()) {
        return createClientResponse(request, old);
      }
      expires = cacheControl.getMaxAge();
    } else if (exp != null) {
      Date date = DateUtil.parseDate(exp);
      expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000);
    }

    if (cc != null) {
      old.getHeaders().putSingle(HttpHeaders.CACHE_CONTROL, cc);
    }
    if (exp != null) {
      old.getHeaders().putSingle(HttpHeaders.CACHE_CONTROL, exp);
    }

    String lastModified =
        (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
    String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG);

    if (etag == null) etag = old.getHeaders().getFirst(HttpHeaders.ETAG);
    else old.getHeaders().putSingle(HttpHeaders.ETAG, etag);

    if (lastModified != null) {
      old.getHeaders().putSingle(HttpHeaders.LAST_MODIFIED, lastModified);
    }

    if (etag == null && lastModified == null && cc == null && exp == null) // don't cache
    {
      return createClientResponse(request, old);
    }

    BrowserCache.Entry entry =
        cache.put(
            request.getUri(),
            old.getMediaType(),
            old.getHeaders(),
            old.getCached(),
            expires,
            etag,
            lastModified);
    return createClientResponse(request, entry);
  }
  @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);
    }
  }
Пример #5
0
  public ClientResponse cacheIfPossible(ClientRequest request, BaseClientResponse response)
      throws Exception {
    String cc = (String) response.getResponseHeaders().getFirst(HttpHeaders.CACHE_CONTROL);
    String exp = (String) response.getResponseHeaders().getFirst(HttpHeaders.EXPIRES);
    int expires = -1;

    if (cc != null) {
      CacheControl cacheControl = CacheControl.valueOf(cc);
      if (cacheControl.isNoCache()) return response;
      expires = cacheControl.getMaxAge();
    } else if (exp != null) {
      Date date = DateUtil.parseDate(exp);
      expires = (int) ((date.getTime() - System.currentTimeMillis()) / 1000);
    }

    String lastModified =
        (String) response.getResponseHeaders().getFirst(HttpHeaders.LAST_MODIFIED);
    String etag = (String) response.getResponseHeaders().getFirst(HttpHeaders.ETAG);

    String contentType = (String) response.getResponseHeaders().getFirst(HttpHeaders.CONTENT_TYPE);

    byte[] cached =
        ReadFromStream.readFromStream(1024, response.getStreamFactory().getInputStream());
    response.getStreamFactory().performReleaseConnection();

    MediaType mediaType = MediaType.valueOf(contentType);
    final BrowserCache.Entry entry =
        cache.put(
            request.getUri(),
            mediaType,
            response.getResponseHeaders(),
            cached,
            expires,
            etag,
            lastModified);

    response.setStreamFactory(new CachedStreamFactory(entry));

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