Example usage for com.vaadin.server VaadinResponse setHeader

List of usage examples for com.vaadin.server VaadinResponse setHeader

Introduction

In this page you can find the example usage for com.vaadin.server VaadinResponse setHeader.

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets the value of a generic response header.

Usage

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   w w w .j a  v a  2 s.co 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:de.uni_tuebingen.qbic.qbicmainportlet.ProxyForGenomeViewerRestApi.java

License:Open Source License

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
        throws IOException {
    if (UI.getCurrent() != null && UI.getCurrent().getPage() != null
            && UI.getCurrent().getPage().getUriFragment() != null) {
        System.out.println(UI.getCurrent().getPage().getUriFragment());
    }/*from  w w w.j a v  a 2  s  .co  m*/
    System.out.println("is handling request");
    System.out.println(request.getPathInfo());
    System.out.println(request.getContextPath());
    PortletRequest portletRequest = VaadinPortletService.getCurrentPortletRequest();
    Map<String, String[]> para = request.getParameterMap();
    Set<Entry<String, String[]>> s = para.entrySet();
    Iterator<Entry<String, String[]>> it = s.iterator();
    while (it.hasNext()) {
        Entry<String, String[]> en = it.next();
        System.out.println("Key: " + en.getKey());
        System.out.println("Value:");
        for (int i = 0; i < en.getValue().length; i++) {
            System.out.println(en.getValue()[i]);
        }
        System.out.println("Next");
    }
    Enumeration<String> enu = portletRequest.getParameterNames();
    while (enu.hasMoreElements()) {
        String su = enu.nextElement();
        System.out.println(su + " " + portletRequest.getParameter(su));
    }
    enu = portletRequest.getPropertyNames();
    while (enu.hasMoreElements()) {
        String su = enu.nextElement();
        System.out.println(su + " " + portletRequest.getProperty(su));
    }
    HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(portletRequest);
    System.out.println(httpRequest.getPathInfo());
    enu = httpRequest.getParameterNames();
    while (enu.hasMoreElements()) {
        String su = enu.nextElement();
        System.out.println(su + " " + httpRequest.getParameter(su));
    }
    System.out.println(httpRequest.getQueryString());
    enu = PortalUtil.getOriginalServletRequest(httpRequest).getParameterNames();
    System.out.println(PortalUtil.getOriginalServletRequest(httpRequest).getPathInfo());
    while (enu.hasMoreElements()) {
        String su = enu.nextElement();
        System.out.println(su + " " + PortalUtil.getOriginalServletRequest(httpRequest).getParameter(su));
    }

    if (!String.valueOf(session).equals(request.getPathInfo())) {
        return false;
    }

    String fileId = request.getParameter("fileId");
    String filepaths = request.getParameter("filepath");
    String removeZeroGenotypes = request.getParameter("removeZeroGenotypes");
    String region = request.getParameter("region");
    String interval = request.getParameter("interval");
    String histogram = request.getParameter("histogram");

    URL u;
    try {
        StringBuilder sb = new StringBuilder();
        sb.append(ConfigurationManagerFactory.getInstance().getGenomeViewerRestApiUrl());
        sb.append(fileId);
        sb.append("/fetch?filepaths=");
        sb.append(filepaths);
        sb.append("&region=");
        sb.append(region);
        if (interval != null) {
            sb.append("&interval=");
            sb.append(interval);
        }
        if (histogram != null) {
            sb.append("&histogram=");
            sb.append(histogram);
        }
        if (removeZeroGenotypes != null) {
            sb.append("");
        }
        u = new URL(sb.toString());
        // u = new
        // URL("http://localhost:7777/vizrest/rest/data/QBAMS001AB.bam/fetch?filepaths=/store/1/0EEF79A2-8140-4FC7-BA67-E51908FE4619/f0/91/36/20141116104428925-3161/original/QBAMS001AB.bam&removeZeroGenotypes=false&region=9:117163200-117164799,9:117164800-117166399,9:117166400-117167999,9:117168000-117169599&interval=1600&h");
        URLConnection urlConnection = u.openConnection();

        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(false);
        urlConnection.connect();
        InputStream is = urlConnection.getInputStream();
        response.setContentType("application/json");
        response.setHeader("Content-Type", "application/json");
        OutputStream out = response.getOutputStream();
        byte[] buffer = new byte[com.vaadin.server.Constants.DEFAULT_BUFFER_SIZE];
        while (true) {
            int readCount = is.read(buffer);
            if (readCount < 0) {
                break;
            }
            out.write(buffer, 0, readCount);
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}