下面列出了javax.ws.rs.core.EntityTag#valueOf() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ResponseBuilder evaluateIfMatch(EntityTag eTag, Date date) {
List<String> ifMatch = headers.getRequestHeader(HttpHeaders.IF_MATCH);
if (ifMatch == null || ifMatch.isEmpty()) {
return date == null ? null : evaluateIfNotModifiedSince(date);
}
try {
for (String value : ifMatch) {
if ("*".equals(value)) {
return null;
}
EntityTag requestTag = EntityTag.valueOf(value);
// must be a strong comparison
if (!requestTag.isWeak() && !eTag.isWeak() && requestTag.equals(eTag)) {
return null;
}
}
} catch (IllegalArgumentException ex) {
// ignore
}
return Response.status(Response.Status.PRECONDITION_FAILED).tag(eTag);
}
/**
* 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;
}
private ResponseBuilder evaluateIfNonMatch(EntityTag eTag, Date lastModified) {
List<String> ifNonMatch = headers.getRequestHeader(HttpHeaders.IF_NONE_MATCH);
if (ifNonMatch == null || ifNonMatch.isEmpty()) {
return lastModified == null ? null : evaluateIfModifiedSince(lastModified);
}
String method = getMethod();
boolean getOrHead = HttpMethod.GET.equals(method) || HttpMethod.HEAD.equals(method);
try {
for (String value : ifNonMatch) {
boolean result = "*".equals(value);
if (!result) {
EntityTag requestTag = EntityTag.valueOf(value);
result = getOrHead ? requestTag.equals(eTag)
: !requestTag.isWeak() && !eTag.isWeak() && requestTag.equals(eTag);
}
if (result) {
Response.Status status = getOrHead ? Response.Status.NOT_MODIFIED
: Response.Status.PRECONDITION_FAILED;
return Response.status(status).tag(eTag);
}
}
} catch (IllegalArgumentException ex) {
// ignore
}
return null;
}
@Test
public void testFromString() {
EntityTag tag = EntityTag.valueOf("\"\"");
assertFalse(tag.isWeak() && "".equals(tag.getValue()));
tag = EntityTag.valueOf("W/");
assertTrue(tag.isWeak() && "".equals(tag.getValue()));
tag = EntityTag.valueOf("W/\"12345\"");
assertTrue(tag.isWeak() && "12345".equals(tag.getValue()));
tag = EntityTag.valueOf("\"12345\"");
assertFalse(tag.isWeak() && "12345".equals(tag.getValue()));
}
@Override
public EntityTag getEntityTag() {
Object value = getMetadata().getFirst(ETAG);
if (value == null) {
return null;
}
if (value instanceof EntityTag) {
return (EntityTag)value;
}
return EntityTag.valueOf(value instanceof String ? (String)value : getHeaderAsString(value));
}
/**
* Comparison for If-Match header and ETag.
*
* @param etag
* the ETag
* @return ResponseBuilder with status 412 (precondition failed) if If-Match header does NOT MATCH to ETag or null otherwise
*/
private ResponseBuilder evaluateIfMatch(EntityTag etag) {
String ifMatch = getRequestHeaders().getFirst(IF_MATCH);
if (isNullOrEmpty(ifMatch)) {
return null;
}
EntityTag otherEtag = EntityTag.valueOf(ifMatch);
if (eTagsStrongEqual(etag, otherEtag)) {
return null;
}
return Response.status(PRECONDITION_FAILED);
}
public EntityTag getEntityTag() {
Object header = metadata.getFirst(HttpHeaders.ETAG);
return header == null || header instanceof EntityTag ? (EntityTag)header
: EntityTag.valueOf(header.toString());
}
@Test(expected = IllegalArgumentException.class)
public void testNullValue() throws Exception {
EntityTag.valueOf(null);
}