Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding user agent to request as per #1, some code cleanup
- Loading branch information
1 parent
c485e4f
commit 01d5c7b
Showing
8 changed files
with
118 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/springframework/social/lastfm/connect/RestTemplateWithHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.springframework.social.lastfm.connect; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.web.client.RequestCallback; | ||
import org.springframework.web.client.ResponseExtractor; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class RestTemplateWithHeaders extends RestTemplate { | ||
|
||
private HttpHeaders headers = new HttpHeaders(); | ||
|
||
public HttpHeaders getRequestHttpHeaders() { | ||
return headers; | ||
} | ||
|
||
|
||
public RestTemplateWithHeaders(ClientHttpRequestFactory requestFactory,HttpHeaders headers) { | ||
super(requestFactory); | ||
} | ||
|
||
|
||
@Override | ||
protected <T> T doExecute(URI url, HttpMethod method, | ||
RequestCallback requestCallback, | ||
ResponseExtractor<T> responseExtractor) throws RestClientException { | ||
|
||
|
||
RequestCallbackDecorator requestCallbackDecorator = | ||
new RequestCallbackDecorator(requestCallback); | ||
|
||
return super.doExecute(url, method, | ||
requestCallbackDecorator, responseExtractor); | ||
|
||
} | ||
|
||
|
||
|
||
private class RequestCallbackDecorator implements RequestCallback | ||
{ | ||
|
||
public RequestCallbackDecorator( | ||
RequestCallback targetRequestCallback) { | ||
this.targetRequestCallback = targetRequestCallback; | ||
} | ||
|
||
private RequestCallback targetRequestCallback; | ||
|
||
@Override | ||
public void doWithRequest(ClientHttpRequest request) | ||
throws IOException { | ||
|
||
request.getHeaders().putAll(headers); | ||
|
||
if (null != targetRequestCallback) { | ||
targetRequestCallback.doWithRequest(request); | ||
} | ||
} | ||
} | ||
} |