Search in sources :

Example 1 with GetableResourceEntity

use of io.milton.http.entity.GetableResourceEntity in project lobcder by skoulouzis.

the class DefaultHttp11ResponseHandler method respondPartialContent.

@Override
public void respondPartialContent(GetableResource resource, Response response, Request request, Map<String, String> params, Range range) throws NotAuthorizedException, BadRequestException, NotFoundException {
    log.debug("respondPartialContent: " + range.getStart() + " - " + range.getFinish());
    response.setStatus(Response.Status.SC_PARTIAL_CONTENT);
    long st = range.getStart() == null ? 0 : range.getStart();
    long fn;
    Long cl = resource.getContentLength();
    if (range.getFinish() == null) {
        if (cl != null) {
            // position is one less then length
            fn = cl.longValue() - 1;
        } else {
            log.warn("Couldnt calculate range end position because the resource is not reporting a content length, and no end position was requested by the client: " + resource.getName() + " - " + resource.getClass());
            fn = -1;
        }
    } else {
        fn = range.getFinish();
    }
    response.setContentRangeHeader(st, fn, cl);
    long contentLength = fn - st + 1;
    response.setDateHeader(new Date());
    String etag = eTagGenerator.generateEtag(resource);
    if (etag != null) {
        response.setEtag(etag);
    }
    String acc = request.getAcceptHeader();
    String ct = resource.getContentType(acc);
    if (ct != null) {
        response.setContentTypeHeader(ct);
    }
    response.setContentLengthHeader(contentLength);
    response.setEntity(new GetableResourceEntity(resource, range, params, ct));
}
Also used : GetableResourceEntity(io.milton.http.entity.GetableResourceEntity) BufferingGetableResourceEntity(io.milton.http.entity.BufferingGetableResourceEntity) Date(java.util.Date)

Example 2 with GetableResourceEntity

use of io.milton.http.entity.GetableResourceEntity in project lobcder by skoulouzis.

the class DefaultHttp11ResponseHandler method respondContent.

@Override
public void respondContent(Resource resource, Response response, Request request, Map<String, String> params) throws NotAuthorizedException, BadRequestException, NotFoundException {
    log.debug("respondContent: " + resource.getClass());
    Auth auth = request.getAuthorization();
    setRespondContentCommonHeaders(response, resource, auth);
    if (resource instanceof GetableResource) {
        GetableResource gr = (GetableResource) resource;
        String acc = request.getAcceptHeader();
        String ct = gr.getContentType(acc);
        if (ct != null) {
            ct = pickBestContentType(ct);
            response.setContentTypeHeader(ct);
        }
        cacheControlHelper.setCacheControl(gr, response, request.getAuthorization());
        Long contentLength = gr.getContentLength();
        Boolean doBuffering = null;
        if (resource instanceof BufferingControlResource) {
            BufferingControlResource bcr = (BufferingControlResource) resource;
            doBuffering = bcr.isBufferingRequired();
        }
        if (doBuffering == null) {
            if (buffering == null || buffering == BUFFERING.whenNeeded) {
                // if no content length then we buffer content to find content length
                doBuffering = (contentLength == null);
            } else {
                // if not null or whenNeeded then buffering is explicitly enabled or disabled
                doBuffering = (buffering == BUFFERING.always);
            }
        }
        if (!doBuffering) {
            log.trace("sending content with known content length: " + contentLength);
            if (contentLength != null) {
                response.setContentLengthHeader(contentLength);
            }
            response.setEntity(new GetableResourceEntity(gr, params, ct));
        } else {
            BufferingGetableResourceEntity e = new BufferingGetableResourceEntity(gr, params, ct, contentLength, getMaxMemorySize());
            response.setEntity(e);
        }
    }
}
Also used : BufferingGetableResourceEntity(io.milton.http.entity.BufferingGetableResourceEntity) GetableResource(io.milton.resource.GetableResource) BufferingControlResource(io.milton.resource.BufferingControlResource) GetableResourceEntity(io.milton.http.entity.GetableResourceEntity) BufferingGetableResourceEntity(io.milton.http.entity.BufferingGetableResourceEntity)

Aggregations

BufferingGetableResourceEntity (io.milton.http.entity.BufferingGetableResourceEntity)2 GetableResourceEntity (io.milton.http.entity.GetableResourceEntity)2 BufferingControlResource (io.milton.resource.BufferingControlResource)1 GetableResource (io.milton.resource.GetableResource)1 Date (java.util.Date)1