Example usage for javax.servlet.http HttpServletRequest getHeaders

List of usage examples for javax.servlet.http HttpServletRequest getHeaders

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getHeaders.

Prototype

public Enumeration<String> getHeaders(String name);

Source Link

Document

Returns all the values of the specified request header as an Enumeration of String objects.

Usage

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilter.java

private boolean checkHeaderToken(HttpServletRequest request) {
    Enumeration<String> headers = request.getHeaders("Authorization");
    while (headers.hasMoreElements()) {
        String value = headers.nextElement();
        if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
            return true;
        }/*from w ww . j av  a2 s.c  o  m*/
    }

    return false;
}

From source file:net.sf.j2ep.requesthandlers.MaxForwardRequestHandler.java

/**
 * Will write all the headers included in the request to the method.
 * The difference between this method and setHeaders in BasicRequestHandler
 * is that the BasicRequestHandler will also add Via, x-forwarded-for, etc.
 * These "special" headers should not be added when the proxy is target
 * directly with a Max-Forwards: 0 headers.
 * @param method The method to write to/*w ww  .ja va  2s  .com*/
 * @param request The incoming request
 * @see RequestHandlerBase#setHeaders(HttpMethod, HttpServletRequest)
 */
@SuppressWarnings("rawtypes")
private void setAllHeaders(HttpMethod method, HttpServletRequest request) {
    Enumeration headers = request.getHeaderNames();

    while (headers.hasMoreElements()) {
        String name = (String) headers.nextElement();
        Enumeration value = request.getHeaders(name);

        while (value.hasMoreElements()) {
            method.addRequestHeader(name, (String) value.nextElement());
        }

    }
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest00325.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String param = "";
    java.util.Enumeration<String> headers = request.getHeaders("BenchmarkTest00325");

    if (headers != null && headers.hasMoreElements()) {
        param = headers.nextElement(); // just grab first element
    }/*from w ww.ja v  a 2  s  .  com*/

    // URL Decode the header value since req.getHeaders() doesn't. Unlike req.getParameters().
    param = java.net.URLDecoder.decode(param, "UTF-8");

    String bar = org.apache.commons.lang.StringEscapeUtils.escapeHtml(param);

    // javax.servlet.http.HttpSession.putValue(java.lang.String,java.lang.Object^)
    request.getSession().putValue("userid", bar);

    response.getWriter().println("Item: 'userid' with value: '"
            + org.owasp.benchmark.helpers.Utils.encodeForHTML(bar) + "' saved in session.");
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest02049.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String param = "";
    java.util.Enumeration<String> headers = request.getHeaders("Referer");

    if (headers != null && headers.hasMoreElements()) {
        param = headers.nextElement(); // just grab first element
    }// w w w .ja  v  a2  s  .  co m

    // URL Decode the header value since req.getHeaders() doesn't. Unlike req.getParameters().
    param = java.net.URLDecoder.decode(param, "UTF-8");

    String bar = doSomething(request, param);

    response.setHeader("X-XSS-Protection", "0");
    Object[] obj = { "a", bar };
    response.getWriter().printf(java.util.Locale.US, "Formatted like: %1$s and %2$s.", obj);
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.admin.WaitForBackgroundThreadsController.java

/**
 * If there is a "return" parameter and a "referer" header, return the
 * referer URL. Otherwise, return null.//from   w w  w . ja v  a2 s .  co m
 */
private String figureRedirect(HttpServletRequest req) {
    if (!req.getParameterMap().containsKey(PARAMETER_RETURN)) {
        return null;
    }

    Enumeration<?> referers = req.getHeaders(HEADER_REFERER);
    if ((referers == null) || (!referers.hasMoreElements())) {
        return null;
    }

    String redirect = (String) referers.nextElement();
    log.debug("Redirect is to '" + redirect + "'");
    return redirect;
}

From source file:com.redhat.rhn.frontend.servlets.DumpFilter.java

private void logHeaders(HttpServletRequest req) {
    Enumeration items = req.getHeaderNames();
    while (items.hasMoreElements()) {
        String name = (String) items.nextElement();
        Enumeration hdrs = req.getHeaders(name);
        while (hdrs.hasMoreElements()) {
            log.debug("Header: name [" + name + "] value [" + (String) hdrs.nextElement() + "]");
        }/*from w  w  w . jav a  2 s.  c o m*/

    }
}

From source file:org.jnap.core.mvc.async.AsyncResponseHandler.java

/**
 * /*from   w ww.  j av  a2  s. c  om*/
 * @param request
 * @param output
 * @return
 */
protected boolean shouldOutputComments(HttpServletRequest request, boolean output) {
    boolean webSocketEnabled = false;
    if (request.getHeaders("Connection") != null && request.getHeaders("Connection").hasMoreElements()) {
        String[] e = ((Enumeration<String>) request.getHeaders("Connection")).nextElement().split(",");
        for (String upgrade : e) {
            if (upgrade.trim().equalsIgnoreCase(WEBSOCKET_UPGRADE)) {
                webSocketEnabled = true;
                break;
            }
        }
    }

    String transport = request.getHeader(X_ATMOSPHERE_TRANSPORT);
    if (webSocketEnabled || (transport != null && transport.equals(LONG_POLLING_TRANSPORT))) {
        return false;
    }

    return output;
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest00320.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    String param = "";
    java.util.Enumeration<String> headers = request.getHeaders("vector");
    if (headers.hasMoreElements()) {
        param = headers.nextElement(); // just grab first element
    }/* ww w  . j  a va2s  .c  om*/

    String bar = org.apache.commons.lang.StringEscapeUtils.escapeHtml(param);

    long l = new java.util.Random().nextLong();
    String rememberMeKey = Long.toString(l);

    String user = "Logan";
    String fullClassName = this.getClass().getName();
    String testCaseNumber = fullClassName
            .substring(fullClassName.lastIndexOf('.') + 1 + "BenchmarkTest".length());
    user += testCaseNumber;

    String cookieName = "rememberMe" + testCaseNumber;

    boolean foundUser = false;
    javax.servlet.http.Cookie[] cookies = request.getCookies();
    for (int i = 0; cookies != null && ++i < cookies.length && !foundUser;) {
        javax.servlet.http.Cookie cookie = cookies[i];
        if (cookieName.equals(cookie.getName())) {
            if (cookie.getValue().equals(request.getSession().getAttribute(cookieName))) {
                foundUser = true;
            }
        }
    }

    if (foundUser) {
        response.getWriter().println("Welcome back: " + user + "<br/>");
    } else {
        javax.servlet.http.Cookie rememberMe = new javax.servlet.http.Cookie(cookieName, rememberMeKey);
        rememberMe.setSecure(true);
        request.getSession().setAttribute(cookieName, rememberMeKey);
        response.addCookie(rememberMe);
        response.getWriter().println(user + " has been remembered with cookie: " + rememberMe.getName()
                + " whose value is: " + rememberMe.getValue() + "<br/>");
    }

    response.getWriter().println("Weak Randomness Test java.util.Random.nextLong() executed");
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest00342.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    String param = "";
    java.util.Enumeration<String> headers = request.getHeaders("vector");
    if (headers.hasMoreElements()) {
        param = headers.nextElement(); // just grab first element
    }//w  w w  . j a v  a  2 s .  co  m

    String bar = org.apache.commons.lang.StringEscapeUtils.escapeHtml(param);

    try {
        int r = java.security.SecureRandom.getInstance("SHA1PRNG").nextInt();
        String rememberMeKey = Integer.toString(r);

        String user = "SafeIngrid";
        String fullClassName = this.getClass().getName();
        String testCaseNumber = fullClassName
                .substring(fullClassName.lastIndexOf('.') + 1 + "BenchmarkTest".length());
        user += testCaseNumber;

        String cookieName = "rememberMe" + testCaseNumber;

        boolean foundUser = false;
        javax.servlet.http.Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && ++i < cookies.length && !foundUser;) {
            javax.servlet.http.Cookie cookie = cookies[i];
            if (cookieName.equals(cookie.getName())) {
                if (cookie.getValue().equals(request.getSession().getAttribute(cookieName))) {
                    foundUser = true;
                }
            }
        }

        if (foundUser) {
            response.getWriter().println("Welcome back: " + user + "<br/>");
        } else {
            javax.servlet.http.Cookie rememberMe = new javax.servlet.http.Cookie(cookieName, rememberMeKey);
            rememberMe.setSecure(true);
            request.getSession().setAttribute(cookieName, rememberMeKey);
            response.addCookie(rememberMe);
            response.getWriter().println(user + " has been remembered with cookie: " + rememberMe.getName()
                    + " whose value is: " + rememberMe.getValue() + "<br/>");
        }

    } catch (java.security.NoSuchAlgorithmException e) {
        System.out.println("Problem executing SecureRandom.nextInt() - TestCase");
        throw new ServletException(e);
    }
    response.getWriter().println("Weak Randomness Test java.security.SecureRandom.nextInt() executed");
}

