Apache httpcomponents-core NIO SSL example broken

The example snippet of a SSL enabled Apache http-components-core-based Webserver did not work out for me. It crashed after every Request with the following message:

Connection closed: [null]
I/O error: I/O dispatch worker terminated abnormally
Shutdown

Printing the StackTrace revealed the following:

org.apache.http.nio.reactor.IOReactorException: I/O dispatch worker terminated abnormally
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor .execute(AbstractMultiworkerIOReactor.java:324)
at com.demandware.tenantssl.NHttpSSLServer.main(SslPaymentServer.java:176)
Caused by: java.lang.UnsupportedOperationException: Entity template does not implement getContent()
at org.apache.http.entity.EntityTemplate.getContent(EntityTemplate.java:57)
at org.apache.http.nio.entity.NHttpEntityWrapper.(NHttpEntityWrapper.java:56)
at org.apache.http.nio.protocol.AsyncNHttpServiceHandler .sendResponse(AsyncNHttpServiceHandler.java:495)
at org.apache.http.nio.protocol.AsyncNHttpServiceHandler .responseReady(AsyncNHttpServiceHandler.java:362)
at org.apache.http.nio.protocol.BufferingHttpServiceHandler .responseReady(BufferingHttpServiceHandler.java:135)
at org.apache.http.impl.nio.DefaultNHttpServerConnection .produceOutput(DefaultNHttpServerConnection.java:221)
at org.apache.http.impl.nio.ssl.SSLServerIOEventDispatch .outputReady(SSLServerIOEventDispatch.java:252)
at org.apache.http.impl.nio.reactor.BaseIOReactor .writable(BaseIOReactor.java:185)
at org.apache.http.impl.nio.reactor.AbstractIOReactor .processEvent(AbstractIOReactor.java:338)
at org.apache.http.impl.nio.reactor.AbstractIOReactor .processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor .execute(AbstractIOReactor.java:275)
at org.apache.http.impl.nio.reactor.BaseIOReactor .execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker .run(AbstractMultiworkerIOReactor.java:542)
at java.lang.Thread.run(Thread.java:662)

As inline subclassed EntityTemplate does not implement getContent(), you just have to add this tiny snippet in the two inline definitions of EntityTemplate for a minimal working SSL-server:

EntityTemplate body = new EntityTemplate(new ContentProducer()
{

	public void writeTo(final OutputStream outstream) throws IOException
	{
		OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
		writer.write("<html><body><h1>");
		writer.write("File ");
		writer.write(file.getPath());
		writer.write(" not found");
		writer.write("</h1></body></html>");
		writer.flush();
	}
}) {
	@Override
	public InputStream getContent() {
		return new InputStream() {

			@Override
			public int read() throws IOException
			{
				return 0;
			}

		};
	}
};

Leave a Reply

Your email address will not be published. Required fields are marked *