Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ai.simon;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import static io.netty.handler.codec.http.HttpMethod.GET;
- import io.netty.bootstrap.Bootstrap;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.ChannelFutureListener;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import io.netty.handler.codec.http.DefaultFullHttpResponse;
- import io.netty.handler.codec.http.DefaultHttpRequest;
- import io.netty.handler.codec.http.FullHttpRequest;
- import io.netty.handler.codec.http.FullHttpResponse;
- import io.netty.handler.codec.http.HttpHeaders;
- import io.netty.handler.codec.http.HttpMethod;
- import io.netty.handler.codec.http.HttpRequest;
- import io.netty.handler.codec.http.HttpVersion;
- import io.netty.handler.codec.http.QueryStringDecoder;
- import io.netty.handler.codec.http.multipart.Attribute;
- import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
- import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
- import io.netty.handler.codec.http.multipart.InterfaceHttpData;
- import io.netty.util.CharsetUtil;
- import static io.netty.handler.codec.http.HttpHeaders.Names.*;
- import static io.netty.handler.codec.http.HttpHeaders.Values;
- import static io.netty.handler.codec.http.HttpResponseStatus.*;
- import static io.netty.handler.codec.http.HttpVersion.*;
- import java.net.URI;
- import io.netty.channel.*;
- import org.json.simple.JSONObject;
- import edu.stanford.nlp.io.EncodingPrintWriter.out;
- public class HttpServerHandler extends ChannelInboundHandlerAdapter {
- //private Parser p = Parser.getInstance();
- @Override
- public void channelReadComplete(ChannelHandlerContext ctx) {
- ctx.flush();
- }
- @SuppressWarnings("unchecked")
- private JSONObject getResponse(String input) {
- JSONObject obj = new JSONObject();
- obj.put("Super", 123);
- return obj;
- }
- private void test(String input, ChannelHandlerContext ctx) {
- try {
- String url = "http://localhost";
- URI uri = new URI(url);
- Bootstrap b = new Bootstrap();
- b.group(new NioEventLoopGroup())
- .channel(NioServerSocketChannel.class)
- .handler(new PostRequestHandler())
- .option(ChannelOption.AUTO_READ, false);
- Channel f = b.connect("REMOTE_HOST", 8888).sync().channel();
- HttpRequest postReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
- HttpMethod.POST, uri.getRawPath());
- postReq.headers().set(HttpHeaders.Names.HOST, "localhost");
- postReq.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
- postReq.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
- f.writeAndFlush(postReq);
- // Wait for the server to close the connection.
- f.closeFuture().sync();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) {
- HttpPostRequestDecoder decoder = null;
- JSONObject obj = null;
- try {
- if (msg instanceof HttpRequest) {
- FullHttpRequest req = (FullHttpRequest) msg;
- out.println(req.getUri());
- if (req.getMethod() == GET) {
- out.println(String.valueOf(req.getDecoderResult().isSuccess()));
- ByteBuf res = req.content();
- byte[] bytes = new byte[res.readableBytes()];
- int readerIndex = res.readerIndex();
- res.getBytes(readerIndex, bytes);
- String inputString = new String(bytes);
- //sendHttpPost(inputString, ctx);
- test(inputString, ctx);
- obj= getResponse(inputString);
- System.out.println(inputString);
- if (HttpHeaders.is100ContinueExpected(req)) {
- ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
- boolean keepAlive = HttpHeaders.isKeepAlive(req);
- FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(obj.toString(),
- CharsetUtil.UTF_8));
- response.headers().set(CONTENT_TYPE, "application/json");
- response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
- if (!keepAlive) {
- ctx.write(response).addListener(ChannelFutureListener.CLOSE);
- } else {
- response.headers().set(CONNECTION, Values.KEEP_ALIVE);
- ctx.write(response);
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (decoder != null) {
- decoder.destroy();
- }
- }
- }
- private void sendHttpPost(String input, ChannelHandlerContext ctx) {
- try {
- CalendarManager.calendarEntry();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement