Receive non Http messages using jetty

一曲冷凌霜 提交于 2020-01-17 05:42:05

问题


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.

回答1:


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.




回答2:


You are in for a wild ride here my friend. You need to realize that HTTP IS TCP ... its just the content being sent on the TCP socket that classifies it as HTTP or not. That being said, you can intercept the Connection with a filter ie

1) create a filter (google Java Application Server Filters and check the Jetty implementation) for ALL incoming connections

2) check for URI on the request, if it fails, then the request is not HTTP (might want to double check on the request testing logic here)

3) Redirect the request to the appropriate Servlet / Function based on serial socket / http request

On another note, why not use https (port 443) for http and port 80 for your socket requirments ?


I stand corrected. Filters wont work. In that case, you will have to code a mini firewall. you have to scan all inputs for https headers and redirect accordingly. Can you at least provide some context on the plain TCP messages you want to receive? do you have any control over the sending code ? you do know you can upgrade a TCP/HTTP connection to a websocket (involves client and server) and it will work even better than plain TCP, same port connections, and comes built in Jetty so no custom boiler plates, just a websocket servlet



来源:https://stackoverflow.com/questions/40687517/receive-non-http-messages-using-jetty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!