Skip to content

Commit 595d33c

Browse files
committed
替换HttpRequestContext的数据传递方式
1 parent dd2730c commit 595d33c

23 files changed

+189
-283
lines changed

src/main/java/love/wangqi/codec/DefaultHttpRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import io.netty.handler.codec.http.multipart.HttpDataFactory;
88
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
99
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
1210
import love.wangqi.exception.GatewayNoRouteException;
1311
import love.wangqi.route.Route;
1412
import love.wangqi.route.RouteMapper;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1515

1616
import java.net.URI;
1717
import java.net.URL;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package love.wangqi.context;
2+
3+
import io.netty.handler.codec.http.FullHttpRequest;
4+
import io.netty.handler.codec.http.FullHttpResponse;
5+
import io.netty.util.AttributeKey;
6+
7+
/**
8+
* @author: wangqi
9+
* @description:
10+
* @date: Created in 2018-11-27 11:30
11+
*/
12+
public interface Attributes {
13+
AttributeKey<FullHttpRequest> REQUEST = AttributeKey.newInstance("httpRequest");
14+
AttributeKey<Exception> EXCEPTION = AttributeKey.newInstance("exception");
15+
AttributeKey<FullHttpResponse> RESPONSE = AttributeKey.newInstance("response");
16+
AttributeKey<Boolean> KEEPALIVE = AttributeKey.newInstance("keepAlive");
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package love.wangqi.context;
2+
3+
import io.netty.channel.Channel;
4+
import io.netty.handler.codec.http.FullHttpRequest;
5+
import io.netty.handler.codec.http.FullHttpResponse;
6+
7+
8+
/**
9+
* @author: wangqi
10+
* @description:
11+
* @date: Created in 2018-11-27 15:02
12+
*/
13+
public class ContextUtil {
14+
public static void setRequest(Channel channel, FullHttpRequest request) {
15+
channel.attr(Attributes.REQUEST).set(request);
16+
}
17+
18+
public static FullHttpRequest getRequest(Channel channel) {
19+
return channel.attr(Attributes.REQUEST).get();
20+
}
21+
22+
public static void setResponse(Channel channel, FullHttpResponse response) {
23+
channel.attr(Attributes.RESPONSE).set(response);
24+
}
25+
26+
public static FullHttpResponse getResponse(Channel channel) {
27+
return channel.attr(Attributes.RESPONSE).get();
28+
}
29+
30+
public static void setKeepAlive(Channel channel, Boolean keepAlive) {
31+
channel.attr(Attributes.KEEPALIVE).set(keepAlive);
32+
}
33+
34+
public static Boolean getKeepAlive(Channel channel) {
35+
return channel.attr(Attributes.KEEPALIVE).get();
36+
}
37+
38+
public static void setException(Channel channel, Exception exception) {
39+
channel.attr(Attributes.EXCEPTION).set(exception);
40+
}
41+
42+
public static Exception getException(Channel channel) {
43+
return channel.attr(Attributes.EXCEPTION).get();
44+
}
45+
}

src/main/java/love/wangqi/context/HttpRequestContext.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

src/main/java/love/wangqi/context/RequestConstant.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/love/wangqi/core/DefaultChannelCloseFutureListener.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import io.netty.channel.Channel;
44
import io.netty.channel.ChannelFuture;
5-
import io.netty.handler.codec.http.FullHttpRequest;
6-
import love.wangqi.context.HttpRequestContext;
7-
import love.wangqi.context.RequestConstant;
5+
import love.wangqi.context.ContextUtil;
86
import love.wangqi.listener.ChannelCloseFutureListener;
97

108
/**
@@ -13,14 +11,12 @@
1311
* @date: Created in 2018/11/22 下午4:21
1412
*/
1513
public class DefaultChannelCloseFutureListener implements ChannelCloseFutureListener {
16-
private HttpRequestContext httpRequestContext = HttpRequestContext.getInstance();
1714

1815
@Override
1916
public void operationComplete(Channel channel, ChannelFuture future) {
20-
Boolean keepAlive = httpRequestContext.get(channel, RequestConstant.KEEPALIVE);
17+
Boolean keepAlive = ContextUtil.getKeepAlive(channel);
2118

22-
((FullHttpRequest)httpRequestContext.getHttpRequest(channel)).release();
23-
httpRequestContext.remove(channel);
19+
ContextUtil.getRequest(channel).release();
2420

2521
if (!keepAlive) {
2622
channel.close();

src/main/java/love/wangqi/core/ResponseHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import io.netty.handler.codec.http.HttpHeaderNames;
88
import io.netty.handler.codec.http.HttpUtil;
99
import love.wangqi.config.GatewayConfig;
10-
import love.wangqi.context.HttpRequestContext;
11-
import love.wangqi.context.RequestConstant;
10+
import love.wangqi.context.ContextUtil;
1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
1413

@@ -21,11 +20,10 @@ public class ResponseHandler {
2120
private Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
2221

2322
private GatewayConfig config = GatewayConfig.getInstance();
24-
private HttpRequestContext httpRequestContext = HttpRequestContext.getInstance();
2523

2624
public void send(Channel channel, FullHttpResponse response) {
2725
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
28-
Boolean keepAlive = httpRequestContext.get(channel, RequestConstant.KEEPALIVE);
26+
Boolean keepAlive = ContextUtil.getKeepAlive(channel);
2927
HttpUtil.setKeepAlive(response, keepAlive == null ? false : keepAlive);
3028
channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
3129
@Override
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package love.wangqi.exception.handler;
22

3-
import io.netty.channel.ChannelHandlerContext;
3+
import io.netty.channel.Channel;
44

55
/**
66
* @author: wangqi
@@ -10,8 +10,8 @@
1010
public abstract class AbstractExceptionHandler implements ExceptionHandler {
1111

1212
@Override
13-
public final void handle(ChannelHandlerContext ctx, Exception exception) {
13+
public final void handle(Channel channel, Exception exception) {
1414
ExceptionResponse exceptionResponse = getExceptionResponse(exception);
15-
send(ctx, exceptionResponse);
15+
send(channel, exceptionResponse);
1616
}
1717
}

src/main/java/love/wangqi/exception/handler/DefaultExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package love.wangqi.exception.handler;
22

3-
import io.netty.channel.ChannelHandlerContext;
3+
import io.netty.channel.Channel;
44
import io.netty.handler.codec.http.*;
55
import love.wangqi.config.GatewayConfig;
66
import love.wangqi.exception.GatewayException;
@@ -41,13 +41,13 @@ public ExceptionResponse getExceptionResponse(Exception exception) {
4141
}
4242

4343
@Override
44-
public void send(ChannelHandlerContext ctx, ExceptionResponse exceptionResponse) {
44+
public void send(Channel channel, ExceptionResponse exceptionResponse) {
4545
String content = exceptionResponse.getContent();
4646
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, exceptionResponse.getStatus());
4747
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
4848
if (content != null) {
4949
response.headers().set("X-Ca-Error-Message", content);
5050
}
51-
config.getResponseHandler().send(ctx.channel(), response);
51+
config.getResponseHandler().send(channel, response);
5252
}
5353
}

src/main/java/love/wangqi/exception/handler/ExceptionHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package love.wangqi.exception.handler;
22

3-
import io.netty.channel.ChannelHandlerContext;
3+
import io.netty.channel.Channel;
44

55
/**
66
* @author: wangqi
@@ -17,15 +17,15 @@ public interface ExceptionHandler {
1717

1818
/**
1919
* 发送异常
20-
* @param ctx
20+
* @param channel
2121
* @param exceptionResponse
2222
*/
23-
void send(ChannelHandlerContext ctx, ExceptionResponse exceptionResponse);
23+
void send(Channel channel, ExceptionResponse exceptionResponse);
2424

2525
/**
2626
* 处理异常
27-
* @param ctx
27+
* @param channel
2828
* @param exception
2929
*/
30-
void handle(ChannelHandlerContext ctx, Exception exception);
30+
void handle(Channel channel, Exception exception);
3131
}

0 commit comments

Comments
 (0)