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

The following examples show how to use io.undertow.util.HeaderMap#get() . 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: CorsHttpHandler.java    From light-4j with Apache License 2.0 6 votes vote down vote up
private void setCorsResponseHeaders(HttpServerExchange exchange) throws Exception {
    HeaderMap headers = exchange.getRequestHeaders();
    if (headers.contains(Headers.ORIGIN)) {
        if(matchOrigin(exchange, allowedOrigins) != null) {
            exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_ORIGIN, headers.get(Headers.ORIGIN));
            exchange.getResponseHeaders().add(Headers.VARY, Headers.ORIGIN_STRING);
        }
    }
    exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_METHODS, allowedMethods);
    HeaderValues requestedHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS);
    if (requestedHeaders != null && !requestedHeaders.isEmpty()) {
        exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_HEADERS, requestedHeaders);
    } else {
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.CONTENT_TYPE_STRING);
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.WWW_AUTHENTICATE_STRING);
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.AUTHORIZATION_STRING);
    }
    exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
    exchange.getResponseHeaders().add(ACCESS_CONTROL_MAX_AGE, ONE_HOUR_IN_SECONDS);
}
 
Example 2
Source File: RequestTimeLogger.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
    try {
        long end = System.currentTimeMillis();
        long duration = end - start;
        if (duration > this.timeThreshold) {
            String method = exchange.getRequestMethod().toString();
            String query = exchange.getQueryString();
            String request_url = exchange.getRequestURI() + (query.isEmpty() ? "" : ("?" + query));
            HeaderMap headers = exchange.getRequestHeaders();
            if (headers.contains(tenantHeader)) {
                String tenantId = headers.get(tenantHeader, 0);
                log.warnf("Request %s %s took: %d ms, exceeds %d ms threshold, tenant-id: %s",
                        method, request_url, duration, timeThreshold, tenantId);
            } else {
                log.warnf("Request %s %s took: %d ms, exceeds %d ms threshold, no tenant",
                        method, request_url, duration, timeThreshold);
            }

        }
    } finally {
        if (nextListener != null) {
            nextListener.proceed();
        }
    }
}
 
Example 3
Source File: CorsHttpHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void setCorsResponseHeaders(HttpServerExchange exchange) throws Exception {
    HeaderMap headers = exchange.getRequestHeaders();
    if (headers.contains(Headers.ORIGIN)) {
        if(matchOrigin(exchange, allowedOrigins) != null) {
            exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_ORIGIN, headers.get(Headers.ORIGIN));
            exchange.getResponseHeaders().add(Headers.VARY, Headers.ORIGIN_STRING);
        }
    }
    HeaderValues requestedMethods = headers.get(ACCESS_CONTROL_REQUEST_METHOD);
    if (requestedMethods != null && !requestedMethods.isEmpty()) {
        exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_METHODS, requestedMethods);
    } else {
        exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_METHODS, Arrays.asList(new String[]{Methods.GET_STRING, Methods.POST_STRING}));
    }
    HeaderValues requestedHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS);
    if (requestedHeaders != null && !requestedHeaders.isEmpty()) {
        exchange.getResponseHeaders().addAll(ACCESS_CONTROL_ALLOW_HEADERS, requestedHeaders);
    } else {
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.CONTENT_TYPE_STRING);
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.WWW_AUTHENTICATE_STRING);
        exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_HEADERS, Headers.AUTHORIZATION_STRING);
    }
    exchange.getResponseHeaders().add(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
    exchange.getResponseHeaders().add(ACCESS_CONTROL_MAX_AGE, ONE_HOUR_IN_SECONDS);
}
 
Example 4
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 5
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 6
Source File: DatawaveAuthenticationMechanism.java    From datawave with Apache License 2.0 5 votes vote down vote up
private String getSingleHeader(HeaderMap headers, String headerName) throws MultipleHeaderException {
    String value = null;
    HeaderValues values = (headers == null) ? null : headers.get(headerName);
    if (values != null) {
        if (values.size() > 1)
            throw new MultipleHeaderException(headerName + " was specified multiple times, which is not allowed!");
        value = values.getFirst();
    }
    return value;
}
 
