Search in sources :

Example 1 with CompletionStageRxInvoker

use of javax.ws.rs.client.CompletionStageRxInvoker in project jersey by jersey.

the class JerseyCompletionStageRxInvokerTest method testNotFoundResponse.

@Test
public void testNotFoundResponse() throws Exception {
    CompletionStageRxInvoker invoker = client.target("http://jersey.java.net").request().header("Response-Status", 404).rx();
    testInvoker(invoker, 404, false);
}
Also used : CompletionStageRxInvoker(javax.ws.rs.client.CompletionStageRxInvoker) Test(org.junit.Test)

Example 2 with CompletionStageRxInvoker

use of javax.ws.rs.client.CompletionStageRxInvoker in project meecrowave by apache.

the class ProxyServlet method doRequest.

protected CompletionStage<Response> doRequest(final Routes.Route route, final HttpServletRequest req, final HttpServletResponse response, final String prefix) throws IOException {
    WebTarget target = route.client.target(route.responseConfiguration.target);
    target = target.path(prefix);
    final Map<String, String> queryParams = ofNullable(req.getQueryString()).filter(it -> !it.isEmpty()).map(queries -> Stream.of(queries.split("&")).map(it -> {
        final int eq = it.indexOf('=');
        if (eq > 0) {
            return new AbstractMap.SimpleEntry<>(it.substring(0, eq), it.substring(eq + 1));
        }
        return new AbstractMap.SimpleEntry<>(it, "");
    }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).orElseGet(Collections::emptyMap);
    for (final Map.Entry<String, String> q : queryParams.entrySet()) {
        target = target.queryParam(q.getKey(), q.getValue());
    }
    final String type = route.requestConfiguration.addedHeaders != null ? route.requestConfiguration.addedHeaders.getOrDefault("Content-Type", req.getContentType()) : req.getContentType();
    Invocation.Builder request = type != null ? target.request(type) : target.request();
    final Enumeration<String> headerNames = req.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        final String name = headerNames.nextElement();
        if (!filterHeader(route.requestConfiguration.skippedHeaders, name)) {
            request = request.header(name, list(req.getHeaders(name)));
        }
    }
    if (route.requestConfiguration.addedHeaders != null) {
        for (final Map.Entry<String, String> header : route.requestConfiguration.addedHeaders.entrySet()) {
            request = request.header(header.getKey(), header.getValue());
        }
    }
    final Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            if (filterCookie(route.requestConfiguration.skippedCookies, cookie.getName(), cookie.getValue())) {
                continue;
            }
            request = request.cookie(new javax.ws.rs.core.Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getVersion()));
        }
    }
    final CompletionStageRxInvoker rx = request.rx();
    final CompletionStage<Response> result;
    if (isWrite(req)) {
        result = rx.method(req.getMethod(), Entity.entity(req.getInputStream(), ofNullable(req.getContentType()).orElse(MediaType.WILDCARD)));
    } else {
        result = rx.method(req.getMethod());
    }
    return result;
}
Also used : Optional.empty(java.util.Optional.empty) Collections.list(java.util.Collections.list) Routes(org.apache.meecrowave.proxy.servlet.configuration.Routes) Enumeration(java.util.Enumeration) ServletException(javax.servlet.ServletException) ConfigurationLoader(org.apache.meecrowave.proxy.servlet.service.ConfigurationLoader) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) HttpMethod(javax.ws.rs.HttpMethod) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Collectors.toMap(java.util.stream.Collectors.toMap) ServletOutputStream(javax.servlet.ServletOutputStream) Map(java.util.Map) Cookie(javax.servlet.http.Cookie) PrintWriter(java.io.PrintWriter) ServletConfig(javax.servlet.ServletConfig) HttpServlet(javax.servlet.http.HttpServlet) Optional.ofNullable(java.util.Optional.ofNullable) CompletionStageRxInvoker(javax.ws.rs.client.CompletionStageRxInvoker) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Invocation(javax.ws.rs.client.Invocation) Entity(javax.ws.rs.client.Entity) AbstractMap(java.util.AbstractMap) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) InputStream(java.io.InputStream) NewCookie(javax.ws.rs.core.NewCookie) Cookie(javax.servlet.http.Cookie) HttpHeaders(javax.ws.rs.core.HttpHeaders) Invocation(javax.ws.rs.client.Invocation) AbstractMap(java.util.AbstractMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Collections(java.util.Collections) CompletionStageRxInvoker(javax.ws.rs.client.CompletionStageRxInvoker) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Aggregations

CompletionStageRxInvoker (javax.ws.rs.client.CompletionStageRxInvoker)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 AbstractMap (java.util.AbstractMap)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.list (java.util.Collections.list)1 Enumeration (java.util.Enumeration)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.ofNullable (java.util.Optional.ofNullable)1 CompletionStage (java.util.concurrent.CompletionStage)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 Function (java.util.function.Function)1 Function.identity (java.util.function.Function.identity)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Stream (java.util.stream.Stream)1 AsyncContext (javax.servlet.AsyncContext)1