0

I want to write an application that listens on a webservice end point:

 hostname:port

and intercepts any response coming off that port. (I will later turn those responses into events and pass them to esper for CEP.) What's the easiest way to do this? (in Java)

P.S. There's no encryption/security/sessions. Just an exposed endpoint that spits out http/xml whenever a request comes in.

Edit: i am not deploying a web service, I am writing a client that will listen and intercept all and any http response coming off a webservice, requested by other clients. i need a listener...

Edit2/@Rami: i could build this as a filter into my ws stack, but lets say i have 100x such webservices and my goal, as stated above, is to route the data to an even processor. i cannot build a filter into every single one, plus others could be owned by vendors, point being, i have the right to consume the webservices i want ot listen on, i just want to monitor for responses for scenarios not originally build into/designed in the original deployment

niken
  • 2,499
  • 3
  • 33
  • 56
  • Not sure what are you asking, clear the question up a bit. You can't listen to a web service endpoint, you can only invoke a web service endpoint. Web services are always being called upon. Do you mean listening an endpoint on the service and then using that request to generate an Esper event? – Aleksandar Stojadinovic Dec 18 '13 at 16:07
  • I mean listening on the socket on which a webservice is "exposed"... Something like this but in java http://stackoverflow.com/questions/12416871/listening-for-http-responses – niken Dec 18 '13 at 16:11
  • is the webservice yours? i mean, do you have access to the code behind it? – Rami.Q Dec 18 '13 at 16:31
  • yes, i do have access, but why do you ask? – niken Dec 18 '13 at 16:34
  • why not using a Filter, from a filter you would catch every request/response. sorry if i misunderstand your question. – Rami.Q Dec 18 '13 at 16:48
  • 1
    'think man in the middle, but with a good purpose' - I'm voting to close. – Martin James Dec 18 '13 at 16:51
  • This question appears to be off-topic because it is about 'think man in the middle, but with a good purpose', ie, spyware. – Martin James Dec 18 '13 at 16:52
  • @Martin i wrote that to put people on the right track... i asked for a way to listen on sockets and people are telling me to use tomcat or teaching me to deploy a web service... as for the closer on that comment, yes, haha, i hear ya man, but that's not my goal – niken Dec 18 '13 at 16:59
  • @Nik: if you have a JEE Project named "abc" under "Tomcat", in this Project are 1000xxx webservices, you need only one Filter. this Filter will catch alll requests/responses on ....:8080/abc/*..., but, i see, you don't "want" to change your original Project. – Rami.Q Dec 18 '13 at 17:17
  • @Rami it's not a "want", some services i own and some dont "own" directly -- they're deployed on different hosts, though on one big lan, hence my reference to man in the middle, it would be faster/cheaper this way – niken Dec 18 '13 at 17:33

2 Answers2

1

The solution for your problem would be to write your own proxy and redirect all traffic thru that application.

You can just write an application that receives the request and forward it to some other server (your endpoint), get the response and send it back to the client that requested it. Something like the following:

ServerSocket server = new ServerSocket(port);
Socket client = server.accept();
// Read input from client here using InputStream

Socket endpoint = new Socket(addressToEndpoint, endpointPort);
// Forward request to endpoint here
// Get reponse

// Do whatever you need with the response here

// Write response back to client
// close everything

Besides that, the only other way I can see that to be possible is to use some native library to communicate with the network adapter to sniff it. From what I know Java doesn't have anything that can make your application a man in the middle.

visola
  • 7,493
  • 1
  • 17
  • 21
  • that's not really what im talking about, im essentially snooping on responses coming from a webservices endpoint. i dont need a "server", i dont need a servlet, i need smth lower level, quickly do handshake and format/classify response. think man in the middle, but with a good purpose – niken Dec 18 '13 at 16:23
  • Sorry I misunderstood your question. I think I answered your question now. – visola Dec 18 '13 at 17:03
  • Any way to do this without having to re-direct traffic? – niken Dec 18 '13 at 17:10
  • 1
    Maybe this is what your looking for: [link]http://stackoverflow.com/questions/3798733/how-do-i-programatically-collect-packets-from-passively-sniffing – visola Dec 18 '13 at 17:46
  • that's cool, but i dont want to figure out how to get an http response out of packets... looking for smth where that part is implemented already – niken Dec 18 '13 at 18:01
0

EDIT: Sorry, I misinterpreted your question. But I would like this answer to stay, somebody can find it useful.

As for your question, I am not sure how to execute that since some port is already reserved by another application. If you have a REST client, maybe you can extend it with some kind of logging filter.

----------------Original answer ------------------

I suggest using a proper JAX-RS web service framework, rather than a simple servlet. It will make your work much more manageable and make it a "proper" REST web service. My usual weapon of choice is JBoss RestEasy, but you can choose any other you find you like, like Apache CXF.

Here is a nice tutorial about starting with RestEasy, but I strongly encourage you to read something about REST web services.

Also, keep in mind that Esper must be somehow persistent all the time the web service is running. If you just put it in a class with resource methods it will not exist outside of the invoke scope. You can make Esper as a singleton and put in the ServletContext and call it like that.

Aleksandar Stojadinovic
  • 4,851
  • 1
  • 34
  • 56