Improve use of Charsets

This commit is contained in:
Andrew Gaul 2012-10-07 22:20:01 -07:00
parent 5b213c2171
commit 9024c77405
10 changed files with 20 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package org.jclouds.cloudstack.functions; package org.jclouds.cloudstack.functions;
import java.nio.charset.Charset;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.spec.KeySpec; import java.security.spec.KeySpec;
@ -14,6 +13,7 @@ import org.jclouds.domain.LoginCredentials;
import org.jclouds.encryption.internal.Base64; import org.jclouds.encryption.internal.Base64;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -48,7 +48,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
cipher.init(Cipher.DECRYPT_MODE, privKey); cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = Base64.decode(dataAndKey.getEncryptedPassword()); byte[] cipherText = Base64.decode(dataAndKey.getEncryptedPassword());
byte[] plainText = cipher.doFinal(cipherText); byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charset.forName("ASCII")); String password = new String(plainText, Charsets.US_ASCII);
return LoginCredentials.builder() return LoginCredentials.builder()
.user("Administrator") .user("Administrator")

View File

@ -18,7 +18,6 @@
*/ */
package org.jclouds.ec2.compute.functions; package org.jclouds.ec2.compute.functions;
import java.nio.charset.Charset;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.spec.KeySpec; import java.security.spec.KeySpec;
@ -33,6 +32,7 @@ import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
import org.jclouds.encryption.internal.Base64; import org.jclouds.encryption.internal.Base64;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.inject.Singleton; import com.google.inject.Singleton;
@ -66,7 +66,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Passwo
cipher.init(Cipher.DECRYPT_MODE, privKey); cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = Base64.decode(dataAndKey.getPasswordData().getPasswordData()); byte[] cipherText = Base64.decode(dataAndKey.getPasswordData().getPasswordData());
byte[] plainText = cipher.doFinal(cipherText); byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charset.forName("ASCII")); String password = new String(plainText, Charsets.US_ASCII);
return LoginCredentials.builder() return LoginCredentials.builder()
.user("Administrator") .user("Administrator")

View File

@ -49,6 +49,7 @@ import org.jclouds.openstack.swift.options.ListContainerOptions;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@ -247,7 +248,7 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
} }
// Test PUT chunked/streamed upload with data of "unknown" length // Test PUT chunked/streamed upload with data of "unknown" length
ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8")); ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
SwiftObject blob = getApi().newSwiftObject(); SwiftObject blob = getApi().newSwiftObject();
blob.getInfo().setName("chunked-object"); blob.getInfo().setName("chunked-object");
blob.setPayload(bais); blob.setPayload(bais);

View File

