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.ericsson.eif.hansoft.utils.HttpUtils.java

/**
 * Gets the credentials from an HTTP request.
 * /*  ww  w  . j a v  a 2  s . c o  m*/
 * @param request
 *            the request
 * @return the Bugzilla credentials or <code>null</code> if the request did
 *         not contain an <code>Authorization</code> header
 * @throws UnauthorizedException
 *             on problems reading the credentials from the
 *             <code>Authorization</code> request header
 */
public static Credentials getCredentials(HttpServletRequest request) throws UnauthorizedException {
    String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER);
    if (authorizationHeader == null || "".equals(authorizationHeader)) {
        return null;
    }

    Credentials credentials = new Credentials();
    if (!authorizationHeader.startsWith(HttpUtils.BASIC_AUTHORIZATION_PREFIX)) {
        throw new UnauthorizedException("Only basic access authentication is supported.");
    }

    String encodedString = authorizationHeader.substring(HttpUtils.BASIC_AUTHORIZATION_PREFIX.length());
    try {
        String unencodedString = new String(Base64.decode(encodedString), "UTF-8");
        int seperator = unencodedString.indexOf(':');
        if (seperator == -1) {
            throw new UnauthorizedException("Invalid Authorization header value.");
        }

        credentials.setUsername(unencodedString.substring(0, seperator));
        credentials.setPassword(unencodedString.substring(seperator + 1));
    } catch (DecodingException e) {
        throw new UnauthorizedException("Username and password not Base64 encoded.");
    } catch (UnsupportedEncodingException e) {
        throw new UnauthorizedException("Invalid Authorization header value.");
    }

    return credentials;
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???gzip?./*from  w  w w  .j  a v a2  s  .c om*/
 */
public static boolean checkAccetptGzip(HttpServletRequest request) {
    //Http1.1 header
    String acceptEncoding = request.getHeader("Accept-Encoding");

    if (StringUtils.contains(acceptEncoding, "gzip")) {
        return true;
    } else {
        return false;
    }
}

From source file:com.kingen.web.Servlets.java

/**
 * ?Ajax//w ww .  ja  v  a  2  s .  co  m
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");
    User user = (User) SecurityUtils.getSubject().getPrincipal();
    //      Principal principal = UserUtils.getPrincipal();

    // ?
    return ((accept != null && accept.indexOf("application/json") != -1
            || (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
            //         || (principal != null && principal.isMobileLogin())));
            || (user != null)));
}

From source file:com.yrdce.ipo.common.web.Servlets.java

/**
 * ?Ajax//w  ww .j  ava  2s  . co m
 * 
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    String xRequestedWith = request.getHeader("X-Requested-With");
    // Principal principal = UserUtils.getPrincipal();

    // ?
    return ((accept != null && accept.indexOf("application/json") != -1
            || (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)));
    // || (principal != null && principal.isMobileLogin())));
}

From source file:BrowserDetect.java

/**
 * This method is for debugging only/* w  w  w .j a  va 2  s .  c  o  m*/
 */
public static String getUserAgentDebugString(HttpServletRequest request) {
    String userAgent = request.getHeader("user-agent");

    int version = getMajorVersionAssumingIE(userAgent);
    if (version != -1) {
        return "IE/" + version;
    }

    version = getMajorVersionAssumingGecko(userAgent);
    if (version != -1) {
        return "Gecko/" + version;
    }

    version = getMajorVersionAssumingAppleWebKit(userAgent);
    if (version != -1) {
        return "WebKit/" + version;
    }

    version = getMajorVersionAssumingOpera(userAgent);
    if (version != -1) {
        return "Opera/" + version;
    }

    return "Unknown: (" + userAgent + ")";
}

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

/**
 * ??Mozilla//from   w  ww  .jav  a 2 s .c  om
 * @param req
 * @return
 */
public static boolean isMozillaCompatible(HttpServletRequest req) {
    String user_agent = req.getHeader("user-agent");
    return user_agent == null || user_agent.indexOf("Mozilla") != -1;
}

From source file:com.ge.predix.web.cors.CORSFilter.java

static boolean isXhrRequest(final HttpServletRequest request) {
    String xRequestedWith = request.getHeader("X-Requested-With");
    String accessControlRequestHeaders = request.getHeader("Access-Control-Request-Headers");
    return StringUtils.hasText(xRequestedWith) || (StringUtils.hasText(accessControlRequestHeaders)
            && containsHeader(accessControlRequestHeaders, "X-Requested-With"));
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

public static String getHeader(String name, HttpServletRequest req) {
    return req.getHeader(name);
}

From source file:edu.usu.sdl.openstorefront.common.util.NetworkUtil.java

/**
 * Get the correct client ip from a request
 *
 * @param request//from  ww  w .ja va  2  s  .c  o  m
 * @return client ip or N/A if not found
 */
public static String getClientIp(HttpServletRequest request) {
    String clientIp = OpenStorefrontConstant.NOT_AVAILABLE;
    if (request != null) {
        clientIp = request.getRemoteAddr();

        //Check for header ip it may be forwarded by a proxy
        String clientIpFromHeader = request.getHeader("x-forwarded-for");
        if (StringUtils.isNotBlank(clientIpFromHeader)) {
            clientIp = clientIp + " Forward for: " + clientIpFromHeader;
        } else {
            clientIpFromHeader = request.getHeader("x-real-ip");
            if (StringUtils.isNotBlank(clientIpFromHeader)) {
                clientIp = clientIp + " X-real IP: " + clientIpFromHeader;
            }
        }
    }
    return clientIp;
}

From source file:io.lavagna.web.security.CSFRFilter.java

private static ImmutablePair<Boolean, ImmutablePair<Integer, String>> checkCSRF(HttpServletRequest request)
        throws IOException {
    String expectedToken = (String) request.getSession().getAttribute(CSRFToken.CSRF_TOKEN);
    String token = request.getHeader(CSRF_TOKEN_HEADER);
    if (token == null) {
        token = request.getParameter(CSRF_FORM_PARAMETER);
    }//w w  w .  j av a2s  .  c  o m

    if (token == null) {
        return of(false, of(HttpServletResponse.SC_FORBIDDEN, "missing token in header or parameter"));
    }
    if (expectedToken == null) {
        return of(false, of(HttpServletResponse.SC_FORBIDDEN, "missing token from session"));
    }
    if (!safeArrayEquals(token.getBytes("UTF-8"), expectedToken.getBytes("UTF-8"))) {
        return of(false, of(HttpServletResponse.SC_FORBIDDEN, "token is not equal to expected"));
    }

    return of(true, null);
}