Java Code Examples for javax.ws.rs.core.Response#notModified()

The following examples show how to use javax.ws.rs.core.Response#notModified() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Comparison for If-None-Match header and ETag.
 *
 * @param etag
 *         the ETag
 * @return ResponseBuilder with status 412 (precondition failed) if If-None-Match header is MATCH to ETag and HTTP method is not GET or
 * HEAD. If method is GET or HEAD and If-None-Match is MATCH to ETag then ResponseBuilder with status 304 (not modified) will be
 * returned.
 */
private ResponseBuilder evaluateIfNoneMatch(EntityTag etag) {
    String ifNoneMatch = getRequestHeaders().getFirst(IF_NONE_MATCH);

    if (Strings.isNullOrEmpty(ifNoneMatch)) {
        return null;
    }

    EntityTag otherEtag = EntityTag.valueOf(ifNoneMatch);
    String httpMethod = getMethod();
    if (httpMethod.equals(GET) || httpMethod.equals(HEAD)) {
        if (eTagsWeakEqual(etag, otherEtag)) {
            return Response.notModified(etag);
        }
    } else {
        if (eTagsStrongEqual(etag, otherEtag)) {
            return Response.status(PRECONDITION_FAILED);
        }
    }
    return null;
}
 
Example 2
/**
 * Comparison for lastModified and modifiedSince times.
 *
 * @param lastModified
 *         the last modified time
 * @return ResponseBuilder with status 304 (not modified) if lastModified time is greater then modifiedSince otherwise return null. If
 * date format in header If-Modified-Since is wrong also null returned
 */
private ResponseBuilder evaluateIfModified(long lastModified) {
    String ifModified = getRequestHeaders().getFirst(IF_MODIFIED_SINCE);

    if (isNullOrEmpty(ifModified)) {
        return null;
    }
    try {
        long modifiedSince = HeaderHelper.parseDateHeader(ifModified).getTime();
        if (lastModified <= modifiedSince) {
            return Response.notModified();
        }
    } catch (IllegalArgumentException ignored) {
    }

    return null;
}
 
Example 3
@Test
public void testPictureResponse() throws IOException, URISyntaxException {
    Request request = Mockito.mock(Request.class);
    doReturn(null).when(request).evaluatePreconditions(any(EntityTag.class));

    InlinePictureEntity mockImage = new InlinePictureEntity();
    byte[] imageContent = Files.readAllBytes(Paths.get(this.getClass().getClassLoader().getResource("media/logo.svg").toURI()));
    mockImage.setContent(imageContent);
    mockImage.setType("image/svg");

    Response response = pictureResourceForTest.createPictureResponse(request, mockImage);

    assertEquals(OK_200, response.getStatus());

    MultivaluedMap<String, Object> headers = response.getHeaders();
    MediaType mediaType = (MediaType) headers.getFirst(HttpHeader.CONTENT_TYPE.asString());
    String etag = ((EntityTag) headers.getFirst("ETag")).getValue();

    assertEquals(mockImage.getType(), mediaType.toString());

    ByteArrayOutputStream baos = (ByteArrayOutputStream)response.getEntity();
    byte[] fileContent = baos.toByteArray();
    assertTrue(Arrays.equals(fileContent, imageContent));

    String expectedTag = Integer.toString(new String(fileContent).hashCode());
    assertEquals(expectedTag, etag);


    // test Cache
    ResponseBuilder responseBuilder = Response.notModified();
    doReturn(responseBuilder).when(request).evaluatePreconditions(any(EntityTag.class));

    final Response cachedResponse = pictureResourceForTest.createPictureResponse(request, mockImage);
    assertEquals(NOT_MODIFIED_304, cachedResponse.getStatus());
}
 
Example 4
Source Project: jaxrs-analyzer   File: TestClass19.java    License: Apache License 2.0 5 votes vote down vote up
@javax.ws.rs.GET public Response method() {
    Response.ResponseBuilder responseBuilder = Response.accepted();
    responseBuilder = Response.created(URI.create(""));
    responseBuilder = Response.noContent();
    responseBuilder = Response.notAcceptable(new LinkedList<>());
    responseBuilder = Response.notModified();
    responseBuilder = Response.ok();
    responseBuilder = Response.ok(1L, new Variant(MediaType.TEXT_PLAIN_TYPE, Locale.ENGLISH, "UTF-8"));
    responseBuilder = Response.seeOther(URI.create(""));
    responseBuilder = Response.serverError();
    responseBuilder = Response.temporaryRedirect(URI.create(""));

    return responseBuilder.build();
}
 
Example 5
Source Project: jaxrs-analyzer   File: TestClass20.java    License: Apache License 2.0 5 votes vote down vote up
@javax.ws.rs.GET public Response method() {
    Response.ResponseBuilder responseBuilder = Response.accepted("Hello");
    responseBuilder = Response.notModified(new EntityTag(""));
    responseBuilder = Response.ok(1, MediaType.APPLICATION_XML);

    return responseBuilder.build();
}
 
Example 6
Source Project: jaxrs-analyzer   File: TestClass21.java    License: Apache License 2.0 5 votes vote down vote up
@javax.ws.rs.GET
public Response method() {
    Response.ResponseBuilder responseBuilder = Response.notModified("");
    responseBuilder = Response.ok(1d, MediaType.APPLICATION_JSON_TYPE);
    responseBuilder = Response.ok(1L, new Variant(MediaType.TEXT_PLAIN_TYPE, Locale.ENGLISH, "UTF-8"));

    return responseBuilder.build();
}