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

The following examples show how to use javax.ws.rs.core.Response#notModified() . 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
Source File: ContainerRequest.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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
Source File: ContainerRequest.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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
Source File: PictureManagementTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@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 File: TestClass19.java    From jaxrs-analyzer with 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 File: TestClass20.java    From jaxrs-analyzer with 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 File: TestClass21.java    From jaxrs-analyzer with 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();
}