From source file:org.owasp.benchmark.testcode.BenchmarkTest00344.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    String param = "";
    java.util.Enumeration<String> headers = request.getHeaders("vector");
    if (headers.hasMoreElements()) {
        param = headers.nextElement(); // just grab first element
    }//  w w  w  . ja  v a 2 s  . co m

    String bar = org.apache.commons.lang.StringEscapeUtils.escapeHtml(param);

    try {
        long l = java.security.SecureRandom.getInstance("SHA1PRNG").nextLong();
        String rememberMeKey = Long.toString(l);

        String user = "SafeLogan";
        String fullClassName = this.getClass().getName();
        String testCaseNumber = fullClassName
                .substring(fullClassName.lastIndexOf('.') + 1 + "BenchmarkTest".length());
        user += testCaseNumber;

        String cookieName = "rememberMe" + testCaseNumber;

        boolean foundUser = false;
        javax.servlet.http.Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && ++i < cookies.length && !foundUser;) {
            javax.servlet.http.Cookie cookie = cookies[i];
            if (cookieName.equals(cookie.getName())) {
                if (cookie.getValue().equals(request.getSession().getAttribute(cookieName))) {
                    foundUser = true;
                }
            }
        }

        if (foundUser) {
            response.getWriter().println("Welcome back: " + user + "<br/>");
        } else {
            javax.servlet.http.Cookie rememberMe = new javax.servlet.http.Cookie(cookieName, rememberMeKey);
            rememberMe.setSecure(true);
            request.getSession().setAttribute(cookieName, rememberMeKey);
            response.addCookie(rememberMe);
            response.getWriter().println(user + " has been remembered with cookie: " + rememberMe.getName()
                    + " whose value is: " + rememberMe.getValue() + "<br/>");
        }

    } catch (java.security.NoSuchAlgorithmException e) {
        System.out.println("Problem executing SecureRandom.nextLong() - TestCase");
        throw new ServletException(e);
    }
    response.getWriter().println("Weak Randomness Test java.security.SecureRandom.nextLong() executed");
}