-
Notifications
You must be signed in to change notification settings - Fork 38.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce interceptors for RestTemplate [SPR-7494] #12152
Comments
Duane Zamrok commented I've attached a maven project that contains code to implement this feature. The project contains interfaces and classes to describe a HttpContext and extends the existing Spring 3.0.3 org.springframework.web.client.RestTemplate class. The major shortcoming, so far as I can tell, of this code base is that the org.springframework.web.client.ExtendedRestTemplate does not account for the fact that the org.springframework.http.client.SimpleClientHttpResponse and the org.springframework.http.client.CommonsClientHttpResponse both maintain a single java.io.InputStream as their body. This is a shortcoming because it means that if a HttpClientInterceptor reads the body of the org.springframework.web.client.context.HttpMessageContext#getResponse then the input stream will be closed when the org.springframework.http.client.ExtendedRestTemplate attempts to extract the response. I've implemented a workaround to this problem with the org.springframework.http.client.RepeatableClientHttpResponse, and demonstrated its use with the org.springframework.web.client.interceptor.LoggingHttpClientInterceptor, but it remains a problem that must be adressed by the interceptor. Note: |
Duane Zamrok commented I think the ClientHttpResponse, at least, could benefit from having a method along the lines of "isBodyRepeatable" to denote whether or not it is safe to read the InputStream returned by "getBody" more than once. I've been thinking about this problem a little bit more, and noticed a problem with the LoggingHttpClientInterceptor. The interceptor will wrap the response without testing to see if it is a RepeatableClientHttpResponse. This means that if a RepeatableClientHttpResponse is received by the LoggingHttpClientInterceptor it will be wrapped inside of yet another RepeatableClientHttpResponse. To avoid this I would need to specifically test to see if the resposne I recieved is a RepeatableClientHttpResponse; which feels rather tightly coupled to a specific implementation. Without the fix however I will waste both memory and CPU on a task that is unnecessary. Please don't take offense to the following, because I think the foundations of Spring 3.0 are way better than anything I could have done myself. However I think the ClientHttpResponse, ClientHttpRequest, HttpInputMessage, and HttpOutputMessage interfaces are all a little bulky compared to the spring-ws implementations. I'm sure it is because the interfaces were not written with multiple consumers in mind, but even so it makes working with them an exercise in extreme caution. I used my debugger extensivly to ensure that the stream states were not violated, and the LoggingInterceptor only works when the body of the request is a ByteArrayOutputStream. |
Arjen Poutsma commented Fixed by adding a ClientHttpRequestInterceptor interface, which can be set on the RestTemplate |
Brian Relph commented What about adding response interceptors? Even if the issues around the response body inputstream remain unresolved, I see value in being able to inspect the headers ... |
Arjen Poutsma commented
Soon, Keith will write a blog entry about these interceptors and how they are used in Spring Social to provide OAuth authentication to the RestTemplate. |
Francisco Lozano commented PLS back-port this to Spring Android's rest-template |
Arjen Poutsma commented
|
Roy Clarkson commented Interceptors are already available in Spring Android. |
chanwook park commented
|
Arjen Poutsma opened SPR-7494 and commented
The RestTemplate will benefit from having interceptor-style delegates, similar to the Spring-WS ClientInterceptors (see http://static.springsource.org/spring-ws/sites/1.5/apidocs/org/springframework/ws/client/support/interceptor/ClientInterceptor.html)
Attachments:
Issue Links:
1 votes, 6 watchers
The text was updated successfully, but these errors were encountered: