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:jeeves.server.sources.ServiceRequestFactory.java

/**
 * Build up a map of the HTTP headers./*from   w  ww .  j a  va2  s . c o m*/
 * @param req The web request
 * @return Map of header keys and values.
 */
@SuppressWarnings("unchecked")
private static Map<String, String> extractHeaders(HttpServletRequest req) {
    Map<String, String> headerMap = new HashMap<String, String>();
    for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements();) {
        String key = e.nextElement();
        headerMap.put(key, req.getHeader(key));
    }
    // The remote user needs to be saved as a header also
    if (req.getRemoteUser() != null) {
        headerMap.put("REMOTE_USER", req.getRemoteUser());
    }
    return headerMap;
}

From source file:com.erudika.para.rest.RestUtils.java

/**
 * Extracts the access key from a request. It can be a header or a parameter.
 * @param request a request/* w ww.  j a v  a 2  s  . com*/
 * @return the access key
 */
public static String extractAccessKey(HttpServletRequest request) {
    if (request == null) {
        return "";
    }
    String auth = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (StringUtils.isBlank(auth)) {
        auth = request.getParameter("X-Amz-Credential");
        if (StringUtils.isBlank(auth)) {
            return "";
        } else {
            return StringUtils.substringBefore(auth, "/");
        }
    } else {
        String credential = StringUtils.substringBetween(auth, "Credential=", ",");
        return StringUtils.substringBefore(credential, "/");
    }
}

From source file:ch.entwine.weblounge.common.content.ResourceUtils.java

/**
 * Returns <code>true</code> if the resource either is more recent than the
 * cached version on the client side or the request does not contain caching
 * information./*from w w w.  j a  va  2  s . c o m*/
 * <p>
 * The calculation is made based on the availability of either the
 * <code>If-None-Match</code> or the <code>If-Modified-Since</code> header (in
 * this order).
 * 
 * @param request
 *          the client request
 * @param date
 *          the date
 * @return <code>true</code> if the resource is more recent than the version
 *         that is cached at the client.
 * @throws IllegalArgumentException
 *           if the <code>If-Modified-Since</code> header cannot be converted
 *           to a date.
 */
public static boolean hasChanged(HttpServletRequest request, long date) throws IllegalArgumentException {
    if (request.getHeader("If-None-Match") != null) {
        return isMismatch(request, getETagValue(date));
    } else if (request.getHeader("If-Modified-Since") != null) {
        return isModified(request, date);
    }
    return true;
}

From source file:com.shishu.utility.string.StringUtil.java

/** 
 * ?IP,??IPBUG/*  w  ww .j  a va 2s .com*/
 * @param request
 * @return 
 * @create  2009-2-24 ?09:19:02 yanghb
 * @history  
 */
