Example usage for javax.servlet.http HttpServletRequest getHeaderNames

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

Introduction

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

Prototype

public Enumeration<String> getHeaderNames();

Source Link

Document

Returns an enumeration of all the header names this request contains.

Usage

From source file:jp.co.opentone.bsol.framework.web.extension.jsf.RequestLoggingPhaseListener.java

@Override
public void beforePhase(PhaseEvent event) {
    HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();/*from w ww  . j  av a  2s  . c  o  m*/

    log.info("Requested: {} {}", req.getMethod(), req.getRequestURI());
    Enumeration<?> enm = req.getHeaderNames();
    log.info("Header:");
    while (enm.hasMoreElements()) {
        String name = (String) enm.nextElement();
        log.info("   {} = {}", name, req.getHeader(name));
    }

    log.info(" Parameters:");
    @SuppressWarnings("unchecked")
    Map<String, String[]> parameterMap = req.getParameterMap();
    for (Map.Entry<String, String[]> e : parameterMap.entrySet()) {
        log.info("  {} = {}", e.getKey(), StringUtils.join(e.getValue()));
    }
}

From source file:org.apache.camel.component.gae.http.GHttpTestServlet.java

@SuppressWarnings("unchecked")
@Override// w w  w.j a  v a2  s  .c om
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // Set default response code
    resp.setStatus(200);
    // Copy headers from request to response
    Enumeration<String> headerNames = req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        Enumeration<String> headers = req.getHeaders(headerName);
        while (headers.hasMoreElements()) {
            String headerValue = headers.nextElement();
            resp.addHeader(headerName, headerValue);
            // Set custom response code
            // if requested by client
            if (headerName.equals("code")) {
                resp.setStatus(Integer.parseInt(headerValue));
            }
        }
    }

    // add some special response headers
    resp.addHeader("testUrl", req.getRequestURL().toString());
    resp.addHeader("testMethod", req.getMethod().toString());
    if (req.getQueryString() != null) {
        resp.addHeader("testQuery", req.getQueryString());
    }

    // Copy body from request to response
    resp.getWriter().println(IOUtils.toString(req.getInputStream()));
    resp.getWriter().flush();
}

From source file:io.seldon.api.interceptor.AuthorisationInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    final String requestURI = request.getRequestURI();
    logger.info("Interceptor for: " + requestURI);

    final Enumeration<String> headerNames = request.getHeaderNames();
    logger.debug("<Headers>");
    while (headerNames.hasMoreElements()) {
        final String header = headerNames.nextElement();
        logger.debug(header + " => " + request.getHeader(header));
    }//  ww  w. j  a  v a 2  s.  c o  m
    logger.debug("</Headers>");

    final String referrer = request.getHeader("Referer"); // sic.; incorrect spelling due to spec
    logger.info("Referrer: " + referrer);

    // Authorise on the basis of the referrer header...

    return super.preHandle(request, response, handler);
}

From source file:ca.uhn.fhir.jpa.provider.BaseJpaProvider.java

public void startRequest(HttpServletRequest theRequest) {
    if (theRequest == null) {
        return;/*from   www.  j  ava  2 s. c  o  m*/
    }

    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);

}

From source file:com.github.reverseproxy.ReverseProxyJettyHandler.java

private Map<String, String> getRequestHeaders(HttpServletRequest request) {
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerKey = headerNames.nextElement();
        if (!headerKey.equalsIgnoreCase("Accept-Encoding") && !headerKey.equalsIgnoreCase("Host")) { //Encoding doesnt work???
            String headerValue = StringUtils.join(Collections.list(request.getHeaders(headerKey)), ",");
            requestHeaders.put(headerKey, headerValue);
        }/*from w w  w .  j a v  a  2 s  . co  m*/
    }
    return requestHeaders;
}

From source file:org.tsm.concharto.web.feedback.FeedbackController.java

@SuppressWarnings("unchecked")
private String getBrowserInfo(HttpServletRequest request) {
    StringBuffer userRequestInfo = new StringBuffer().append(request.getRemoteAddr()).append("\n")
            .append(request.getRemoteHost()).append("\n");

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        //don't email cookies for security reasons
        if (!"cookie".equals(headerName)) {
            userRequestInfo.append(headerName).append(": ").append(request.getHeader(headerName)).append("\n");
        }/*w w w . j  ava 2s  .  c o m*/

    }
    return userRequestInfo.toString();
}

From source file:org.gatherdata.camel.http.internal.ServletProxyProducerImpl.java

private Map<String, Object> extractHeaders(HttpServletRequest request) {
    Map<String, Object> headers = new HashMap<String, Object>();

    String contentType = "";
    //apply the headerFilterStrategy
    Enumeration names = request.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        Object value = request.getHeader(name);
        // mapping the content-type 
        if (name.toLowerCase().equals("content-type")) {
            name = Exchange.CONTENT_TYPE;
            contentType = (String) value;
        }//from w  ww .  jav  a 2 s.  co m
        headers.put(name, value);
    }

    //we populate the http request parameters for GET and POST 
    String method = request.getMethod();
    if (method.equalsIgnoreCase("GET") || (method.equalsIgnoreCase("POST")
            && contentType.equalsIgnoreCase("application/x-www-form-urlencoded"))) {
        names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Object value = request.getParameter(name);
            headers.put(name, value);
        }
    }

    // store the method and query and other info in headers
    headers.put(Exchange.HTTP_METHOD, request.getMethod());
    headers.put(Exchange.HTTP_QUERY, request.getQueryString());
    //headers.put(Exchange.HTTP_URL, request.getRequestURL());
    headers.put(Exchange.HTTP_URI, request.getRequestURI());
    headers.put(Exchange.HTTP_PATH, request.getPathInfo());
    headers.put(Exchange.CONTENT_TYPE, request.getContentType());
    headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());

    return headers;
}

From source file:org.springframework.boot.actuate.trace.WebRequestTraceFilter.java

private Map<String, Object> getRequestHeaders(HttpServletRequest request) {
    Map<String, Object> headers = new LinkedHashMap<String, Object>();
    Enumeration<String> names = request.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        List<String> values = Collections.list(request.getHeaders(name));
        Object value = values;//from   w w  w  .  j a  v a2 s .com
        if (values.size() == 1) {
            value = values.get(0);
        } else if (values.isEmpty()) {
            value = "";
        }
        headers.put(name, value);
    }
    return headers;
}

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

/**
 * If logging on the subclass is set to the TRACE level, dump the HTTP
 * headers on the request.//from   w  ww  .j a v a 2 s  .  c  o  m
 */
private void dumpRequestHeaders(HttpServletRequest req) {
    @SuppressWarnings("unchecked")
    Enumeration<String> names = req.getHeaderNames();

    Log subclassLog = LogFactory.getLog(this.getClass());
    subclassLog.trace("----------------------request:" + req.getRequestURL());
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        if (!BORING_HEADERS.contains(name)) {
            subclassLog.trace(name + "=" + req.getHeader(name));
        }
    }
}

From source file:com.eviware.soapui.impl.rest.mock.RestMockActionTest.java

private RestMockRequest makeRestMockRequest() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    Enumeration enumeration = mock(Enumeration.class);
    when(request.getHeaderNames()).thenReturn(enumeration);

    HttpServletResponse response = mock(HttpServletResponse.class);
    ServletOutputStream os = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(os);

    WsdlMockRunContext context = mock(WsdlMockRunContext.class);

    return new RestMockRequest(request, response, context);
}