Example usage for javax.servlet.http HttpServletRequest getHeader

List of usage examples for javax.servlet.http HttpServletRequest getHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getHeader.

Prototype

public String getHeader(String name);

Source Link

Document

Returns the value of the specified request header as a String.

Usage

From source file:com.meltmedia.cadmium.servlets.FileServletTest.java

public static HttpServletRequest mockGetWithoutGzip(String pathInfo) {
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(GET_METHOD);
    when(request.getRequestURI()).thenReturn(pathInfo);
    when(request.getDateHeader(anyString())).thenReturn(new Long(-1));
    when(request.getHeader(ACCEPT_ENCODING_HEADER)).thenReturn("gzip;q=0");
    return request;
}

From source file:com.ziduye.base.web.Servlets.java

/**
 * ??Header.//from  ww  w  .j  av  a 2  s. com
 * 
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    // ???
    String encodedfileName = null;
    // ??firefox??,???+?
    encodedfileName = fileName.trim().replaceAll(" ", "_");
    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().indexOf("MSIE") != -1);
    if (isMSIE) {
        encodedfileName = Encodes.urlEncode(fileName);
    } else {
        encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
    }
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");
}

From source file:com.dell.asm.asmcore.asmmanager.util.ProxyUtil.java

public static void setProxyHeaders(Object ret, HttpServletRequest servletRequest) {
    Client client = WebClient.client(ret);
    if (client != null) {
        if (servletRequest == null) {
            logger.error("Null server request.");
            throw new IllegalArgumentException("Servlet request must not be null");
        }//from w  ww .  j  a va  2 s . co m

        String acceptHeader = servletRequest.getHeader(HttpHeaders.ACCEPT);
        if (!MediaType.APPLICATION_XML.equals(acceptHeader)
                && !MediaType.APPLICATION_JSON.equals(acceptHeader)) {
            // default to XML; IOM delete does not like text/plain ...
            acceptHeader = MediaType.APPLICATION_XML;
        }

        client.header(HttpHeaders.ACCEPT, acceptHeader);

        String contentTypeHeader = servletRequest.getHeader(HttpHeaders.CONTENT_TYPE);
        if (!StringUtils.isBlank(contentTypeHeader)) {
            WebClient.client(ret).header(HttpHeaders.CONTENT_TYPE, contentTypeHeader);
        }
    }
}

From source file:com.binarybirchtree.filters.IpFilter.java

public static String getForwardedIp(HttpServletRequest request, int degree) {
    if (degree == 0) {
        return request.getRemoteAddr();
    }//  ww  w. java 2s  . c o m

    // Believe the X-Forwarded-For header only if it came from a trusted internal IP.
    if (!ipIsInList(request.getRemoteAddr(), internalIps)) {
        System.err.println(
                "Last IP address was " + request.getRemoteAddr() + ", which is not a trusted internal IP.");
        return null;
    }

    // Return null if the X-Forwarded-For header was not found.
    String header = request.getHeader("X-Forwarded-For");
    if (header == null)
        return null;

    String[] ips = header.split(",");
    if (degree > ips.length)
        return null;
    return ips[ips.length - degree].trim();
}

From source file:org.frontcache.core.FCUtils.java

public static String getDomainFromSiteKeyHeader(HttpServletRequest request) {
    String siteKey = request.getHeader(FCHeaders.X_FRONTCACHE_SITE_KEY);
    if (siteKey == null) {
        return FCConfig.DEFAULT_DOMAIN;
    }/*from   w ww.j  a v  a2s.c  om*/

    String domain = null;

    DomainContext domainContext = FrontCacheEngine.getFrontCache().getDomainContexBySiteKey(siteKey);
    if (null != domainContext)
        domain = domainContext.getDomain();

    if (domain == null)
        return FCConfig.DEFAULT_DOMAIN;

    return domain;
}

From source file:com.core.util.wx.PayUtils.java

public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }//from   ww  w. j  a  v  a 2  s  .  c  o m
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:com.github.hateoas.forms.spring.AffordanceBuilder.java

/**
 * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the request contains
 * an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an {@code X-Forwarded-Ssl} header
 *
 * @return builder/*from  w w w. jav  a 2 s.c  om*/
 */
static UriComponentsBuilder getBuilder() {

    HttpServletRequest request = getCurrentRequest();
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

    String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

    if (StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
        builder.scheme("https");
    }

    String host = request.getHeader("X-Forwarded-Host");

    if (!StringUtils.hasText(host)) {
        return builder;
    }

    String[] hosts = StringUtils.commaDelimitedListToStringArray(host);
    String hostToUse = hosts[0];

    if (hostToUse.contains(":")) {

        String[] hostAndPort = StringUtils.split(hostToUse, ":");

        builder.host(hostAndPort[0]);
        builder.port(Integer.parseInt(hostAndPort[1]));
    } else {
        builder.host(hostToUse);
        builder.port(-1); // reset port if it was forwarded from default port
    }

    String port = request.getHeader("X-Forwarded-Port");

    if (StringUtils.hasText(port)) {
        builder.port(Integer.parseInt(port));
    }

    return builder;
}

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the preferred one out of of those languages that are requested by
 * the client through the <code>Accept-Language</code> header and are
 * supported by the site. If there is no match, the site's default language is
 * returned.//w  ww . jav a2s.  c  o  m
 * <p>
 * The preferred one is defined by the following priorities:
 * <ul>
 * <li>Requested by the client</li>
 * <li>The site default language</li>
 * </ul>
 * 
 * @param request
 *          the http request
 * @param site
 *          the site
 */
public static Language getPreferredLanguage(HttpServletRequest request, Site site) {

    // Accept-Language header
    if (request.getHeader("Accept-Language") != null) {
        Enumeration<?> locales = request.getLocales();
        while (locales.hasMoreElements()) {
            try {
                Language l = getLanguage((Locale) locales.nextElement());
                if (site.supportsLanguage(l))
                    return l;
            } catch (UnknownLanguageException e) {
                // never mind, some clients will send stuff like "*" as the locale
            }
        }
    }

    return site.getDefaultLanguage();
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

@SuppressWarnings({ "unchecked" })
private static void addCustomHeaders(HttpServletRequest original, HttpEntityEnclosingRequest request,
        String... skipElements) {
    Enumeration<String> en = original.getHeaderNames();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        if (contains(skipElements, name)) {
            continue;
        } else if ("X-HTTP-Method-Override".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Source".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("preferred-role".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-File-Name".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Requested-With".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("X-Requested-By".equals(name)) {
            request.setHeader(name, original.getHeader(name));
        } else if ("Content-type".equalsIgnoreCase(name)) {
            request.setHeader(name, original.getHeader(name));
        }//from  w  ww.  java 2s  . co  m

    }
}

From source file:com.groupon.odo.HttpUtilities.java

/**
 * Obtain newline-delimited headers from request
 *
 * @param request/*  www.ja v a  2 s .  co m*/
 * @return
 */
public static String getHeaders(HttpServletRequest request) {
    String headerString = "";
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        if (headerString.length() != 0)
            headerString += "\n";

        String name = headerNames.nextElement();
        headerString += name + ": " + request.getHeader(name);
    }

    return headerString;
}