diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2012-01-22 12:01:47 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2012-01-22 12:01:47 +0100 |
commit | aa68184d2b6ed595660060b3e10e6f60ae8cbe1f (patch) | |
tree | 40f406f4d7fe76ed75f7a82bfa2dd59c68cd155e /main/src/cgeo/geocaching/network | |
parent | a7c2d9dcf53ad6c3a0acfeaa483b9e0cfd43ab94 (diff) | |
download | cgeo-aa68184d2b6ed595660060b3e10e6f60ae8cbe1f.zip cgeo-aa68184d2b6ed595660060b3e10e6f60ae8cbe1f.tar.gz cgeo-aa68184d2b6ed595660060b3e10e6f60ae8cbe1f.tar.bz2 |
refactoring: moved some classes to packages, renaming
Diffstat (limited to 'main/src/cgeo/geocaching/network')
-rw-r--r-- | main/src/cgeo/geocaching/network/OAuth.java | 1 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/network/Parameters.java | 74 |
2 files changed, 74 insertions, 1 deletions
diff --git a/main/src/cgeo/geocaching/network/OAuth.java b/main/src/cgeo/geocaching/network/OAuth.java index 9e0a703..6d82bf7 100644 --- a/main/src/cgeo/geocaching/network/OAuth.java +++ b/main/src/cgeo/geocaching/network/OAuth.java @@ -1,6 +1,5 @@ package cgeo.geocaching.network; -import cgeo.geocaching.Parameters; import cgeo.geocaching.Settings; import cgeo.geocaching.cgBase; import cgeo.geocaching.utils.CryptUtils; diff --git a/main/src/cgeo/geocaching/network/Parameters.java b/main/src/cgeo/geocaching/network/Parameters.java new file mode 100644 index 0000000..e65bec2 --- /dev/null +++ b/main/src/cgeo/geocaching/network/Parameters.java @@ -0,0 +1,74 @@ +package cgeo.geocaching.network; + +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.protocol.HTTP; + +import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +/** + * List of key/values pairs to be used in a GET or POST request. + * + */ +public class Parameters extends ArrayList<NameValuePair> { + + private static final long serialVersionUID = 1L; + + /** + * @param keyValues + * list of initial key/value pairs + * @throws InvalidParameterException + * if the number of key/values is unbalanced + */ + public Parameters(final String... keyValues) { + super(); + put(keyValues); + } + + private static final Comparator<NameValuePair> comparator = new Comparator<NameValuePair>() { + @Override + public int compare(final NameValuePair nv1, final NameValuePair nv2) { + final int comparedKeys = nv1.getName().compareTo(nv2.getName()); + return comparedKeys != 0 ? comparedKeys : nv1.getValue().compareTo(nv2.getValue()); + } + }; + + /** + * Add new key/value pairs to the current parameters. + * + * @param keyValues + * list of key/value pairs + * @throws InvalidParameterException + * if the number of key/values is unbalanced + */ + public void put(final String... keyValues) { + if (keyValues.length % 2 == 1) { + throw new InvalidParameterException("odd number of parameters"); + } + for (int i = 0; i < keyValues.length; i += 2) { + add(new BasicNameValuePair(keyValues[i], keyValues[i + 1])); + } + } + + /** + * Lexically sort key/value pairs first by key, then by value. + * + * Some signing algorithms need the values to be ordered before issuing the signature. + */ + public void sort() { + Collections.sort(this, comparator); + } + + /** + * @return the URL encoded string corresponding to those parameters + */ + @Override + public String toString() { + return URLEncodedUtils.format(this, HTTP.UTF_8); + } + +} |