List of usage examples for com.vaadin.server VaadinResponse sendError
public void sendError(int errorCode, String message) throws IOException;
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. j a va2 s. c o m*/ 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); } }
From source file:com.haulmont.cuba.web.sys.CubaWebJarsHandler.java
License:Apache License
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { String path = request.getPathInfo(); if (StringUtils.isEmpty(path) || StringUtils.isNotEmpty(path) && !path.startsWith(VAADIN_WEBJARS_PREFIX)) { return false; }/*from ww w . j a v a 2 s .c o m*/ log.trace("WebJar resource requested: {}", path.replace(VAADIN_WEBJARS_PREFIX, "")); String errorMessage = checkResourcePath(path); if (StringUtils.isNotEmpty(errorMessage)) { log.warn(errorMessage); response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage); return false; } URL resourceUrl = getStaticResourceUrl(path); if (resourceUrl == null) { resourceUrl = getClassPathResourceUrl(path); } if (resourceUrl == null) { String msg = String.format("Requested WebJar resource is not found: %s", path); response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); log.warn(msg); return false; } String resourceName = getResourceName(path); String mimeType = servletContext.getMimeType(resourceName); response.setContentType(mimeType != null ? mimeType : FileTypesHelper.DEFAULT_MIME_TYPE); String cacheControl = "public, max-age=0, must-revalidate"; int resourceCacheTime = getCacheTime(resourceName); if (resourceCacheTime > 0) { cacheControl = "max-age=" + String.valueOf(resourceCacheTime); } response.setHeader("Cache-Control", cacheControl); response.setDateHeader("Expires", System.currentTimeMillis() + (resourceCacheTime * 1000)); InputStream inputStream = null; try { URLConnection connection = resourceUrl.openConnection(); long lastModifiedTime = connection.getLastModified(); // Remove milliseconds to avoid comparison problems (milliseconds // are not returned by the browser in the "If-Modified-Since" // header). lastModifiedTime = lastModifiedTime - lastModifiedTime % 1000; response.setDateHeader("Last-Modified", lastModifiedTime); if (browserHasNewestVersion(request, lastModifiedTime)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } inputStream = connection.getInputStream(); copy(inputStream, response.getOutputStream()); return true; } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:org.panifex.web.vaadin.runtime.PageletAwareUI.java
License:Open Source License
@Override protected void init(VaadinRequest request) { VaadinPagelet pagelet = pageletTracker.matchPathToPagelet(request.getPathInfo()); if (pagelet != null) { try {/* www . ja va 2 s. c om*/ pagelet.service(request); } catch (Exception e) { log.error("Unable to service request", e); throw new RuntimeException(e); } } else { // pagelet not found - return HTTP 404 - Not found VaadinResponse response = CurrentInstance.get(VaadinResponse.class); try { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found"); } catch (IOException e) { e.printStackTrace(); } } }