Example usage for com.vaadin.server VaadinServletResponse setCacheTime

List of usage examples for com.vaadin.server VaadinServletResponse setCacheTime

Introduction

In this page you can find the example usage for com.vaadin.server VaadinServletResponse setCacheTime.

Prototype

@Override
    public void setCacheTime(long milliseconds) 

Source Link

Usage

From source file:annis.gui.requesthandler.BinaryRequestHandler.java

License:Apache License

public void sendResponse(VaadinSession session, VaadinRequest request, VaadinResponse pureResponse,
        boolean sendContent) throws IOException {
    if (!(pureResponse instanceof VaadinServletResponse)) {
        pureResponse.sendError(500, "Binary requests only work with servlets");
    }/* ww w . java 2s  . c  om*/

    VaadinServletResponse response = (VaadinServletResponse) pureResponse;

    Map<String, String[]> binaryParameter = request.getParameterMap();
    String toplevelCorpusName = binaryParameter.get("toplevelCorpusName")[0];
    String documentName = binaryParameter.get("documentName")[0];

    String mimeType = null;
    if (binaryParameter.containsKey("mime")) {
        mimeType = binaryParameter.get("mime")[0];
    }
    try {
        // always set the buffer size to the same one we will use for coyping, 
        // otherwise we won't notice any client disconnection
        response.reset();
        response.setCacheTime(-1);
        response.resetBuffer();
        response.setBufferSize(BUFFER_SIZE); // 4K

        String requestedRangeRaw = request.getHeader("Range");

        WebResource binaryRes = Helper.getAnnisWebResource().path("query").path("corpora")
                .path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName))
                .path("binary");

        WebResource metaBinaryRes = Helper.getAnnisWebResource().path("meta").path("binary")
                .path(urlPathEscape.escape(toplevelCorpusName)).path(urlPathEscape.escape(documentName));

        // tell client that we support byte ranges
        response.setHeader("Accept-Ranges", "bytes");

        Preconditions.checkNotNull(mimeType, "No mime type given (parameter \"mime\"");

        AnnisBinaryMetaData meta = getMatchingMetadataFromService(metaBinaryRes, mimeType);
        if (meta == null) {
            response.sendError(404, "Binary file not found");
            return;
        }

        ContentRange fullRange = new ContentRange(0, meta.getLength() - 1, meta.getLength());

        ContentRange r = fullRange;
        try {
            if (requestedRangeRaw != null) {
                List<ContentRange> requestedRanges = ContentRange.parseFromHeader(requestedRangeRaw,
                        meta.getLength(), 1);

                if (!requestedRanges.isEmpty()) {
                    r = requestedRanges.get(0);
                }
            }

            long contentLength = (r.getEnd() - r.getStart() + 1);

            boolean useContentRange = !fullRange.equals(r);

            response.setContentType(meta.getMimeType());
            if (useContentRange) {
                response.setHeader("Content-Range", r.toString());
            }
            response.setContentLength((int) contentLength);
            response.setStatus(useContentRange ? 206 : 200);

            response.flushBuffer();
            if (sendContent) {
                try (OutputStream out = response.getOutputStream();) {
                    writeFromServiceToClient(r.getStart(), contentLength, binaryRes, out, mimeType);
                }
            }

        } catch (ContentRange.InvalidRangeException ex) {
            response.setHeader("Content-Range", "bytes */" + meta.getLength());
            response.sendError(416, "Requested range not satisfiable: " + ex.getMessage());
            return;
        }

    } catch (IOException ex) {
        log.warn("IOException in BinaryRequestHandler", ex);
        response.setStatus(500);
    } catch (ClientHandlerException | UniformInterfaceException ex) {
        log.error(null, ex);
        response.setStatus(500);
    }
}