@ -18,6 +18,8 @@
*/ */
package org.jclouds.osgi; package org.jclouds.osgi;
import com.google.common.base.Charsets;
import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.ApiMetadata;
import org.jclouds.apis.ApiRegistry; import org.jclouds.apis.ApiRegistry;
import org.jclouds.providers.ProviderMetadata; import org.jclouds.providers.ProviderMetadata;
@ -158,7 +160,7 @@ public class MetadataBundleListener implements BundleListener {
try { try {
is = resource.openStream(); is = resource.openStream();
reader = new InputStreamReader(is, "UTF-8"); reader = new InputStreamReader(is, Charsets.UTF_8);
bufferedReader = new BufferedReader(reader); bufferedReader = new BufferedReader(reader);
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {

View File

@ -62,6 +62,7 @@ import org.jclouds.rest.annotations.Credential;
import org.jclouds.rest.annotations.Identity; import org.jclouds.rest.annotations.Identity;
import org.jclouds.rest.config.RestClientModule; import org.jclouds.rest.config.RestClientModule;
import com.google.common.base.Charsets;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.inject.Provides; import com.google.inject.Provides;
@ -139,7 +140,7 @@ public class AzureManagementRestClientModule extends RestClientModule<AzureManag
CertificateFactory cf = CertificateFactory.getInstance("X.509"); CertificateFactory cf = CertificateFactory.getInstance("X.509");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Collection<Certificate> certs = (Collection<Certificate>) cf.generateCertificates(new ByteArrayInputStream( Collection<Certificate> certs = (Collection<Certificate>) cf.generateCertificates(new ByteArrayInputStream(
pemCerts.getBytes("UTF-8"))); pemCerts.getBytes(Charsets.UTF_8)));
keyStore.load(null); keyStore.load(null);
keyStore.setKeyEntry("dummy", privateKey, keyStorePassword.toCharArray(), keyStore.setKeyEntry("dummy", privateKey, keyStorePassword.toCharArray(),
certs.toArray(new java.security.cert.Certificate[0])); certs.toArray(new java.security.cert.Certificate[0]));

View File

@ -257,7 +257,7 @@ public class DataApiLiveTest extends BaseCDMIApiLiveTest {
bytes = bos.toByteArray(); bytes = bos.toByteArray();
// String.getBytes causes an exception CreateDataObjectOptions need to investigate byte // String.getBytes causes an exception CreateDataObjectOptions need to investigate byte
// arrays // arrays
// bytes = value.getBytes("UTF-8"); // bytes = value.getBytes(Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(bytes); pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(bytes);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions); dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject); assertNotNull(dataObject);

View File

@ -130,7 +130,7 @@ public class DataNonCDMIContentTypeApiLiveTest extends BaseCDMIApiLiveTest {
// exercise create data object with none cdmi put with payload byte array. // exercise create data object with none cdmi put with payload byte array.
value = "Hello CDMI World non-cdmi byte array"; value = "Hello CDMI World non-cdmi byte array";
bytes = value.getBytes("UTF-8"); bytes = value.getBytes(Charsets.UTF_8);
payloadIn = new ByteArrayPayload(bytes); payloadIn = new ByteArrayPayload(bytes);
payloadIn.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(payloadIn.getContentMetadata() payloadIn.setContentMetadata(BaseMutableContentMetadata.fromContentMetadata(payloadIn.getContentMetadata()
.toBuilder().contentType(MediaType.PLAIN_TEXT_UTF_8.toString()).build())); .toBuilder().contentType(MediaType.PLAIN_TEXT_UTF_8.toString()).build()));

View File

@ -79,6 +79,7 @@ import org.jclouds.rest.annotations.Identity;
import org.jclouds.rest.config.RestClientModule; import org.jclouds.rest.config.RestClientModule;
import org.jclouds.xml.XMLParser; import org.jclouds.xml.XMLParser;
import com.google.common.base.Charsets;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.inject.Provides; import com.google.inject.Provides;
@ -348,7 +349,7 @@ public class FGCPRestClientModule extends
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Collection<Certificate> certs = (Collection<Certificate>) cf Collection<Certificate> certs = (Collection<Certificate>) cf
.generateCertificates(new ByteArrayInputStream(pemCerts .generateCertificates(new ByteArrayInputStream(pemCerts
.getBytes("UTF-8"))); .getBytes(Charsets.UTF_8)));
keyStore.load(null); keyStore.load(null);
keyStore.setKeyEntry("dummy", privateKey, keyStore.setKeyEntry("dummy", privateKey,
keyStorePassword.toCharArray(), keyStorePassword.toCharArray(),

View File

@ -26,6 +26,7 @@ import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.Signature; import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException; import java.security.UnrecoverableKeyException;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
@ -203,10 +204,10 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
String signed; String signed;
try { try {
signer.update(stringToSign.getBytes("UTF-8")); signer.update(stringToSign.getBytes(Charsets.UTF_8));
signed = Base64.encodeBytes(signer.sign()).replace("\n", "\r\n"); signed = Base64.encodeBytes(signer.sign()).replace("\n", "\r\n");
// signed = CryptoStreams.base64(signer.sign()); // signed = CryptoStreams.base64(signer.sign());
} catch (Exception e) { } catch (SignatureException e) {
throw new HttpException("error signing request", e); throw new HttpException("error signing request", e);
} }
// if (signatureWire.enabled()) // if (signatureWire.enabled())

View File

@ -49,6 +49,7 @@ import org.jclouds.util.Strings2;
import org.jclouds.util.Throwables2; import org.jclouds.util.Throwables2;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -306,7 +307,7 @@ public class AzureBlobClientLiveTest extends BaseBlobStoreIntegrationTest {
assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422); assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
} }
ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8")); ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
object = getApi().newBlob(); object = getApi().newBlob();
object.getProperties().setName("chunked-object"); object.getProperties().setName("chunked-object");
object.setPayload(bais); object.setPayload(bais);