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

/**????????
 * @param request//w w w.  ja va2  s . co m
 * @param srcFileName ??
 * @return ????
 */
public static String encodeFileName(HttpServletRequest request, String srcFileName) {
    String _fileName;
    // ???
    boolean isIE = request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") != -1;
    try {
        if (isIE) {// IE?
            _fileName = URLEncoder.encode(srcFileName, "UTF-8");
            if (_fileName.length() > 150) {
                // URLEncoder?17IE6 IEbug??KB816868
                // ?????ie6 sp1
                String guessCharset = "gb2312"; //?requestlocale???gb2312
                _fileName = new String(srcFileName.getBytes(guessCharset), "ISO8859-1");
            }
        } else {// ?IE?:?URLEncoder.encode???url?
            _fileName = new String(srcFileName.getBytes("UTF-8"), "ISO8859-1");
        }
    } catch (UnsupportedEncodingException e) {
        logger.warn(e.getMessage());
        _fileName = srcFileName;
    }
    return _fileName;
}

From source file:info.magnolia.cms.security.Authenticator.java

/**
 * Authenticate authorization request using JAAS login module as configured
 * @param request as received by the servlet engine
 * @return boolean//w w w . ja  va 2  s  .co m
 */
public static boolean authenticate(HttpServletRequest request) {
    String credentials = request.getHeader("Authorization");
    String userid;
    String pswd;
    CredentialsCallbackHandler callbackHandler;
    String loginModuleToInitialize = "magnolia"; // default login module

    if (StringUtils.isEmpty(credentials) || credentials.length() <= 6) {
        // check for form based login request
        if (StringUtils.isNotEmpty(request.getParameter(PARAMETER_USER_ID))) {
            userid = request.getParameter(PARAMETER_USER_ID);
            pswd = StringUtils.defaultString(request.getParameter(PARAMETER_PSWD));
            callbackHandler = new PlainTextCallbackHandler(userid, pswd.toCharArray());
        } else {
            // select login module to use if user is authenticated against the container
            if (request.getUserPrincipal() != null) {
                loginModuleToInitialize = "magnolia_authorization";
                callbackHandler = new PlainTextCallbackHandler(request.getUserPrincipal().getName(),
                        "".toCharArray());
            } else {
                // invalid auth request
                return false;
            }
        }
    } else {
        // its a basic authentication request
        callbackHandler = new Base64CallbackHandler(credentials);
    }

    Subject subject;
    try {
        LoginContext loginContext = new LoginContext(loginModuleToInitialize, callbackHandler);
        loginContext.login();
        subject = loginContext.getSubject();
        // ok, we NEED a session here since the user has been authenticated
        HttpSession httpsession = request.getSession(true);
        httpsession.setAttribute(ATTRIBUTE_JAAS_SUBJECT, subject);
    } catch (LoginException le) {
        if (log.isDebugEnabled())
            log.debug("Exception caught", le);

        HttpSession httpsession = request.getSession(false);
        if (httpsession != null) {
            httpsession.invalidate();
        }
        return false;
    }

    return true;
}

From source file:org.carewebframework.ui.util.RequestUtil.java

/**
 * Return client's ip address./*from   www. jav a 2  s.c o  m*/
 * <p>
 * Must be called in the scope of an Execution/ServletRequest. This considers header
 * X-FORWARDED-FOR (i.e. useful if behind a proxy)
 * 
 * @return the client's IP
 * @throws IllegalStateException if called outside scope of an Execution/ServletRequest
 */
public static String getRemoteAddress() {
    //final Execution execution = assertExecution();
    final HttpServletRequest request = assertRequest();
    String ipAddress = request.getHeader("x-forwarded-for");
    boolean ipFromHeader = true;
    if (isEmpty(ipAddress)) {
        ipAddress = request.getHeader("X_FORWARDED_FOR");
        if (isEmpty(ipAddress)) {
            ipFromHeader = false;
            ipAddress = request.getRemoteAddr();
        }
        logHeaderNames();
    }
    //log headers in case we find a case where above logic doesn't return correct ip
    if (log.isTraceEnabled()) {
        logHeaderNames();
        log.trace(String.format("Remote address: %s , obtained from X-FORWARDED_FOR header?", ipAddress,
                ipFromHeader));
    }
    return ipAddress;
}

From source file:org.dspace.webmvc.controller.ResourceController.java

protected static boolean acceptsDeflate(HttpServletRequest req) {
    final String ae = req.getHeader("Accept-Encoding");
    return ae != null && ae.contains("gzip");
}

From source file:com.github.ipaas.ifw.util.IPUtil.java

/**
 * //w  w  w  .  jav  a 2s .  c o  m
 * ??
 * 
 * @param request
 *            http
 * @return int
 */
public static int getUserPort(HttpServletRequest request) {
    String portStr = request.getHeader("Ty_Remote_Port");
    if (StringUtil.isNullOrBlank(portStr))
        return request.getRemotePort();
    try {
        return Integer.parseInt(portStr);
    } catch (NumberFormatException e) {
        return request.getRemotePort();
    }
}

From source file:de.metas.ui.web.config.ServletLoggingFilter.java

private static final String extractUserAgent(final HttpServletRequest httpRequest) {
    try {/*from w  w w .  j av a2s.  c  o m*/
        final String userAgent = httpRequest.getHeader("User-Agent");
        return userAgent;
    } catch (final Exception e) {
        e.printStackTrace();
        return "?";
    }
}

From source file:com.shz.foundation.utils.Servlets.java

/**
 * ?ip??????ip??//from   ww w.jav  a2  s  . co  m
 * @param request
 * @return
 */
public static String getClientIP(HttpServletRequest request) {
    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("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    return ip;
}

From source file:com.cisco.oss.foundation.http.server.AbstractInfraHttpFilter.java

/**
 * Determine the original client. If there is an x-forwarded-for header,
 * take the first host/ip from the list. Else, take the value from the
 * Request object's remote host./*from  www  .ja  va 2 s  .  co  m*/
 * 
 * @param request
 *            The HTTP Servlet request object being handled.
 * 
 * @return The original client host or IP value.
 */
public static String getOriginalClient(final HttpServletRequest request) {
    return getOriginalClient(request.getRemoteHost(),
            request.getHeader(HttpServerFactory.X_FORWARD_FOR_HEADER));
}

From source file:springfox.documentation.swagger2.web.HostNameProvider.java

static UriComponents componentsFrom(HttpServletRequest request) {
    ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);

    ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader(ForwardedHeader.NAME));
    String proto = hasText(forwarded.getProto()) ? forwarded.getProto()
            : request.getHeader("X-Forwarded-Proto");
    String forwardedSsl = request.getHeader("X-Forwarded-Ssl");

    if (hasText(proto)) {
        builder.scheme(proto);//from w  w  w  . ja v  a 2 s .  com
    } else if (hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) {
        builder.scheme("https");
    }

    String host = forwarded.getHost();
    host = hasText(host) ? host : request.getHeader("X-Forwarded-Host");

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

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

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

        String[] hostAndPort = 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 (hasText(port)) {
        builder.port(Integer.parseInt(port));
    }

    return builder.build();
}

From source file:com.betfair.cougar.transport.api.protocol.http.ExecutionContextFactory.java

public static DehydratedExecutionContext resolveExecutionContext(final HttpCommand command,
        final List<IdentityToken> tokens, final String uuidHeader, final String uuidParentsHeader,
        GeoLocationDeserializer geoLocationDeserializer, final GeoIPLocator geoIPLocator,
        final String inferredCountry, final int transportSecurityStrengthFactor,
        final boolean ignoreSubsequentWritesOfIdentity, Date requestTime) {
    final HttpServletRequest request = command.getRequest();
    String uuidString = request.getHeader(uuidHeader);
    String uuidParentsString = request.getHeader(uuidParentsHeader);

    return resolveExecutionContext(tokens, uuidString, uuidParentsString, request.getRemoteAddr(),
            geoLocationDeserializer.deserialize(request, request.getRemoteAddr()), geoIPLocator,
            inferredCountry, request.getHeader(TRACE_ME_HEADER_PARAM), transportSecurityStrengthFactor,
            ignoreSubsequentWritesOfIdentity, requestTime);
}