Java Code Examples for io.undertow.util.HeaderMap#getHeaderNames()

The following examples show how to use io.undertow.util.HeaderMap#getHeaderNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiHandler.java    From light-rest-4j with Apache License 2.0 6 votes vote down vote up
public static Map<String, ?> getHeaderParameters(final HttpServerExchange exchange, final boolean deserializedValueOnly){
	Map<String, Object> deserializedHeaderParamters = exchange.getAttachment(DESERIALIZED_HEADER_PARAMETERS);
	
	if (!deserializedValueOnly) {
		HeaderMap headers = exchange.getRequestHeaders();
		
		if (null==headers) {
			return Collections.emptyMap();
		}
		
		Map<String, HeaderValues> headerMap = new HashMap<>();
		
		for (HttpString headerName: headers.getHeaderNames()) {
			headerMap.put(headerName.toString(), headers.get(headerName));
		}
		
		return mergeMaps(deserializedHeaderParamters, headerMap);
	}
	
	return nonNullMap(deserializedHeaderParamters);
}
 
Example 2
Source File: UndertowXhrTransport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static HttpHeaders toHttpHeaders(HeaderMap headerMap) {
	HttpHeaders httpHeaders = new HttpHeaders();
	for (HttpString name : headerMap.getHeaderNames()) {
		for (String value : headerMap.get(name)) {
			httpHeaders.add(name.toString(), value);
		}
	}
	return httpHeaders;
}
 
Example 3
Source File: UndertowXhrTransport.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static HttpHeaders toHttpHeaders(HeaderMap headerMap) {
	HttpHeaders httpHeaders = new HttpHeaders();
	for (HttpString name : headerMap.getHeaderNames()) {
		for (String value : headerMap.get(name)) {
			httpHeaders.add(name.toString(), value);
		}
	}
	return httpHeaders;
}
 
Example 4
Source File: UndertowXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static HttpHeaders toHttpHeaders(HeaderMap headerMap) {
	HttpHeaders httpHeaders = new HttpHeaders();
	for (HttpString name : headerMap.getHeaderNames()) {
		for (String value : headerMap.get(name)) {
			httpHeaders.add(name.toString(), value);
		}
	}
	return httpHeaders;
}
 
Example 5
Source File: JaegerHandler.java    From light-4j with Apache License 2.0 4 votes vote down vote up
/**
 * Extract the context, start and stop the span here.
 *
 * @param exchange HttpServerExchange
 * @throws Exception Exception
 */
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    // get the path and method to construct the endpoint for the operation of tracing.
    Map<String, Object> auditInfo = exchange.getAttachment(AttachmentConstants.AUDIT_INFO);
    String endpoint = null;
    if(auditInfo != null) {
        endpoint = (String)auditInfo.get(Constants.ENDPOINT_STRING);
    } else {
        endpoint = exchange.getRequestPath() + "@" + exchange.getRequestMethod();
    }

    HeaderMap headerMap = exchange.getRequestHeaders();
    final HashMap<String, String> headers = new HashMap<>();
    for(HttpString key : headerMap.getHeaderNames()) {
        headers.put(key.toString(), headerMap.getFirst(key));
    }
    TextMap carrier = new TextMapAdapter(headers);

    // start the server span.
    Tracer.SpanBuilder spanBuilder;
    try {
        SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, carrier);
        if (parentSpanCtx == null) {
            spanBuilder = tracer.buildSpan(endpoint);
        } else {
            spanBuilder = tracer.buildSpan(endpoint).asChildOf(parentSpanCtx);
        }
    } catch (IllegalArgumentException e) {
        spanBuilder = tracer.buildSpan(endpoint);
    }
    Span rootSpan = spanBuilder
            .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
            .withTag(Tags.PEER_HOSTNAME.getKey(), NetUtils.getLocalAddressByDatagram())
            .withTag(Tags.PEER_PORT.getKey(), Server.config.getHttpsPort())
            .start();
    tracer.activateSpan(rootSpan);
    // This can be retrieved in the business handler to add tags and logs for tracing.
    exchange.putAttachment(ROOT_SPAN, rootSpan);
    // The client module can use this to inject tracer.
    exchange.putAttachment(EXCHANGE_TRACER, tracer);

    // add an exchange complete listener to close the Root Span for the request.
    exchange.addExchangeCompleteListener((exchange1, nextListener) -> {
        Span span = exchange1.getAttachment(ROOT_SPAN);
        if(span != null) {
            span.finish();
        }
        nextListener.proceed();
    });

    Handler.next(exchange, next);
}
 