public static String getIpAddr(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:ch.entwine.weblounge.common.content.ResourceUtils.java

/**
 * Returns <code>true</code> if the resource either is more recent than the
 * cached version on the client side or the request does not contain caching
 * information./*ww  w. j  a  v  a2 s .  c  o  m*/
 * <p>
 * The calculation is made based on the availability of the
 * <code>If-Modified-Since</code> header. Use
 * {@link #hasChanged(HttpServletRequest, long)} to also take the
 * <code>If-None-Match</code> header into account.
 * 
 * @param request
 *          the client request
 * @param date
 *          the date
 * @return <code>true</code> if the resource is more recent than the version
 *         that is cached at the client.
 * @throws IllegalArgumentException
 *           if the <code>If-Modified-Since</code> header cannot be converted
 *           to a date.
 */
public static boolean isModified(HttpServletRequest request, long date) throws IllegalArgumentException {
    if (request.getHeader("If-Modified-Since") != null) {
        try {
            long cachedModificationDate = request.getDateHeader("If-Modified-Since");
            return cachedModificationDate < date;
        } catch (IllegalArgumentException e) {
            logger.debug("Client sent malformed 'If-Modified-Since' header: {}");
        }
    }
    return true;
}

From source file:net.oneandone.jasmin.main.Servlet.java

private static boolean canGzip(HttpServletRequest request) {
    String accepted;/*  w w  w. j  a va 2  s  .c  om*/

    accepted = request.getHeader("Accept-Encoding");
    return accepted != null && contains(accepted, "gzip");
}

From source file:ch.entwine.weblounge.common.content.ResourceUtils.java

/**
 * Returns <code>true</code> if the resource has not been modified according
 * to the expected <code>ETag</code> value, <code>false</code> if the cached
 * version on the client side is out dated or if the request did not contain
 * caching information./* w w  w  . ja v  a  2s  . c  o  m*/
 * <p>
 * The decision is based on the availability and value of the
 * <code>If-None-Match</code> header (called <code>ETag</code>). The computed
 * value of <code>eTag</code> is expected to be plain, i. e. without
 * surrounding quotes.
 * 
 * @param eTag
 *          the expected eTag value
 * @param request
 *          the client request
 * @return <code>true</code> if the resource's calculated eTag matches the one
 *         specified
 * @throws IllegalArgumentException
 *           if the <code>If-Modified-Since</code> cannot be converted to a
 *           date.
 */
public static boolean isMismatch(HttpServletRequest request, String eTag) throws IllegalArgumentException {
    String eTagHeader = request.getHeader("If-None-Match");
    if (StringUtils.isBlank(eTagHeader))
        return true;
    return !eTagHeader.equals(eTag);
}

From source file:edu.uci.ics.asterix.api.http.servlet.RESTAPIServlet.java

/**
 * Initialize the Content-Type of the response, and construct a
 * SessionConfig with the appropriate output writer and output-format
 * based on the Accept: header and other servlet parameters.
 *///from  w  ww  .  ja  va2 s  .c o m
static SessionConfig initResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("utf-8");

    // JSON output is the default; most generally useful for a
    // programmatic HTTP API
    OutputFormat format = OutputFormat.JSON;

    // First check the "output" servlet parameter.
    String output = request.getParameter("output");
    String accept = request.getHeader("Accept");
    if (output != null) {
        if (output.equals("CSV")) {
            format = OutputFormat.CSV;
        } else if (output.equals("ADM")) {
            format = OutputFormat.ADM;
        }
    } else {
        // Second check the Accept: HTTP header.
        if (accept != null) {
            if (accept.contains("application/x-adm")) {
                format = OutputFormat.ADM;
            } else if (accept.contains("text/csv")) {
                format = OutputFormat.CSV;
            }
        }
    }

    SessionConfig sessionConfig = new SessionConfig(response.getWriter(), format);

    // Now that format is set, output the content-type
    switch (format) {
    case ADM:
        response.setContentType("application/x-adm");
        break;
    case JSON:
        response.setContentType("application/json");
        break;
    case CSV: {
        // Check for header parameter or in Accept:.
        if ("present".equals(request.getParameter("header"))
                || (accept != null && accept.contains("header=present"))) {
            response.setContentType("text/csv; header=present");
            sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, true);
        } else {
            response.setContentType("text/csv; header=absent");
        }
    }
    }
    ;

    return sessionConfig;
}

From source file:com.zimbra.common.util.HttpUtil.java

public static Browser guessBrowser(HttpServletRequest req) {
    String ua = req.getHeader("User-Agent");
    return guessBrowser(ua);
}

From source file:edu.usu.sdl.openstorefront.security.HeaderRealm.java

public static boolean handleHeaderLogin(HttpServletRequest request) {
    boolean loginSuccessful = false;

    final String STUB_HEADER = "X_STUBHEADER_X";
    if (isUsingHeaderRealm()) {
        HeaderAuthToken headerAuthToken = new HeaderAuthToken();
        headerAuthToken.setRequest(request);
        headerAuthToken//  ww w. j  a va2  s  .  c  o  m
                .setAdminGroupName(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_ADMIN_GROUP));
        headerAuthToken.setEmail(request
                .getHeader(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_EMAIL, STUB_HEADER)));
        headerAuthToken.setFirstname(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_FIRSTNAME, STUB_HEADER)));

        Enumeration<String> groupValues = request
                .getHeaders(PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_GROUP, STUB_HEADER));
        StringBuilder group = new StringBuilder();
        while (groupValues.hasMoreElements()) {
            group.append(groupValues.nextElement());
            group.append(" | ");
        }

        headerAuthToken.setGroup(group.toString());
        headerAuthToken.setGuid(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_LDAPGUID, STUB_HEADER)));
        headerAuthToken.setLastname(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_LASTNAME, STUB_HEADER)));
        headerAuthToken.setOrganization(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_ORGANIZATION, STUB_HEADER)));
        headerAuthToken.setUsername(request.getHeader(
                PropertiesManager.getValue(PropertiesManager.KEY_OPENAM_HEADER_USERNAME, STUB_HEADER)));

        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.login(headerAuthToken);
            loginSuccessful = true;
        } catch (Exception ex) {
            log.log(Level.WARNING, "Login failed on header; Check configuration, if needed", ex);
        }
    }

    return loginSuccessful;
}