| 1 | package oauth.signpost.signature;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.Iterator;
|
|---|
| 4 |
|
|---|
| 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 | @Override
|
|---|
| 23 | public String writeSignature(String signature, HttpRequest request,
|
|---|
| 24 | HttpParameters requestParameters) {
|
|---|
| 25 |
|
|---|
| 26 | // add all (x_)oauth parameters
|
|---|
| 27 | HttpParameters oauthParams = requestParameters.getOAuthParameters();
|
|---|
| 28 | oauthParams.put(OAuth.OAUTH_SIGNATURE, signature, true);
|
|---|
| 29 |
|
|---|
| 30 | Iterator<String> iter = oauthParams.keySet().iterator();
|
|---|
| 31 |
|
|---|
| 32 | // add the first query parameter (we always have at least the signature)
|
|---|
| 33 | String firstKey = iter.next();
|
|---|
| 34 | StringBuilder sb = new StringBuilder(OAuth.addQueryString(request.getRequestUrl(),
|
|---|
| 35 | oauthParams.getAsQueryString(firstKey)));
|
|---|
| 36 |
|
|---|
| 37 | while (iter.hasNext()) {
|
|---|
| 38 | sb.append("&");
|
|---|
| 39 | String key = iter.next();
|
|---|
| 40 | sb.append(oauthParams.getAsQueryString(key));
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | String signedUrl = sb.toString();
|
|---|
| 44 |
|
|---|
| 45 | request.setRequestUrl(signedUrl);
|
|---|
| 46 |
|
|---|
| 47 | return signedUrl;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | }
|
|---|