Search in sources :

Example 1 with NettyInputStream

use of org.glassfish.jersey.netty.connector.internal.NettyInputStream in project jersey by jersey.

the class JerseyHttp2ServerHandler method createContainerRequest.

/**
     * Create Jersey {@link ContainerRequest} based on Netty {@link HttpRequest}.
     *
     * @param ctx          Netty channel context.
     * @param http2Headers Netty Http/2 headers.
     * @return created Jersey Container Request.
     */
private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, Http2HeadersFrame http2Headers) {
    String path = http2Headers.headers().path().toString();
    String s = path.startsWith("/") ? path.substring(1) : path;
    URI requestUri = URI.create(baseUri + ContainerUtils.encodeUnsafeCharacters(s));
    ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, http2Headers.headers().method().toString(), getSecurityContext(), new PropertiesDelegate() {

        private final Map<String, Object> properties = new HashMap<>();

        @Override
        public Object getProperty(String name) {
            return properties.get(name);
        }

        @Override
        public Collection<String> getPropertyNames() {
            return properties.keySet();
        }

        @Override
        public void setProperty(String name, Object object) {
            properties.put(name, object);
        }

        @Override
        public void removeProperty(String name) {
            properties.remove(name);
        }
    });
    // request entity handling.
    if (!http2Headers.isEndStream()) {
        ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                isList.add(NettyInputStream.END_OF_INPUT_ERROR);
            }
        });
        requestContext.setEntityStream(new NettyInputStream(isList));
    } else {
        requestContext.setEntityStream(new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        });
    }
    // copying headers from netty request to jersey container request context.
    for (CharSequence name : http2Headers.headers().names()) {
        requestContext.headers(name.toString(), mapToString(http2Headers.headers().getAll(name)));
    }
    return requestContext;
}
Also used : HashMap(java.util.HashMap) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) URI(java.net.URI) IOException(java.io.IOException) Collection(java.util.Collection) Future(io.netty.util.concurrent.Future) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PropertiesDelegate(org.glassfish.jersey.internal.PropertiesDelegate)

Example 2 with NettyInputStream

use of org.glassfish.jersey.netty.connector.internal.NettyInputStream in project jersey by jersey.

the class JerseyServerHandler method createContainerRequest.

/**
     * Create Jersey {@link ContainerRequest} based on Netty {@link HttpRequest}.
     *
     * @param ctx Netty channel context.
     * @param req Netty Http request.
     * @return created Jersey Container Request.
     */
private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, HttpRequest req) {
    String s = req.uri().startsWith("/") ? req.uri().substring(1) : req.uri();
    URI requestUri = URI.create(baseUri + ContainerUtils.encodeUnsafeCharacters(s));
    ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri, req.method().name(), getSecurityContext(), new PropertiesDelegate() {

        private final Map<String, Object> properties = new HashMap<>();

        @Override
        public Object getProperty(String name) {
            return properties.get(name);
        }

        @Override
        public Collection<String> getPropertyNames() {
            return properties.keySet();
        }

        @Override
        public void setProperty(String name, Object object) {
            properties.put(name, object);
        }

        @Override
        public void removeProperty(String name) {
            properties.remove(name);
        }
    });
    // request entity handling.
    if ((req.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(req) > 0) || HttpUtil.isTransferEncodingChunked(req)) {
        ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                isList.add(NettyInputStream.END_OF_INPUT_ERROR);
            }
        });
        requestContext.setEntityStream(new NettyInputStream(isList));
    } else {
        requestContext.setEntityStream(new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        });
    }
    // copying headers from netty request to jersey container request context.
    for (String name : req.headers().names()) {
        requestContext.headers(name, req.headers().getAll(name));
    }
    return requestContext;
}
Also used : HashMap(java.util.HashMap) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) URI(java.net.URI) IOException(java.io.IOException) Collection(java.util.Collection) Future(io.netty.util.concurrent.Future) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PropertiesDelegate(org.glassfish.jersey.internal.PropertiesDelegate)

Example 3 with NettyInputStream

use of org.glassfish.jersey.netty.connector.internal.NettyInputStream in project jersey by jersey.

the class JerseyClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        final HttpResponse response = (HttpResponse) msg;
        final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() {

            @Override
            public int getStatusCode() {
                return response.status().code();
            }

            @Override
            public Response.Status.Family getFamily() {
                return Response.Status.Family.familyOf(response.status().code());
            }

            @Override
            public String getReasonPhrase() {
                return response.status().reasonPhrase();
            }
        }, jerseyRequest);
        for (Map.Entry<String, String> entry : response.headers().entries()) {
            jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue());
        }
        // request entity handling.
        if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) {
            ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() {

                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    isList.add(NettyInputStream.END_OF_INPUT_ERROR);
                }
            });
            jerseyResponse.setEntityStream(new NettyInputStream(isList));
        } else {
            jerseyResponse.setEntityStream(new InputStream() {

                @Override
                public int read() throws IOException {
                    return -1;
                }
            });
        }
        if (asyncConnectorCallback != null) {
            connector.executorService.execute(new Runnable() {

                @Override
                public void run() {
                    asyncConnectorCallback.response(jerseyResponse);
                    future.complete(jerseyResponse);
                }
            });
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            // copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all
            // relates ByteBuffs.
            byte[] bytes = new byte[content.readableBytes()];
            content.getBytes(content.readerIndex(), bytes);
            isList.add(new ByteArrayInputStream(bytes));
        }
        if (msg instanceof LastHttpContent) {
            isList.add(NettyInputStream.END_OF_INPUT);
        }
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) InputStream(java.io.InputStream) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) NettyInputStream(org.glassfish.jersey.netty.connector.internal.NettyInputStream) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) IOException(java.io.IOException) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Aggregations

Future (io.netty.util.concurrent.Future)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 NettyInputStream (org.glassfish.jersey.netty.connector.internal.NettyInputStream)3 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)2 URI (java.net.URI)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 PropertiesDelegate (org.glassfish.jersey.internal.PropertiesDelegate)2 ContainerRequest (org.glassfish.jersey.server.ContainerRequest)2 ByteBuf (io.netty.buffer.ByteBuf)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Response (javax.ws.rs.core.Response)1 ClientResponse (org.glassfish.jersey.client.ClientResponse)1