Skip to content

Commit

Permalink
Fix reliance on default encoding, so that SignPost works on non-ASCII…
Browse files Browse the repository at this point in the history
… platforms.

Fix test which fails on some IBM JVMs due to reliance on HashMap ordering.
  • Loading branch information
RobinFernandes authored and RobinFernandes committed Jun 5, 2010
1 parent c403143 commit f0aa236
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 11 additions & 4 deletions signpost-core/src/main/java/oauth/signpost/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +64,8 @@ public class OAuth {
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
Expand Down Expand Up @@ -110,9 +116,9 @@ public static <T extends Map.Entry<String, String>> void formEncode(Collection<T
} 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()));
}
}
}
Expand All @@ -126,7 +132,8 @@ public static <T extends Map.Entry<String, String>> String formEncode(Collection
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. */
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion signpost-core/src/test/java/oauth/signpost/OAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
Expand Down

0 comments on commit f0aa236

Please sign in to comment.