@Override public void respondHead(Resource resource, Response response, Request request) { // setRespondContentCommonHeaders(response, resource, Response.Status.SC_NO_CONTENT, // request.getAuthorization()); setRespondContentCommonHeaders( response, resource, Response.Status.SC_OK, request.getAuthorization()); if (!(resource instanceof GetableResource)) { return; } GetableResource gr = (GetableResource) resource; Long contentLength = gr.getContentLength(); if (contentLength != null) { response.setContentLengthHeader(contentLength); } else { log.trace("No content length is available for HEAD request"); } String acc = request.getAcceptHeader(); String ct = gr.getContentType(acc); if (ct != null) { ct = pickBestContentType(ct); if (ct != null) { response.setContentTypeHeader(ct); } } }
@Override public void processExistingResource( HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException { try { org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(); // Prevent possibily of malicious clients using remote the parser to load remote resources builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); org.jdom.Document doc = builder.build(request.getInputStream()); String reportName = doc.getRootElement().getName(); Report r = reports.get(reportName); if (r == null) { log.error("report not known: " + reportName); throw new BadRequestException(resource); } else { log.info("process report: " + reportName + " with : " + r.getClass()); String xml = r.process(request.getHostHeader(), request.getAbsolutePath(), resource, doc); if (log.isTraceEnabled()) { log.trace("Report XML:\n" + xml); } response.setStatus(Response.Status.SC_MULTI_STATUS); response.setContentTypeHeader("text/xml"); response.setEntity(new ByteArrayEntity(xml.getBytes("UTF-8"))); } } catch (JDOMException ex) { java.util.logging.Logger.getLogger(ReportHandler.class.getName()).log(Level.SEVERE, null, ex); } catch (ReadingException ex) { throw new RuntimeException(ex); } catch (WritingException ex) { throw new RuntimeException(ex); } catch (IOException ex) { throw new RuntimeException(ex); } }
@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) { doBuffering = (contentLength == null); // if no content length then we buffer content to find content length } else { doBuffering = (buffering == BUFFERING .always); // if not null or whenNeeded then buffering is explicitly enabled or // disabled } } 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); } } }
@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) { fn = cl.longValue() - 1; // position is one less then length } 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.setContentLengthHeader(contentLength); 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.setEntity(new GetableResourceEntity(resource, range, params, ct)); }
private void processCreateAndLock( HttpManager manager, Request request, Response response, Resource parentResource, String name) throws NotAuthorizedException { if (parentResource instanceof LockingCollectionResource) { log.debug("parent supports lock-null. doing createAndLock"); LockingCollectionResource lockingParent = (LockingCollectionResource) parentResource; LockTimeout timeout = LockTimeout.parseTimeout(request); response.setContentTypeHeader(Response.XML); LockInfo lockInfo; try { lockInfo = LockInfoSaxHandler.parseLockInfo(request); } catch (SAXException ex) { throw new RuntimeException("Exception reading request body", ex); } catch (IOException ex) { throw new RuntimeException("Exception reading request body", ex); } // TODO: this should be refactored to return a LockResult as for existing entities log.debug("Creating lock on unmapped resource: " + name); LockToken tok = lockingParent.createAndLock(name, timeout, lockInfo); if (tok == null) { throw new RuntimeException( "createAndLock returned null, from resource of type: " + lockingParent.getClass().getCanonicalName()); } response.setStatus(Status.SC_CREATED); response.setLockTokenHeader( "<opaquelocktoken:" + tok.tokenId + ">"); // spec says to set response header. See 8.10.1 respondWithToken(tok, request, response); } else { log.debug("parent does not support lock-null, respondong method not allowed"); responseHandler.respondMethodNotImplemented(parentResource, response, request); } }
protected void processExistingResource( HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException { if (handlerHelper.isNotCompatible(resource, request.getMethod()) || !isCompatible(resource)) { responseHandler.respondMethodNotImplemented(resource, response, request); return; } if (!handlerHelper.checkAuthorisation(manager, resource, request)) { responseHandler.respondUnauthorised(resource, response, request); return; } handlerHelper.checkExpects(responseHandler, request, response); LockableResource r = (LockableResource) resource; LockTimeout timeout = LockTimeout.parseTimeout(request); String ifHeader = request.getIfHeader(); response.setContentTypeHeader(Response.XML); if (ifHeader == null || ifHeader.length() == 0) { processNewLock(manager, request, response, r, timeout); } else { processRefresh(manager, request, response, r, timeout, ifHeader); } }
@Override public void respondNotFound(Response response, Request request) { response.setStatus(Response.Status.SC_NOT_FOUND); response.setContentTypeHeader("text/html"); contentGenerator.generate(null, request, response, Status.SC_NOT_FOUND); }