Is it possible to intercept Java 11 HttpClient requests? - java

Basically as the title says. Apache HttpClient and Spring RestTemplate allow for defining custom interceptors which wrap around requests/responses and allow for additional (global) modification of request parameters, logging, etc...
I do not see such a feature available in standard Java implementation of java.net.http.HttpClient (as of Java 11). Am I missing something or is there no way to intercept all requests/responses on a single HttpClient?

There isn't a built-in solution, but you can write your own code as in this answer or use this interceptable-http-client library.

Related

Implementing Deprecated Header in HTTP Response

I am trying to implement deprecation header for api responses in my application. The problem is that recently our codebase has gone through major refactoring and restructuring with lots of apis being deprecated. all of these api endpoints are annotated with #Deprecated. is there anyway i can use this annotation to change the response without having to make code changes at each and every endpoint location?
I have tried to change response in the code at each location and this would be tedious, expecting a smarter way to go about this.
Note- My application is in JAVA and uses spring-boot framework.

Does restlet support ContainerResponseFilter on GAE?

Reslet 2.2.1 for GAE does not include ContainerResponseFilter (or more generally javax.ws.rs.container.*).
How do I implement such a filter in this environment?
Or is there another work-around to add access-control headers (can be a temporary kludge)?
Restlet Framework only supports JAX-RS 1.1 spec at this point, an upgrade is planned for RF V2.3 to JAX-RS 2.0 but isn't available or certain at this point.
I would recommend working around this class if possible, or try using Jersey or RESTeasy maybe.
UPDATE: as a workaround, you can manually set the response (CORS) headers by using an org.restlet.routing.Filter subclass that would achieve the same effects by overloading its afterHandle() method.

I have REST API and want to reuse it for exposing differen format API on same server

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. :-)

Dump http headers

I would like to enable dumping of HTTP headers in a JAX-WS RI client.
I do not want to dump the actual content, only the headers. Beware that under the hood JAX-WS RI uses HttpURLConnection so please don't reply with something related to Apache's HttpClient.
I need to enable it programmatically, not by way of a configuration file.
I'm aware of the answer on this question, but I'm really struggling to make that work with Java 7 so I'm wondering if I'm going about this the wrong way. In essence what I want to do is to be able to see the HTTP headers on the http traffic underlying JAX-WS and I don't really care how it is achieved as long as I can enable it programmatically.
This section of JAX-WS guide about using handlers to access HTTP headers might help.

Can Jersey-Json/Jackson be configured to produce json-rpc-1.0?

I have an api that I need to call that uses json-rpc-1.0 (I have no control of the implementation). I'd like to be able to manipulate jaxb objects and have them marshal into json-rpc-1.0 format. Is this something that jackson/jersey-json can accomplish or is there a different library that can achieve this goal?
No, Jersey cannot be configured to support JSON-RPC, but Jersey could be used to implement JSON-RPC. JSON-RPC specifies a protocol with requests and responses and can be implemented over HTTP. Jersey (and all JAX-RPC implementations) can be used to implement any HTTP based protocol.
However, since the latest JSON-RPC spec (2.0 - http://www.jsonrpc.org/specification) does not specify how request and responses are mapped to HTTP verbs (POST, GET, etc.) and status codes, there could be some incompatibilities between JSON-RPC implementations.
Instead of using Jersey to implement JSON-RPC, you might be better off using an existing JSON-RPC implementation, see also http://en.wikipedia.org/wiki/JSON-RPC#Implementations

Categories