Example usage for javax.servlet.http HttpServletResponse containsHeader

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

Introduction

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

Prototype

public boolean containsHeader(String name);

Source Link

Document

Returns a boolean indicating whether the named response header has already been set.

Usage

From source file:net.sf.ehcache.constructs.web.ResponseUtil.java

/**
 * Adds the gzip HTTP header to the response. This is need when a gzipped body is returned so that browsers can properly decompress it.
 * <p/>//  w ww.java 2  s. c  om
 * @param response the response which will have a header added to it. I.e this method changes its parameter
 * @throws ResponseHeadersNotModifiableException Either the response is committed or we were called using the include method
 * from a {@link javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
 * method and the set set header is ignored.
 */
public static void addGzipHeader(final HttpServletResponse response)
        throws ResponseHeadersNotModifiableException {
    response.setHeader("Content-Encoding", "gzip");
    boolean containsEncoding = response.containsHeader("Content-Encoding");
    if (!containsEncoding) {
        throw new ResponseHeadersNotModifiableException(
                "Failure when attempting to set " + "Content-Encoding: gzip");
    }
}

From source file:org.ambraproject.wombat.util.HttpMessageUtil.java

/**
 * Copy content with whitelisted headers between responses. Headers already written to {@code responseTo} are
 * <em>not</em> overwritten if {@code responseFrom} has a header with the same name.
 *
 * @param responseFrom an incoming response to copy from
 * @param responseTo   an outgoing response to copy into
 * @param headerFilter describes whether and how to copy headers
 * @throws IOException//from w w w  .ja va 2 s. c o  m
 */
public static void copyResponseWithHeaders(HttpResponse responseFrom, HttpServletResponse responseTo,
        HeaderFilter headerFilter) throws IOException {
    for (Header header : responseFrom.getAllHeaders()) {
        String headerName = header.getName();
        if (!responseTo.containsHeader(headerName)) {
            String newValue = headerFilter.getValue(header);
            if (newValue != null) {
                responseTo.setHeader(headerName, newValue);
            }
        }
    }
    copyResponse(responseFrom, responseTo);
}

From source file:com.github.rnewson.couchdb.lucene.util.ServletUtils.java

public static void setResponseContentTypeAndEncoding(final HttpServletRequest req,
        final HttpServletResponse resp) {
    final String accept = req.getHeader("Accept");
    if (getBooleanParameter(req, "force_json") || (accept != null && accept.contains("application/json"))) {
        resp.setContentType("application/json");
    } else {//from  w  w w . ja  va2 s.co m
        resp.setContentType("text/plain");
    }
    if (!resp.containsHeader("Vary")) {
        resp.addHeader("Vary", "Accept");
    }
    resp.setCharacterEncoding("utf-8");
}

From source file:org.openlaszlo.utils.LZHttpUtils.java

/**
 * Mark response with no-store cache control
 *///w  w  w . j  av  a2 s  .  c o  m
static public void noStore(HttpServletResponse res) {
    if (res.containsHeader(LZHttpUtils.CACHE_CONTROL)) {
        mLogger.warn(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="over-riding back-end cache-control header to: no-store"
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(LZHttpUtils.class.getName(), "051018-408"));
    }
    res.setHeader("Cache-Control", "cache, must-revalidate");
    res.setHeader("Pragma", "public");
    //        res.setHeader(CACHE_CONTROL, NO_STORE);
}

From source file:org.apache.atlas.web.filters.AtlasAuthenticationFilter.java

private static String readUserFromCookie(HttpServletResponse response1) {
    String userName = null;//from  w  w  w . j ava 2s  . c  o  m
    boolean isCookieSet = response1.containsHeader("Set-Cookie");
    if (isCookieSet) {
        Collection<String> authUserName = response1.getHeaders("Set-Cookie");
        if (authUserName != null) {
            for (String cookie : authUserName) {
                if (!StringUtils.isEmpty(cookie)) {
                    if (cookie.toLowerCase().startsWith(AuthenticatedURL.AUTH_COOKIE.toLowerCase())
                            && cookie.contains("u=")) {
                        String[] split = cookie.split(";");
                        if (split != null) {
                            for (String s : split) {
                                if (!StringUtils.isEmpty(s) && s.toLowerCase()
                                        .startsWith(AuthenticatedURL.AUTH_COOKIE.toLowerCase())) {
                                    int ustr = s.indexOf("u=");
                                    if (ustr != -1) {
                                        int andStr = s.indexOf("&", ustr);
                                        if (andStr != -1) {
                                            try {
                                                userName = s.substring(ustr + 2, andStr);
                                                break;
                                            } catch (Exception e) {
                                                userName = null;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return userName;
}

From source file:org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.java

@Override
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
    if (this.requestMatcher.matches(request)) {
        if (!response.containsHeader(CLEAR_SITE_DATA_HEADER)) {
            response.setHeader(CLEAR_SITE_DATA_HEADER, this.headerValue);
        }/* ww  w  .j ava 2 s.com*/
    } else if (logger.isDebugEnabled()) {
        logger.debug("Not injecting Clear-Site-Data header since it did not match the " + "requestMatcher "
                + this.requestMatcher);
    }
}

From source file:com.netflix.conductor.server.JerseyModule.java

@Provides
@Singleton/* w  w  w.  j  a  v  a  2  s. co  m*/
public Filter apiOriginFilter() {
    return new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            HttpServletResponse res = (HttpServletResponse) response;
            if (!res.containsHeader("Access-Control-Allow-Origin")) {
                res.setHeader("Access-Control-Allow-Origin", "*");
            }
            res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
            res.addHeader("Access-Control-Allow-Headers", "Content-Type, api_key, Authorization");

            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }

    };
}

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

public IView addDateHeader(String name, long date) {
    HttpServletResponse _response = WebContext.getResponse();
    if (_response.containsHeader(name)) {
        _response.addDateHeader(name, date);
    } else {/*w  w  w .ja v  a2  s  .  c  om*/
        _response.setDateHeader(name, date);
    }
    return this;
}

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

public IView addHeader(String name, String value) {
    HttpServletResponse _response = WebContext.getResponse();
    if (_response.containsHeader(name)) {
        _response.addHeader(name, value);
    } else {/*from  w ww. j  a  v  a  2 s.c  o m*/
        _response.setHeader(name, value);
    }
    return this;
}

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 {/*from   w ww. ja  v  a2  s .c  o m*/
        _response.setIntHeader(name, value);
    }
    return this;
}