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.eastcom.baseframe.web.common.utils.Servlets.java

/**
 * ??//  w w  w .j a  v a  2  s  .  c  o  m
 * 
 * @param request
 * @return
 */
public static String getUserAgent(HttpServletRequest request) {
    return request.getHeader("user-agent");
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * ?header????//from   w  ww .  jav  a 2 s. co  m
 * @param req
 * @param name
 * @return
 */
public static String getHeader(HttpServletRequest req, String name) {
    String value = req.getHeader(name);
    if (value != null)
        return value;
    Enumeration names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String n = (String) names.nextElement();
        if (n.equalsIgnoreCase(name)) {
            return req.getHeader(n);
        }
    }
    return null;
}

From source file:cn.com.qiqi.order.utils.Servlets.java

/**
 * ?? If-None-Match Header, Etag?./* ww w  .jav a  2 s.  c  o m*/
 * 
 * Etag, checkIfNoneMatchfalse, 304 not modify status.
 * 
 * @param etag ETag.
 */
public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response,
        String etag) {
    String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH);
    if (headerValue != null) {
        boolean conditionSatisfied = false;
        if (!"*".equals(headerValue)) {
            StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ",");

            while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
                String currentToken = commaTokenizer.nextToken();
                if (currentToken.trim().equals(etag)) {
                    conditionSatisfied = true;
                }
            }
        } else {
            conditionSatisfied = true;
        }

        if (conditionSatisfied) {
            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            response.setHeader(HttpHeaders.ETAG, etag);
            return false;
        }
    }
    return true;
}

From source file:com.npower.wall.TagUtil.java

/**
 * Get the UA from request if any otherwise use the parameter passed
 *//*www.  ja  v a2  s  .  c o  m*/

public static String getUA(HttpServletRequest request) {
    String UA = "";
    if (request.getParameter("UA") != null) {
        UA = request.getParameter("UA");
    } else {
        UA = request.getHeader("User-Agent");
    }
    if (StringUtils.isEmpty(UA)) {
        // Default UA
        UA = "Nokia6681";
    }
    return UA;
}

From source file:net.ontopia.topicmaps.webed.impl.utils.ReqParamUtils.java

/**
 * INTERNAL: Builds the Parameters object from an HttpServletRequest
 * object./*from   ww w  .  j  ava 2 s .  co  m*/
 * @since 2.0
 */
public static Parameters decodeParameters(HttpServletRequest request, String charenc)
        throws ServletException, IOException {

    String ctype = request.getHeader("content-type");
    log.debug("Content-type: " + ctype);
    Parameters params = new Parameters();

    if (ctype != null && ctype.startsWith("multipart/form-data")) {
        // special file upload request, so use FileUpload to decode
        log.debug("Decoding with FileUpload; charenc=" + charenc);
        try {
            FileUpload upload = new FileUpload(new DefaultFileItemFactory());
            Iterator iterator = upload.parseRequest(request).iterator();
            while (iterator.hasNext()) {
                FileItem item = (FileItem) iterator.next();
                log.debug("Reading: " + item);
                if (item.isFormField()) {
                    if (charenc != null)
                        params.addParameter(item.getFieldName(), item.getString(charenc));
                    else
                        params.addParameter(item.getFieldName(), item.getString());
                } else
                    params.addParameter(item.getFieldName(), new FileParameter(item));
            }
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }

    } else {
        // ordinary web request, so retrieve info and stuff into Parameters object
        log.debug("Normal parameter decode, charenc=" + charenc);
        if (charenc != null)
            request.setCharacterEncoding(charenc);
        Enumeration enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String param = (String) enumeration.nextElement();
            params.addParameter(param, request.getParameterValues(param));
        }
    }

    return params;
}

From source file:com.gson.WeChat.java

/**
 * ??, 5.0 ??/*from  w  ww.  j a  va  2 s.  co  m*/
 * @param request
 * @return
 */
public static boolean isWeiXin(HttpServletRequest request) {
    String userAgent = request.getHeader("User-Agent");
    if (StringUtils.isNotBlank(userAgent)) {
        Pattern p = Pattern.compile("MicroMessenger/(\\d+).+");
        Matcher m = p.matcher(userAgent);
        String version = null;
        if (m.find()) {
            version = m.group(1);
        }
        return (null != version && NumberUtils.toInt(version) >= 5);
    }
    return false;
}

From source file:com.telefonica.iot.perseo.Utils.java

/**
 * Capture correlator and generate//from   w  ww. j  a v  a 2  s  .  com
 *
 * @param req HttpServletRequest incomming request
 *
 */
public static void putCorrelatorAndTrans(HttpServletRequest req) {
    String correlatorId = req.getHeader(Constants.CORRELATOR_HEADER);
    String transId = UUID.randomUUID().toString();
    if (correlatorId == null) {
        correlatorId = transId;
    }
    MDC.put(Constants.TRANSACTION_ID, transId);
    MDC.put(Constants.CORRELATOR_ID, correlatorId);
    String service = req.getHeader(Constants.SERVICE_HEADER);
    if (service == null) {
        service = "?";
    }
    MDC.put(Constants.SERVICE_FIELD, service);

    String subservice = req.getHeader(Constants.SUBSERVICE_HEADER);
    if (subservice == null) {
        subservice = "?";
    }
    MDC.put(Constants.SUBSERVICE_FIELD, subservice);

    {
        String realIP = req.getHeader(Constants.REALIP_HEADER);

        if (realIP == null) {
            realIP = req.getRemoteAddr();
        }
        MDC.put(Constants.REALIP_FIELD, realIP);
    }

}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static String getHeaderValue(HttpServletRequest request, String name) {
    String value = request.getHeader(name);
    if (StringUtils.isBlank(value)) {
        value = request.getHeader(StringUtils.lowerCase(name));
    }//w  ww. ja v a 2 s  .c o  m
    return value;
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Get the IP of the user from a request. If the user is behind an apache
 * server, return the ip of the user instead of the ip of the server.
 * @param request The request//from   ww  w  .j a  v  a  2s . co m
 * @return The IP of the user that made the request
 */
public static String getRealIp(HttpServletRequest request) {
    String strIPAddress = request.getHeader(CONSTANT_HTTP_HEADER_X_FORWARDED_FOR);

    if (strIPAddress != null) {
        String strIpForwarded = null;

        while (!strIPAddress.matches(PATTERN_IP_ADDRESS) && strIPAddress.contains(CONSTANT_COMMA)) {
            strIpForwarded = strIPAddress.substring(0, strIPAddress.indexOf(CONSTANT_COMMA));
            strIPAddress = strIPAddress.substring(strIPAddress.indexOf(CONSTANT_COMMA))
                    .replaceFirst(CONSTANT_COMMA, StringUtils.EMPTY).trim();

            if ((strIpForwarded != null) && strIpForwarded.matches(PATTERN_IP_ADDRESS)) {
                strIPAddress = strIpForwarded;
            }
        }

        if (!strIPAddress.matches(PATTERN_IP_ADDRESS)) {
            strIPAddress = request.getRemoteAddr();
        }
    } else {
        strIPAddress = request.getRemoteAddr();
    }

    return strIPAddress;
}

From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java

public static String getScheme(HttpServletRequest request) {
    if (request.getHeader("X-Forwarded-Protocol") != null) {
        return request.getHeader("X-Forwarded-Protocol");
    } else {//  w ww  . j ava  2  s. co m
        return request.getScheme();
    }
}