下面列出了javax.ws.rs.core.Response#notModified() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
@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());
}
@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();
}
@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();
}
@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();
}