22
33import io .github .kimmking .gateway .filter .HeaderHttpRequestFilter ;
44import io .github .kimmking .gateway .filter .HttpRequestFilter ;
5+ import io .github .kimmking .gateway .filter .HttpResponseFilter ;
6+ import io .github .kimmking .gateway .filter .MyFilter ;
57import io .github .kimmking .gateway .outbound .httpclient4 .HttpOutboundHandler ;
8+ import io .netty .buffer .Unpooled ;
9+ import io .netty .channel .ChannelFutureListener ;
610import io .netty .channel .ChannelHandlerContext ;
711import io .netty .channel .ChannelInboundHandlerAdapter ;
12+ import io .netty .handler .codec .http .DefaultFullHttpResponse ;
813import io .netty .handler .codec .http .FullHttpRequest ;
14+ import io .netty .handler .codec .http .FullHttpResponse ;
15+ import io .netty .handler .codec .http .HttpUtil ;
916import io .netty .util .ReferenceCountUtil ;
1017import org .slf4j .Logger ;
1118import org .slf4j .LoggerFactory ;
1219
1320import java .util .List ;
1421
22+ import static io .netty .handler .codec .http .HttpResponseStatus .NO_CONTENT ;
23+ import static io .netty .handler .codec .http .HttpResponseStatus .OK ;
24+ import static io .netty .handler .codec .http .HttpVersion .HTTP_1_1 ;
25+ import static io .netty .handler .codec .rtsp .RtspHeaderNames .CONNECTION ;
26+ import static io .netty .handler .codec .rtsp .RtspHeaderValues .KEEP_ALIVE ;
27+
1528public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1629
1730 private static Logger logger = LoggerFactory .getLogger (HttpInboundHandler .class );
1831 private final List <String > proxyServer ;
1932 private HttpOutboundHandler handler ;
2033 private HttpRequestFilter filter = new HeaderHttpRequestFilter ();
34+ private MyFilter myFilter = new MyFilter ();
2135
2236 public HttpInboundHandler (List <String > proxyServer ) {
2337 this .proxyServer = proxyServer ;
@@ -37,10 +51,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
3751// String uri = fullRequest.uri();
3852// //logger.info("接收到的请求url为{}", uri);
3953// if (uri.contains("/test")) {
40- // handlerTest(fullRequest, ctx);
54+ handlerTest (fullRequest , ctx , myFilter );
4155// }
4256
43- handler .handle (fullRequest , ctx , filter );
57+ // handler.handle(fullRequest, ctx, filter);
4458
4559 } catch (Exception e ) {
4660 e .printStackTrace ();
@@ -49,33 +63,33 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
4963 }
5064 }
5165
52- // private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
53- // FullHttpResponse response = null;
54- // try {
55- // String value = "hello,kimmking";
56- // response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
57- // response.headers().set("Content-Type", "application/json");
58- // response.headers().setInt("Content-Length", response.content().readableBytes());
59- //
60- // } catch (Exception e) {
61- // logger.error("处理测试接口出错", e);
62- // response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
63- // } finally {
64- // if (fullRequest != null) {
65- // if (!HttpUtil.isKeepAlive(fullRequest)) {
66- // ctx.write(response).addListener(ChannelFutureListener.CLOSE);
67- // } else {
68- // response.headers().set(CONNECTION, KEEP_ALIVE);
69- // ctx.write(response);
70- // }
71- // }
72- // }
73- // }
74- //
75- // @Override
76- // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
77- // cause.printStackTrace();
78- // ctx.close();
79- // }
66+ private void handlerTest (FullHttpRequest fullRequest , ChannelHandlerContext ctx , HttpResponseFilter filter ) {
67+ FullHttpResponse response = null ;
68+ try {
69+ String value = "hello,kimmking" ;
70+ response = new DefaultFullHttpResponse (HTTP_1_1 , OK , Unpooled .wrappedBuffer (value .getBytes ("UTF-8" )));
71+ response .headers ().set ("Content-Type" , "application/json" );
72+ response .headers ().setInt ("Content-Length" , response .content ().readableBytes ());
73+ filter . filter ( response );
74+ } catch (Exception e ) {
75+ logger .error ("处理测试接口出错" , e );
76+ response = new DefaultFullHttpResponse (HTTP_1_1 , NO_CONTENT );
77+ } finally {
78+ if (fullRequest != null ) {
79+ if (!HttpUtil .isKeepAlive (fullRequest )) {
80+ ctx .write (response ).addListener (ChannelFutureListener .CLOSE );
81+ } else {
82+ response .headers ().set (CONNECTION , KEEP_ALIVE );
83+ ctx .write (response );
84+ }
85+ }
86+ }
87+ }
88+
89+ @ Override
90+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) {
91+ cause .printStackTrace ();
92+ ctx .close ();
93+ }
8094
8195}
0 commit comments