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