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:cn.bc.web.util.WebUtils.java

/**
 * ???//from   w ww  .  jav a2 s  .  c om
 * 
 * @param request
 * @return
 */
public static String getBrowser(HttpServletRequest request) {
    return request.getHeader("User-Agent");
}

From source file:com.log4ic.compressor.utils.HttpUtils.java

/**
 * ???// w  w  w. j  a  va2s .  c o  m
 *
 * @param request
 * @return
 */
public static List<BrowserInfo> getRequestBrowserInfo(HttpServletRequest request) {
    List<BrowserInfo> list = new FastList<BrowserInfo>();
    String userAgent = request.getHeader("User-Agent");
    for (BrowserTester tester : browserTesterList) {
        //??
        String browserType = findPattern(userAgent, tester.getRegex());
        if (browserType != null) {
            BrowserInfo info = new BrowserInfo();
            info.setBrowserType(tester.getIdentity());
            //?
            String browserVersion = findPattern(userAgent, tester.getVersionRegex());
            if (StringUtils.isNotBlank(browserVersion)) {
                try {
                    browserVersion = findPattern(browserVersion, "\\d+\\.\\d+");
                    info.setBrowserVersion(Double.parseDouble(browserVersion));
                } catch (Exception e) {
                }

                list.add(info);
            }
        }
    }
    return list;
}

From source file:org.artifactory.webapp.servlet.RequestUtils.java

/**
 * Verifies user agent is Internet Explorer according to:
 * https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
 * https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx
 * http://www.useragentstring.com/pages/Internet%20Explorer/
 *
 * And adds the compatibility header to avoid explorer defaulting to IE7 mode when launching compatibility view.
 * see RTFACT-7928/*w w  w  .ja  v  a2  s.  co  m*/
 */
private static void verifyExplorerUserAgentAndSetHeader(HttpServletRequest request,
        HttpServletResponse response) {
    String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
    if (isNewExplorer(userAgent)) {
        response.setHeader("X-UA-Compatible", "IE=Edge");
    }
}

From source file:ru.anr.base.facade.web.api.CommandUtils.java

/**
 * Building a command with data extracted from {@link HttpServletRequest}
 * (method, headers).//  www.j a v a2  s  . com
 * 
 * 
 * @param commandId
 *            Identifier of Command
 * @param apiVersion
 *            API Version
 * @param request
 *            Http request
 * @return A command instance
 */
public static APICommand build(String commandId, String apiVersion, HttpServletRequest request) {

    APICommand cmd = new APICommand(commandId, apiVersion);

    if (MediaType.APPLICATION_XML_VALUE.equals(request.getContentType())) {
        cmd.setRequestFormat(RawFormatTypes.XML);
    } else {
        cmd.setRequestFormat(RawFormatTypes.JSON);
    }
    if (MediaType.APPLICATION_XML_VALUE.equals(request.getHeader("Accept"))) {
        cmd.setResponseFormat(RawFormatTypes.XML);
    } else {
        cmd.setResponseFormat(RawFormatTypes.JSON);
    }
    cmd.method(request.getMethod());
    return cmd;
}

From source file:com.soolr.core.web.Servlets.java

/**
 * ??Header./*w w w .j  a v a2s. c  om*/
 * 
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    try {
        String agent = request.getHeader("User-Agent");
        boolean isMSIE = ((agent != null) && (agent.indexOf("MSIE") != -1));
        String encoded = isMSIE ? URLEncoder.encode(fileName, "UTF-8")
                : new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encoded + "\"");
    } catch (UnsupportedEncodingException e) {

    }
}

From source file:com.squid.kraken.v4.auth.RequestHelper.java