Example 7
Source File: Connectors.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies that the provided request headers are valid according to rfc7230. In particular:
 * - At most one content-length or transfer encoding
 */
public static boolean areRequestHeadersValid(HeaderMap headers) {
    HeaderValues te = headers.get(Headers.TRANSFER_ENCODING);
    HeaderValues cl = headers.get(Headers.CONTENT_LENGTH);
    if(te != null && cl != null) {
        return false;
    } else if(te != null && te.size() > 1) {
        return false;
    } else if(cl != null && cl.size() > 1) {
        return false;
    }
    return true;
}
 
Example 8
Source File: Http2ReceiveListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs HTTP2 specification compliance check for headers and pseudo-headers of a current request.
 *
 * @param headers map of the request headers
 * @return true if check was successful, false otherwise
 */
private boolean checkRequestHeaders(HeaderMap headers) {
    // :method pseudo-header must be present always exactly one time;
    // HTTP2 request MUST NOT contain 'connection' header
    if (headers.count(METHOD) != 1 || headers.contains(Headers.CONNECTION)) {
        return false;
    }

    // if CONNECT type is used, then we expect :method and :authority to be present only;
    // :scheme and :path must not be present
    if (headers.get(METHOD).contains(Methods.CONNECT_STRING)) {
        if (headers.contains(SCHEME) || headers.contains(PATH) || headers.count(AUTHORITY) != 1) {
            return false;
        }
    // For other HTTP methods we expect that :scheme, :method, and :path pseudo-headers are
    // present exactly one time.
    } else if (headers.count(SCHEME) != 1 || headers.count(PATH) != 1) {
        return false;
    }

    // HTTP2 request MAY contain TE header but if so, then only with 'trailers' value.
    if (headers.contains(Headers.TE)) {
        for (String value : headers.get(Headers.TE)) {
            if (!value.equals("trailers")) {
                return false;
            }
        }
    }

    return true;
}
 
Example 9
Source File: HttpContinue.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static boolean requiresContinueResponse(HeaderMap requestHeaders) {
    List<String> expect = requestHeaders.get(Headers.EXPECT);
    if (expect != null) {
        for (String header : expect) {
            if (header.equalsIgnoreCase(CONTINUE)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 10
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 11
Source File: LimitHandler.java    From mangooio with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a key for used for limit an request containing the
 * requested url and the source host
 * 
 * @param exchange The HttpServerExchange
 * @return The key url + host
 */
private String getCacheKey(HttpServerExchange exchange) {
    String host = "";

    HeaderMap headerMap = exchange.getRequestHeaders();
    if (headerMap != null) {
        HeaderValues headerValues = headerMap.get(Header.X_FORWARDED_FOR.toHttpString());
        if (headerValues != null) {
            host = headerValues.element();
        }
    }
    
    if (StringUtils.isBlank(host)) {
        InetSocketAddress inetSocketAddress = exchange.getSourceAddress();
        if (inetSocketAddress != null) {
            host = inetSocketAddress.getHostString();
        }
    }
    
    if (StringUtils.isNotBlank(host)) {
        host = host.toLowerCase(Locale.ENGLISH);
    }
    
    String url = exchange.getRequestURL();
    if (StringUtils.isNotBlank(url)) {
        url = url.toLowerCase(Locale.ENGLISH);
    }
    
    return url + host;
}
 
Example 12
Source File: RequestUtils.java    From mangooio with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the requests content-type contains application/json
 *
 * @param exchange The Undertow HttpServerExchange
 * @return True if the request content-type contains application/json, false otherwise
 */
public static boolean isJsonRequest(HttpServerExchange exchange) {
    Objects.requireNonNull(exchange, Required.HTTP_SERVER_EXCHANGE.toString());

    final HeaderMap headerMap = exchange.getRequestHeaders();
    return headerMap != null && headerMap.get(Header.CONTENT_TYPE.toHttpString()) != null &&
            headerMap.get(Header.CONTENT_TYPE.toHttpString()).element().toLowerCase(Locale.ENGLISH).contains(MediaType.JSON_UTF_8.withoutParameters().toString());
}