com.vaadin.server.VaadinResponse Java Examples

The following examples show how to use com.vaadin.server.VaadinResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebExportDisplay.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected boolean handleFileNotFoundException(Exception exception, VaadinResponse response) {
    if (!(exception instanceof RuntimeFileStorageException)) {
        return false;
    }

    FileStorageException storageException = ((RuntimeFileStorageException) exception).getCause();
    if (storageException.getType() == FileStorageException.Type.FILE_NOT_FOUND) {
        try {
            writeFileNotFoundException(response, messages.formatMessage(
                    getClass(), "fileNotFound.message", storageException.getFileName()));
            return true;
        } catch (IOException e) {
            log.debug("Can't write file not found exception to the response body for: {}",
                    storageException.getFileName(), e);
            return false;
        }
    } else {
        return false;
    }
}
 
Example #2
Source File: SchemeRequestHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
  response.setContentType("text/css");

  byte[] text = generateCss();

  response.setCacheTime(-1);
  response.setStatus(HttpURLConnection.HTTP_OK);

  try (OutputStream stream = response.getOutputStream()) {
    response.setContentLength(text.length);
    stream.write(text);
  }

  return true;
}
 
Example #3
Source File: WebExportDisplay.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected void writeFileNotFoundException(VaadinResponse response, String message) throws IOException {
    response.setStatus(SC_NOT_FOUND);
    response.setHeader("Content-Type", "text/html; charset=utf-8");

    PrintWriter writer = response.getWriter();
    writer.write("<h1 style=\"font-size:40px;\">404</h1><p style=\"font-size: 25px\">" + message + "</p>");
    writer.flush();
}
 
Example #4
Source File: LoginWindow.java    From jpa-invoicer with The Unlicense 5 votes vote down vote up
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request,
        VaadinResponse response) throws IOException {
    if (request.getParameter("code") != null) {
        String code = request.getParameter("code");
        Verifier v = new Verifier(code);
        Token t = service.getAccessToken(null, v);

        OAuthRequest r = new OAuthRequest(Verb.GET,
                "https://www.googleapis.com/plus/v1/people/me");
        service.signRequest(t, r);
        Response resp = r.send();

        GooglePlusAnswer answer = new Gson().fromJson(resp.getBody(),
                GooglePlusAnswer.class);

        userSession.login(answer.emails[0].value, answer.displayName);

        close();
        VaadinSession.getCurrent().removeRequestHandler(this);

        ((VaadinServletResponse) response).getHttpServletResponse().
                sendRedirect(redirectUrl);
        return true;
    }

    return false;
}
 
Example #5
Source File: CubaWebJarsHandler.java    From cuba with Apache License 2.0 4 votes vote down vote up
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String path = request.getPathInfo();
    if (StringUtils.isEmpty(path)
            || !path.startsWith(VAADIN_WEBJARS_PATH_PREFIX)) {
        return false;
    }

    log.trace("WebJar resource requested: {}", path.replace(VAADIN_WEBJARS_PATH_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);

    long resourceCacheTime = getCacheTime();

    String cacheControl = resourceCacheTime > 0
            ? "max-age=" + String.valueOf(resourceCacheTime)
            : "public, max-age=0, no-cache, no-store, must-revalidate";
    response.setHeader("Cache-Control", cacheControl);

    long expires = resourceCacheTime > 0
            ? System.currentTimeMillis() + (resourceCacheTime * 1000)
            : 0;
    response.setDateHeader("Expires", expires);

    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();
        }
    }
}
 
Example #6
Source File: OnDemandFileDownloader.java    From XACML with MIT License 4 votes vote down vote up
@Override
public boolean handleConnectorRequest(VaadinRequest request,
		VaadinResponse response, String path) throws IOException {
	this.getResource().setFilename(this.resource.getFilename());
	return super.handleConnectorRequest(request, response, path);
}