Similar to HttpFilter (javax.Servlet.Filter ) which when added in web.xml can intercept any incoming request to JVM / outgoing (as response) independent of framework ( Spring/CXF/Jersy etc ) , I am trying to find an API which could intercept any outgoing HTTP calls from JVM to add/modify headers independent of framework. Also routing the requests through a proxy sounds overwhelming.
Quite often the word Outgoing HTTP call is misinterpreted in the forums so let me explain with example.
Let us assume there are two JVMs, jvm1 and jvm2. and there are HTTP calls being made from JVM1 to JVM2. I would like to intercept the HTTP connection being made from JVM1 to modify the headers information before the call happens. I do not want the code to be tied to a specific framework so that I can as bundle the interceptor as a jar and share it with application team. Changes in web.xml is fine.
Any suggestions? Please HELP!
Both JVM may use java.net.Socket or java.net.ServerSocket in any way for communication and there is no way to intercept anything here.
You may intercept any HTTP traffic if you connect thru a (transparent) proxy. The proxy will intercept anything and you may modify any content.
We went with aspectj. Not the best solution, but the only option we got.
Related
I implemented and exposed REST API on my server. Now I need to expose same API, but I can't use REST (it's actually websocket messages), it will be some custom format. Don't ask why )
I imagine message transformation from my custom format to http request, then process it in my web-server, transform response back to my custom format, and send to client.
The simplest way is regular http call to localhost. For example (java):
HttpURLConnection con = (HttpURLConnection) new URL("http://localhost/api/...").openConnection();
and so on, or using some http client library.
But I'm afraid there will be too much overhead, creating connection, etc.
Another ways:
I use Tomcat. Push my request directly to tomcat somehow.
I use Guice, and all requests go through GuiceFilter. Craft ServetRequest, ServletResponce and FilterChain objects and directly call GuiceFilter.doFilter.
I use GuiceContainer for Jersey. Some test frameworks use it for REST API testing, but also need to craft request/response objects.
There is no standard way to craft request object at all.
And I don't know on which level it's better to add my custom requests.
Hope I described my problem clearly.
Atmosphere may be a good fit here. It's allegedly compatible with both Resteasy and Jersey, so that's a plus, it simply adds WebSocket functionality on top of these.
Caveat: I haven't tried this myself, but came across it when looking for the same capability. :-)
People commonly believe that asynchronized IO operations (such as HTTP calls via async client implementation) are superior than synchronous counterparts in terms of system scalability and resource usage.
I am developing a classic Java web application, it uses conventional servlet and runs in Tomcat. There is a request processing routine which looks like:
doPost() {
// make HTTP call to remote host
// upon completion of the HTTP call, return success
}
There is a choice: to make the HTTP call, I may choose to use a conventional blocking HTTP client library (such as Apache HTTPClient) or a non-blocking asynchronous HTTP client (such as Apache AsyncHTTPClient).
In this scenario, is there any advantage of using asynchronous HTTP client implementation? I am in disbelief:
The doPost() must wait for the HTTP call response, therefore using asynchronous HTTP client does not improve responsiveness.
Tomcat starts a thread pool for processing requests. The doPost() routine must block at the HTTP call and therefore using asynchronous HTTP client does not decrease the total number of JVM threads.
Are my thoughts valid?
IMMO your thoughts are perfectly valid. Async is no magic, in your scenario you need to wait to external resource to respond to the servlet client, i see no benefit in use AsyncHttpClient. Probably in other circumstances there is and advantage of the async solution, for example:
Your client application is async too, you receive the request call async http and immediately returns the control to your client. When you really receive the answer from the external resource you need to communicate with your client via comet or websockets. This way your server its never blocked and the scalability its better, but probably its a huge change in your architecture and you need to be sure that you really need to make a change like this. And off course this systems has other problems.
You have more than one call to external resources, in this scenario make async calls and then waits before return to your client for the completion of the various external resources its clearly a big improvement.
Only one more thing, remember ALWAYS put a timeout in a call to external resources, probably your are aware of this, but i learn this the hard way in the past.
I need to develop a client interface to rest webservices in Java.
The idea is to provide a form-gui that permits to choose the url, method, Headers, cookie and body (for post request)
and then clicking on the submit a generic rest Proxy client method is called, this set up the informations and invokes Jersey / JAX-RS client methods (via a series of if / then condition).
In order to not to reinvent the wheel, is there something already built to allow that?
Thank you!
We usually use Chrome REST console in order to do that:
https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn
It's pretty useful but still have some limits, for instance when you need to change the UserAgent.
In those specific case I recommend the old commands curl or wget.
EDIT: this client proved to be even more useful: https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
It allow you to save your favourite HTTP request to your service.
You can use Rest Client in Firefox or Advanced Rest Client in chrome. They are actually plugins for each browser.
I have a couple of http methods in my application which is hosted in JBoss.
And Now I am trying to find some kind of hot-way to disable one of them,like click a button in a certain of page or calling a certain of http method. What i means of 'disable it' is making any web client which intends to send get/post request to it will go to failure . Maybe we can say the web client will got a http 404 response.
Can anybody give me some solutions? Thanks.
I think JMX would be appropriate for this situation.
You can pretty easily create an MBean (Managed Bean, a component of JMX) with Managed Attributes corresponding to boolean's for each of the endpoints you want to be disable-able. Registering it is the hard part, but there are libraries out there that make working with JMX easier. Spring has good support for setting up and working with MBeans.
As far as a JMX client goes, I usually use VisualVM, which ships with the JDK. From it, you can invoke methods on your MBeans at runtime, or even change their properties.
I am a student building a http proxy server. I want to cache those requests that are frequently accessed. May I get any idea about this? Especially in java.
To figure out what you need to implement, read and understand the HTTP specification. Focus particularly on the sections on how a proxy is supposed to behave.
You could possibly base part of the implementation on the Apache HttpClient library, but I have a feeling that the APIs will prove to be unsuitable for the proxy server use-case.
I'd also like to point out that a more practical way to implement an HTTP proxy server would be to simply deploy an existing server like Squid.