Ejemplo n.º 1
0
  private Response.ResponseBuilder getNoCacheResponseBuilder(Response.Status status) {
    CacheControl cc = new CacheControl();
    cc.setNoCache(true);
    cc.setMaxAge(-1);
    cc.setMustRevalidate(true);

    return Response.status(status).cacheControl(cc);
  }
  /**
   * Get the binary content of a datastream
   *
   * @param rangeValue the range value
   * @return Binary blob
   * @throws IOException if io exception occurred
   */
  protected Response getBinaryContent(final String rangeValue) throws IOException {
    final FedoraBinary binary = (FedoraBinary) resource();

    // we include an explicit etag, because the default behavior is to use the JCR node's etag, not
    // the jcr:content node digest. The etag is only included if we are not within a transaction.
    final String txId = TransactionServiceImpl.getCurrentTransactionId(session());
    if (txId == null) {
      checkCacheControlHeaders(request, servletResponse, binary, session());
    }
    final CacheControl cc = new CacheControl();
    cc.setMaxAge(0);
    cc.setMustRevalidate(true);
    Response.ResponseBuilder builder;

    if (rangeValue != null && rangeValue.startsWith("bytes")) {

      final Range range = Range.convert(rangeValue);

      final long contentSize = binary.getContentSize();

      final String endAsString;

      if (range.end() == -1) {
        endAsString = Long.toString(contentSize - 1);
      } else {
        endAsString = Long.toString(range.end());
      }

      final String contentRangeValue =
          String.format("bytes %s-%s/%s", range.start(), endAsString, contentSize);

      if (range.end() > contentSize || (range.end() == -1 && range.start() > contentSize)) {

        builder =
            status(REQUESTED_RANGE_NOT_SATISFIABLE).header("Content-Range", contentRangeValue);
      } else {
        final RangeRequestInputStream rangeInputStream =
            new RangeRequestInputStream(binary.getContent(), range.start(), range.size());

        builder =
            status(PARTIAL_CONTENT)
                .entity(rangeInputStream)
                .header("Content-Range", contentRangeValue);
      }

    } else {
      final InputStream content = binary.getContent();
      builder = ok(content);
    }

    // we set the content-type explicitly to avoid content-negotiation from getting in the way
    return builder.type(binary.getMimeType()).cacheControl(cc).build();
  }
  private static void evaluateRequestPreconditions(
      final Request request,
      final HttpServletResponse servletResponse,
      final FedoraResource resource,
      final Session session,
      final boolean cacheControl) {

    final String txId = TransactionServiceImpl.getCurrentTransactionId(session);
    if (txId != null) {
      // Force cache revalidation if in a transaction
      servletResponse.addHeader(CACHE_CONTROL, "must-revalidate");
      servletResponse.addHeader(CACHE_CONTROL, "max-age=0");
      return;
    }

    final EntityTag etag = new EntityTag(resource.getEtagValue());
    final Date date = resource.getLastModifiedDate();
    final Date roundedDate = new Date();

    if (date != null) {
      roundedDate.setTime(date.getTime() - date.getTime() % 1000);
    }

    Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
    if (builder != null) {
      builder = builder.entity("ETag mismatch");
    } else {
      builder = request.evaluatePreconditions(roundedDate);
      if (builder != null) {
        builder = builder.entity("Date mismatch");
      }
    }

    if (builder != null && cacheControl) {
      final CacheControl cc = new CacheControl();
      cc.setMaxAge(0);
      cc.setMustRevalidate(true);
      // here we are implicitly emitting a 304
      // the exception is not an error, it's genuinely
      // an exceptional condition
      builder = builder.cacheControl(cc).lastModified(date).tag(etag);
    }
    if (builder != null) {
      throw new WebApplicationException(builder.build());
    }
  }
Ejemplo n.º 4
0
  /**
   * Get the binary content of a datastream
   *
   * @param pathList
   * @return Binary blob
   * @throws RepositoryException
   */
  @GET
  public Response getContent(
      @PathParam("path") List<PathSegment> pathList, @Context final Request request)
      throws RepositoryException {

    String path = toPath(pathList);
    final Datastream ds = datastreamService.getDatastream(path);

    final EntityTag etag = new EntityTag(ds.getContentDigest().toString());
    final Date date = ds.getLastModifiedDate();
    final Date roundedDate = new Date();
    roundedDate.setTime(date.getTime() - date.getTime() % 1000);
    ResponseBuilder builder = request.evaluatePreconditions(roundedDate, etag);

    final CacheControl cc = new CacheControl();
    cc.setMaxAge(0);
    cc.setMustRevalidate(true);

    if (builder == null) {
      builder = Response.ok(ds.getContent(), ds.getMimeType());
    }

    return builder.cacheControl(cc).lastModified(date).tag(etag).build();
  }