Advertisement
pbanavara

Untitled

Oct 27th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. package ai.simon;
  2.  
  3. import io.netty.channel.ChannelInboundHandlerAdapter;
  4. import io.netty.channel.nio.NioEventLoopGroup;
  5. import io.netty.channel.socket.nio.NioServerSocketChannel;
  6.  
  7. import static io.netty.handler.codec.http.HttpMethod.GET;
  8.  
  9. import io.netty.bootstrap.Bootstrap;
  10. import io.netty.buffer.ByteBuf;
  11. import io.netty.buffer.Unpooled;
  12. import io.netty.channel.ChannelFutureListener;
  13. import io.netty.channel.ChannelHandlerContext;
  14. import io.netty.channel.ChannelInboundHandlerAdapter;
  15. import io.netty.handler.codec.http.DefaultFullHttpResponse;
  16. import io.netty.handler.codec.http.DefaultHttpRequest;
  17. import io.netty.handler.codec.http.FullHttpRequest;
  18. import io.netty.handler.codec.http.FullHttpResponse;
  19. import io.netty.handler.codec.http.HttpHeaders;
  20. import io.netty.handler.codec.http.HttpMethod;
  21. import io.netty.handler.codec.http.HttpRequest;
  22.  
  23. import io.netty.handler.codec.http.HttpVersion;
  24. import io.netty.handler.codec.http.QueryStringDecoder;
  25. import io.netty.handler.codec.http.multipart.Attribute;
  26. import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
  27. import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
  28. import io.netty.handler.codec.http.multipart.InterfaceHttpData;
  29. import io.netty.util.CharsetUtil;
  30.  
  31. import static io.netty.handler.codec.http.HttpHeaders.Names.*;
  32. import static io.netty.handler.codec.http.HttpHeaders.Values;
  33. import static io.netty.handler.codec.http.HttpResponseStatus.*;
  34. import static io.netty.handler.codec.http.HttpVersion.*;
  35.  
  36. import java.net.URI;
  37.  
  38. import io.netty.channel.*;
  39.  
  40. import org.json.simple.JSONObject;
  41.  
  42. import edu.stanford.nlp.io.EncodingPrintWriter.out;
  43.  
  44. public class HttpServerHandler extends ChannelInboundHandlerAdapter {
  45.  
  46.  
  47. //private Parser p = Parser.getInstance();
  48.  
  49. @Override
  50. public void channelReadComplete(ChannelHandlerContext ctx) {
  51. ctx.flush();
  52. }
  53.  
  54. @SuppressWarnings("unchecked")
  55. private JSONObject getResponse(String input) {
  56. JSONObject obj = new JSONObject();
  57. obj.put("Super", 123);
  58. return obj;
  59.  
  60. }
  61.  
  62. private void test(String input, ChannelHandlerContext ctx) {
  63. try {
  64. String url = "http://localhost";
  65. URI uri = new URI(url);
  66. Bootstrap b = new Bootstrap();
  67. b.group(new NioEventLoopGroup())
  68. .channel(NioServerSocketChannel.class)
  69. .handler(new PostRequestHandler())
  70. .option(ChannelOption.AUTO_READ, false);
  71. Channel f = b.connect("REMOTE_HOST", 8888).sync().channel();
  72. HttpRequest postReq = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
  73. HttpMethod.POST, uri.getRawPath());
  74. postReq.headers().set(HttpHeaders.Names.HOST, "localhost");
  75. postReq.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
  76. postReq.headers().set(HttpHeaders.Names.CONTENT_TYPE,"application/x-www-form-urlencoded");
  77. f.writeAndFlush(postReq);
  78.  
  79. // Wait for the server to close the connection.
  80. f.closeFuture().sync();
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86. @Override
  87. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  88. HttpPostRequestDecoder decoder = null;
  89. JSONObject obj = null;
  90. try {
  91. if (msg instanceof HttpRequest) {
  92. FullHttpRequest req = (FullHttpRequest) msg;
  93. out.println(req.getUri());
  94. if (req.getMethod() == GET) {
  95. out.println(String.valueOf(req.getDecoderResult().isSuccess()));
  96. ByteBuf res = req.content();
  97. byte[] bytes = new byte[res.readableBytes()];
  98. int readerIndex = res.readerIndex();
  99. res.getBytes(readerIndex, bytes);
  100. String inputString = new String(bytes);
  101. //sendHttpPost(inputString, ctx);
  102. test(inputString, ctx);
  103. obj= getResponse(inputString);
  104. System.out.println(inputString);
  105. if (HttpHeaders.is100ContinueExpected(req)) {
  106. ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
  107.  
  108. boolean keepAlive = HttpHeaders.isKeepAlive(req);
  109. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(obj.toString(),
  110. CharsetUtil.UTF_8));
  111. response.headers().set(CONTENT_TYPE, "application/json");
  112. response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
  113.  
  114. if (!keepAlive) {
  115. ctx.write(response).addListener(ChannelFutureListener.CLOSE);
  116. } else {
  117. response.headers().set(CONNECTION, Values.KEEP_ALIVE);
  118. ctx.write(response);
  119. }
  120. }
  121.  
  122. }
  123.  
  124.  
  125. }
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. } finally {
  129. if (decoder != null) {
  130. decoder.destroy();
  131. }
  132.  
  133. }
  134. }
  135.  
  136. private void sendHttpPost(String input, ChannelHandlerContext ctx) {
  137. try {
  138. CalendarManager.calendarEntry();
  139.  
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143.  
  144. }
  145.  
  146. @Override
  147. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  148. cause.printStackTrace();
  149. ctx.close();
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement