Java Code Examples for com.vaadin.server.VaadinResponse#setStatus()

The following examples show how to use com.vaadin.server.VaadinResponse#setStatus() . 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: 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 2
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 3
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();
        }
    }
}