DaManDOH

HttpComponentsConciseTest.java

May 30th, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1.  
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Locale;
  6. import java.util.concurrent.TimeUnit;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import org.apache.http.ExceptionLogger;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpException;
  12. import org.apache.http.HttpRequest;
  13. import org.apache.http.HttpResponse;
  14. import static org.apache.http.HttpStatus.SC_OK;
  15. import org.apache.http.MethodNotSupportedException;
  16. import org.apache.http.entity.ByteArrayEntity;
  17. import org.apache.http.entity.ContentType;
  18. import org.apache.http.entity.mime.MultipartEntityBuilder;
  19. import org.apache.http.entity.mime.content.FileBody;
  20. import org.apache.http.entity.mime.content.StringBody;
  21. import org.apache.http.impl.nio.bootstrap.HttpServer;
  22. import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
  23. import org.apache.http.impl.nio.reactor.IOReactorConfig;
  24. import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
  25. import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
  26. import org.apache.http.nio.protocol.HttpAsyncExchange;
  27. import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
  28. import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
  29. import org.apache.http.protocol.HttpContext;
  30.  
  31. public class HttpComponentsConciseTest {
  32.  
  33.     /*
  34.      * NB: The random.png.gz file must be large (> 2 MiB)
  35.      */
  36.     private static final String LARGE_COMPRESSED_FILE_NAME = "random.png.gz";
  37.     private static final String LARGE_FILE_SOAP_RESPONSE_MESSAGE_XML
  38.             = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
  39.             + "   <soapenv:Header/>\n"
  40.             + "   <soapenv:Body>\n"
  41.             + "      <AResponse>\n"
  42.             + "         <ResponseFile>" + LARGE_COMPRESSED_FILE_NAME + "</ResponseFile>\n"
  43.             + "      </AResponse>\n"
  44.             + "   </soapenv:Body>\n"
  45.             + "</soapenv:Envelope>";
  46.     private static final File LARGE_TEST_GZIP_FILE = new File(LARGE_COMPRESSED_FILE_NAME);
  47.  
  48.     private static final int PORT_NUMBER = 8080;
  49.  
  50.     public static void main(String[] args) {
  51.         startHttpServer();
  52.     }
  53.  
  54.     private static void startHttpServer() {
  55.         IOReactorConfig config = IOReactorConfig.custom()
  56.                 .setIoThreadCount(1)
  57.                 .setSoTimeout(15000)
  58.                 .setTcpNoDelay(true)
  59.                 .build();
  60.  
  61.         final HttpServer server = ServerBootstrap.bootstrap()
  62.                 .setListenerPort(PORT_NUMBER)
  63.                 .setServerInfo("Test/1.1")
  64.                 .setIOReactorConfig(config)
  65.                 .setExceptionLogger(ExceptionLogger.STD_ERR)
  66.                 .registerHandler("*", new PrimaryRequestHandler())
  67.                 .create();
  68.  
  69.         Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(5,
  70.                 TimeUnit.MILLISECONDS)));
  71.  
  72.         Thread serverOwner = new Thread(() -> {
  73.  
  74.             try {
  75.                 server.start();
  76.             } catch (IOException ex) {
  77.                 Logger.getLogger(HttpComponentsServerOnlyTest.class.getName()).log(Level.SEVERE,
  78.                         null,
  79.                         ex);
  80.             }
  81.         }, "ServerOwner");
  82.         serverOwner.start();
  83.     }
  84.  
  85.     private static class PrimaryRequestHandler implements HttpAsyncRequestHandler<HttpRequest> {
  86.  
  87.         @Override
  88.         public HttpAsyncRequestConsumer<HttpRequest> processRequest(
  89.                 final HttpRequest request,
  90.                 final HttpContext context) {
  91.             return new BasicAsyncRequestConsumer();
  92.         }
  93.  
  94.         @Override
  95.         public void handle(
  96.                 final HttpRequest request,
  97.                 final HttpAsyncExchange httpexchange,
  98.                 final HttpContext context) throws HttpException, IOException {
  99.             HttpResponse response = httpexchange.getResponse();
  100.  
  101.             String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
  102.             if (!method.equals("POST")) {
  103.                 throw new MethodNotSupportedException(method + " method not supported");
  104.             }
  105.  
  106.             StringBody soapResponseStringBody = new StringBody(LARGE_FILE_SOAP_RESPONSE_MESSAGE_XML,
  107.                     ContentType.create("text/xml", "utf-8"));
  108.             FileBody soapAttachment = new FileBody(LARGE_TEST_GZIP_FILE);
  109.             HttpEntity responseEntity = MultipartEntityBuilder.create()
  110.                     .addPart("SOAP Envelope", soapResponseStringBody)
  111.                     .addPart(LARGE_TEST_GZIP_FILE.getName(), soapAttachment)
  112.                     .build();
  113. //            response.setEntity(responseEntity);
  114.             try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
  115.                 responseEntity.writeTo(out);
  116. //                response.setEntity(new ByteArrayEntity(out.toByteArray()));
  117.                 response.setEntity(new ByteArrayEntity(out.toByteArray(), ContentType.get(
  118.                         responseEntity)));
  119.             }
  120.  
  121.             response.setStatusCode(SC_OK);
  122.             httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment