Search in sources :

Example 6 with GetableResource

use of io.milton.resource.GetableResource in project lobcder by skoulouzis.

the class PostJsonResource method sendContent.

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
    if (res == null) {
        String method = params.get(methodParamName);
        res = jsonResourceFactory.wrapResource(host, this, method, href);
    }
    if (res instanceof GetableResource) {
        GetableResource gr = (GetableResource) res;
        gr.sendContent(out, range, params, contentType);
    }
}
Also used : GetableResource(io.milton.resource.GetableResource)

Example 7 with GetableResource

use of io.milton.resource.GetableResource in project lobcder by skoulouzis.

the class DefaultStorageChecker method checkStorageOnReplace.

public StorageErrorReason checkStorageOnReplace(Request request, CollectionResource parent, Resource replaced, String host) {
    if (parent instanceof QuotaResource) {
        QuotaResource qr = (QuotaResource) parent;
        Long llAvail = qr.getQuotaAvailable();
        if (llAvail == null) {
            log.debug("no quota data available");
            return null;
        }
        if (llAvail <= 0) {
            // new content length must be less then existing
            Long newContentLength = request.getContentLengthHeader();
            if (newContentLength == null) {
                log.debug("new content length is not available, cant check quota, reject");
                return StorageErrorReason.SER_QUOTA_EXCEEDED;
            }
            if (replaced instanceof GetableResource) {
                GetableResource gr = (GetableResource) replaced;
                Long existingLength = gr.getContentLength();
                if (existingLength == null) {
                    log.debug("existing content length cant be determined, cant check quota, reject");
                    return StorageErrorReason.SER_QUOTA_EXCEEDED;
                } else {
                    long diff = existingLength - newContentLength;
                    if (diff > 0) {
                        return null;
                    } else {
                        log.debug("new content is larger then existing content, but no quota is available, reject");
                        return StorageErrorReason.SER_QUOTA_EXCEEDED;
                    }
                }
            } else {
                log.debug("existing content length cant be determined, cant check quota, reject");
                return StorageErrorReason.SER_QUOTA_EXCEEDED;
            }
        } else {
            // difference of new content to existing must be less then available, but if in doubt allow
            Long newContentLength = request.getContentLengthHeader();
            if (newContentLength == null) {
                log.debug("new content length is not available, cant check quota, allow");
                return null;
            }
            if (replaced instanceof GetableResource) {
                GetableResource gr = (GetableResource) replaced;
                Long existingLength = gr.getContentLength();
                if (existingLength == null) {
                    log.debug("existing content length cant be determined, cant check quota, allow");
                    return null;
                } else {
                    // this is the amount extra needed
                    long diff = newContentLength - existingLength;
                    if (diff <= llAvail) {
                        return null;
                    } else {
                        log.debug("new content is larger then existing content, but no quota is available, reject");
                        return StorageErrorReason.SER_QUOTA_EXCEEDED;
                    }
                }
            } else {
                log.debug("existing content length cant be determined, cant check quota, allow");
                return null;
            }
        }
    // if difference between new content and existing is less then available, then ok
    } else {
        return null;
    }
}
Also used : GetableResource(io.milton.resource.GetableResource) QuotaResource(io.milton.resource.QuotaResource)

Example 8 with GetableResource

use of io.milton.resource.GetableResource in project lobcder by skoulouzis.

the class LoginResponseHandler method attemptRespondLoginPage.

private void attemptRespondLoginPage(Request request, Resource resource, Response response) throws RuntimeException {
    Resource rLogin;
    try {
        rLogin = resourceFactory.getResource(request.getHostHeader(), loginPage);
    } catch (NotAuthorizedException e) {
        throw new RuntimeException(e);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    }
    if (rLogin == null || !(rLogin instanceof GetableResource)) {
        log.info("Couldnt find login resource: " + request.getHostHeader() + loginPage + " with resource factory: " + resourceFactory.getClass());
        wrapped.respondUnauthorised(resource, response, request);
    } else {
        log.trace("respond with 200 to suppress login prompt, using resource: " + rLogin.getName() + " - " + rLogin.getClass());
        try {
            // set request attribute so rendering knows it authorisation failed, or authentication is required
            Auth auth = request.getAuthorization();
            if (auth != null && auth.getTag() != null) {
                // no authentication was attempted,
                request.getAttributes().put("authReason", "notPermitted");
            } else {
                request.getAttributes().put("authReason", "required");
            }
            GetableResource gr = (GetableResource) rLogin;
            wrapped.respondContent(gr, response, request, null);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        } catch (NotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : GetableResource(io.milton.resource.GetableResource) Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 9 with GetableResource

use of io.milton.resource.GetableResource 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

GetableResource (io.milton.resource.GetableResource)9 BadRequestException (io.milton.http.exceptions.BadRequestException)3 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)2 NotFoundException (io.milton.http.exceptions.NotFoundException)2 BufferingOutputStream (io.milton.common.BufferingOutputStream)1 RandomFileOutputStream (io.milton.common.RandomFileOutputStream)1 GetEvent (io.milton.event.GetEvent)1 Range (io.milton.http.Range)1 BufferingGetableResourceEntity (io.milton.http.entity.BufferingGetableResourceEntity)1 CompressedResourceEntity (io.milton.http.entity.CompressedResourceEntity)1 GetableResourceEntity (io.milton.http.entity.GetableResourceEntity)1 InputStreamEntity (io.milton.http.entity.InputStreamEntity)1 BufferingControlResource (io.milton.resource.BufferingControlResource)1 QuotaResource (io.milton.resource.QuotaResource)1 Resource (io.milton.resource.Resource)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1