Java Code Examples for javax.servlet.http.HttpServletRequest.getHeaders()

The following are Jave code examples for showing how to use getHeaders() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: xrd4j   File: AdapterUtils.java   View Source Code Vote up 7 votes
/**
 * Returns a string containg all the HTTP request headers and their values.
 *
 * @param req HTTP request
 * @return String containg all the HTTP request headers and their values
 */
public static String getHeaderInfo(HttpServletRequest req) {
    StringBuilder builder = new StringBuilder("HTTP request headers :");

    Enumeration<String> headerNames = req.getHeaderNames();

    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        builder.append("\n\"").append(headerName).append("\" => \"");
        Enumeration<String> headers = req.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String headerValue = headers.nextElement();
            builder.append(headerValue);
            if (headers.hasMoreElements()) {
                builder.append(",");
            }
        }
        builder.append("\"");
    }
    return builder.toString();
}
 
Example 2
Project: minijax   File: MinijaxHttpHeaders.java   View Source Code Vote up 7 votes
public MinijaxHttpHeaders(final HttpServletRequest request) {
    headers = new MultivaluedHashMap<>();

    final Enumeration<String> ne = request.getHeaderNames();
    while (ne.hasMoreElements()) {
        final String name = ne.nextElement();
        final Enumeration<String> ve = request.getHeaders(name);
        while (ve.hasMoreElements()) {
            headers.add(name, ve.nextElement());
        }
    }

    cookies = CookieUtils.convertServletToJax(request.getCookies());
}
 
Example 3
Project: FCat   File: PathUtil.java   View Source Code Vote up 6 votes
@SuppressWarnings("rawtypes")
public static List<String> getRequestHeaderList(HttpServletRequest request) {
	Enumeration headerNames = request.getHeaderNames();
	List<String> resultList;
	if (headerNames == null || !headerNames.hasMoreElements()) {
		return Collections.emptyList();
	}
	resultList = new ArrayList<String>();
	while (headerNames != null && headerNames.hasMoreElements()) {
		String headerName = headerNames.nextElement().toString();
		String headerValue = "";
		Enumeration header = request.getHeaders(headerName);
		while (header != null && header.hasMoreElements()) {
			headerValue = headerValue + "," + header.nextElement().toString();
		}
		if (headerValue.length() > 0) {
			headerValue = headerValue.substring(1, headerValue.length());
		}
		resultList.add(headerName + "=" + headerValue);
	}
	return resultList;
}
 
Example 4
Project: java-web-servlet-filter   File: HttpServletRequestExtractAdapter.java   View Source Code Vote up 6 votes
protected Map<String, List<String>> servletHeadersToMultiMap(HttpServletRequest httpServletRequest) {
    Map<String, List<String>> headersResult = new HashMap<>();

    Enumeration<String> headerNamesIt = httpServletRequest.getHeaderNames();
    while (headerNamesIt.hasMoreElements()) {
        String headerName = headerNamesIt.nextElement();

        Enumeration<String> valuesIt = httpServletRequest.getHeaders(headerName);
        List<String> valuesList = new ArrayList<>(1);
        while (valuesIt.hasMoreElements()) {
            valuesList.add(valuesIt.nextElement());
        }

        headersResult.put(headerName, valuesList);
    }

    return headersResult;
}
 
Example 5
Project: uavstack   File: UEMRewritePageGHHandler.java   View Source Code Vote up 5 votes
/**
 * isPage
 * 
 * @param request
 * @return
 */
private boolean isPage(HttpServletRequest request) {

    /**
     * Step 1: check if is html,htm,jsp
     */
    String url = request.getRequestURL().toString();

    if (MonitorServerUtil.isIncludeMonitorURLForPage(url)) {
        return true;
    }

    /**
     * Step 2: if has no extension, check if accept is a page style
     */
    Enumeration<String> accepts = request.getHeaders("Accept");

    while (accepts.hasMoreElements()) {

        String accept = accepts.nextElement();

        for (String mime : acceptMIME) {
            if (accept.indexOf(mime) > -1) {
                return true;
            }
        }
    }

    return false;
}
 
