Example usage for javax.servlet.http HttpServletResponse addIntHeader

List of usage examples for javax.servlet.http HttpServletResponse addIntHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addIntHeader.

Prototype

public void addIntHeader(String name, int value);

Source Link

Document

Adds a response header with the given name and integer value.

Usage

From source file:gov.nih.nci.caarray.web.filter.CacheControlFilter.java

/**
 * {@inheritDoc}/*  w w  w  .ja va 2s .c  o m*/
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(response instanceof HttpServletResponse)) {
        chain.doFilter(request, response);
        return;
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    if (StringUtils.substringAfterLast(httpRequest.getRequestURI(), ".").equals(DYNAMIC_URL_EXTENSION)) {
        if (!request.isSecure()) { // workaround for IE files bug http://support.microsoft.com/kb/812935
            httpResponse.addHeader("Cache-control", "no-cache");
            httpResponse.addHeader("Pragma", "no-cache");
        }
        httpResponse.addIntHeader("Expires", 0);
    }
    chain.doFilter(request, response);
}

From source file:grails.plugin.cache.web.filter.PageFragmentCachingFilter.java

/**
 * Set the headers in the response object, excluding the Gzip header
 * @param pageInfo//from   w  ww  .  j a  va2  s.  c  om
 * @param response
 */
protected void setHeaders(final PageInfo pageInfo, final HttpServletResponse response) {

    Collection<Header<? extends Serializable>> headers = pageInfo.getHeaders();

    // Track which headers have been set so all headers of the same name
    // after the first are added
    TreeSet<String> setHeaders = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);

    for (Header<? extends Serializable> header : headers) {
        String name = header.getName();

        switch (header.getType()) {
        case STRING:
            if (setHeaders.contains(name)) {
                response.addHeader(name, (String) header.getValue());
            } else {
                setHeaders.add(name);
                response.setHeader(name, (String) header.getValue());
            }
            break;
        case DATE:
            if (setHeaders.contains(name)) {
                response.addDateHeader(name, (Long) header.getValue());
            } else {
                setHeaders.add(name);
                response.setDateHeader(name, (Long) header.getValue());
            }
            break;
        case INT:
            if (setHeaders.contains(name)) {
                response.addIntHeader(name, (Integer) header.getValue());
            } else {
                setHeaders.add(name);
                response.setIntHeader(name, (Integer) header.getValue());
            }
            break;
        default:
            throw new IllegalArgumentException("No mapping for Header: " + header);
        }
    }
}

From source file:net.ymate.platform.webmvc.view.AbstractView.java

public IView addIntHeader(String name, int value) {
    HttpServletResponse _response = WebContext.getResponse();
    if (_response.containsHeader(name)) {
        _response.addIntHeader(name, value);
    } else {/* w ww .  java 2  s.c  o m*/
        _response.setIntHeader(name, value);
    }
    return this;
}

From source file:org.asynchttpclient.test.EchoHandler.java

