Permalink
Browse files

Fix reliance on default encoding, so that SignPost works on non-ASCII…

… platforms.

Fix test which fails on some IBM JVMs due to reliance on HashMap ordering.
  • Loading branch information...
RobinFernandes RobinFernandes
RobinFernandes authored and RobinFernandes committed Jun 5, 2010
1 parent c403143 commit f0aa236d214734ab0acebfe4cc40926a032ae88e
@@ -10,6 +10,8 @@
*/
package oauth.signpost;
import static oauth.signpost.OAuth.ISO8859_1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
@@ -225,7 +227,7 @@ protected void handleUnexpectedResponse(int statusCode, HttpResponse response) t
if (response == null) {
return;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getContent()));
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getContent(), ISO8859_1));
StringBuilder responseBody = new StringBuilder();
String line = reader.readLine();
@@ -14,13 +14,17 @@
*/
package oauth.signpost;
import static oauth.signpost.OAuth.ISO8859_1;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -60,6 +64,8 @@
public static final String OAUTH_CALLBACK_CONFIRMED = "oauth_callback_confirmed";
public static final String OAUTH_VERIFIER = "oauth_verifier";
public static final Charset ISO8859_1 = Charset.forName("ISO8859-1");
/**
* Pass this value as the callback "url" upon retrieving a request token if
@@ -110,9 +116,9 @@ public static String percentDecode(String s) {
} else {
into.write('&');
}
into.write(percentEncode(safeToString(entry.getKey())).getBytes());
into.write(percentEncode(safeToString(entry.getKey())).getBytes(ISO8859_1.name()));
into.write('=');
into.write(percentEncode(safeToString(entry.getValue())).getBytes());
into.write(percentEncode(safeToString(entry.getValue())).getBytes(ISO8859_1.name()));
}
}
}
@@ -126,7 +132,8 @@ public static String percentDecode(String s) {
throws IOException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
formEncode(parameters, b);
return new String(b.toByteArray());
ByteBuffer bb = ByteBuffer.wrap(b.toByteArray());
return ISO8859_1.decode(bb).toString();
}
/** Parse a form-urlencoded document. */
@@ -155,7 +162,7 @@ public static HttpParameters decodeForm(String form) {
public static HttpParameters decodeForm(InputStream content)
throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
content, ISO8859_1));
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
@@ -14,9 +14,15 @@
*/
package oauth.signpost.signature;
import static oauth.signpost.OAuth.ISO8859_1;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import oauth.signpost.OAuth;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.http.HttpRequest;
import oauth.signpost.http.HttpParameters;
@@ -59,11 +65,18 @@ public void setTokenSecret(String tokenSecret) {
}
protected byte[] decodeBase64(String s) {
return base64.decode(s.getBytes());
try {
return base64.decode(s.getBytes(ISO8859_1.name()));
} catch (UnsupportedEncodingException e) {
// Won't happen - all JVMs must support ISO8859_1.
return null;
}
}
protected String base64Encode(byte[] b) {
return new String(base64.encode(b));
ByteBuffer bb = ByteBuffer.wrap(base64.encode(b));
return ISO8859_1.decode(bb).toString();
}
private void readObject(java.io.ObjectInputStream stream)
@@ -1,6 +1,7 @@
package oauth.signpost;
import static junit.framework.Assert.assertNull;
import static oauth.signpost.OAuth.ISO8859_1;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -88,7 +89,7 @@ public void shouldIncludeOAuthAndQueryAndBodyParams() throws Exception {
// mock a request that has custom query, body, and header params set
HttpRequest request = mock(HttpRequest.class);
when(request.getRequestUrl()).thenReturn("http://example.com?a=1+1");
ByteArrayInputStream body = new ByteArrayInputStream("b=2+2".getBytes());
ByteArrayInputStream body = new ByteArrayInputStream("b=2+2".getBytes(ISO8859_1.name()));
when(request.getMessagePayload()).thenReturn(body);
when(request.getContentType()).thenReturn(
"application/x-www-form-urlencoded; charset=ISO-8859-1");
@@ -50,7 +50,7 @@ public void shouldCorrectlyPercentDecodeReservedCharacters() {
@Test
public void shouldCorrectlyFormEncodeParameters() throws Exception {
HashMap<String, String> params = new HashMap<String, String>();
HashMap<String, String> params = new LinkedHashMap<String, String>();
params.put("one", rfc3986ReservedCharacters);
params.put(rfc3986ReservedCharacters, rfc3986UnreservedCharacters);
@@ -1,11 +1,13 @@
package oauth.signpost.basic;
import static junit.framework.Assert.assertTrue;
import static oauth.signpost.OAuth.ISO8859_1;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import oauth.signpost.OAuth;
import oauth.signpost.http.HttpRequest;
import org.junit.Before;
@@ -59,7 +61,7 @@ public void shouldReturnCorrectContentType() {
@Test
public void shouldReturnCorrectMessagePayload() throws Exception {
String actual = new BufferedReader(new InputStreamReader(
request.getMessagePayload())).readLine();
request.getMessagePayload(), ISO8859_1)).readLine();
assertEquals(PAYLOAD, actual);
}
}
@@ -1,5 +1,6 @@
package oauth.signpost.mocks;
import static oauth.signpost.OAuth.ISO8859_1;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -24,7 +25,7 @@ public DefaultOAuthProviderMock(String requestTokenUrl, String accessTokenUrl, S
public void mockConnection(String responseBody) throws Exception {
this.connectionMock = mock(HttpURLConnection.class);
InputStream is = new ByteArrayInputStream(responseBody.getBytes());
InputStream is = new ByteArrayInputStream(responseBody.getBytes(ISO8859_1.name()));
when(connectionMock.getResponseCode()).thenReturn(200);
when(connectionMock.getInputStream()).thenReturn(is);
}

0 comments on commit f0aa236

Please sign in to comment.