Example 6
Project: rate-limiting   File: HeaderFilter.java   View Source Code Vote up 5 votes
@Override
public String filter(HttpServletRequest request) {
	if (!matchesHttpMethod(request)) {// don't apply filter unless HTTP
										// method matches defined in
										// settings
		return null;
	}

	@SuppressWarnings("rawtypes")
	Enumeration headers = request.getHeaders(settings.getHeaderName());

	while (headers.hasMoreElements()) {
		String header = (String) headers.nextElement();

		if (headerPattern != null) {
			Matcher m = headerPattern.matcher(header);
			if (!m.matches())
				continue;

			if (settings.getExtractIndex() > 0)
				return m.group(settings.getExtractIndex());
			else
				return m.group(0);
		}
		return header;
	}
	return null;
}
 
Example 7
Project: apache-tomcat-7.0.73-with-comment   File: TestInternalInputBuffer.java   View Source Code Vote up 5 votes
/**
 * Only interested in the request headers from a GET request
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // Just echo the header value back as plain text
    resp.setContentType("text/plain");

    PrintWriter out = resp.getWriter();

    Enumeration<String> values = req.getHeaders("X-Bug48839");
    while (values.hasMoreElements()) {
        out.println(values.nextElement());
    }
}
 
Example 8
Project: tomcat7   File: WebSocketServlet.java   View Source Code Vote up 5 votes
private boolean headerContainsToken(HttpServletRequest req,
        String headerName, String target) {
    Enumeration<String> headers = req.getHeaders(headerName);
    while (headers.hasMoreElements()) {
        String header = headers.nextElement();
        String[] tokens = header.split(",");
        for (String token : tokens) {
            if (target.equalsIgnoreCase(token.trim())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 9
Project: careconnect-reference-implementation   File: BaseProvider.java   View Source Code Vote up 5 votes
public void startRequest(HttpServletRequest theRequest) {
    if (theRequest == null) {
        return;
    }

    Set<String> headerNames = new TreeSet<String>();
    for (Enumeration<String> enums = theRequest.getHeaderNames(); enums.hasMoreElements();) {
        headerNames.add(enums.nextElement());
    }
    ourLog.debug("Request headers: {}", headerNames);

    Enumeration<String> forwardedFors = theRequest.getHeaders("x-forwarded-for");
    StringBuilder b = new StringBuilder();
    for (Enumeration<String> enums = forwardedFors; enums != null && enums.hasMoreElements();) {
        if (b.length() > 0) {
            b.append(" / ");
        }
        b.append(enums.nextElement());
    }

    String forwardedFor = b.toString();
    String ip = theRequest.getRemoteAddr();
    if (StringUtils.isBlank(forwardedFor)) {
        org.slf4j.MDC.put(REMOTE_ADDR, ip);
        ourLog.debug("Request is from address: {}", ip);
    } else {
        org.slf4j.MDC.put(REMOTE_ADDR, forwardedFor);
        ourLog.debug("Request is from forwarded address: {}", forwardedFor);
    }

    String userAgent = StringUtils.defaultString(theRequest.getHeader("user-agent"));
    org.slf4j.MDC.put(REMOTE_UA, userAgent);

}
 
Example 10
Project: tomcat7   File: UpgradeUtil.java   View Source Code Vote up 5 votes
private static List<String> getTokensFromHeader(HttpServletRequest req,
        String headerName) {
    List<String> result = new ArrayList<String>();
    Enumeration<String> headers = req.getHeaders(headerName);
    while (headers.hasMoreElements()) {
        String header = headers.nextElement();
        String[] tokens = header.split(",");
        for (String token : tokens) {
            result.add(token.trim());
        }
    }
    return result;
}
 
Example 11
Project: jerrydog   File: FormAuthenticator.java   View Source Code Vote up 5 votes
/**
 * Save the original request information into our session.
 *
 * @param request The request to be saved
 * @param session The session to contain the saved information
 */
