Skip to content

Commit

Permalink
Merge pull request #78 from victornoel/76-webactors-public-httpreques…
Browse files Browse the repository at this point in the history
…t-impl

Make HttpRequest implementation publicly usable
  • Loading branch information
pron authored Oct 10, 2016
2 parents d4b2e7e + 4934ca1 commit f413328
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected AutoContext newActorContext(FullHttpRequest req) {
}

private String getSessionId(FullHttpRequest req) {
final Set<Cookie> cookies = HttpRequestWrapper.getNettyCookies(req);
final Set<Cookie> cookies = NettyHttpRequest.getNettyCookies(req);
if (cookies != null) {
for (final Cookie c : cookies) {
if (c != null && SESSION_COOKIE_KEY.equals(c.name()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* @author circlespainter
*/
final class HttpRequestWrapper extends HttpRequest {
public final class NettyHttpRequest extends HttpRequest {
public static final String CHARSET_MARKER_STRING = "charset=";

final ActorRef<? super HttpResponse> actorRef;
Expand All @@ -65,7 +65,7 @@ final class HttpRequestWrapper extends HttpRequest {
private Charset encoding;
private String contentType;

public HttpRequestWrapper(ActorRef<? super HttpResponse> actorRef, ChannelHandlerContext ctx, FullHttpRequest req, String sessionId) {
public NettyHttpRequest(ActorRef<? super HttpResponse> actorRef, ChannelHandlerContext ctx, FullHttpRequest req, String sessionId) {
this.actorRef = actorRef;
this.ctx = ctx;
this.req = req;
Expand Down Expand Up @@ -266,6 +266,14 @@ public final String getSessionId() {
return sessionId;
}

public ChannelHandlerContext getContext() {
return ctx;
}

public FullHttpRequest getRequest() {
return req;
}

private String decodeStringBody() {
if (reqContent != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
addActorToContextAndUnlock(actorCtx, internalActor, lock);
}
//noinspection unchecked
((HttpActorAdapter) internalActor).handleRequest(new HttpRequestWrapper(internalActor.ref(), ctx, req, sessionId));
((HttpActorAdapter) internalActor).handleRequest(new NettyHttpRequest(internalActor.ref(), ctx, req, sessionId));
return;
}
}
Expand Down Expand Up @@ -489,7 +489,7 @@ final boolean handleLifecycle(LifecycleMessage l) {
return false;
}

final void handleRequest(HttpRequestWrapper s) throws SuspendExecution, InterruptedException {
final void handleRequest(NettyHttpRequest s) throws SuspendExecution, InterruptedException {
blockSessionRequests();

ctx = s.ctx;
Expand All @@ -506,7 +506,7 @@ final void handleRequest(HttpRequestWrapper s) throws SuspendExecution, Interrup
@Suspendable
final void handleReply(HttpResponse message) throws SuspendExecution, InterruptedException {
try {
final HttpRequestWrapper nettyRequest = (HttpRequestWrapper) message.getRequest();
final NettyHttpRequest nettyRequest = (NettyHttpRequest) message.getRequest();
final FullHttpRequest req = nettyRequest.req;
final ChannelHandlerContext ctx = nettyRequest.ctx;
final String sessionId = nettyRequest.getSessionId();
Expand Down Expand Up @@ -788,7 +788,7 @@ private static final class HttpStreamChannelAdapter implements SendPort<WebDataM

public HttpStreamChannelAdapter(ChannelHandlerContext ctx, FullHttpRequest req) {
this.ctx = ctx;
this.encoding = HttpRequestWrapper.extractCharacterEncodingOrDefault(req.headers());
this.encoding = NettyHttpRequest.extractCharacterEncodingOrDefault(req.headers());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void httpHeaderCaseInsensitivity() {
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "uri", new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT));
String headerValue = "application/json";
httpRequest.headers().add("Content-Type", headerValue);
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(null, null, httpRequest, "sessionId");
NettyHttpRequest requestWrapper = new NettyHttpRequest(null, null, httpRequest, "sessionId");
assertEquals(headerValue, requestWrapper.getHeader("content-type"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/**
* Wraps a {@link HttpServletRequest} as a {@link HttpRequest}
*/
final class HttpRequestWrapper extends HttpRequest {
public final class ServletHttpRequest extends HttpRequest {
final HttpServletRequest request;
final HttpServletResponse response;

Expand All @@ -61,12 +61,20 @@ final class HttpRequestWrapper extends HttpRequest {
* @param request the {@link HttpServletRequest}
* @param response the {@link HttpServletResponse}
*/
public HttpRequestWrapper(ActorRef<? super HttpResponse> sender, HttpServletRequest request, HttpServletResponse response) {
public ServletHttpRequest(ActorRef<? super HttpResponse> sender, HttpServletRequest request, HttpServletResponse response) {
this.sender = sender;
this.request = request;
this.response = response;
}

public HttpServletRequest getServletRequest() {
return request;
}

public HttpServletResponse getServletResponse() {
return response;
}

@Override
public final String getSourceHost() {
return request.getRemoteHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ final void handleRequest(final AsyncContext asyncCtx, final HttpServletRequest r
this.res = resp;

//noinspection unchecked
userWebActorRef.send(new HttpRequestWrapper(ref(), req, resp));
userWebActorRef.send(new ServletHttpRequest(ref(), req, resp));
}

@Suspendable
Expand Down Expand Up @@ -415,7 +415,7 @@ private void possiblyReplyDeadAndUnblock(final Throwable cause) throws IOExcepti
final void handleReply(final HttpResponse msg) throws SuspendExecution {
HttpServletRequest request;
try {
request = ((HttpRequestWrapper) msg.getRequest()).request;
request = ((ServletHttpRequest) msg.getRequest()).request;

final AsyncContext ctx = request.getAsyncContext();
final HttpServletResponse response = (HttpServletResponse) ctx.getResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void httpHeaderCaseInsensitivity() {
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
String headerValue = "application/json";
mockRequest.addHeader("Authorization", headerValue);
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(null, mockRequest, null);
ServletHttpRequest requestWrapper = new ServletHttpRequest(null, mockRequest, null);
assertEquals(headerValue, requestWrapper.getHeader("authorization"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
/**
* @author circlespainter
*/
final class HttpRequestWrapper extends HttpRequest {
public final class UndertowHttpRequest extends HttpRequest {
final ActorRef<? super HttpResponse> actorRef;
final HttpServerExchange xch;
private final ByteBuffer reqContent;
Expand All @@ -51,12 +51,16 @@ final class HttpRequestWrapper extends HttpRequest {
private String contentType;
private Charset encoding;

public HttpRequestWrapper(ActorRef<? super HttpResponse> actorRef, HttpServerExchange xch, ByteBuffer reqContent) {
public UndertowHttpRequest(ActorRef<? super HttpResponse> actorRef, HttpServerExchange xch, ByteBuffer reqContent) {
this.actorRef = actorRef;
this.xch = xch;
this.reqContent = reqContent;
}

public HttpServerExchange getServerExchange() {
return xch;
}

static ImmutableListMultimap<String, String> extractHeaders(HeaderMap headers) {
if (headers != null) {
final ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void run() {
protected final void byteArrayDone(final byte[] ba) {
try {
// adapter.ch.send(new HttpRequestWrapper(adapter.ref(), xch, ByteBuffer.wrap(ba)));
adapter.handleRequest(new HttpRequestWrapper(adapter.ref(), xch, ByteBuffer.wrap(ba)));
adapter.handleRequest(new UndertowHttpRequest(adapter.ref(), xch, ByteBuffer.wrap(ba)));
} catch (final SuspendExecution e) {
throw new AssertionError(e);
} catch (final InterruptedException e) {
Expand Down Expand Up @@ -485,7 +485,7 @@ protected final void interrupt() {
}

@Suspendable
final void handleRequest(HttpRequestWrapper s) throws SuspendExecution, InterruptedException {
final void handleRequest(UndertowHttpRequest s) throws SuspendExecution, InterruptedException {
blockSessionRequests();
xch = s.xch;
if (needsRestart) {
Expand All @@ -499,7 +499,7 @@ final void handleRequest(HttpRequestWrapper s) throws SuspendExecution, Interrup

final void handleReply(final HttpResponse message) throws InterruptedException {
try {
final HttpRequestWrapper undertowRequest = (HttpRequestWrapper) message.getRequest();
final UndertowHttpRequest undertowRequest = (UndertowHttpRequest) message.getRequest();
final HttpServerExchange xch = undertowRequest.xch;

final int status = message.getStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void httpHeaderCaseInsensitivity() {
String headerValue = "application/json";
HttpServerExchange httpServerExchange = new HttpServerExchange(null);
httpServerExchange.getRequestHeaders().put(new HttpString("Content-Type"), headerValue);
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(null, httpServerExchange, null);
UndertowHttpRequest requestWrapper = new UndertowHttpRequest(null, httpServerExchange, null);
assertEquals(headerValue, requestWrapper.getHeader("content-type"));
}
}

0 comments on commit f413328

Please sign in to comment.