@Override
public void handle(String pathInContext, Request request, HttpServletRequest httpRequest,
        HttpServletResponse httpResponse) throws IOException, ServletException {

    LOGGER.debug("Echo received request {} on path {}", request, pathInContext);

    if (httpRequest.getHeader("X-HEAD") != null) {
        httpResponse.setContentLength(1);
    }//  w w w.  ja va 2  s  .c o m

    if (httpRequest.getHeader("X-ISO") != null) {
        httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_ISO_8859_1_CHARSET);
    } else {
        httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET);
    }

    if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
        httpResponse.addHeader("Allow", "GET,HEAD,POST,OPTIONS,TRACE");
    }

    Enumeration<String> e = httpRequest.getHeaderNames();
    String headerName;
    while (e.hasMoreElements()) {
        headerName = e.nextElement();
        if (headerName.startsWith("LockThread")) {
            final int sleepTime = httpRequest.getIntHeader(headerName);
            try {
                Thread.sleep(sleepTime == -1 ? 40 : sleepTime * 1000);
            } catch (InterruptedException ex) {
                //
            }
        }

        if (headerName.startsWith("X-redirect")) {
            httpResponse.sendRedirect(httpRequest.getHeader("X-redirect"));
            return;
        }
        httpResponse.addHeader("X-" + headerName, httpRequest.getHeader(headerName));
    }

    String pathInfo = httpRequest.getPathInfo();
    if (pathInfo != null)
        httpResponse.addHeader("X-pathInfo", pathInfo);

    String queryString = httpRequest.getQueryString();
    if (queryString != null)
        httpResponse.addHeader("X-queryString", queryString);

    httpResponse.addHeader("X-KEEP-ALIVE", httpRequest.getRemoteAddr() + ":" + httpRequest.getRemotePort());

    Cookie[] cs = httpRequest.getCookies();
    if (cs != null) {
        for (Cookie c : cs) {
            httpResponse.addCookie(c);
        }
    }

    Enumeration<String> i = httpRequest.getParameterNames();
    if (i.hasMoreElements()) {
        StringBuilder requestBody = new StringBuilder();
        while (i.hasMoreElements()) {
            headerName = i.nextElement();
            httpResponse.addHeader("X-" + headerName, httpRequest.getParameter(headerName));
            requestBody.append(headerName);
            requestBody.append("_");
        }

        if (requestBody.length() > 0) {
            String body = requestBody.toString();
            httpResponse.getOutputStream().write(body.getBytes());
        }
    }

    String requestBodyLength = httpRequest.getHeader("X-" + CONTENT_LENGTH);

    if (requestBodyLength != null) {
        byte[] requestBodyBytes = IOUtils.toByteArray(httpRequest.getInputStream());
        int total = requestBodyBytes.length;

        httpResponse.addIntHeader("X-" + CONTENT_LENGTH, total);
        String md5 = TestUtils.md5(requestBodyBytes, 0, total);
        httpResponse.addHeader(CONTENT_MD5.toString(), md5);

        httpResponse.getOutputStream().write(requestBodyBytes, 0, total);
    } else {
        int size = 16384;
        if (httpRequest.getContentLength() > 0) {
            size = httpRequest.getContentLength();
        }
        if (size > 0) {
            int read = 0;
            while (read > -1) {
                byte[] bytes = new byte[size];
                read = httpRequest.getInputStream().read(bytes);
                if (read > 0) {
                    httpResponse.getOutputStream().write(bytes, 0, read);
                }
            }
        }
    }

    request.setHandled(true);
    httpResponse.getOutputStream().flush();
    // FIXME don't always close, depends on the test, cf ReactiveStreamsTest
    httpResponse.getOutputStream().close();
}

From source file:org.wings.externalizer.AbstractExternalizeManager.java

public void deliver(ExternalizedResource extInfo, HttpServletResponse response, Device out) throws IOException {
    /* FIXME: re-implement.
    if ( extInfo.deliverOnce() ) {//  w ww.  jav a2s .  c o  m
    removeExternalizedResource(identifier);
    }
    */

    if (extInfo.getMimeType() != null) {
        response.setContentType(extInfo.getMimeType());
    }

    // FIXME find out, if this is correct: if the content length
    // is not size preserving (like a gzip-device), then we must not
    // send the content size we know..
    if (out.isSizePreserving()) {
        int resourceLen = extInfo.getExternalizer().getLength(extInfo.getObject());
        if (resourceLen > 0) {
            LOG.debug(extInfo.getMimeType() + ": " + resourceLen);
            response.setContentLength(resourceLen);
        }
    }

    Collection headers = extInfo.getHeaders();
    if (headers != null) {
        for (Object header : headers) {
            Map.Entry entry = (Map.Entry) header;
            if (entry.getValue() instanceof String) {
                response.addHeader((String) entry.getKey(), (String) entry.getValue());
            } else if (entry.getValue() instanceof Date) {
                response.addDateHeader((String) entry.getKey(), ((Date) entry.getValue()).getTime());

            } else if (entry.getValue() instanceof Integer) {
                response.addIntHeader((String) entry.getKey(), ((Integer) entry.getValue()).intValue());

            } // end of if ()
        }
    }

    if (!response.containsHeader("Expires")) {
        /*
        * This would be the correct way to do it; alas, that means, that
        * for static resources, after a day or so, no caching could take
        * place, since the last modification was at the first time, the
        * resource was externalized (since it doesn't change).
        * .. have to think about it.
        */
        //response.setDateHeader("Expires",
        //                      (1000*FINAL_EXPIRES)
        //                       + extInfo.getLastModified());
        // .. so do this for now, which is the best approximation of what
        // we want.
        response.setDateHeader("Expires", System.currentTimeMillis() + (1000 * FINAL_EXPIRES));
    }

    try {
        extInfo.getExternalizer().write(extInfo.getObject(), out);
    } catch (ResourceNotFoundException e) {
        LOG.debug("Unable to deliver resource due to:  " + e.getMessage() + ". Sending 404!");
        response.reset();
        response.sendError(404, e.getMessage());
    }
    out.flush();
}