Guest User

Untitled

a guest
Sep 15th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.83 KB | None | 0 0
  1. package httpcorenioserver;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.nio.ByteBuffer;
  6. import java.util.Locale;
  7.  
  8. import config.SocketConfigurationConstants;
  9. import org.apache.http.HttpException;
  10. import org.apache.http.HttpRequest;
  11. import org.apache.http.HttpResponse;
  12. import org.apache.http.HttpResponseInterceptor;
  13. import org.apache.http.HttpVersion;
  14. import org.apache.http.client.protocol.ResponseContentEncoding;
  15. import org.apache.http.client.protocol.ResponseProcessCookies;
  16. import org.apache.http.config.ConnectionConfig;
  17. import org.apache.http.entity.BasicHttpEntity;
  18. import org.apache.http.entity.ContentType;
  19. import org.apache.http.impl.nio.DefaultHttpServerIODispatch;
  20. import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
  21. import org.apache.http.impl.nio.reactor.IOReactorConfig;
  22. import org.apache.http.message.BasicHttpResponse;
  23. import org.apache.http.nio.ContentDecoder;
  24. import org.apache.http.nio.ContentEncoder;
  25. import org.apache.http.nio.IOControl;
  26. import org.apache.http.nio.entity.NStringEntity;
  27. import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
  28. import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
  29. import org.apache.http.nio.protocol.HttpAsyncExchange;
  30. import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
  31. import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
  32. import org.apache.http.nio.protocol.HttpAsyncResponseProducer;
  33. import org.apache.http.nio.protocol.HttpAsyncService;
  34. import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
  35. import org.apache.http.nio.reactor.IOEventDispatch;
  36. import org.apache.http.nio.reactor.ListeningIOReactor;
  37. import org.apache.http.protocol.HttpContext;
  38. import org.apache.http.protocol.HttpProcessor;
  39. import org.apache.http.protocol.ImmutableHttpProcessor;
  40. import org.apache.http.protocol.ResponseConnControl;
  41. import org.apache.http.protocol.ResponseContent;
  42. import org.apache.http.protocol.ResponseDate;
  43. import org.apache.http.protocol.ResponseServer;
  44.  
  45. public class AsyncHttpCoreNIOServer
  46. {
  47.  
  48.     public AsyncHttpCoreNIOServer() throws IOException, InterruptedException
  49.     {
  50.         final HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
  51.                 new ResponseDate(),
  52.                 new ResponseServer("HttpCore-NIO-Test/1.1"),
  53.                 new ResponseContent(),
  54.                 new ResponseConnControl()
  55.         });
  56.  
  57.         final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
  58.         registry.register("*", new AsyncHttpHandler());
  59.  
  60.         final HttpAsyncService handler = new HttpAsyncService(httpproc, registry);
  61.  
  62.         final IOReactorConfig reactorConfig = IOReactorConfig.custom()
  63.                 .setSoReuseAddress(true)
  64.                 .setTcpNoDelay(true)
  65.                 .build();
  66.         final ListeningIOReactor ioReactor = new DefaultListeningIOReactor(reactorConfig);
  67.         final ConnectionConfig connectionConfig = ConnectionConfig.custom()
  68.                 .setBufferSize(8 * 1024)
  69.                 .setFragmentSizeHint(8 * 1024)
  70.                 .build();
  71.         final IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(handler,
  72.                                                                                 connectionConfig);
  73.  
  74.         ioReactor.listen(new InetSocketAddress(SocketConfigurationConstants.HTTP_SERVER_PORT));
  75.         ioReactor.execute(ioEventDispatch);
  76.     }
  77.  
  78.     public static void main(final String[] args) throws Exception
  79.     {
  80.         new AsyncHttpCoreNIOServer();
  81.     }
  82.  
  83.     class AsyncHttpHandler implements HttpAsyncRequestHandler<HttpRequest>
  84.     {
  85.         @Override
  86.         public HttpAsyncRequestConsumer<HttpRequest> processRequest(
  87.                 final HttpRequest request,
  88.                 final HttpContext context) throws HttpException, IOException
  89.         {
  90.             Echoer echoer = new Echoer();
  91.             context.setAttribute("echoer", echoer);
  92.             return echoer;
  93.         }
  94.  
  95.         @Override
  96.         public void handle(
  97.                 final HttpRequest request,
  98.                 final HttpAsyncExchange httpexchange,
  99.                 final HttpContext context) throws HttpException, IOException
  100.         {
  101.             httpexchange.submitResponse((HttpAsyncResponseProducer) context.getAttribute("echoer"));
  102.         }
  103.     }
  104.  
  105.     class Echoer implements HttpAsyncResponseProducer, HttpAsyncRequestConsumer
  106.     {
  107.  
  108.         private volatile HttpRequest request;
  109.         private volatile ByteBuffer buffer = ByteBuffer.allocate(2048);
  110.  
  111.         private volatile boolean requestCompleted;
  112.         private volatile boolean responseCompleted;
  113.  
  114.  
  115.         public HttpResponse generateResponse()
  116.         {
  117.             System.out.println("GENERATE RESPONSE");
  118.             HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
  119.             BasicHttpEntity entity = new BasicHttpEntity();
  120.             entity.setContentType("text/plain");
  121.             entity.setChunked(false);
  122.             entity.setContent(null);
  123.             response.setEntity(entity);
  124.             return response;
  125.         }
  126.  
  127.         public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException
  128.         {
  129.             System.out.println("PRODUCE CONTENT");
  130.             buffer.flip();
  131.             encoder.write(buffer);
  132.             buffer.compact();
  133.             if (buffer.hasRemaining() && !requestCompleted)
  134.             {
  135.                 ioctrl.requestInput();
  136.                 System.out.println(" request client input");
  137.             }
  138.             if (buffer.position() == 0)
  139.             {
  140.                 if (requestCompleted)
  141.                 {
  142.                     encoder.complete();
  143.                     System.out.println("content fully written");
  144.                 }
  145.                 else
  146.                 {
  147.                     // Input buffer is empty. Wait until the client fills up
  148.                     // the buffer
  149.                     ioctrl.suspendOutput();
  150.                     System.out.println(" suspend origin output");
  151.                 }
  152.             }
  153.         }
  154.  
  155.         public void responseCompleted(HttpContext context)
  156.         {
  157.             System.out.println("RESPONSE COMPLETE");
  158.             this.responseCompleted = true;
  159.         }
  160.  
  161.         public void failed(Exception ex)
  162.         {
  163.             ex.printStackTrace();
  164.         }
  165.  
  166.         public void requestReceived(HttpRequest request) throws HttpException, IOException
  167.         {
  168.             System.out.println("REQUEST RECEIVED");
  169.             this.request = request;
  170.         }
  171.  
  172.         public void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException
  173.         {
  174.             System.out.println("CONSUME CONTENT");
  175.  
  176.             decoder.read(buffer);
  177.             if (decoder.isCompleted()) {
  178.                 System.out.println("Content fully read");
  179.             }
  180.             if (!buffer.hasRemaining())
  181.             {
  182.                 ioctrl.suspendInput();
  183.                 System.out.println("suspend client input");
  184.             }
  185.             if (buffer.position() > 0)
  186.             {
  187.                 System.out.println("request output");
  188.                 ioctrl.requestOutput();
  189.             }
  190.         }
  191.  
  192.         public void requestCompleted(HttpContext context)
  193.         {
  194.             System.out.println("REQUEST COMPLETED");
  195.             requestCompleted = true;
  196.         }
  197.  
  198.         public Exception getException()
  199.         {
  200.             return null;
  201.         }
  202.  
  203.         public Object getResult()
  204.         {
  205.             System.out.println("GET RESULT");
  206.             return request;
  207.         }
  208.  
  209.         public boolean isDone()
  210.         {
  211.             return requestCompleted;
  212.         }
  213.  
  214.         public void close() throws IOException
  215.         {
  216.             System.out.println("CLOSE");
  217.         }
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment