balint.steinbach balint.steinbach - 13 days ago 12
HTTP Question

Receive non Http messages using jetty

I am using embedded jetty and spring for java to java communication over http. My problem is that my server application must handle plain TCP messages also on the same port.

Is there a way to detect if a TCP message arrived which cannot be handled by the servlet?

Thanks for the answers I add some more details:


  • I cannot modify the client. The reason for this is that the old version of the client uses pure java tcp socket and it turned out that the new server must be backward compatible with the old client.

  • Have to use the same port

  • Old client messages are short serialized text over simple socket. 1: open connection, 2: send text, 3: close connection

  • My server looks something like this: http://kielczewski.eu/2013/11/using-embedded-jetty-spring-mvc/

  • I do not need to parse the message. It is enough to detect that a message was arrived which is not using http and get the source host name.


Answer

You might want to take a look at how to add a custom ConnectionFactory to the ServerConnector of your HTTP port.

This ConnectionFactory concept is how the PROXY Protocol is supported within Jetty currently.

In your case, you might have something like ...

MyTcpConnectionFactory tcpConnectionFactory = new MyTcpConnectionFactory();
ServerConnector http = new ServerConnector(server);
http.addFirstConnectionFactory(tcpConnectionFactory);
server.addConnector(http);

In your case, you would override the newConnection(Connector connector, EndPoint endPoint) method and implement the check for your TCP flow, or the HTTP flow.

If its your flow, you handle the communications on that connection yourself and then throw an IOException when you are done indicating that you don't want jetty to process that connection as HTTP.

Otherwise you return that Connection object to Jetty to process as HTTP.

Comments