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;
}
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;
}
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);
}
}
}
Aggregations