private CacheItem putAndGet(HTTPRequest request) {
   HTTPResponse response = new HTTPResponse(null, Status.OK, new Headers());
   response = storage.insert(REQUEST, response);
   response.consume();
   assertEquals(1, storage.size());
   return storage.get(request);
 }
 @Test
 public void testPutCacheItem() {
   HTTPResponse response = new HTTPResponse(null, Status.OK, new Headers());
   response = storage.insert(REQUEST, response);
   response.consume();
   assertEquals(1, storage.size());
 }
 @Test
 public void testInvalidate() {
   HTTPResponse response = new HTTPResponse(null, Status.OK, new Headers());
   URI requestURI = URI.create("foo");
   HTTPResponse res = storage.insert(REQUEST, response);
   res.consume();
   assertEquals(1, storage.size());
   storage.invalidate(requestURI);
   assertEquals(0, storage.size());
 }
 @Test
 public void testPutUpdatedCacheItem() {
   CacheItem item = putAndGet(REQUEST);
   item.getResponse().consume();
   HTTPResponse response = new HTTPResponse(null, Status.OK, new Headers());
   HTTPResponse res = storage.update(REQUEST, response);
   res.consume();
   final CacheItem cacheItem = storage.get(REQUEST);
   assertNotSame("Items were the same", cacheItem.getCachedTime(), item.getCachedTime());
   cacheItem.getResponse().consume();
 }
 @Test
 public void testPUTWithRealPayload() throws Exception {
   HTTPResponse response =
       new HTTPResponse(
           new InputStreamPayload(new NullInputStream(10), MIMEType.APPLICATION_OCTET_STREAM),
           Status.OK,
           new Headers());
   assertEquals(0, storage.size());
   HTTPResponse res = storage.insert(REQUEST, response);
   res.consume();
   assertEquals(1, storage.size());
 }
  @Test
  public void testResolveGETWithNoHeaders() throws IOException {
    HTTPRequest request = new HTTPRequest(URI.create("http://dummy/uri/123"), HTTPMethod.GET);
    final HttpMethod method = mock(GetMethod.class);
    HTTPClientResponseResolver resolver =
        createResponseResolver(method, Status.valueOf(200), new Header[0]);

    HTTPResponse response = resolver.resolve(request);
    assertNotNull("Response was null", response);
    assertEquals(200, response.getStatus().getCode());
    assertEquals(0, response.getHeaders().size());
  }
  @Test
  public void testResolvePUTWithNoHeaders() throws IOException {
    HTTPRequest request = new HTTPRequest(URI.create("http://dummy/uri/123"), HTTPMethod.PUT);
    final Payload payload = mock(Payload.class);
    request = request.payload(payload);
    when(payload.getMimeType()).thenReturn(new MIMEType("text/plain"));
    final HttpMethod method = mock(PostMethod.class);
    HTTPClientResponseResolver resolver =
        createResponseResolver(method, Status.valueOf(200), new Header[0]);

    HTTPResponse response = resolver.resolve(request);
    assertNotNull(response);
    assertEquals(200, response.getStatus().getCode());
    assertEquals(0, response.getHeaders().size());
  }
예제 #8
0
  @Override
  public HTTPResponse update(HTTPRequest request, HTTPResponse response) {
    Key key = Key.create(request, response);
    Connection connection = getConnection();

    PreparedStatement statement = null;
    try {
      JdbcUtil.startTransaction(connection);
      statement =
          connection.prepareStatement(
              "update response set headers = ?, cachetime = ? where uri = ? and vary = ?");
      statement.setString(1, response.getHeaders().toJSON());
      statement.setTimestamp(2, new Timestamp(DateTimeUtils.currentTimeMillis()));
      statement.setString(3, key.getURI().toString());
      statement.setString(4, key.getVary().toJSON());
      statement.executeUpdate();
      connection.commit();
      return getImpl(connection, key);
    } catch (SQLException e) {
      JdbcUtil.rollback(connection);
      JdbcUtil.close(connection);
      throw new DataAccessException(e);
    } finally {
      JdbcUtil.endTransaction(connection);
      JdbcUtil.close(statement);
    }
  }
예제 #9
0
  @Override
  public HTTPResponse insert(HTTPRequest request, HTTPResponse response) {
    Key key = Key.create(request, response);
    Connection connection = getConnection();

    String sql =
        "insert into response(uri, vary, status, headers, payload, mimeType, cachetime) values (?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement statement = null;
    try {
      JdbcUtil.startTransaction(connection);
      invalidate(key, connection);
      statement = connection.prepareStatement(sql);
      statement.setString(1, key.getURI().toString());
      statement.setString(2, key.getVary().toJSON());
      statement.setInt(3, response.getStatus().getCode());
      statement.setString(4, response.getHeaders().toJSON());
      InputStream inputStream = null;
      if (response.hasPayload() && response.getPayload().isAvailable()) {
        statement.setString(6, response.getPayload().getMimeType().toString());
        inputStream = response.getPayload().getInputStream();
        statement.setBinaryStream(5, inputStream);
      } else {
        statement.setNull(5, Types.BLOB);
        statement.setNull(6, Types.VARCHAR);
      }
      statement.setTimestamp(7, new Timestamp(DateTimeUtils.currentTimeMillis()));
      try {
        statement.executeUpdate();
      } finally {
        IOUtils.closeQuietly(inputStream);
      }
      connection.commit();
      return getImpl(connection, key);
    } catch (SQLException e) {
      JdbcUtil.rollback(connection);
      JdbcUtil.close(connection);
      throw new DataAccessException(e);
    } finally {
      JdbcUtil.endTransaction(connection);
      JdbcUtil.close(statement);
    }
  }