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:org.apache.hadoop.gateway.filter.BasicAuthChallengeFilter.java

private static UsernamePasswordCredentials createValidatedCredentials(HttpServletRequest request) {
    UsernamePasswordCredentials credentials = null;
    String basicAuthResponse = request.getHeader(AUTH.WWW_AUTH_RESP);
    if (basicAuthResponse != null) {
        String[] parts = basicAuthResponse.split(" ");
        if (parts.length == 2) {
            String usernamePassword = new String(Base64.decodeBase64(parts[1]));
            parts = usernamePassword.split(":");
            if (parts.length == 2) {
                String username = parts[0];
                String password = parts[1];
                if (username.length() > 0 && password.length() > 0) {
                    credentials = new UsernamePasswordCredentials(username, password);
                }//from   www  .jav  a2s  .c om
            }
        }
        //System.out.println( basicAuthResponse );
    }
    return credentials;
}

From source file:com.fengduo.spark.commons.util.HttpUtil.java

/**
 * IP?// w  w  w  .  j  a  v a2s  .c  o  m
 * 
 * @param request
 * @return
 */
public static String toIpAddr(HttpServletRequest request) {
    if (request == null) {
        return "unknown";
    }
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Forwarded-For");
    }
    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.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

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.  j a  v  a  2s. c  o m
        resp.setContentType("text/plain");
    }
    if (!resp.containsHeader("Vary")) {
        resp.addHeader("Vary", "Accept");
    }
    resp.setCharacterEncoding("utf-8");
}

From source file:costumetrade.common.util.IPUtils.java

public static String getClientIpAddress(HttpServletRequest request) {

    if (request == null) {
        return "";
    }/*from w  w w  .j  ava  2  s  . c  om*/

    for (String header : HEADERS_TO_TRY) {
        String ip = request.getHeader(header);
        if (StringUtils.isNotEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
    }
    return request.getRemoteAddr();
}

From source file:com.fengduo.bee.commons.util.HttpUtil.java

/**
 * IP?//from  ww  w  . j  a v a  2 s  .c om
 * 
 * @param request
 * @return
 */
public static String toIpAddr(HttpServletRequest request) {
    if (request == null) {
        return "unknown";
    }

    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("X-Forwarded-For");
    }
    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.getHeader("X-Real-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:feedme.controller.HomePageServlet.java

public static boolean isAjaxRequest(HttpServletRequest request) {
    return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
}

From source file:com.eryansky.common.web.utils.WebUtils.java

/**
 * ?Ajax.//from www .  java  2 s  . co  m
 * 
 * @param request
 * @return
 */
public static boolean isAjaxRequest(HttpServletRequest request) {
    String header = request.getHeader("X-Requested-With");
    if (header != null && "XMLHttpRequest".equals(header)) {
        return true;
    } else {
        return false;
    }
}

From source file:com.netsteadfast.greenstep.sys.GreenStepHessianUtils.java

public static String getHttpRequestHeaderCheckValue(HttpServletRequest request) {
    return request.getHeader(HEADER_CHECK_VALUE_PARAM_NAME);
}

From source file:com.flexive.war.filter.FxRequestUtils.java

/**
 * Returns the server name as seen by the client. Takes into account proxies
 * like Apache2 which may alter the server name in their requests.
 *
 * @param request   the request// w  w w .  jav  a2s. co m
 * @return  the server name as seen by the client
 */
public static String getExternalServerName(HttpServletRequest request) {
    if (request.getHeader("x-forwarded-host") != null) {
        // use external (forwarded) host - FX-330
        return request.getHeader("x-forwarded-host");
    } else {
        // use our own server name
        return request.getServerName()
                + (request.getServerPort() == 80 || (request.isSecure() && request.getServerPort() == 443) ? ""
                        : ":" + request.getServerPort());
    }
}

From source file:com.mashape.galileo.agent.common.IPAddressParserUtil.java

/**
 * Returns the true IP address of the client from specified request after parsing the header's value in the following order
 *  Forwarded// w  w  w  .j a  va2 s. c  om
 *  X-Real-IP 
 *  X-FORWARDED-FOR
 *  Fastly-Client-IP
 *  CF-Connecting-IP
 *  X-Cluster-Client-IP
 *  X-Cluster-Client-IP
 *  Z-Forwarded-For
 *  WL-Proxy-Client-IP
 *  Proxy-Client-IP
 *  
 *  if none of above headers set fall back to remote address
 *  
 * @param request
 * @return a <code>String</code> containing the the client IP address
 */
public static String getClientIPAddress(HttpServletRequest request) {
    for (String header : headers) {
        String ipAddress = request.getHeader(header);
        if (ipAddress == null)
            break;
        if ("Forwarded".equals(header))
            ipAddress = findForwadedClientIpAddress(ipAddress);
        else if ("X-FORWARDED-FOR".equals(header))
            ipAddress = findXFFClientIpAddress(ipAddress);
        if (isValidIp(ipAddress)) {
            return ipAddress;
        }
    }
    return request.getRemoteAddr();
}