Example 6
Source File: UndertowHTTPDestinationTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void setUpDoService(boolean setRedirectURL,
                            boolean sendResponse,
                            boolean decoupled,
                            String method,
                            String query,
                            int status
                            ) throws Exception {

    is = EasyMock.createMock(ServletInputStream.class);
    os = EasyMock.createMock(ServletOutputStream.class);
    request = EasyMock.createMock(HttpServletRequest.class);
    response = EasyMock.createMock(HttpServletResponse.class);
    request.getMethod();
    EasyMock.expectLastCall().andReturn(method).atLeastOnce();
    request.getUserPrincipal();
    EasyMock.expectLastCall().andReturn(null).anyTimes();

    if (setRedirectURL) {
        policy.setRedirectURL(NOWHERE + "foo/bar");
        response.sendRedirect(EasyMock.eq(NOWHERE + "foo/bar"));
        EasyMock.expectLastCall();
        response.flushBuffer();
        EasyMock.expectLastCall();
        EasyMock.expectLastCall();
    } else {
        //getQueryString for if statement
        request.getQueryString();
        EasyMock.expectLastCall().andReturn(query);

        if ("GET".equals(method) && "?wsdl".equals(query)) {
            verifyGetWSDLQuery();
        } else { // test for the post
            EasyMock.expect(request.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE))
                .andReturn(null);


            EasyMock.expect(request.getInputStream()).andReturn(is);
            EasyMock.expect(request.getContextPath()).andReturn("/bar");
            EasyMock.expect(request.getServletPath()).andReturn("");
            EasyMock.expect(request.getPathInfo()).andReturn("/foo");
            EasyMock.expect(request.getRequestURI()).andReturn("/foo");
            EasyMock.expect(request.getRequestURL())
                .andReturn(new StringBuffer("http://localhost/foo")).anyTimes();
            EasyMock.expect(request.getCharacterEncoding()).andReturn(StandardCharsets.UTF_8.name());
            EasyMock.expect(request.getQueryString()).andReturn(query);
            EasyMock.expect(request.getHeader("Accept")).andReturn("*/*");
            EasyMock.expect(request.getContentType()).andReturn("text/xml charset=utf8").times(2);
            EasyMock.expect(request.getAttribute("http.service.redirection")).andReturn(null).anyTimes();

            HeaderMap httpFields = new HeaderMap();
            httpFields.add(new HttpString("content-type"), "text/xml");
            httpFields.add(new HttpString("content-type"), "charset=utf8");
            httpFields.put(new HttpString(UndertowHTTPDestinationTest.AUTH_HEADER),
                           UndertowHTTPDestinationTest.BASIC_AUTH);
            List<String> headers = new ArrayList<>();
            for (HttpString header : httpFields.getHeaderNames()) {
                headers.add(header.toString());
            }
            EasyMock.expect(request.getHeaderNames()).andReturn(Collections.enumeration(headers));
            request.getHeaders("content-type");
            EasyMock.expectLastCall().andReturn(Collections.enumeration(httpFields.get("content-type")));
            request.getHeaders(UndertowHTTPDestinationTest.AUTH_HEADER);
            EasyMock.expectLastCall().andReturn(Collections.enumeration(
                                                httpFields.get(UndertowHTTPDestinationTest.AUTH_HEADER)));

            EasyMock.expect(request.getInputStream()).andReturn(is);
            EasyMock.expectLastCall();
            response.flushBuffer();
            EasyMock.expectLastCall();
            if (sendResponse) {
                response.setStatus(status);
                EasyMock.expectLastCall();
                response.setContentType("text/xml charset=utf8");
                EasyMock.expectLastCall();
                response.addHeader(EasyMock.isA(String.class), EasyMock.isA(String.class));
                EasyMock.expectLastCall().anyTimes();
                response.setContentLength(0);
                EasyMock.expectLastCall().anyTimes();
                response.getOutputStream();
                EasyMock.expectLastCall().andReturn(os);
                response.getStatus();
                EasyMock.expectLastCall().andReturn(status).anyTimes();
                response.flushBuffer();
                EasyMock.expectLastCall();
            }
            request.getAttribute("javax.servlet.request.cipher_suite");
            EasyMock.expectLastCall().andReturn("anythingwilldoreally");
            request.getAttribute("javax.net.ssl.session");
            EasyMock.expectLastCall().andReturn(null);
            request.getAttribute("javax.servlet.request.X509Certificate");
            EasyMock.expectLastCall().andReturn(null);
        }
    }

    if (decoupled) {
        setupDecoupledBackChannel();
    }
    EasyMock.replay(response);
    EasyMock.replay(request);
}