Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.util.Locale;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.apache.http.ExceptionLogger;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpException;
- import org.apache.http.HttpRequest;
- import org.apache.http.HttpResponse;
- import static org.apache.http.HttpStatus.SC_OK;
- import org.apache.http.MethodNotSupportedException;
- import org.apache.http.entity.ByteArrayEntity;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.nio.bootstrap.HttpServer;
- import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
- import org.apache.http.impl.nio.reactor.IOReactorConfig;
- import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
- import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
- import org.apache.http.nio.protocol.HttpAsyncExchange;
- import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
- import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
- import org.apache.http.protocol.HttpContext;
- public class HttpComponentsConciseTest {
- /*
- * NB: The random.png.gz file must be large (> 2 MiB)
- */
- private static final String LARGE_COMPRESSED_FILE_NAME = "random.png.gz";
- private static final String LARGE_FILE_SOAP_RESPONSE_MESSAGE_XML
- = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
- + " <soapenv:Header/>\n"
- + " <soapenv:Body>\n"
- + " <AResponse>\n"
- + " <ResponseFile>" + LARGE_COMPRESSED_FILE_NAME + "</ResponseFile>\n"
- + " </AResponse>\n"
- + " </soapenv:Body>\n"
- + "</soapenv:Envelope>";
- private static final File LARGE_TEST_GZIP_FILE = new File(LARGE_COMPRESSED_FILE_NAME);
- private static final int PORT_NUMBER = 8080;
- public static void main(String[] args) {
- startHttpServer();
- }
- private static void startHttpServer() {
- IOReactorConfig config = IOReactorConfig.custom()
- .setIoThreadCount(1)
- .setSoTimeout(15000)
- .setTcpNoDelay(true)
- .build();
- final HttpServer server = ServerBootstrap.bootstrap()
- .setListenerPort(PORT_NUMBER)
- .setServerInfo("Test/1.1")
- .setIOReactorConfig(config)
- .setExceptionLogger(ExceptionLogger.STD_ERR)
- .registerHandler("*", new PrimaryRequestHandler())
- .create();
- Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(5,
- TimeUnit.MILLISECONDS)));
- Thread serverOwner = new Thread(() -> {
- try {
- server.start();
- } catch (IOException ex) {
- Logger.getLogger(HttpComponentsServerOnlyTest.class.getName()).log(Level.SEVERE,
- null,
- ex);
- }
- }, "ServerOwner");
- serverOwner.start();
- }
- private static class PrimaryRequestHandler implements HttpAsyncRequestHandler<HttpRequest> {
- @Override
- public HttpAsyncRequestConsumer<HttpRequest> processRequest(
- final HttpRequest request,
- final HttpContext context) {
- return new BasicAsyncRequestConsumer();
- }
- @Override
- public void handle(
- final HttpRequest request,
- final HttpAsyncExchange httpexchange,
- final HttpContext context) throws HttpException, IOException {
- HttpResponse response = httpexchange.getResponse();
- String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
- if (!method.equals("POST")) {
- throw new MethodNotSupportedException(method + " method not supported");
- }
- StringBody soapResponseStringBody = new StringBody(LARGE_FILE_SOAP_RESPONSE_MESSAGE_XML,
- ContentType.create("text/xml", "utf-8"));
- FileBody soapAttachment = new FileBody(LARGE_TEST_GZIP_FILE);
- HttpEntity responseEntity = MultipartEntityBuilder.create()
- .addPart("SOAP Envelope", soapResponseStringBody)
- .addPart(LARGE_TEST_GZIP_FILE.getName(), soapAttachment)
- .build();
- // response.setEntity(responseEntity);
- try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
- responseEntity.writeTo(out);
- // response.setEntity(new ByteArrayEntity(out.toByteArray()));
- response.setEntity(new ByteArrayEntity(out.toByteArray(), ContentType.get(
- responseEntity)));
- }
- response.setStatusCode(SC_OK);
- httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment