[4231] | 1 | package oauth.signpost.signature;
|
---|
| 2 |
|
---|
[6849] | 3 | import java.util.Iterator;
|
---|
| 4 |
|
---|
[4231] | 5 | import oauth.signpost.OAuth;
|
---|
| 6 | import oauth.signpost.http.HttpParameters;
|
---|
| 7 | import oauth.signpost.http.HttpRequest;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Writes to a URL query string. <strong>Note that this currently ONLY works
|
---|
| 11 | * when signing a URL directly, not with HTTP request objects.</strong> That's
|
---|
| 12 | * because most HTTP request implementations do not allow the client to change
|
---|
| 13 | * the URL once the request has been instantiated, so there is no way to append
|
---|
| 14 | * parameters to it.
|
---|
| 15 | *
|
---|
| 16 | * @author Matthias Kaeppler
|
---|
| 17 | */
|
---|
| 18 | public class QueryStringSigningStrategy implements SigningStrategy {
|
---|
| 19 |
|
---|
| 20 | private static final long serialVersionUID = 1L;
|
---|
| 21 |
|
---|
| 22 | public String writeSignature(String signature, HttpRequest request,
|
---|
| 23 | HttpParameters requestParameters) {
|
---|
| 24 |
|
---|
[6849] | 25 | // add all (x_)oauth parameters
|
---|
| 26 | HttpParameters oauthParams = requestParameters.getOAuthParameters();
|
---|
| 27 | oauthParams.put(OAuth.OAUTH_SIGNATURE, signature, true);
|
---|
[4231] | 28 |
|
---|
[6849] | 29 | Iterator<String> iter = oauthParams.keySet().iterator();
|
---|
| 30 |
|
---|
| 31 | // add the first query parameter (we always have at least the signature)
|
---|
| 32 | String firstKey = iter.next();
|
---|
| 33 | StringBuilder sb = new StringBuilder(OAuth.addQueryString(request.getRequestUrl(),
|
---|
| 34 | oauthParams.getAsQueryString(firstKey)));
|
---|
| 35 |
|
---|
| 36 | while (iter.hasNext()) {
|
---|
[4231] | 37 | sb.append("&");
|
---|
[6849] | 38 | String key = iter.next();
|
---|
| 39 | sb.append(oauthParams.getAsQueryString(key));
|
---|
[4231] | 40 | }
|
---|
| 41 |
|
---|
| 42 | String signedUrl = sb.toString();
|
---|
| 43 |
|
---|
| 44 | request.setRequestUrl(signedUrl);
|
---|
| 45 |
|
---|
| 46 | return signedUrl;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | }
|
---|