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);
}
}
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;
}
}
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);
}
}
}
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);
}
}
}
Aggregations