com.vaadin.flow.server.VaadinResponse

Here are the examples of the java api com.vaadin.flow.server.VaadinResponse taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

10 Examples 7

19 Source : VertxVaadinResponse.java
with MIT License
from mcollovati

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

19 Source : VertxBootstrapHandler.java
with MIT License
from mcollovati

@Override
protected BootstrapContext createBootstrapContext(VaadinRequest request, VaadinResponse response, UI ui, Function<VaadinRequest, String> contextPathCallback) {
    return new VertxBC(request, response, ui.getInternals().getSession(), ui, contextPathCallback);
}

18 Source : InternalErrorView.java
with MIT License
from mcollovati

protected void showCriticalNotification(String caption, String message, String details, String url) {
    VaadinService service = VaadinService.getCurrent();
    VaadinResponse response = VaadinService.getCurrentResponse();
    try {
        service.writeUncachedStringResponse(response, JsonConstants.JSON_CONTENT_TYPE, VaadinService.createCriticalNotificationJSON(caption, message, details, url));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

18 Source : VertxUidlRequestHandler.java
with MIT License
from mcollovati

@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    boolean result = super.synchronizedHandleRequest(session, request, response);
    if (result && response instanceof VertxVaadinResponse) {
        // end request in order to send messages to the client immediately
        // this is manly needed to ensure correct messages order when using push
        ((VertxVaadinResponse) response).end();
    }
    return result;
}

18 Source : VertxStreamRequestHandler.java
with MIT License
from mcollovati

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String pathInfo = request.getPathInfo();
    if (pathInfo == null) {
        return false;
    }
    // remove leading '/'
    replacedert pathInfo.startsWith(Character.toString(PATH_SEPARATOR));
    pathInfo = pathInfo.substring(1);
    if (!pathInfo.startsWith(DYN_RES_PREFIX)) {
        return false;
    }
    Optional<AbstractStreamResource> abstractStreamResource;
    session.lock();
    try {
        abstractStreamResource = VertxStreamRequestHandler.getPathUri(pathInfo).flatMap(session.getResourceRegistry()::getResource);
        if (!abstractStreamResource.isPresent()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Resource is not found for path=" + pathInfo);
            return true;
        }
    } finally {
        session.unlock();
    }
    if (abstractStreamResource.isPresent()) {
        AbstractStreamResource resource = abstractStreamResource.get();
        if (resource instanceof StreamResource) {
            resourceHandler.handleRequest(session, request, response, (StreamResource) resource);
        } else if (resource instanceof StreamReceiver) {
            StreamReceiver streamReceiver = (StreamReceiver) resource;
            String[] parts = parsePath(pathInfo);
            receiverHandler.handleRequest(session, request, response, streamReceiver, parts[0], parts[1]);
        } else {
            getLogger().warn("Received unknown stream resource.");
        }
    }
    return true;
}

17 Source : StreamReceiverHandler.java
with MIT License
from mcollovati

/**
 * Method used to stream content from a multipart request to given
 * StreamVariable.
 * <p>
 * This method takes care of locking the session as needed and does not
 * replacedume the caller has locked the session. This allows the session to be
 * locked only when needed and not when handling the upload data.
 *
 * @param session        The session containing the stream variable
 * @param request        The upload request
 * @param response       The upload response
 * @param streamReceiver the receiver containing the destination stream variable
 * @param owner          The owner of the stream
 * @throws IOException If there is a problem reading the request or writing the
 *                     response
 */
protected void doHandleMultipartFileUpload(VaadinSession session, VaadinRequest request, VaadinResponse response, Set<FileUpload> uploads, StreamReceiver streamReceiver, StateNode owner) throws IOException {
    long contentLength = getContentLength(request);
    FileSystem fileSystem = ((VertxVaadinRequest) request).getService().getVertx().fileSystem();
    try {
        uploads.forEach(item -> handleStream(session, fileSystem, streamReceiver, owner, contentLength, item));
    } catch (Exception e) {
        getLogger().warn("File upload failed.", e);
    }
    sendUploadResponse(response);
}

16 Source : StreamResourceHandler.java
with MIT License
from mcollovati

/**
 * Handle sending for a stream resource request.
 *
 * @param session        session for the request
 * @param request        request to handle
 * @param response       response object to which a response can be written.
 * @param streamResource stream resource that handles data writer
 * @throws IOException if an IO error occurred
 */
public void handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamResource streamResource) throws IOException {
    StreamResourceWriter writer;
    session.lock();
    try {
        ServletContext context = ((VertxVaadinRequest) request).getService().getServletContext();
        response.setContentType(streamResource.getContentTypeResolver().apply(streamResource, context));
        response.setCacheTime(streamResource.getCacheTime());
        writer = streamResource.getWriter();
        if (writer == null) {
            throw new IOException("Stream resource produces null input stream");
        }
    } finally {
        session.unlock();
    }
    try (OutputStream outputStream = response.getOutputStream()) {
        writer.accept(outputStream, session);
    }
}

15 Source : StreamReceiverHandler.java
with MIT License
from mcollovati

/**
 * Used to stream plain file post (aka XHR2.post(File))
 * <p>
 * This method takes care of locking the session as needed and does not
 * replacedume the caller has locked the session. This allows the session to be
 * locked only when needed and not when handling the upload data.
 * </p>
 *
 * @param session        The session containing the stream variable
 * @param request        The upload request
 * @param response       The upload response
 * @param streamReceiver the receiver containing the destination stream variable
 * @param owner          The owner of the stream
 * @param contentLength  The length of the request content
 * @throws IOException If there is a problem reading the request or writing the
 *                     response
 */
protected void doHandleXhrFilePost(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamReceiver streamReceiver, StateNode owner, long contentLength) throws IOException {
    // These are unknown in filexhr ATM, maybe add to Accept header that
    // is accessible in portlets
    final String filename = "unknown";
    final String mimeType = filename;
    final InputStream stream = request.getInputStream();
    try {
        handleFileUploadValidationAndData(session, stream, streamReceiver, filename, mimeType, contentLength, owner);
    } catch (UploadException e) {
        session.getErrorHandler().error(new ErrorEvent(e));
    }
    sendUploadResponse(response);
}

14 Source : StreamReceiverHandler.java
with MIT License
from mcollovati

/**
 * 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();
        }
    }
}

13 Source : StreamReceiverHandler.java
with MIT License
from mcollovati

/**
 * Handle reception of incoming stream from the client.
 *
 * @param session        The session for the request
 * @param request        The request to handle
 * @param response       The response object to which a response can be written.
 * @param streamReceiver the receiver containing the destination stream variable
 * @param uiId           id of the targeted ui
 * @param securityKey    security from the request that should match registered stream
 *                       receiver id
 * @throws IOException if an IO error occurred
 */
public void handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamReceiver streamReceiver, String uiId, String securityKey) throws IOException {
    StateNode source;
    session.lock();
    try {
        String secKey = streamReceiver.getId();
        if (secKey == null || !secKey.equals(securityKey)) {
            getLogger().warn("Received incoming stream with faulty security key.");
            return;
        }
        UI ui = session.getUIById(Integer.parseInt(uiId));
        UI.setCurrent(ui);
        source = streamReceiver.getNode();
    } finally {
        session.unlock();
    }
    try {
        Set<FileUpload> fileUploads = ((VertxVaadinRequest) request).getRoutingContext().fileUploads();
        if (!fileUploads.isEmpty()) {
            doHandleMultipartFileUpload(session, request, response, fileUploads, streamReceiver, source);
        } else {
            // if boundary string does not exist, the posted file is from
            // XHR2.post(File)
            doHandleXhrFilePost(session, request, response, streamReceiver, source, getContentLength(request));
        }
    } finally {
        UI.setCurrent(null);
    }
}