@Override public javax.ws.rs.core.Response.ResponseBuilder tag(String tag) { return tag(tag == null ? null : new EntityTag(tag)); }
private EntityTag computeEntityTag(final URI u) { return new EntityTag( computeDigest(u.getRawPath() + u.getRawQuery())); }
@Override public javax.ws.rs.core.Response.ResponseBuilder tag(String tag) { return tag(tag == null ? null : new EntityTag(tag)); }
/** * Set a strong entity tag. This is a shortcut * for <code>tag(new EntityTag(<i>value</i>))</code>. * * @param tag the string content of a strong entity tag. The JAX-RS * runtime will quote the supplied value when creating the header. If * null any existing entity tag value will be removed. * @return the updated instance */ public B tag(String tag) { return tag(tag == null ? null : new EntityTag(tag)); }
@Override public EntityTag fromString(String header) { throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.ENTITY_TAG_IS_NULL()); try { HttpHeaderReader reader = HttpHeaderReader.newInstance(header); Event e = reader.next(false); if (e == Event.QuotedString) { return new EntityTag(reader.getEventValue().toString()); } else if (e == Event.Token) { final CharSequence ev = reader.getEventValue(); if (ev != null && ev.length() > 0 && ev.charAt(0) == 'W') { reader.nextSeparator('/'); return new EntityTag(reader.nextQuotedString().toString(), true); } } } catch (ParseException ex) { throw new IllegalArgumentException( "Error parsing entity tag '" + header + "'", ex); } throw new IllegalArgumentException( "Error parsing entity tag '" + header + "'"); } }
@Override public EntityTag fromString(String header) { throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.ENTITY_TAG_IS_NULL()); try { HttpHeaderReader reader = HttpHeaderReader.newInstance(header); Event e = reader.next(false); if (e == Event.QuotedString) { return new EntityTag(reader.getEventValue().toString()); } else if (e == Event.Token) { final CharSequence ev = reader.getEventValue(); if (ev != null && ev.length() > 0 && ev.charAt(0) == 'W') { reader.nextSeparator('/'); return new EntityTag(reader.nextQuotedString().toString(), true); } } } catch (ParseException ex) { throw new IllegalArgumentException( "Error parsing entity tag '" + header + "'", ex); } throw new IllegalArgumentException( "Error parsing entity tag '" + header + "'"); } }
private Response.ResponseBuilder evaluateIfNoneMatch(final EntityTag eTag, final Set<? extends EntityTag> matchingTags, final boolean isGetOrHead) { if (isGetOrHead) { if (matchingTags == MatchingEntityTag.ANY_MATCH) { // 304 Not modified return Response.notModified(eTag); } // The weak comparison function may be used to compare entity tags if (matchingTags.contains(eTag) || matchingTags.contains(new EntityTag(eTag.getValue(), !eTag.isWeak()))) { // 304 Not modified return Response.notModified(eTag); } } else { // The strong comparison function must be used to compare the entity // tags. Thus if the entity tag of the entity is weak then matching // of entity tags in the If-None-Match header should fail if the // HTTP method is not GET or not HEAD. if (eTag.isWeak()) { return null; } if (matchingTags == MatchingEntityTag.ANY_MATCH || matchingTags.contains(eTag)) { // 412 Precondition Failed return Response.status(Response.Status.PRECONDITION_FAILED); } } return null; }
private Response.ResponseBuilder evaluateIfNoneMatch(final EntityTag eTag, final Set<? extends EntityTag> matchingTags, final boolean isGetOrHead) { if (isGetOrHead) { if (matchingTags == MatchingEntityTag.ANY_MATCH) { // 304 Not modified return Response.notModified(eTag); } // The weak comparison function may be used to compare entity tags if (matchingTags.contains(eTag) || matchingTags.contains(new EntityTag(eTag.getValue(), !eTag.isWeak()))) { // 304 Not modified return Response.notModified(eTag); } } else { // The strong comparison function must be used to compare the entity // tags. Thus if the entity tag of the entity is weak then matching // of entity tags in the If-None-Match header should fail if the // HTTP method is not GET or not HEAD. if (eTag.isWeak()) { return null; } if (matchingTags == MatchingEntityTag.ANY_MATCH || matchingTags.contains(eTag)) { // 412 Precondition Failed return Response.status(Response.Status.PRECONDITION_FAILED); } } return null; }
final Date lastModified = Date.from(lastModifiedTime.toInstant()); final HashCode hashCode = Hashing.sha256().hashBytes(fileContents); final EntityTag entityTag = new EntityTag(hashCode.toString());
private ResponseBuilder evaluateIfNoneMatch( EntityTag eTag, Set<MatchingEntityTag> matchingTags, boolean isGetOrHead) { if (isGetOrHead) { if (matchingTags == MatchingEntityTag.ANY_MATCH) { // 304 Not modified return Response.notModified(eTag); } // The weak comparison function may be used to compare entity tags if (matchingTags.contains(eTag) || matchingTags.contains(new EntityTag(eTag.getValue(), !eTag.isWeak()))) { // 304 Not modified return Response.notModified(eTag); } } else { // The strong comparison function must be used to compare the entity // tags. Thus if the entity tag of the entity is weak then matching // of entity tags in the If-None-Match header should fail if the // HTTP method is not GET or not HEAD. if (eTag.isWeak()) { return null; } if (matchingTags == MatchingEntityTag.ANY_MATCH || matchingTags.contains(eTag)) { // 412 Precondition Failed return Responses.preconditionFailed(); } } return null; }
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { response.getHttpHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); // add vary header String acceptEncoding = request.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING); String contentEncoding = (String) response.getHttpHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if (acceptEncoding != null && contentEncoding == null && acceptEncoding.contains("gzip")) { // Check EntityTag header if (response.getHttpHeaders().containsKey(HttpHeaders.ETAG)) { EntityTag entityTag = (EntityTag) response.getHttpHeaders().getFirst(HttpHeaders.ETAG); if (entityTag != null) { response.getHttpHeaders().putSingle(HttpHeaders.ETAG, new EntityTag(entityTag.getValue() + ENTITY_TAG_GZIP_SUFFIX_VALUE, entityTag.isWeak())); } } // wrap entity with gzip if (response.getEntity() != null) { response.getHttpHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); response.setContainerResponseWriter(new Adapter(response.getContainerResponseWriter())); } } return response; } }
public EntityTag fromString(String value) throws IllegalArgumentException { if (value == null) throw new IllegalArgumentException(Messages.MESSAGES.entityTagValueNull()); boolean weakTag = false; if (value.startsWith("W/")) { weakTag = true; value = value.substring(2); } if (value.startsWith("\"")) { value = value.substring(1); } if (value.endsWith("\"")) { value = value.substring(0, value.length() - 1); } return new EntityTag(value, weakTag); }
EntityTag tag = new EntityTag(Hashing.sha1().hashString(json, Charsets.UTF_8).toString());
@Override public Response.ResponseBuilder tag(String tag) { if (tag == null) { metadata.remove(HttpHeaderNames.ETAG); return this; } return tag(new EntityTag(tag)); }
private Response.ResponseBuilder evaluateIfNoneMatch(final EntityTag eTag, final Set<? extends EntityTag> matchingTags, final boolean isGetOrHead) { if (isGetOrHead) { if (matchingTags == MatchingEntityTag.ANY_MATCH) { // 304 Not modified return Response.notModified(eTag); } // The weak comparison function may be used to compare entity tags if (matchingTags.contains(eTag) || matchingTags.contains(new EntityTag(eTag.getValue(), !eTag.isWeak()))) { // 304 Not modified return Response.notModified(eTag); } } else { // The strong comparison function must be used to compare the entity // tags. Thus if the entity tag of the entity is weak then matching // of entity tags in the If-None-Match header should fail if the // HTTP method is not GET or not HEAD. if (eTag.isWeak()) { return null; } if (matchingTags == MatchingEntityTag.ANY_MATCH || matchingTags.contains(eTag)) { // 412 Precondition Failed return Response.status(Response.Status.PRECONDITION_FAILED); } } return null; }
Response.ResponseBuilder builder = validation.evaluatePreconditions(new EntityTag(entry.getEtag())); CacheControl cc = new CacheControl(); cc.setMaxAge(entry.getExpirationInSeconds());
Response.ResponseBuilder validatedResponse = validation.evaluatePreconditions(new EntityTag(etag)); if (validatedResponse != null)
@GET @Path("/books/response/{bookId}/") @Produces("application/xml") public Response getBookAsResponse(@PathParam("bookId") String id) throws BookNotFoundFault { Book entity = doGetBook(id); EntityTag etag = new EntityTag(Integer.toString(entity.hashCode())); return Response.ok().tag(etag).entity(entity).build(); }