Remove unneeded UnsupportedEncodingExceptions

Enabled by use of Charsets.  Remaining calls due to URLEncoder.encode
and decode.
This commit is contained in:
Andrew Gaul 2012-10-07 17:07:59 -07:00
parent 79f0361e4a
commit 07ca6ab27b
7 changed files with 28 additions and 60 deletions

View File

@ -24,7 +24,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
@ -342,16 +341,12 @@ public class CryptoStreams {
int currentPos = 0; int currentPos = 0;
public boolean processBytes(byte[] buf, int off, int len) { public boolean processBytes(byte[] buf, int off, int len) {
try { if (currentPos == 0 && new String(Arrays.copyOfRange(buf, off, 2), Charsets.US_ASCII).equals("0x")) {
if (currentPos == 0 && new String(Arrays.copyOfRange(buf, off, 2), "ASCII").equals("0x")) {
off += 2; off += 2;
} }
byte[] decoded = hex(new String(Arrays.copyOfRange(buf, off, len), "ASCII")); byte[] decoded = hex(new String(Arrays.copyOfRange(buf, off, len), Charsets.US_ASCII));
out.write(decoded, 0, decoded.length); out.write(decoded, 0, decoded.length);
currentPos += len; currentPos += len;
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("ASCII must be supported");
}
return true; return true;
} }

View File

@ -20,7 +20,6 @@ package org.jclouds.http.filters;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
@ -40,7 +39,7 @@ public class BasicAuthenticationTest {
private static final String USER = "Aladdin"; private static final String USER = "Aladdin";
private static final String PASSWORD = "open sesame"; private static final String PASSWORD = "open sesame";
public void testAuth() throws UnsupportedEncodingException, NoSuchAlgorithmException, CertificateException { public void testAuth() throws NoSuchAlgorithmException, CertificateException {
BasicAuthentication filter = new BasicAuthentication(USER, PASSWORD, new JCECrypto(null)); BasicAuthentication filter = new BasicAuthentication(USER, PASSWORD, new JCECrypto(null));
HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://localhost").build(); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http://localhost").build();
request = filter.filter(request); request = filter.filter(request);

View File

@ -22,8 +22,6 @@ package org.jclouds.abiquo.http.filters;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.abiquo.config.AbiquoProperties.CREDENTIAL_IS_TOKEN; import static org.jclouds.abiquo.config.AbiquoProperties.CREDENTIAL_IS_TOKEN;
import java.io.UnsupportedEncodingException;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -37,6 +35,7 @@ import org.jclouds.rest.annotations.Credential;
import org.jclouds.rest.annotations.Identity; import org.jclouds.rest.annotations.Identity;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
/** /**
* Authenticates using Basic Authentication or a generated token from previous API sessions. * Authenticates using Basic Authentication or a generated token from previous API sessions.
@ -68,28 +67,19 @@ public class AbiquoAuthentication implements HttpRequestFilter
@Override @Override
public HttpRequest filter(final HttpRequest request) throws HttpException public HttpRequest filter(final HttpRequest request) throws HttpException
{ {
try String header = credentialIsToken ? tokenAuth(credential) : basicAuth(identity, credential);
{
String header =
credentialIsToken ? tokenAuth(credential) : basicAuth(identity, credential);
return request return request
.toBuilder() .toBuilder()
.replaceHeader(credentialIsToken ? HttpHeaders.COOKIE : HttpHeaders.AUTHORIZATION, .replaceHeader(credentialIsToken ? HttpHeaders.COOKIE : HttpHeaders.AUTHORIZATION,
header).build(); header).build();
} }
catch (UnsupportedEncodingException ex)
{
throw new HttpException(ex);
}
}
@VisibleForTesting @VisibleForTesting
static String basicAuth(final String user, final String password) static String basicAuth(final String user, final String password)
throws UnsupportedEncodingException
{ {
return "Basic " return "Basic "
+ CryptoStreams.base64(String.format("%s:%s", checkNotNull(user, "user"), + CryptoStreams.base64(String.format("%s:%s", checkNotNull(user, "user"),
checkNotNull(password, "password")).getBytes("UTF-8")); checkNotNull(password, "password")).getBytes(Charsets.UTF_8));
} }
@VisibleForTesting @VisibleForTesting

View File

@ -27,7 +27,6 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.util.Collection; import java.util.Collection;
import java.util.Properties; import java.util.Properties;
@ -142,7 +141,7 @@ public class AbiquoAuthenticationLiveApiTest
fail("Token authentication should have failed"); fail("Token authentication should have failed");
} }
private String getAuthtenticationToken() throws UnsupportedEncodingException private String getAuthtenticationToken()
{ {
String token = null; String token = null;

View File

@ -22,7 +22,6 @@ package org.jclouds.abiquo.http.filters;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
@ -41,8 +40,7 @@ import org.testng.annotations.Test;
public class AbiquoAuthenticationTest public class AbiquoAuthenticationTest
{ {
public void testBasicAuthentication() throws UnsupportedEncodingException, public void testBasicAuthentication() throws NoSuchAlgorithmException, CertificateException
NoSuchAlgorithmException, CertificateException
{ {
HttpRequest request = HttpRequest request =
HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build(); HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build();
@ -60,8 +58,7 @@ public class AbiquoAuthenticationTest
} }
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testBasicAuthenticationWithoutIdentity() throws UnsupportedEncodingException, public void testBasicAuthenticationWithoutIdentity() throws NoSuchAlgorithmException, CertificateException
NoSuchAlgorithmException, CertificateException
{ {
HttpRequest request = HttpRequest request =
HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build(); HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build();
@ -71,8 +68,7 @@ public class AbiquoAuthenticationTest
} }
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testBasicAuthenticationWithoutCredential() throws UnsupportedEncodingException, public void testBasicAuthenticationWithoutCredential() throws NoSuchAlgorithmException, CertificateException
NoSuchAlgorithmException, CertificateException
{ {
HttpRequest request = HttpRequest request =
HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build(); HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build();
@ -81,8 +77,7 @@ public class AbiquoAuthenticationTest
filter.filter(request); filter.filter(request);
} }
public void testTokenAuthentication() throws UnsupportedEncodingException, public void testTokenAuthentication() throws NoSuchAlgorithmException, CertificateException
NoSuchAlgorithmException, CertificateException
{ {
HttpRequest request = HttpRequest request =
HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build(); HttpRequest.builder().method("GET").endpoint(URI.create("http://foo")).build();

View File

@ -20,7 +20,6 @@ package org.jclouds.fujitsu.fgcp.filters;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.KeyStoreException; import java.security.KeyStoreException;
@ -57,6 +56,7 @@ import org.jclouds.rest.annotations.ApiVersion;
import org.jclouds.rest.annotations.Credential; import org.jclouds.rest.annotations.Credential;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
/** /**
@ -223,14 +223,8 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
String signatureData = String.format("%s&%s&%s&%s", timezone, expires, String signatureData = String.format("%s&%s&%s&%s", timezone, expires,
signatureVersion, signatureMethod); signatureVersion, signatureMethod);
try { String accessKeyId = Base64.encodeBytes(signatureData.getBytes(Charsets.UTF_8));
String accessKeyId = Base64.encodeBytes(signatureData.getBytes("UTF-8"));
return accessKeyId.replace("\n", "\r\n"); return accessKeyId.replace("\n", "\r\n");
// return CryptoStreams.base64(signatureData.getBytes("UTF-8")).;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // should never happen as
// signatureData contains only ASCII
}
} }
@Override @Override

View File

@ -20,7 +20,6 @@ package org.jclouds.vcloud.director.v1_5.binders;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.UnsupportedEncodingException;
import java.util.Map; import java.util.Map;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -30,6 +29,7 @@ import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder; import org.jclouds.rest.MapBinder;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
/** /**
@ -45,15 +45,11 @@ public class BindUserOrgAndPasswordAsBasicAuthorizationHeader implements MapBind
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) { public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
try {
String header = "Basic " String header = "Basic "
+ CryptoStreams.base64(String.format("%s@%s:%s", checkNotNull(postParams.get("user"), "user"), + CryptoStreams.base64(String.format("%s@%s:%s", checkNotNull(postParams.get("user"), "user"),
checkNotNull(postParams.get("org"), "org"), checkNotNull(postParams.get("org"), "org"),
checkNotNull(postParams.get("password"), "password")).getBytes("UTF-8")); checkNotNull(postParams.get("password"), "password")).getBytes(Charsets.UTF_8));
return (R) request.toBuilder().replaceHeader(HttpHeaders.AUTHORIZATION, header).build(); return (R) request.toBuilder().replaceHeader(HttpHeaders.AUTHORIZATION, header).build();
} catch (UnsupportedEncodingException e) {
throw Throwables.propagate(e);
}
} }
@Override @Override