Example #1
0
  private Response.ResponseBuilder evaluateIfNoneMatch(
      EntityTag eTag, Set<? extends EntityTag> 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 Response.status(Response.Status.PRECONDITION_FAILED);
      }
    }

    return null;
  }
  /**
   * Add ETag and Last-Modified cache control headers to the response
   *
   * @param servletResponse the servlet response
   * @param resource the fedora resource
   * @param session the session
   */
  protected static void addCacheControlHeaders(
      final HttpServletResponse servletResponse,
      final FedoraResource resource,
      final Session session) {

    final String txId = TransactionServiceImpl.getCurrentTransactionId(session);
    if (txId != null) {
      // Do not add caching headers if in a transaction
      return;
    }

    final FedoraResource mutableResource =
        resource instanceof NonRdfSourceDescription
            ? ((NonRdfSourceDescription) resource).getDescribedResource()
            : resource;
    final EntityTag etag = new EntityTag(mutableResource.getEtagValue());
    final Date date = mutableResource.getLastModifiedDate();

    if (!etag.getValue().isEmpty()) {
      servletResponse.addHeader("ETag", etag.toString());
    }

    if (date != null) {
      servletResponse.addDateHeader("Last-Modified", date.getTime());
    }
  }
Example #3
0
 @Override
 public EntityTag getEntityTag() {
   Object d = metadata.getFirst(HttpHeaders.ETAG);
   if (d == null) return null;
   if (d instanceof EntityTag) return (EntityTag) d;
   return EntityTag.valueOf(getHeaderValueProcessor().toHeaderString(d));
 }
Example #4
0
  // Private methods
  private Response.ResponseBuilder evaluateIfMatch(EntityTag eTag) {
    Set<? extends EntityTag> matchingTags = getIfMatch();
    if (matchingTags == null) {
      return null;
    }

    // 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-Match header should fail.
    if (eTag.isWeak()) {
      return Response.status(Response.Status.PRECONDITION_FAILED);
    }

    if (matchingTags != MatchingEntityTag.ANY_MATCH && !matchingTags.contains(eTag)) {
      // 412 Precondition Failed
      return Response.status(Response.Status.PRECONDITION_FAILED);
    }

    return null;
  }
Example #5
0
 /** {@inheritDoc} */
 public Client match(EntityTag tag, boolean ifNot) {
   String hName = ifNot ? HttpHeaders.IF_NONE_MATCH : HttpHeaders.IF_MATCH;
   state.getRequestHeaders().putSingle(hName, tag.toString());
   return this;
 }