Search in sources :

Example 1 with VaadinServletResponse

use of com.vaadin.server.VaadinServletResponse in project ANNIS by korpling.

the class BinaryRequestHandler method sendResponse.

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");
    }
    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();
        // 4K
        response.setBufferSize(BUFFER_SIZE);
        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);
    }
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) OutputStream(java.io.OutputStream) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisBinaryMetaData(annis.service.objects.AnnisBinaryMetaData)

Example 2 with VaadinServletResponse

use of com.vaadin.server.VaadinServletResponse in project ANNIS by korpling.

the class ShortenerRequestHandler method handleRequest.

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String id = request.getParameter("id");
    if (id == null) {
        return false;
    }
    // get the actual URL
    WebResource res = Helper.getAnnisWebResource();
    res = res.path("shortener").path(id);
    String longURL = res.get(String.class);
    // redirects only work in http servlets
    if (response instanceof VaadinServletResponse) {
        ServletResponse servletResponse = ((VaadinServletResponse) response).getResponse();
        if (servletResponse instanceof HttpServletResponse) {
            HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
            httpResponse.setHeader("Location", request.getContextPath() + longURL);
            // temporary redirect
            httpResponse.setStatus(307);
            return true;
        }
    }
    return false;
}
Also used : ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) WebResource(com.sun.jersey.api.client.WebResource) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 3 with VaadinServletResponse

use of com.vaadin.server.VaadinServletResponse in project ANNIS by korpling.

the class CommonUI method checkUrlShortenerRedirect.

private boolean checkUrlShortenerRedirect(VaadinRequest request, VaadinResponse response) {
    String id = request.getParameter("id");
    if (id == null) {
        return false;
    }
    // get the actual URL
    WebResource res = Helper.getAnnisWebResource();
    res = res.path("shortener").path(id);
    String longURL = res.get(String.class);
    // redirects only work in http servlets
    if (response instanceof VaadinServletResponse) {
        ServletResponse servletResponse = ((VaadinServletResponse) response).getResponse();
        if (servletResponse instanceof HttpServletResponse) {
            HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
            httpResponse.setHeader("Location", request.getContextPath() + longURL);
            // temporary redirect
            httpResponse.setStatus(307);
            return true;
        }
    }
    return false;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) ServletResponse(javax.servlet.ServletResponse) VaadinServletResponse(com.vaadin.server.VaadinServletResponse) WebResource(com.sun.jersey.api.client.WebResource) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

WebResource (com.sun.jersey.api.client.WebResource)3 VaadinServletResponse (com.vaadin.server.VaadinServletResponse)3 ServletResponse (javax.servlet.ServletResponse)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AnnisBinaryMetaData (annis.service.objects.AnnisBinaryMetaData)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1