/** * Gets the currently processed Vaadin response. The current response is * automatically defined when the request is started. The current response * can not be used in e.g. background threads because of the way server * implementations reuse response instances. * * @return the current Vaadin response instance if available, otherwise * <code>null</code> * @see #setCurrentInstances(VaadinRequest, VaadinResponse) */ public static VaadinResponse getCurrentResponse() { return VaadinResponse.getCurrent(); }
/** * Commit the JSON response. We can't write immediately to the output stream * as we want to write only a critical notification if something goes wrong * during the response handling. * * @param response * The response to write to * @param json * The JSON to write * @throws IOException * If there was an exception while writing to the output */ public static void commitJsonResponse(VaadinResponse response, String json) throws IOException { response.setContentType(JsonConstants.JSON_CONTENT_TYPE); // Ensure that the browser does not cache UIDL responses. // iOS 6 Safari requires this (#9732) response.setHeader("Cache-Control", "no-cache"); byte[] b = json.getBytes(UTF_8); response.setContentLength(b.length); OutputStream outputStream = response.getOutputStream(); outputStream.write(b); // NOTE GateIn requires the buffers to be flushed to work outputStream.flush(); } }
@Override public boolean handleSessionExpired(VaadinRequest request, VaadinResponse response) throws IOException { if (!ServletHelper.isRequestType(request, RequestType.HEARTBEAT)) { return false; } response.sendError(HttpServletResponse.SC_GONE, "Session expired"); return true; } }
private static void doSetCacheTime(VaadinResponse response, long milliseconds) { if (milliseconds <= 0) { response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); } else { response.setHeader("Cache-Control", "max-age=" + milliseconds / 1000); response.setDateHeader("Expires", System.currentTimeMillis() + milliseconds); // Required to apply caching in some Tomcats response.setHeader("Pragma", "cache"); } }
private static void writeBootstrapPage(VaadinResponse response, String html) throws IOException { response.setContentType( ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); try (BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(response.getOutputStream(), UTF_8))) { writer.append(html); } }
requestHandlerMap.put(icon.getRelHref(), (session, request, response) -> { response.setContentType(icon.getType()); response.setHeader("Cache-Control", "no-cache, must-revalidate"); try (OutputStream out = response.getOutputStream()) { icon.write(out); pwaRegistry.getPwaConfiguration().relOfflinePath(), (session, request, response) -> { response.setContentType("text/html"); try (PrintWriter writer = response.getWriter()) { writer.write(pwaRegistry.getOfflineHtml()); pwaRegistry.getPwaConfiguration().relManifestPath(), (session, request, response) -> { response.setContentType("application/manifest+json"); try (PrintWriter writer = response.getWriter()) { writer.write(pwaRegistry.getManifestJson()); pwaRegistry.getPwaConfiguration().relServiceWorkerPath(), (session, request, response) -> { response.setContentType("application/javascript"); try (PrintWriter writer = response.getWriter()) { writer.write(pwaRegistry.getServiceWorkerJs());
ServletContext context = ((VaadinServletRequest) request) .getServletContext(); response.setContentType(streamResource.getContentTypeResolver() .apply(streamResource, context)); response.setCacheTime(streamResource.getCacheTime()); writer = streamResource.getWriter(); if (writer == null) { session.unlock(); try (OutputStream outputStream = response.getOutputStream()) { writer.accept(outputStream, session);
private boolean handleWorkboxResource(String fileName, VaadinResponse response) { try (InputStream stream = BootstrapHandler.class .getResourceAsStream(fileName); InputStreamReader reader = new InputStreamReader( stream, StandardCharsets.UTF_8);) { PrintWriter writer = response.getWriter(); if (fileName.endsWith(".js")) { response.setContentType("application/javascript"); } else { response.setContentType("text/plain"); } final char[] buffer = new char[1024]; int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } return true; } catch (NullPointerException e) { getLogger().debug("Workbox file '{}' does not exist", fileName, e); return false; } catch (IOException e) { getLogger().warn("Error while reading workbox file '{}'", fileName, e); return false; } }
/** * Handles a heartbeat request for the given session. Reads the GET * parameter named {@link ApplicationConstants#UI_ID_PARAMETER} to identify * the UI. If the UI is found in the session, sets it * {@link UIInternals#getLastHeartbeatTimestamp() heartbeat timestamp} to * the current time. Otherwise, writes a HTTP Not Found error to the * response. */ @Override public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { UI ui = session.getService().findUI(request); if (ui != null) { ui.getInternals() .setLastHeartbeatTimestamp(System.currentTimeMillis()); // Ensure that the browser does not cache heartbeat responses. // iOS 6 Safari requires this (#10370) response.setHeader("Cache-Control", "no-cache"); // If Content-Type is not set, browsers assume text/html and may // complain about the empty response body (#12182) response.setHeader("Content-Type", "text/plain"); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND, "UI not found"); } return true; }
/** * Sets all conceivable headers that might prevent a response from being * stored in any caches. * */ default void setNoCacheHeaders() { // no-store to disallow storing even if cache would be revalidated // must-revalidate to not use stored value even if someone asks for it setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // Also set legacy values in case of old proxies in between setHeader("Pragma", "no-cache"); setHeader("Expires", "0"); }
Writer page = response.getWriter();
/** * Properly formats a timestamp as a date in a header that will be included * in the HTTP response. If the header had already been set, the new value * overwrites the previous one. * * @see #setHeader(String, String) * @see VaadinResponse#setDateHeader(String, long) * * @param name * the name of the header. * @param timestamp * the number of milliseconds since epoch. */ public void setDateHeader(String name, long timestamp) { response.setDateHeader(name, timestamp); }
/** * Writes the given string as a response with headers to prevent caching and * using the given content type. * * @param response * The response reference * @param contentType * The content type of the response * @param responseString * The actual response * @throws IOException * If an error occurred while writing the response */ public void writeUncachedStringResponse(VaadinResponse response, String contentType, String responseString) throws IOException { // Response might contain sensitive information, so prevent all forms of // caching response.setNoCacheHeaders(); writeStringResponse(response, contentType, responseString); }
/** * Writes the given string as a response using the given content type. * * @param response * The response reference * @param contentType * The content type of the response * @param responseString * The actual response * @throws IOException * If an error occurred while writing the response */ public void writeStringResponse(VaadinResponse response, String contentType, String responseString) throws IOException { response.setContentType(contentType); final OutputStream out = response.getOutputStream(); final PrintWriter outWriter = new PrintWriter( new BufferedWriter(new OutputStreamWriter(out, UTF_8))); outWriter.print(responseString); outWriter.close(); }
/** * Sets a header value that will be added to the HTTP response. If the * header had already been set, the new value overwrites the previous one. * * @see VaadinResponse#setHeader(String, String) * * @param name * the name of the header. * @param value * the header value. */ public void setHeader(String name, String value) { response.setHeader(name, value); }
Writer page = response.getWriter(); WebBrowser browser = VaadinSession.getCurrent().getBrowser();
/** * Build response for handled download. * * @param response * response to write to * @throws IOException * exception when writing to stream */ private void sendUploadResponse(VaadinResponse response) throws IOException { response.setContentType( ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); try (OutputStream out = response.getOutputStream()) { final PrintWriter outWriter = new PrintWriter( new BufferedWriter(new OutputStreamWriter(out, UTF_8))); try { outWriter.print("<html><body>download handled</body></html>"); } finally { outWriter.flush(); } } }
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { if (!ServletHelper.isRequestType(request, RequestType.PUSH)) { return false; } if (request instanceof VaadinServletRequest) { if (atmosphere == null) { response.sendError(500, "Atmosphere initialization failed. No push available."); return true; } try { atmosphere.doCometSupport( AtmosphereRequestImpl .wrap((VaadinServletRequest) request), AtmosphereResponseImpl .wrap((VaadinServletResponse) response)); } catch (ServletException e) { // TODO PUSH decide how to handle throw new RuntimeException(e); } } else { throw new IllegalArgumentException( "Only VaadinServletRequests are supported"); } return true; }
/** * Gets the currently processed Vaadin servlet response. The current * response is automatically defined when the request is started. The * current response can not be used in e.g. background threads because of * the way server implementations reuse response instances. * * @return the current Vaadin servlet response instance if available, * otherwise <code>null</code> */ public static VaadinServletResponse getCurrent() { VaadinResponse currentResponse = VaadinResponse.getCurrent(); if (currentResponse instanceof VaadinServletResponse) { return (VaadinServletResponse) currentResponse; } else { return null; } } }
.flatMap(session.getResourceRegistry()::getResource); if (!abstractStreamResource.isPresent()) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource is not found for path=" + pathInfo); return true;