diff --git a/compute/src/main/java/org/jclouds/ssh/SshKeys.java b/compute/src/main/java/org/jclouds/ssh/SshKeys.java index cffaacbe3c..66cb7177d9 100644 --- a/compute/src/main/java/org/jclouds/ssh/SshKeys.java +++ b/compute/src/main/java/org/jclouds/ssh/SshKeys.java @@ -54,8 +54,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import com.google.common.hash.HashCode; import com.google.common.hash.Hashing; -import com.google.common.io.ByteStreams; -import com.google.common.io.InputSupplier; +import com.google.common.io.ByteSource; /** * Utilities for ssh key pairs @@ -69,16 +68,16 @@ import com.google.common.io.InputSupplier; public class SshKeys { /** - * Executes {@link Pems#publicKeySpecFromOpenSSH(InputSupplier)} on the string which was OpenSSH + * Executes {@link Pems#publicKeySpecFromOpenSSH(ByteSource)} on the string which was OpenSSH * Base64 Encoded {@code id_rsa.pub} * * @param idRsaPub * formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} - * @see Pems#publicKeySpecFromOpenSSH(InputSupplier) + * @see Pems#publicKeySpecFromOpenSSH(ByteSource) */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(String idRsaPub) { try { - return publicKeySpecFromOpenSSH(ByteStreams.newInputStreamSupplier( + return publicKeySpecFromOpenSSH(ByteSource.wrap( idRsaPub.getBytes(Charsets.UTF_8))); } catch (IOException e) { throw propagate(e); @@ -95,9 +94,9 @@ public class SshKeys { * @throws IOException * if an I/O error occurs */ - public static RSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier supplier) + public static RSAPublicKeySpec publicKeySpecFromOpenSSH(ByteSource supplier) throws IOException { - InputStream stream = supplier.getInput(); + InputStream stream = supplier.openStream(); Iterable parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)), "bad format, should be: ssh-rsa AAAAB3..."); diff --git a/compute/src/test/java/org/jclouds/ssh/config/RsaSshKeyPairGeneratorTest.java b/compute/src/test/java/org/jclouds/ssh/config/RsaSshKeyPairGeneratorTest.java index a5e4840e1e..f5205cee23 100644 --- a/compute/src/test/java/org/jclouds/ssh/config/RsaSshKeyPairGeneratorTest.java +++ b/compute/src/test/java/org/jclouds/ssh/config/RsaSshKeyPairGeneratorTest.java @@ -43,7 +43,9 @@ import org.jclouds.ssh.internal.RsaSshKeyPairGenerator; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteSource; import com.google.inject.AbstractModule; import com.google.inject.Guice; @@ -60,9 +62,9 @@ public class RsaSshKeyPairGeneratorTest { @BeforeClass public void setup() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException { KeyFactory keyfactory = KeyFactory.getInstance("RSA"); - PrivateKey privateKey = keyfactory.generatePrivate(Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY))); + PrivateKey privateKey = keyfactory.generatePrivate(Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8)))); - PublicKey publicKey = keyfactory.generatePublic(Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY))); + PublicKey publicKey = keyfactory.generatePublic(Pems.publicKeySpec(ByteSource.wrap(PUBLIC_KEY.getBytes(Charsets.UTF_8)))); keyPair = new KeyPair(publicKey, privateKey); openSshKey = SshKeys.encodeAsOpenSSH(RSAPublicKey.class.cast(publicKey)); diff --git a/core/src/main/java/org/jclouds/crypto/Pems.java b/core/src/main/java/org/jclouds/crypto/Pems.java index b343f82025..94927cb723 100644 --- a/core/src/main/java/org/jclouds/crypto/Pems.java +++ b/core/src/main/java/org/jclouds/crypto/Pems.java @@ -25,7 +25,6 @@ import static com.google.common.base.Splitter.fixedLength; import static com.google.common.base.Throwables.propagate; import static com.google.common.base.Throwables.propagateIfInstanceOf; import static com.google.common.io.BaseEncoding.base64; -import static com.google.common.io.ByteStreams.readBytes; import static org.jclouds.crypto.ASN1Codec.decodeRSAPrivateKey; import static org.jclouds.crypto.ASN1Codec.decodeRSAPublicKey; import static org.jclouds.crypto.ASN1Codec.encode; @@ -58,8 +57,7 @@ import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteProcessor; -import com.google.common.io.ByteStreams; -import com.google.common.io.InputSupplier; +import com.google.common.io.ByteSource; /** * Reads and writes PEM encoded Strings and Streams @@ -194,10 +192,10 @@ public class Pems { * @throws IOException * if an I/O error occurs */ - public static T fromPem(InputSupplier supplier, PemProcessor processor) + public static T fromPem(ByteSource supplier, PemProcessor processor) throws IOException { try { - return readBytes(supplier, processor); + return supplier.read(processor); } catch (RuntimeException e) { propagateIfInstanceOf(e.getCause(), IOException.class); propagateIfInstanceOf(e, IOException.class); @@ -215,7 +213,7 @@ public class Pems { * @throws IOException * if an I/O error occurs */ - public static KeySpec privateKeySpec(InputSupplier supplier) throws IOException { + public static KeySpec privateKeySpec(ByteSource supplier) throws IOException { return fromPem( supplier, new PemProcessor(ImmutableMap.> of( @@ -245,16 +243,16 @@ public class Pems { } /** - * Executes {@link Pems#privateKeySpec(InputSupplier)} on the string which contains an encoded private key in PEM + * Executes {@link Pems#privateKeySpec(ByteSource)} on the string which contains an encoded private key in PEM * format. * * @param pem * private key in pem encoded format. - * @see Pems#privateKeySpec(InputSupplier) + * @see Pems#privateKeySpec(ByteSource) */ public static KeySpec privateKeySpec(String pem) { try { - return privateKeySpec(ByteStreams.newInputStreamSupplier( + return privateKeySpec(ByteSource.wrap( pem.getBytes(Charsets.UTF_8))); } catch (IOException e) { throw propagate(e); @@ -271,7 +269,7 @@ public class Pems { * @throws IOException * if an I/O error occurs */ - public static KeySpec publicKeySpec(InputSupplier supplier) throws IOException { + public static KeySpec publicKeySpec(ByteSource supplier) throws IOException { return fromPem( supplier, new PemProcessor(ImmutableMap.> of(PUBLIC_PKCS1_MARKER, @@ -306,15 +304,15 @@ public class Pems { } /** - * Executes {@link Pems#publicKeySpec(InputSupplier)} on the string which contains an encoded public key in PEM + * Executes {@link Pems#publicKeySpec(ByteSource)} on the string which contains an encoded public key in PEM * format. * * @param pem * public key in pem encoded format. - * @see Pems#publicKeySpec(InputSupplier) + * @see Pems#publicKeySpec(ByteSource) */ public static KeySpec publicKeySpec(String pem) throws IOException { - return publicKeySpec(ByteStreams.newInputStreamSupplier( + return publicKeySpec(ByteSource.wrap( pem.getBytes(Charsets.UTF_8))); } @@ -331,7 +329,7 @@ public class Pems { * if an I/O error occurs * @throws CertificateException */ - public static X509Certificate x509Certificate(InputSupplier supplier, + public static X509Certificate x509Certificate(ByteSource supplier, @Nullable CertificateFactory certFactory) throws IOException, CertificateException { final CertificateFactory certs = certFactory != null ? certFactory : CertificateFactory.getInstance("X.509"); try { @@ -357,15 +355,15 @@ public class Pems { } /** - * Executes {@link Pems#x509Certificate(InputSupplier, CertificateFactory)} on the string which contains an X.509 + * Executes {@link Pems#x509Certificate(ByteSource, CertificateFactory)} on the string which contains an X.509 * certificate in PEM format. * * @param pem * certificate in pem encoded format. - * @see Pems#x509Certificate(InputSupplier, CertificateFactory) + * @see Pems#x509Certificate(ByteSource, CertificateFactory) */ public static X509Certificate x509Certificate(String pem) throws IOException, CertificateException { - return x509Certificate(ByteStreams.newInputStreamSupplier( + return x509Certificate(ByteSource.wrap( pem.getBytes(Charsets.UTF_8)), null); } diff --git a/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java b/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java index 67997c6744..2223a3a046 100644 --- a/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java +++ b/core/src/main/java/org/jclouds/io/internal/BasePayloadSlicer.java @@ -37,10 +37,12 @@ import org.jclouds.io.Payload; import org.jclouds.io.PayloadSlicer; import org.jclouds.io.payloads.BaseMutableContentMetadata; import org.jclouds.io.payloads.ByteArrayPayload; +import org.jclouds.io.payloads.ByteSourcePayload; import org.jclouds.io.payloads.InputStreamPayload; import org.jclouds.io.payloads.InputStreamSupplierPayload; import com.google.common.base.Throwables; +import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; import com.google.common.io.Files; import com.google.common.io.InputSupplier; @@ -141,6 +143,8 @@ public class BasePayloadSlicer implements PayloadSlicer { returnVal = doSlice((byte[]) input.getRawContent(), offset, length); } else if (input.getRawContent() instanceof InputStream) { returnVal = doSlice((InputStream) input.getRawContent(), offset, length); + } else if (input.getRawContent() instanceof ByteSource) { + returnVal = doSlice((ByteSource) input.getRawContent(), offset, length); } else { returnVal = doSlice(input, offset, length); } @@ -156,7 +160,7 @@ public class BasePayloadSlicer implements PayloadSlicer { } protected Payload doSlice(File content, long offset, long length) { - return doSlice(Files.newInputStreamSupplier(content), offset, length); + return doSlice(Files.asByteSource(content), offset, length); } protected Payload doSlice(InputStream content, long offset, long length) { @@ -168,6 +172,10 @@ public class BasePayloadSlicer implements PayloadSlicer { return new InputStreamPayload(ByteStreams.limit(content, length)); } + protected Payload doSlice(ByteSource content, long offset, long length) { + return new ByteSourcePayload(content.slice(offset, length)); + } + protected Payload doSlice(InputSupplier content, long offset, long length) { return new InputStreamSupplierPayload(ByteStreams.slice(content, offset, length)); } @@ -177,7 +185,7 @@ public class BasePayloadSlicer implements PayloadSlicer { checkArgument(offset <= Integer.MAX_VALUE, "offset is too big for an array"); checkArgument(length <= Integer.MAX_VALUE, "length is too big for an array"); returnVal = new InputStreamSupplierPayload( - ByteStreams.newInputStreamSupplier(content, (int) offset, (int) length)); + ByteSource.wrap(content).slice(offset, length)); return returnVal; } diff --git a/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java b/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java index 8ae44dd853..d77b8b1c74 100644 --- a/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java +++ b/core/src/main/java/org/jclouds/io/payloads/MultipartForm.java @@ -18,12 +18,13 @@ package org.jclouds.io.payloads; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.io.ByteStreams.join; -import static com.google.common.io.ByteStreams.newInputStreamSupplier; import java.io.IOException; import java.io.InputStream; import java.util.Map.Entry; +import com.google.common.collect.ImmutableList; +import com.google.common.io.ByteSource; import com.google.common.io.InputSupplier; /** @@ -65,12 +66,12 @@ public class MultipartForm extends BasePayload> { this(BOUNDARY, parts); } - private InputSupplier addLengthAndReturnRn() { + private ByteSource addLengthAndReturnRn() { getContentMetadata().setContentLength(getContentMetadata().getContentLength() + rn.length()); - return newInputStreamSupplier(rn.getBytes()); + return ByteSource.wrap(rn.getBytes()); } - private InputSupplier addLengthAndReturnHeaders(String boundaryrn, Part part) { + private ByteSource addLengthAndReturnHeaders(String boundaryrn, Part part) { StringBuilder builder = new StringBuilder(dd).append(boundaryrn); for (Entry entry : part.getHeaders().entries()) { String header = String.format("%s: %s%s", entry.getKey(), entry.getValue(), rn); @@ -78,13 +79,13 @@ public class MultipartForm extends BasePayload> { } builder.append(rn); getContentMetadata().setContentLength(getContentMetadata().getContentLength() + builder.length()); - return newInputStreamSupplier(builder.toString().getBytes()); + return ByteSource.wrap(builder.toString().getBytes()); } - private InputSupplier addLengthAndReturnFooter(String boundary) { + private ByteSource addLengthAndReturnFooter(String boundary) { String end = dd + boundary + dd + rn; getContentMetadata().setContentLength(getContentMetadata().getContentLength() + end.length()); - return newInputStreamSupplier(end.getBytes()); + return ByteSource.wrap(end.getBytes()); } @Override diff --git a/core/src/test/java/org/jclouds/crypto/PemsTest.java b/core/src/test/java/org/jclouds/crypto/PemsTest.java index 4670492dc9..212de697db 100644 --- a/core/src/test/java/org/jclouds/crypto/PemsTest.java +++ b/core/src/test/java/org/jclouds/crypto/PemsTest.java @@ -30,6 +30,9 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.security.spec.RSAPublicKeySpec; +import com.google.common.base.Charsets; +import com.google.common.io.ByteSource; + import org.jclouds.io.Payloads; import org.testng.annotations.Test; @@ -58,38 +61,38 @@ public class PemsTest { @Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "^Invalid PEM: no parsers for marker -----BEGIN FOO PRIVATE KEY----- .*") public void testPrivateKeySpecFromPemWithInvalidMarker() throws IOException { - Pems.privateKeySpec(Payloads.newStringPayload(INVALID_PRIVATE_KEY)); + Pems.privateKeySpec(ByteSource.wrap(INVALID_PRIVATE_KEY.getBytes(Charsets.UTF_8))); } @Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "^Invalid PEM: no parsers for marker -----BEGIN FOO PUBLIC KEY----- .*") public void testPublicKeySpecFromPemWithInvalidMarker() throws IOException { - Pems.publicKeySpec(Payloads.newStringPayload(INVALID_PUBLIC_KEY)); + Pems.publicKeySpec(ByteSource.wrap(INVALID_PUBLIC_KEY.getBytes(Charsets.UTF_8))); } @Test public void testPrivateKeySpecFromPem() throws IOException { - Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY)); + Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8))); } @Test public void testPublicKeySpecFromPem() throws IOException { - Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY)); + Pems.publicKeySpec(ByteSource.wrap(PUBLIC_KEY.getBytes(Charsets.UTF_8))); } @Test public void testX509CertificateFromPemDefault() throws IOException, CertificateException { - Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), null); + Pems.x509Certificate(ByteSource.wrap(CERTIFICATE.getBytes(Charsets.UTF_8)), null); } @Test public void testX509CertificateFromPemSuppliedCertFactory() throws IOException, CertificateException { - Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), CertificateFactory.getInstance("X.509")); + Pems.x509Certificate(ByteSource.wrap(CERTIFICATE.getBytes(Charsets.UTF_8)), CertificateFactory.getInstance("X.509")); } @Test public void testPrivateKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { RSAPrivateCrtKey key = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate( - Pems.privateKeySpec(Payloads.newStringPayload(PRIVATE_KEY))); + Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8)))); String encoded = Pems.pem(key); assertEquals(encoded, PRIVATE_KEY.replaceAll("\n", ls)); } @@ -97,20 +100,20 @@ public class PemsTest { @Test public void testRSAPublicKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePublic( - Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY)))); + Pems.publicKeySpec(PUBLIC_KEY))); assertEquals(encoded, PUBLIC_KEY.replaceAll("PUBLIC", "RSA PUBLIC").replaceAll("\n", ls)); } @Test public void testRSAPKCS1PublicKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePublic( - Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY_PKCS1)))); + Pems.publicKeySpec(PUBLIC_KEY_PKCS1))); assertEquals(encoded, PUBLIC_KEY_PKCS1.replaceAll("\n", ls)); } @Test public void testRSAPKCS1RawPublicKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { - KeySpec spec = Pems.publicKeySpec(Payloads.newStringPayload(PUBLIC_KEY_PKCS1_RAW)); + KeySpec spec = Pems.publicKeySpec(PUBLIC_KEY_PKCS1_RAW); String encoded = Pems.pem(KeyFactory.getInstance("RSA").generatePublic(spec)); KeySpec generatedSpec = Pems.publicKeySpec(encoded); @@ -130,7 +133,7 @@ public class PemsTest { @Test public void testX509CertificatePem() throws IOException, CertificateException { - String encoded = Pems.pem(Pems.x509Certificate(Payloads.newStringPayload(CERTIFICATE), + String encoded = Pems.pem(Pems.x509Certificate(ByteSource.wrap(CERTIFICATE.getBytes(Charsets.UTF_8)), CertificateFactory.getInstance("X.509"))); assertEquals(encoded, CERTIFICATE.replaceAll("\n", ls)); }