Search in sources :

Example 21 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project cdap by caskdata.

the class RouterPathTest method testRouterFeedsLookup.

@Test
public void testRouterFeedsLookup() {
    final String namespacePath = "/v3//feeds/test";
    HttpRequest httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("PUT"), namespacePath);
    RouteDestination result = pathLookup.getRoutingService(FALLBACKSERVICE, namespacePath, httpRequest);
    Assert.assertEquals(null, result);
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 22 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project cdap by caskdata.

the class RouterPathTest method testAppFabricPath.

@Test
public void testAppFabricPath() throws Exception {
    // Default destination for URIs will APP_FABRIC_HTTP
    String path = "/v3/ping/";
    HttpRequest httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), path);
    RouteDestination result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
    Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
    path = "/status";
    httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), path);
    result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
    Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
    path = "/v3/monitor///abcd/";
    httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("POST"), path);
    result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
    Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 23 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project cdap by caskdata.

the class RouterPathTest method testRouterDeployPathLookUp.

@Test
public void testRouterDeployPathLookUp() throws Exception {
    String path = "/v3/namespaces/default//apps/";
    HttpRequest httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("PUT"), path);
    RouteDestination result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
    Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 24 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project cdap by caskdata.

the class RouterAuditLookUp method createMatcher.

private int createMatcher() {
    List<ClassPath.ClassInfo> handlerClasses;
    try {
        handlerClasses = getAllHandlerClasses();
    } catch (IOException e) {
        LOG.error("Failed to get all handler classes for audit logging: {}", e.getCause());
        return -1;
    }
    int count = 0;
    for (ClassPath.ClassInfo classInfo : handlerClasses) {
        Class<?> handlerClass = classInfo.load();
        Path classPath = handlerClass.getAnnotation(Path.class);
        String classPathStr = classPath == null ? "" : classPath.value();
        for (Method method : handlerClass.getMethods()) {
            Path methodPath = method.getAnnotation(Path.class);
            AuditPolicy auditPolicy = method.getAnnotation(AuditPolicy.class);
            HttpMethod httpMethod = getHttpMethod(method);
            if (methodPath == null || auditPolicy == null || httpMethod == null) {
                continue;
            }
            String methodPathStr = methodPath.value();
            String completePath = classPathStr.endsWith("/") || methodPathStr.startsWith("/") ? classPathStr + methodPathStr : classPathStr + "/" + methodPathStr;
            List<AuditDetail> auditContents = Arrays.asList(auditPolicy.value());
            List<String> headerNames = new ArrayList<>();
            if (auditContents.contains(AuditDetail.HEADERS)) {
                Annotation[][] annotations = method.getParameterAnnotations();
                for (Annotation[] annotationArr : annotations) {
                    if (annotationArr.length > 0) {
                        for (Annotation annotation : annotationArr) {
                            if (annotation instanceof HeaderParam) {
                                headerNames.add(((HeaderParam) annotation).value());
                            }
                        }
                    }
                }
            }
            AuditLogConfig auditLogConfig = new AuditLogConfig(httpMethod, auditContents.contains(AuditDetail.REQUEST_BODY), auditContents.contains(AuditDetail.RESPONSE_BODY), headerNames);
            LOG.trace("Audit log lookup: bootstrapped with path: {}", completePath);
            patternMatcher.add(completePath, auditLogConfig);
            // Don't count classes in unit-tests
            if (!isTestClass(classInfo)) {
                count++;
            }
        }
    }
    LOG.debug("Audit log lookup: bootstrapped with {} paths", count);
    return count;
}
Also used : Path(javax.ws.rs.Path) ClassPath(co.cask.cdap.common.internal.guava.ClassPath) ClassPath(co.cask.cdap.common.internal.guava.ClassPath) HeaderParam(javax.ws.rs.HeaderParam) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Method(java.lang.reflect.Method) HttpMethod(io.netty.handler.codec.http.HttpMethod) Annotation(java.lang.annotation.Annotation) AuditLogConfig(co.cask.cdap.common.logging.AuditLogConfig) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) AuditDetail(co.cask.cdap.common.security.AuditDetail) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 25 with HttpMethod

use of io.netty.handler.codec.http.HttpMethod in project spring-cloud-gateway by spring-cloud.

the class NettyRoutingFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    String scheme = requestUrl.getScheme();
    if (isAlreadyRouted(exchange) || (!"http".equals(scheme) && !"https".equals(scheme))) {
        return chain.filter(exchange);
    }
    setAlreadyRouted(exchange);
    ServerHttpRequest request = exchange.getRequest();
    final HttpMethod method = HttpMethod.valueOf(request.getMethod().toString());
    final String url = requestUrl.toString();
    HttpHeaders filtered = filterRequest(this.headersFilters.getIfAvailable(), exchange);
    final DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
    filtered.forEach(httpHeaders::set);
    String transferEncoding = request.getHeaders().getFirst(HttpHeaders.TRANSFER_ENCODING);
    boolean chunkedTransfer = "chunked".equalsIgnoreCase(transferEncoding);
    boolean preserveHost = exchange.getAttributeOrDefault(PRESERVE_HOST_HEADER_ATTRIBUTE, false);
    return this.httpClient.request(method, url, req -> {
        final HttpClientRequest proxyRequest = req.options(NettyPipeline.SendOptions::flushOnEach).headers(httpHeaders).chunkedTransfer(chunkedTransfer).failOnServerError(false).failOnClientError(false);
        if (preserveHost) {
            String host = request.getHeaders().getFirst(HttpHeaders.HOST);
            proxyRequest.header(HttpHeaders.HOST, host);
        }
        return // I shouldn't need this
        proxyRequest.sendHeaders().send(request.getBody().map(dataBuffer -> ((NettyDataBuffer) dataBuffer).getNativeBuffer()));
    }).doOnNext(res -> {
        ServerHttpResponse response = exchange.getResponse();
        // put headers and status so filters can modify the response
        HttpHeaders headers = new HttpHeaders();
        res.responseHeaders().forEach(entry -> headers.add(entry.getKey(), entry.getValue()));
        HttpHeaders filteredResponseHeaders = HttpHeadersFilter.filter(this.headersFilters.getIfAvailable(), headers, exchange, Type.RESPONSE);
        response.getHeaders().putAll(filteredResponseHeaders);
        response.setStatusCode(HttpStatus.valueOf(res.status().code()));
        // Defer committing the response until all route filters have run
        // Put client response as ServerWebExchange attribute and write response later NettyWriteResponseFilter
        exchange.getAttributes().put(CLIENT_RESPONSE_ATTR, res);
    }).then(chain.filter(exchange));
}
Also used : HttpHeadersFilter.filterRequest(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.filterRequest) CLIENT_RESPONSE_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR) Ordered(org.springframework.core.Ordered) PRESERVE_HOST_HEADER_ATTRIBUTE(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.PRESERVE_HOST_HEADER_ATTRIBUTE) ServerWebExchangeUtils.setAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Type(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter.Type) HttpHeadersFilter(org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter) HttpMethod(io.netty.handler.codec.http.HttpMethod) Mono(reactor.core.publisher.Mono) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) ObjectProvider(org.springframework.beans.factory.ObjectProvider) NettyPipeline(reactor.ipc.netty.NettyPipeline) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) GATEWAY_REQUEST_URL_ATTR(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpClientRequest(reactor.ipc.netty.http.client.HttpClientRequest) URI(java.net.URI) ServerWebExchangeUtils.isAlreadyRouted(org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) URI(java.net.URI) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) HttpClientRequest(reactor.ipc.netty.http.client.HttpClientRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)95 Test (org.junit.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)28 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)21 URI (java.net.URI)15 IOException (java.io.IOException)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)11 HttpVersion (io.netty.handler.codec.http.HttpVersion)11 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 Map (java.util.Map)9