private void saveRequest(HttpRequest request, Session session) {

    // Create and populate a SavedRequest object for this request
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    SavedRequest saved = new SavedRequest();
    Cookie cookies[] = hreq.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++)
            saved.addCookie(cookies[i]);
    }
    Enumeration names = hreq.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        Enumeration values = hreq.getHeaders(name);
        while (values.hasMoreElements()) {
            String value = (String) values.nextElement();
            saved.addHeader(name, value);
        }
    }
    Enumeration locales = hreq.getLocales();
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        saved.addLocale(locale);
    }
    Map parameters = hreq.getParameterMap();
    Iterator paramNames = parameters.keySet().iterator();
    while (paramNames.hasNext()) {
        String paramName = (String) paramNames.next();
        String paramValues[] = (String[]) parameters.get(paramName);
        saved.addParameter(paramName, paramValues);
    }
    saved.setMethod(hreq.getMethod());
    saved.setQueryString(hreq.getQueryString());
    saved.setRequestURI(hreq.getRequestURI());

    // Stash the SavedRequest in our session for later use
    session.setNote(Constants.FORM_REQUEST_NOTE, saved);

}
 
Example 12
Project: lazycat   File: UpgradeUtil.java   View Source Code Vote up 5 votes
private static boolean headerContainsToken(HttpServletRequest req, String headerName, String target) {
	Enumeration<String> headers = req.getHeaders(headerName);
	while (headers.hasMoreElements()) {
		String header = headers.nextElement();
		String[] tokens = header.split(",");
		for (String token : tokens) {
			if (target.equalsIgnoreCase(token.trim())) {
				return true;
			}
		}
	}
	return false;
}
 