public static <T> T processRequest(Class<T> type, HttpServletRequest request, HttpRequestBase req)
        throws IOException, URISyntaxException, ServerUnavailableException, ServiceException,
        SSORedirectException {// ww  w.j av  a2  s.  c o m

    // set client information to the header
    String reqXFF = request.getHeader(STRING_XFF_HEADER);
    String postXFF;
    if (reqXFF != null) {
        // X-Forwarded-For header already exists in the request
        logger.info(STRING_XFF_HEADER + " : " + reqXFF);
        if (reqXFF.length() > 0) {
            // just add the remoteHost to it
            postXFF = reqXFF + ", " + request.getRemoteHost();
        } else {
            postXFF = request.getRemoteHost();
        }
    } else {
        postXFF = request.getRemoteHost();
    }

    // add a new X-Forwarded-For header containing the remoteHost
    req.addHeader(STRING_XFF_HEADER, postXFF);

    // execute the login request
    HttpResponse executeCode;
    try {
        HttpClient client = HttpClientBuilder.create().build();
        executeCode = client.execute(req);
    } catch (ConnectException e) {
        // Authentication server unavailable
        throw new ServerUnavailableException(e);
    }

    // process the result
    BufferedReader rd = new BufferedReader(new InputStreamReader(executeCode.getEntity().getContent()));

    StringBuffer resultBuffer = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        resultBuffer.append(line);
    }
    String result = resultBuffer.toString();

    T fromJson;
    Gson gson = new Gson();
    int statusCode = executeCode.getStatusLine().getStatusCode();
    if (statusCode != 200) {
        if (executeCode.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            String redirectURL = executeCode.getFirstHeader("Location").getValue();
            throw new SSORedirectException("SSO Redirect Exception", redirectURL);
        } else {
            logger.info("Error : " + req.getURI() + " resulted in : " + result);
            WebServicesException exception;
            try {
                exception = gson.fromJson(result, WebServicesException.class);
            } catch (Exception e) {
                if ((statusCode >= 500) && (statusCode < 600)) {
                    // Authentication server unavailable
                    throw new ServerUnavailableException();
                } else {
                    throw new ServiceException();
                }
            }
            throw new ServiceException(exception);
        }
    } else {
        // forward to input page displaying ok message
        try {
            fromJson = gson.fromJson(result, type);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
    return fromJson;
}

From source file:com.openkm.util.WebUtils.java

/**
 * Get HTTP header.//from  ww  w  . j  a  va 2  s  .c  o m
 */
private static String getHeader(HttpServletRequest request, String name) {
    String value = request.getHeader(name);

    if (value != null) {
        return value;
    } else {
        return EMPTY_STRING;
    }
}

From source file:cn.bc.web.util.WebUtils.java

/**
 * ?IP??/*from w w  w  .j a  v a  2s .c  o m*/
 * 
 * @param request
 * @return
 */
public static String getClientIP(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");// apache?
    logger.debug("get clientIP by request.getHeader(\"x-forwarded-for\")");
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
        ip = request.getHeader("Proxy-Client-IP");// ?
        logger.debug("get clientIP by request.getHeader(\"Proxy-Client-IP\")");
    }
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
        ip = request.getHeader("WL-Proxy-Client-IP");// weblogic
        logger.debug("get clientIP by request.getHeader(\"WL-Proxy-Client-IP\")");
    }
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
        ip = request.getRemoteAddr();// 
        logger.debug("get clientIP by request.getRemoteAddr()");
    }
    return ip;
}

From source file:your.microservice.core.util.YourServletUriComponentsBuilder.java

/**
 * Prepare a builder by copying the scheme, host, port, path, and
 * query string of an HttpServletRequest.
 * @param request to build path from//from ww w  . ja  v  a2s . c o  m
 * @return a URI component builder
 */
public static YourServletUriComponentsBuilder fromRequest(HttpServletRequest request) {
    String scheme = request.getScheme();
    String host = request.getServerName();
    int port = request.getServerPort();

    String hostHeader = request.getHeader("X-Forwarded-Host");
    if (StringUtils.hasText(hostHeader)) {
        String[] hosts = StringUtils.commaDelimitedListToStringArray(hostHeader);
        String hostToUse = hosts[0];
        if (hostToUse.contains(":")) {
            String[] hostAndPort = StringUtils.split(hostToUse, ":");
            host = hostAndPort[0];
            port = Integer.parseInt(hostAndPort[1]);
        } else {
            host = hostToUse;
            port = -1;
        }
    }

    String portHeader = request.getHeader("X-Forwarded-Port");
    if (StringUtils.hasText(portHeader)) {
        port = Integer.parseInt(portHeader);
    }

    String protocolHeader = request.getHeader("X-Forwarded-Proto");
    if (StringUtils.hasText(protocolHeader)) {
        scheme = protocolHeader;
    }

    YourServletUriComponentsBuilder builder = new YourServletUriComponentsBuilder();
    builder.scheme(scheme);
    builder.host(host);
    if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
        builder.port(port);
    }
    builder.pathFromRequest(request);
    builder.query(request.getQueryString());
    return builder;
}

From source file:com.o2o.util.WebUtils.java

/**
 * ??//from  w ww  . jav  a  2 s . com
 * @param request
 * @return
 */
public static Boolean isWeiXin(HttpServletRequest request) {
    String ua = request.getHeader("user-agent").toLowerCase();
    if (ua.indexOf("micromessenger") > 0) {// ?
        return true;
    }
    return false;
}