Skip to content

Commit

Permalink
ISPN-9688 Move existing REST resources as ResourceHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Fernandes authored and galderz committed Nov 13, 2018
1 parent d1a6abf commit cd3a25e
Show file tree
Hide file tree
Showing 33 changed files with 1,062 additions and 1,458 deletions.
@@ -0,0 +1,36 @@
package org.infinispan.rest;

import static java.nio.charset.StandardCharsets.UTF_8;

import org.infinispan.rest.framework.ContentSource;

import io.netty.buffer.ByteBuf;

public class ByteBufContentSource implements ContentSource {

private final ByteBuf byteBuf;

ByteBufContentSource(ByteBuf byteBuf) {
this.byteBuf = byteBuf;
}

@Override
public String asString() {
return byteBuf.toString(UTF_8);
}

@Override
public byte[] rawContent() {
if (byteBuf != null) {
if (byteBuf.hasArray()) {
return byteBuf.array();
} else {
byte[] bufferCopy = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bufferCopy);
return bufferCopy;
}
}
return null;
}

}
Expand Up @@ -31,8 +31,8 @@
*/
@ChannelHandler.Sharable
public class Http11To2UpgradeHandler extends ApplicationProtocolNegotiationHandler {
protected static final int MAX_INITIAL_LINE_SIZE = 4096;
protected static final int MAX_HEADER_SIZE = 8192;
private static final int MAX_INITIAL_LINE_SIZE = 4096;
private static final int MAX_HEADER_SIZE = 8192;

protected final RestServer restServer;
private final List<CorsConfig> corsRules;
Expand All @@ -44,7 +44,7 @@ public Http11To2UpgradeHandler(RestServer restServer) {
}

@Override
public void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
public void configurePipeline(ChannelHandlerContext ctx, String protocol) {
configurePipeline(ctx.pipeline(), protocol);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ protected int maxContentLength() {
*
* @return new instance of {@link HttpToHttp2ConnectionHandler}.
*/
protected HttpToHttp2ConnectionHandler getHttp11To2ConnectionHandler() {
private HttpToHttp2ConnectionHandler getHttp11To2ConnectionHandler() {
DefaultHttp2Connection connection = new DefaultHttp2Connection(true);

InboundHttp2ToHttpAdapter listener = new InboundHttp2ToHttpAdapterBuilder(connection)
Expand Down Expand Up @@ -126,7 +126,7 @@ public ChannelHandler getHttp1Handler() {
*
* @return HTTP/2 handler.
*/
public ChannelHandler getHttp2Handler() {
private ChannelHandler getHttp2Handler() {
return new Http20RequestHandler(restServer);
}

Expand Down
Expand Up @@ -3,9 +3,10 @@
import static io.netty.handler.codec.http.HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

import org.infinispan.rest.authentication.AuthenticationException;
import org.infinispan.rest.authentication.Authenticator;
import org.infinispan.rest.configuration.RestServerConfiguration;
import org.infinispan.rest.context.WrongContextException;
import org.infinispan.rest.framework.RestResponse;
import org.infinispan.rest.logging.Log;
import org.infinispan.rest.logging.RestAccessLoggingHandler;
import org.infinispan.util.logging.LogFactory;
Expand All @@ -18,6 +19,12 @@
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http2.HttpConversionUtil;

/**
* Netty REST handler for HTTP/2.0
Expand Down Expand Up @@ -46,24 +53,40 @@ public Http20RequestHandler(RestServer restServer) {

@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
InfinispanResponse response;
InfinispanRequest infinispanRequest = null;
RestResponse response;
NettyRestRequest restRequest = new NettyRestRequest(request);
try {
restAccessLoggingHandler.preLog(request);
infinispanRequest = InfinispanRequestFactory.createRequest(restServer, request, ctx);
this.checkContext(infinispanRequest);
authenticator.challenge(infinispanRequest);
response = infinispanRequest.execute();
authenticator.challenge(restRequest, ctx);
response = restServer.getRestDispatcher().dispatch(restRequest);
if (response == null) {
response = new NettyRestResponse.Builder().status(HttpResponseStatus.NOT_FOUND).build();
}
addCorrelatedHeaders(request, ((NettyRestResponse) response).getResponse());
} catch (AuthenticationException authException) {
response = new NettyRestResponse.Builder()
.status(HttpResponseStatus.UNAUTHORIZED)
.authenticate(authException.getAuthenticationHeader())
.build();

} catch (RestResponseException responseException) {
logger.errorWhileResponding(responseException);
response = responseException.toResponse(infinispanRequest);
response = new NettyRestResponse.Builder().status(responseException.getStatus()).entity(responseException.getText()).build();
}
sendResponse(ctx, request, response.toNettyHttpResponse());

NettyRestResponse nettyRestResponse = (NettyRestResponse) response;
sendResponse(ctx, request, nettyRestResponse.getResponse());
}

private void checkContext(InfinispanRequest infinispanRequest) {
if (configuration.startTransport() && !infinispanRequest.getContext().equals(configuration.contextPath())) {
throw new WrongContextException();
private void addCorrelatedHeaders(FullHttpRequest request, FullHttpResponse response) {
String streamId = request.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
if (streamId != null) {
response.headers().add(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
}
boolean isKeepAlive = HttpUtil.isKeepAlive(request);
HttpVersion httpVersion = response.protocolVersion();
if ((httpVersion == HttpVersion.HTTP_1_1 || httpVersion == HttpVersion.HTTP_1_0) && isKeepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
}

Expand All @@ -75,7 +98,7 @@ protected void sendResponse(ChannelHandlerContext ctx, FullHttpRequest request,
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
// handle the case of to big requests.
if (e.getCause() instanceof TooLongFrameException) {
DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, REQUEST_ENTITY_TOO_LARGE);
Expand Down

This file was deleted.

0 comments on commit cd3a25e

Please sign in to comment.