Example 13
Project: apache-tomcat-7.0.73-with-comment   File: UpgradeUtil.java   View Source Code Vote up 5 votes
private static boolean headerContainsToken(HttpServletRequest req,
        String headerName, String target) {
    Enumeration<String> headers = req.getHeaders(headerName);
    while (headers.hasMoreElements()) {
        String header = headers.nextElement();
        String[] tokens = header.split(",");
        for (String token : tokens) {
            if (target.equalsIgnoreCase(token.trim())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 14
Project: apache-tomcat-7.0.73-with-comment   File: CompressionFilterTestServlet.java   View Source Code Vote up 5 votes
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    ServletOutputStream out = response.getOutputStream();
    response.setContentType("text/plain");

    Enumeration<String> e = request.getHeaders("Accept-Encoding");
    while (e.hasMoreElements()) {
        String name = e.nextElement();
        out.println(name);
        if (name.indexOf("gzip") != -1) {
            out.println("gzip supported -- able to compress");
        }
        else {
            out.println("gzip not supported");
        }
    }


    out.println("Compression Filter Test Servlet");
    out.println("Minimum content length for compression is 128 bytes");
    out.println("**********  32 bytes  **********");
    out.println("**********  32 bytes  **********");
    out.println("**********  32 bytes  **********");
    out.println("**********  32 bytes  **********");
    out.close();
}
 
Example 15
Project: lazycat   File: UpgradeUtil.java   View Source Code Vote up 5 votes
private static List<String> getTokensFromHeader(HttpServletRequest req, String headerName) {
	List<String> result = new ArrayList<String>();
	Enumeration<String> headers = req.getHeaders(headerName);
	while (headers.hasMoreElements()) {
		String header = headers.nextElement();
		String[] tokens = header.split(",");
		for (String token : tokens) {
			result.add(token.trim());
		}
	}
	return result;
}
 
Example 16
Project: apache-tomcat-7.0.73-with-comment   File: TestInternalInputBuffer.java   View Source Code Vote up 5 votes
private void processHeaders(String header, HttpServletRequest req,
        PrintWriter out) {
    Enumeration<String> values = req.getHeaders(header);
    while (values.hasMoreElements()) {
        out.println(values.nextElement());
    }
}
 
Example 17
Project: lazycat   File: WebSocketServlet.java   View Source Code Vote up 5 votes
private boolean headerContainsToken(HttpServletRequest req, String headerName, String target) {
	Enumeration<String> headers = req.getHeaders(headerName);
	while (headers.hasMoreElements()) {
		String header = headers.nextElement();
		String[] tokens = header.split(",");
		for (String token : tokens) {
			if (target.equalsIgnoreCase(token.trim())) {
				return true;
			}
		}
	}
	return false;
}
 
Example 18
Project: lams   File: ImplicitObjectELResolver.java   View Source Code Vote up 5 votes
/**
 *
 * Creates the Map that maps header name to an array of header
 * values.
 **/
public static Map<String, String[]> createHeadersMap (PageContext pContext)
{
  final HttpServletRequest request =
    (HttpServletRequest) pContext.getRequest ();
  return new EnumeratedMap<String, String[]> ()
    {
      public Enumeration<String> enumerateKeys () 
      {
        return request.getHeaderNames ();
      }

      public String[] getValue (Object pKey) 
      {
        if (pKey instanceof String) {
          // Drain the header enumeration
          List<String> l = new ArrayList<String> ();
          Enumeration<String> e = request.getHeaders ((String) pKey);
          if (e != null) {
            while (e.hasMoreElements ()) {
              l.add (e.nextElement ());
            }
          }
          return l.toArray (new String [l.size ()]);
        }
        else {
          return null;
        }
      }

      public boolean isMutable ()
      {
        return false;
      }
    };
}
 
Example 19
Project: lazycat   File: WebSocketServlet.java   View Source Code Vote up 5 votes
private List<String> getTokensFromHeader(HttpServletRequest req, String headerName) {
	List<String> result = new ArrayList<String>();

	Enumeration<String> headers = req.getHeaders(headerName);
	while (headers.hasMoreElements()) {
		String header = headers.nextElement();
		String[] tokens = header.split(",");
		for (String token : tokens) {
			result.add(token.trim());
		}
	}
	return result;
}
 
Example 20
Project: gitplex-mit   File: ServletRequestCopy.java   View Source Code Vote up 4 votes
public ServletRequestCopy(HttpServletRequest request) {
	this.servletPath = request.getServletPath();
	this.contextPath = request.getContextPath();
	this.pathInfo = request.getPathInfo();
	this.requestUri = request.getRequestURI();
	this.requestURL = request.getRequestURL();
	this.method = request.getMethod();
	this.serverName = request.getServerName();
	this.serverPort = request.getServerPort();
	this.protocol = request.getProtocol();
	this.scheme = request.getScheme();
	
	
	/*
	 * have to comment out below two lines as otherwise web socket will
	 * report UnSupportedOperationException upon connection
	 */
	//this.characterEncoding = request.getCharacterEncoding();
	//this.contentType = request.getContentType();
	//this.requestedSessionId = request.getRequestedSessionId();
	this.characterEncoding = null;
	this.contentType = null;
	this.requestedSessionId = null;
	
	this.locale = request.getLocale();
	this.locales = request.getLocales();
	this.isSecure = request.isSecure();
	this.remoteUser = request.getRemoteUser();
	this.remoteAddr = request.getRemoteAddr();
	this.remoteHost = request.getRemoteHost();
	this.remotePort = request.getRemotePort();
	this.localAddr = request.getLocalAddr();
	this.localName = request.getLocalName();
	this.localPort = request.getLocalPort();
	this.pathTranslated = request.getPathTranslated();
	this.principal = request.getUserPrincipal();

	HttpSession session = request.getSession(true);
	httpSession = new HttpSessionCopy(session);

	String s;
	Enumeration<String> e = request.getHeaderNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		Enumeration<String> headerValues = request.getHeaders(s);
		this.headers.put(s, headerValues);
	}

	e = request.getAttributeNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		attributes.put(s, request.getAttribute(s));
	}

	e = request.getParameterNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		parameters.put(s, request.getParameterValues(s));
	}
}