diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java new file mode 100644 index 00000000..692a06b0 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java @@ -0,0 +1,44 @@ +package io.github.kimmking.gateway.filter; + + +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; + +/** + *
+ * dongjiang + * reference: + * HJ43 + * + *
+ */ +public class HttpRequestFilterImpl implements HttpRequestFilter{ + + @Override + public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + HttpMethod method = fullRequest.method(); + if(method.equals(HttpMethod.POST)){ + FullHttpRequest response =null; + + try{ + response = (FullHttpRequest) new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED); + }catch (Exception e){ + e.printStackTrace(); + response= (FullHttpRequest) new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.NO_CONTENT); + }finally { + if(fullRequest!=null){ + if(!HttpUtil.isKeepAlive(fullRequest)){ + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + }else { + ctx.write(response); + } + } + ctx.flush(); + ctx.close(); + } + + + } + } +} diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java new file mode 100644 index 00000000..58b89a29 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java @@ -0,0 +1,22 @@ +package io.github.kimmking.gateway.filter; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; + +public class UriHttpRequestFilter implements HttpRequestFilter{ + + @Override + public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + String url =fullRequest.uri(); + System.out.println("url:" + url); + if(!url.contains("api/hello")){ + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); + ctx.write(response); + ctx.flush(); + ctx.close(); + } + } +} diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java index 79aeb148..650cf6d2 100644 --- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java @@ -1,51 +1,69 @@ -//package io.github.kimmking.gateway.outbound; -// -//import io.netty.bootstrap.Bootstrap; -//import io.netty.channel.ChannelFuture; -//import io.netty.channel.ChannelInitializer; -//import io.netty.channel.ChannelOption; -//import io.netty.channel.EventLoopGroup; -//import io.netty.channel.nio.NioEventLoopGroup; -//import io.netty.channel.socket.SocketChannel; -//import io.netty.channel.socket.nio.NioSocketChannel; -//import io.netty.handler.codec.http.HttpRequestEncoder; -//import io.netty.handler.codec.http.HttpResponseDecoder; -// -//public class NettyHttpClient { -// public void connect(String host, int port) throws Exception { -// EventLoopGroup workerGroup = new NioEventLoopGroup(); -// -// try { -// Bootstrap b = new Bootstrap(); -// b.group(workerGroup); -// b.channel(NioSocketChannel.class); -// b.option(ChannelOption.SO_KEEPALIVE, true); -// b.handler(new ChannelInitializer+ * reference : + * HJ43 + * huyanatown + + *
+ + */ public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter { - + + private NettyHttpClient client; + public NettyHttpClientOutboundHandler(String proxyServer){ + this.client= new NettyHttpClient(proxyServer); + } + + @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java new file mode 100644 index 00000000..d063feb3 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java @@ -0,0 +1,14 @@ +package io.github.kimmking.gateway.router; + +import java.util.List; +import java.util.Random; + +public class HttpEndpointRouterImpl implements HttpEndpointRouter{ + @Override + public String route(List