Package containing all of the classes used as part of the Manta Multipart 11 | * upload API.
12 | * 13 | *This package contains two very different implementations for doing 14 | * multipart upload:
15 | *There are also abstract classes and interfaces that allow you to 27 | * implement your own API compatible implementation to use for testing. 28 | * Additionally, there are encryption implementations that allow for 29 | * the wrapping of a backing multipart implementation and seamlessly 30 | * encrypting the uploaded parts.
31 | * 32 | * @author Elijah Zupancic 33 | * @since 2.5.0 34 | */ 35 | package com.joyent.manta.client.multipart; 36 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/client/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * Manta Java client package. 11 | * 12 | * @author Yunong Xiao 13 | */ 14 | package com.joyent.manta.client; 15 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/config/ChainedConfigContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.config; 9 | 10 | /** 11 | * Implementation of {@link ConfigContext} that links together multiple contexts. 12 | * This allows you to create tiers of configuration in which certain configuration 13 | * contexts are given priority over others. 14 | * 15 | * @author Elijah Zupancic 16 | */ 17 | public class ChainedConfigContext extends BaseChainedConfigContext { 18 | /** 19 | * Creates a new {@link ConfigContext} implementation that allows you 20 | * to chain together multiple configuration contexts that progressively 21 | * overwrite the values of the previous contexts (but never overwriting 22 | * with null). 23 | * 24 | * @param contexts N number of configuration contexts 25 | */ 26 | public ChainedConfigContext(final ConfigContext... contexts) { 27 | for (ConfigContext c : contexts) { 28 | overwriteWithContext(c); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/config/EncryptionAuthenticationMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.config; 9 | 10 | /** 11 | * Enum specifying the value object encryption authentication modes. 12 | * 13 | * @author Elijah Zupancic 14 | * @since 2.8.0 15 | */ 16 | public enum EncryptionAuthenticationMode { 17 | /** 18 | * Optional mode allows for the selective skipping of authentication if it is 19 | * incompatible with an operation being attempted like HTTP range requests. 20 | */ 21 | Optional, 22 | 23 | /** 24 | * Mandatory mode will require that every operation authenticates ciphertext 25 | * and operations that are incompatible will fail with a 26 | * {@link com.joyent.manta.exception.UnauthenticatableOperationException} 27 | * exception. 28 | */ 29 | Mandatory, 30 | 31 | /** 32 | * VerificationDisabled mode will disable ciphertext verification upon decryption. 33 | * HMACs will still be created when encrypting. 34 | */ 35 | VerificationDisabled; 36 | 37 | /** 38 | * The default encryption object authentication mode (Mandatory). 39 | */ 40 | public static final EncryptionAuthenticationMode DEFAULT_MODE = Mandatory; 41 | } 42 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/config/MetricReporterMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.config; 9 | 10 | /** 11 | * Enum specifying the available metric reporting modes. 12 | * 13 | * @author Tomas Celaya 14 | * @since 3.1.9, 3.2.2 15 | */ 16 | public enum MetricReporterMode { 17 | /** 18 | * No metrics will be collected or reported. 19 | */ 20 | DISABLED, 21 | 22 | /** 23 | * Report metrics through JMX (as MBeans). 24 | */ 25 | JMX, 26 | 27 | /** 28 | * Report metrics through SLF4J (as logs). 29 | */ 30 | SLF4J; 31 | 32 | /** 33 | * The default metric reporting mode is to disable metric reporting. 34 | */ 35 | public static final MetricReporterMode DEFAULT = DISABLED; 36 | } 37 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/config/StandardConfigContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.config; 9 | 10 | /** 11 | * Configuration context that is entirely driven by in-memory parameters. 12 | * 13 | * @author Elijah Zupancic 14 | */ 15 | public class StandardConfigContext extends BaseChainedConfigContext { 16 | /** 17 | * Creates a new {@link ConfigContext} implementation that allows for 18 | * programmatic configuration. 19 | */ 20 | public StandardConfigContext() { 21 | super(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/config/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2018, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * This package contains classes related to configuring instances of 11 | * {@link com.joyent.manta.client.MantaClient}. 12 | * 13 | *When a new instance of {@link com.joyent.manta.client.MantaClient} is created 16 | * it takes a parameter of an instance that implements 17 | * {@link com.joyent.manta.config.ConfigContext}. Within this package are many 18 | * implementations of {@link com.joyent.manta.config.ConfigContext}. By allowing 19 | * for an interface based configuration, we allow users of the SDK to integrate 20 | * into existing configuration systems in their own applications. One example of 21 | * this pattern can be seen in the HadoopConfigurationContext 22 | * class in the hadoop-manta 23 | * project.
24 | * 25 | *Many of the classes with {@link com.joyent.manta.config} inherit from the 26 | * abstract class {@link com.joyent.manta.config.BaseChainedConfigContext} 27 | * because it supplies methods for allowing you to chain together multiple 28 | * configuration context implementations such that they will support default 29 | * values and allow for layered overwrites of settings.
30 | * 31 | *For example, the following code would allow you to chain together 32 | * multiple configuration implementations:
33 | * 34 | *{@code 35 | * // Creates a composite context from three separate contexts 36 | * ChainedConfigContext chained = new ChainedConfigContext( 37 | * // This context will overwrite both previous contexts if its values are set 38 | * new EnvVarConfigContext(), 39 | * // This context will overwrite the previous context if its values are set 40 | * new MapConfigContext(System.getProperties()), 41 | * // This context provides the hardcoded default settings for the Manta client 42 | * new DefaultsConfigContext()); 43 | *}44 | */ 45 | package com.joyent.manta.config; 46 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/domain/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.domain; 9 | 10 | import java.io.Serializable; 11 | import java.util.Map; 12 | 13 | /** 14 | * Interface indicating that an object is considered a core domain object. 15 | * 16 | * @author Elijah Zupancic 17 | * @since 3.0.0 18 | */ 19 | public interface Entity extends Serializable { 20 | 21 | /** 22 | * Converts this object into a {@link Map} that has a key 23 | * for each bean in the object. This is useful for systems that need 24 | * to process each bean as part of an ingestion phase. 25 | * @return Map with each bean as an key/value 26 | */ 27 | Map
Note: This changes the signature of {@link AutoCloseable#close()} to 34 | * only throw {@link IOException}.
35 | * 36 | * @throws IOException thrown if there was a problem closing resources 37 | */ 38 | @Override 39 | void close() throws IOException; 40 | } 41 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/MantaConnectionFactoryConfigurator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http; 9 | 10 | import org.apache.commons.lang3.Validate; 11 | import org.apache.http.conn.HttpClientConnectionManager; 12 | import org.apache.http.impl.client.HttpClientBuilder; 13 | 14 | /** 15 | * Object for providing pre-configured HttpClient objects to use with {@link MantaConnectionFactory}. 16 | * 17 | * @author Tomas Celaya 18 | * @since 3.1.7 19 | */ 20 | public class MantaConnectionFactoryConfigurator { 21 | 22 | /** 23 | * An existing {@link HttpClientBuilder} to further configure. 24 | */ 25 | private final HttpClientBuilder httpClientBuilder; 26 | 27 | /** 28 | * Packages together an externally-configured {@link HttpClientBuilder} and {@link HttpClientConnectionManager} 29 | * for use with the {@link com.joyent.manta.client.MantaClient} through a {@link MantaConnectionFactory}. 30 | * 31 | * @param httpClientBuilder the client builder 32 | */ 33 | public MantaConnectionFactoryConfigurator(final HttpClientBuilder httpClientBuilder) { 34 | Validate.notNull(httpClientBuilder, "HttpClientBuilder must not be null"); 35 | 36 | this.httpClientBuilder = httpClientBuilder; 37 | } 38 | 39 | HttpClientBuilder getHttpClientBuilder() { 40 | return httpClientBuilder; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/MantaContentTypes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http; 9 | 10 | import org.apache.http.entity.ContentType; 11 | 12 | /** 13 | * Enum defining custom content-types used only by Manta. 14 | * 15 | * @author Elijah Zupancic 16 | * @since 3.0.0 17 | */ 18 | public enum MantaContentTypes { 19 | /** 20 | * The content-type used to represent Manta directory resources in http requests. 21 | */ 22 | DIRECTORY_LIST("application/json; type=directory"), 23 | 24 | /** 25 | * The content-type used to represent Manta link resources. 26 | */ 27 | SNAPLINK("application/json; type=link"), 28 | 29 | /** 30 | * The content-type used to represent encrypted objects. 31 | */ 32 | ENCRYPTED_OBJECT(ContentType.APPLICATION_OCTET_STREAM.toString()); 33 | 34 | /** 35 | * Plain-text representation of the enum's content-type. 36 | */ 37 | private final String contentType; 38 | 39 | /** 40 | * Creates a new instance with the specified content-type. 41 | * 42 | * @param contentType content-type to initialize 43 | */ 44 | MantaContentTypes(final String contentType) { 45 | this.contentType = contentType; 46 | } 47 | 48 | /** 49 | * @return the RFC 2616 content-type 50 | */ 51 | public String getContentType() { 52 | return this.contentType; 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return getContentType(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/RequestIdInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http; 9 | 10 | import com.fasterxml.uuid.EthernetAddress; 11 | import com.fasterxml.uuid.Generators; 12 | import com.fasterxml.uuid.impl.TimeBasedGenerator; 13 | import org.apache.http.Header; 14 | import org.apache.http.HttpException; 15 | import org.apache.http.HttpRequest; 16 | import org.apache.http.HttpRequestInterceptor; 17 | import org.apache.http.message.BasicHeader; 18 | import org.apache.http.protocol.HttpContext; 19 | import org.slf4j.MDC; 20 | 21 | import java.io.IOException; 22 | import java.security.NoSuchAlgorithmException; 23 | import java.security.NoSuchProviderException; 24 | import java.security.SecureRandom; 25 | import java.util.Random; 26 | import java.util.UUID; 27 | 28 | /** 29 | * Add the request id for an HTTP header to the SLF4J MDC logging implementation. This 30 | * is useful because it allows us to view the request id in all of the logs associated 31 | * with the request. 32 | * 33 | * @author Elijah Zupancic 34 | * @since 3.0.0 35 | */ 36 | public class RequestIdInterceptor implements HttpRequestInterceptor { 37 | /** 38 | * Time-based UUID generator for generating request ids. 39 | */ 40 | private static final TimeBasedGenerator TIME_BASED_GENERATOR; 41 | 42 | static { 43 | Random nonBlockingRandomness; 44 | 45 | try { 46 | nonBlockingRandomness = SecureRandom.getInstance("NativePRNGNonBlocking", "SUN"); 47 | } catch (NoSuchAlgorithmException | NoSuchProviderException e) { 48 | nonBlockingRandomness = new Random(System.nanoTime()); 49 | } 50 | 51 | // Fake ethernet address based on a random value 52 | final EthernetAddress ethernetAddress = EthernetAddress.constructMulticastAddress( 53 | nonBlockingRandomness); 54 | TIME_BASED_GENERATOR = Generators.timeBasedGenerator(ethernetAddress); 55 | } 56 | 57 | /** 58 | * Constant identifying the request id as a MDC attribute. 59 | */ 60 | public static final String MDC_REQUEST_ID_STRING = "mantaRequestId"; 61 | 62 | @Override 63 | public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { 64 | final UUID id = TIME_BASED_GENERATOR.generate(); 65 | final String requestId = id.toString(); 66 | final Header idHeader = new BasicHeader(MantaHttpHeaders.REQUEST_ID, requestId); 67 | request.addHeader(idHeader); 68 | 69 | MDC.put(MDC_REQUEST_ID_STRING, requestId); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/RetryConfigAware.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http; 9 | 10 | /** 11 | * Interface indicating that an object understands the underlying retry behavior and a common key to use 12 | * when indicating that retry cancellation is possible through {@link HttpContextRetryCancellation}. 13 | * 14 | * @author Tomas Celaya 15 | * @since 3.2.3 16 | */ 17 | public interface RetryConfigAware { 18 | 19 | /** 20 | * Whether or not automatic retries are enabled. 21 | * 22 | * @return if retries are enabled 23 | */ 24 | boolean isRetryEnabled(); 25 | 26 | /** 27 | * Whether or not retries can be cancelled using {@link HttpContextRetryCancellation}. 28 | * 29 | * @return if retry cancellation is supported 30 | */ 31 | boolean isRetryCancellable(); 32 | } 33 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/ShufflingDnsResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http; 9 | 10 | import org.apache.http.conn.DnsResolver; 11 | 12 | import java.net.InetAddress; 13 | import java.net.UnknownHostException; 14 | import java.util.Random; 15 | import java.util.concurrent.ThreadLocalRandom; 16 | 17 | /** 18 | * Implementation {@link DnsResolver} that shuffles the results of a DNS query 19 | * upon every query, so that we get better distribution of connections to 20 | * different hosts backed by the same name. 21 | * 22 | * @author Elijah Zupancic 23 | */ 24 | public class ShufflingDnsResolver implements DnsResolver { 25 | @Override 26 | public InetAddress[] resolve(final String host) throws UnknownHostException { 27 | final InetAddress[] addresses = InetAddress.getAllByName(host); 28 | shuffle(addresses); 29 | 30 | return addresses; 31 | } 32 | 33 | /** 34 | * Shuffles an array of addresses that were returned from a DNS query. 35 | * Shuffle algorithm inspired by this stackoverflow post. 36 | * @param addresses addresses to shuffle 37 | */ 38 | private static void shuffle(final InetAddress[] addresses) { 39 | // Only shuffle if we have 2 or more addresses 40 | if (addresses.length < 2) { 41 | return; 42 | } 43 | 44 | Random random = ThreadLocalRandom.current(); 45 | 46 | for (int i = addresses.length - 1; i > 0; i--) { 47 | int index = random.nextInt(i + 1); 48 | // Simple swap 49 | InetAddress a = addresses[index]; 50 | addresses[index] = addresses[i]; 51 | addresses[i] = a; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/entity/MantaInputStreamEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http.entity; 9 | 10 | import org.apache.http.entity.ContentType; 11 | import org.apache.http.entity.InputStreamEntity; 12 | 13 | import java.io.InputStream; 14 | 15 | /** 16 | * A Manta-specific implementation of {@link org.apache.http.entity.InputStreamEntity}. 17 | * 18 | * @author Elijah Zupancic 19 | * @since 3.0.0 20 | */ 21 | public class MantaInputStreamEntity extends InputStreamEntity { 22 | /** 23 | * Creates an entity with an unknown length. 24 | * Equivalent to {@code new InputStreamEntity(instream, -1)}. 25 | * 26 | * @param instream input stream 27 | * @throws IllegalArgumentException if {@code instream} is {@code null} 28 | */ 29 | public MantaInputStreamEntity(final InputStream instream) { 30 | super(instream); 31 | } 32 | 33 | /** 34 | * Creates an entity with a specified content length. 35 | * 36 | * @param instream input stream 37 | * @param length of the input stream, {@code -1} if unknown 38 | * @throws IllegalArgumentException if {@code instream} is {@code null} 39 | */ 40 | public MantaInputStreamEntity(final InputStream instream, final long length) { 41 | super(instream, length); 42 | } 43 | 44 | /** 45 | * Creates an entity with a content type and unknown length. 46 | * Equivalent to {@code new InputStreamEntity(instream, -1, contentType)}. 47 | * 48 | * @param instream input stream 49 | * @param contentType content type 50 | * @throws IllegalArgumentException if {@code instream} is {@code null} 51 | */ 52 | public MantaInputStreamEntity(final InputStream instream, final ContentType contentType) { 53 | super(instream, contentType); 54 | } 55 | 56 | /** 57 | * @param instream input stream 58 | * @param length of the input stream, {@code -1} if unknown 59 | * @param contentType for specifying the {@code Content-Type} header, may be {@code null} 60 | * @throws IllegalArgumentException if {@code instream} is {@code null} 61 | */ 62 | public MantaInputStreamEntity(final InputStream instream, final long length, 63 | final ContentType contentType) { 64 | super(instream, length, contentType); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/entity/MemoryBackedEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http.entity; 9 | 10 | import java.nio.ByteBuffer; 11 | 12 | /** 13 | * Provides an interface for entity that can expose a backing buffer of their 14 | * content. 15 | * 16 | * @author Elijah Zupancic 17 | * @since 3.0.0 18 | */ 19 | public interface MemoryBackedEntity { 20 | /** 21 | * @return the backing byte array as a {@link ByteBuffer} instance 22 | */ 23 | ByteBuffer getBackingBuffer(); 24 | } 25 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/entity/NoContentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.http.entity; 9 | 10 | import org.apache.http.Header; 11 | import org.apache.http.HttpEntity; 12 | 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | import java.io.OutputStream; 16 | 17 | /** 18 | * Entity type used when not sending any content to the server. 19 | * 20 | * @author Elijah Zupancic 21 | * @since 3.0.0 22 | */ 23 | public final class NoContentEntity implements HttpEntity { 24 | /** 25 | * Constant indicating an unknown content length. 26 | */ 27 | private static final long NO_CONTENT_LENGTH = -1L; 28 | 29 | /** 30 | * Reusable instance of entity class. 31 | */ 32 | public static final NoContentEntity INSTANCE = new NoContentEntity(); 33 | 34 | /** 35 | * Private constructor because a single instance is all that we need. 36 | */ 37 | private NoContentEntity() { 38 | 39 | } 40 | 41 | @Override 42 | public boolean isRepeatable() { 43 | return true; 44 | } 45 | 46 | @Override 47 | public boolean isChunked() { 48 | return true; 49 | } 50 | 51 | @Override 52 | public long getContentLength() { 53 | return NO_CONTENT_LENGTH; 54 | } 55 | 56 | @Override 57 | public Header getContentType() { 58 | return null; 59 | } 60 | 61 | @Override 62 | public Header getContentEncoding() { 63 | return null; 64 | } 65 | 66 | @Override 67 | public InputStream getContent() throws IOException, UnsupportedOperationException { 68 | return null; 69 | } 70 | 71 | @Override 72 | public void writeTo(final OutputStream outstream) throws IOException { 73 | } 74 | 75 | @Override 76 | public boolean isStreaming() { 77 | return false; 78 | } 79 | 80 | @Deprecated 81 | @Override 82 | public void consumeContent() throws IOException { 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/entity/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * Package containing implementations of {@link org.apache.http.HttpEntity}. 11 | * 12 | * @author Elijah Zupancic 13 | * @since 1.0.0 14 | */ 15 | package com.joyent.manta.http.entity; 16 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/http/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * Package containing classes used for accessing Manta over HTTP(S). 11 | * 12 | * @author Elijah Zupancic 13 | * @since 3.0.0 14 | */ 15 | package com.joyent.manta.http; 16 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/util/CipherCloner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.util; 9 | 10 | import com.joyent.manta.client.crypto.ExternalSecurityProviderLoader; 11 | import com.joyent.manta.exception.MantaMemoizationException; 12 | 13 | import javax.crypto.Cipher; 14 | import java.security.Provider; 15 | 16 | /** 17 | * Utility class for cloning Cipher objects. 18 | */ 19 | public final class CipherCloner implements ClonerThe class to which this annotation is applied is not thread-safe. 37 | * This annotation primarily exists for clarifying the non-thread-safety of a 38 | * class that might otherwise be assumed to be thread-safe, despite the fact 39 | * that it is a bad idea to assume a class is thread-safe without good reason.
40 | * 41 | *Based on code developed by Brian Goetz and Tim Peierls and concepts 42 | * published in 'Java Concurrency in Practice' by Brian Goetz, Tim Peierls, 43 | * Joshua Bloch, Joseph Bowbeer, David Holmes and Doug Lea.
44 | * 45 | *The only modifications to this class from its original is this block 46 | * comment and the package name
47 | * 48 | * @see Apache httpcore Implementation 49 | */ 50 | @Documented 51 | @Target(ElementType.TYPE) 52 | @Retention(RetentionPolicy.CLASS) // The original version used RUNTIME 53 | public @interface NotThreadSafe { 54 | } 55 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/joyent/manta/util/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * Package containing utility classes. 11 | * 12 | * @author Elijah Zupancic 13 | * @since 3.0.0 14 | */ 15 | package com.joyent.manta.util; 16 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/main/java/com/twmacinta/util/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | /** 10 | * Package containing helper classes for Timothy W Macinta's FastMD5 11 | * implementation. 12 | * 13 | * @author Elijah Zupancic 14 | * @since 3.0.0 15 | */ 16 | package com.twmacinta.util; 17 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/test/java/com/joyent/manta/client/crypto/ByteRangeConversionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.client.crypto; 9 | 10 | import org.testng.Assert; 11 | import org.testng.annotations.Test; 12 | 13 | 14 | @Test 15 | public class ByteRangeConversionTest { 16 | public void setsCorrectProperties() { 17 | ByteRangeConversion byteConversion = new ByteRangeConversion(100, 10, 80, 70, 1); 18 | Assert.assertEquals(byteConversion.getCiphertextStartPositionInclusive(), 100); 19 | Assert.assertEquals(byteConversion.getPlaintextBytesToSkipInitially(), 10); 20 | Assert.assertEquals(byteConversion.getCiphertextEndPositionInclusive(), 80); 21 | Assert.assertEquals(byteConversion.getLengthOfPlaintextIncludingSkipBytes(), 70); 22 | Assert.assertEquals(byteConversion.getStartingBlockNumberInclusive(), 1); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/test/java/com/joyent/manta/client/crypto/IncompleteByteReadInputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.client.crypto; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | 13 | /** 14 | * {@link java.io.InputStream} implementation that will always read one byte 15 | * less than the length specified in a {@link InputStream#read(byte[], int, int)} 16 | * method invocation. 17 | * 18 | * @author Elijah Zupancic 19 | * @since 3.1.1 20 | */ 21 | public class IncompleteByteReadInputStream extends InputStream { 22 | private final InputStream wrapped; 23 | 24 | public IncompleteByteReadInputStream(final InputStream wrapped) { 25 | this.wrapped = wrapped; 26 | } 27 | 28 | @Override 29 | public int read() throws IOException { 30 | return wrapped.read(); 31 | } 32 | 33 | @Override 34 | public int read(final byte[] b) throws IOException { 35 | return read(b, 0, b.length); 36 | } 37 | 38 | @Override 39 | public int read(byte[] b, int off, int len) throws IOException { 40 | if (len > 1 && len - off - 1 > 0) { 41 | return wrapped.read(b, off, len - 1); 42 | } else { 43 | return wrapped.read(b, off, len); 44 | } 45 | } 46 | 47 | @Override 48 | public void close() throws IOException { 49 | wrapped.close(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/test/java/com/joyent/manta/client/crypto/LocallyIllegalAesCipherDetailsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.client.crypto; 9 | 10 | import org.testng.annotations.Test; 11 | 12 | public class LocallyIllegalAesCipherDetailsTest { 13 | 14 | @Test(expectedExceptions = Error.class, 15 | expectedExceptionsMessageRegExp = "This cipher is not compatible with the current runtime: .*") 16 | public void throwsUncheckedErrorsForAnyMethodCall() { 17 | // keyLengthBits passed here is for error reporting, no validation is applied 18 | LocallyIllegalAesCipherDetails illegalAesCipherDetails = new LocallyIllegalAesCipherDetails(0); 19 | 20 | illegalAesCipherDetails.getCipher(); // or any other method call from the SupportedCipherDetails interface 21 | } 22 | } -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/test/java/com/joyent/manta/client/jobs/MantaJobPhaseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.client.jobs; 9 | 10 | import org.testng.annotations.Test; 11 | 12 | /** 13 | * Tests the behavior of the validations and accessors of {@link MantaJobPhase} 14 | * instance. 15 | * 16 | * @author Elijah Zupancic 17 | */ 18 | public class MantaJobPhaseTest { 19 | 20 | @Test 21 | public void canCreateMapPhase() { 22 | new MantaJobPhase().setType("map"); 23 | } 24 | 25 | @Test 26 | public void canCreateReducePhase() { 27 | new MantaJobPhase().setType("reduce"); 28 | } 29 | 30 | @Test(expectedExceptions = IllegalArgumentException.class) 31 | public void cantCreateUnknownPhase() { 32 | new MantaJobPhase().setType("anything else"); 33 | } 34 | 35 | @Test 36 | public void canSetCountForReduce() { 37 | new MantaJobPhase() 38 | .setType("reduce") 39 | .setCount(2); 40 | } 41 | 42 | @Test(expectedExceptions = IllegalArgumentException.class) 43 | public void cantSetCountForMapPhase() { 44 | new MantaJobPhase() 45 | .setType("map") 46 | .setCount(2); 47 | } 48 | 49 | @Test(expectedExceptions = IllegalArgumentException.class) 50 | public void cantSetBadCountValue() { 51 | new MantaJobPhase() 52 | .setCount(0); 53 | } 54 | 55 | @Test(expectedExceptions = IllegalArgumentException.class) 56 | public void cantSetBadMemoryValue() { 57 | new MantaJobPhase() 58 | .setMemory(0); 59 | } 60 | 61 | @Test(expectedExceptions = IllegalArgumentException.class) 62 | public void cantSetBadDiskValue() { 63 | new MantaJobPhase() 64 | .setDisk(0); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /java-manta-client-unshaded/src/test/java/com/joyent/manta/client/jobs/MantaJobTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-2017, Joyent, Inc. All rights reserved. 3 | * 4 | * This Source Code Form is subject to the terms of the Mozilla Public 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | package com.joyent.manta.client.jobs; 9 | 10 | import org.testng.Assert; 11 | import org.testng.annotations.Test; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * Tests the behavior of the validations and accessors of {@link MantaJob} 18 | * instance. 19 | * 20 | * @author Elijah Zupancic 21 | */ 22 | public class MantaJobTest { 23 | @Test 24 | public void canGetMapPhases() { 25 | MantaJobPhase phase1 = new MantaJobPhase().setType("map").setExec("echo 1"); 26 | MantaJobPhase phase2 = new MantaJobPhase().setType("map").setExec("echo 2"); 27 | MantaJobPhase phase3 = new MantaJobPhase().setType("reduce").setExec("echo 3"); 28 | List
28 | * On the other hand, we can't use {@link FailingInputStream} because that generates its own exceptions, so we
29 | * wouldn't be able to use {@link org.testng.Assert#assertSame} and check that no suppressed exceptions were added.
30 | */
31 | private static final class ReadExceptionInputStream extends ProxyInputStream {
32 |
33 | private final IOException exception;
34 |
35 | public ReadExceptionInputStream(final IOException exception) {
36 | super(ClosedInputStream.CLOSED_INPUT_STREAM);
37 | this.exception = exception;
38 | }
39 |
40 | @Override
41 | protected void beforeRead(final int n) throws IOException {
42 | throw this.exception;
43 | }
44 | }
45 |
46 | public void rethrowsUnrecoverableExceptionsDirectly() throws Exception {
47 | // the exception to consider fatal
48 | final IOException ex = new IOException("oops");
49 |
50 | // source stream always throws that exception
51 | final InputStream original = new ReadExceptionInputStream(ex);
52 |
53 | // pretend that it was a fatal exception and should be rethrown
54 | final InputStreamContinuator continuator = mock(InputStreamContinuator.class);
55 | when(continuator.buildContinuation(same(ex), anyLong())).thenThrow(ex);
56 |
57 | final IOException caught = expectThrows(IOException.class, () -> {
58 | try (final AutoContinuingInputStream in = new AutoContinuingInputStream(original, continuator)) {
59 | IOUtils.toByteArray(in);
60 | }
61 | });
62 |
63 | assertSame(caught, ex);
64 | assertEquals(caught.getSuppressed().length, 0);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/java-manta-client-unshaded/src/test/java/com/joyent/manta/util/FailingOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved.
3 | *
4 | * This Source Code Form is subject to the terms of the Mozilla Public
5 | * License, v. 2.0. If a copy of the MPL was not distributed with this
6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 | */
8 | package com.joyent.manta.util;
9 |
10 | import java.io.IOException;
11 | import java.io.OutputStream;
12 | import java.util.concurrent.atomic.AtomicLong;
13 |
14 | public class FailingOutputStream extends OutputStream {
15 |
16 | public static final int NO_FAILURE = -1;
17 |
18 | /**
19 | * The wrapped InputStream
20 | */
21 | private final OutputStream wrapped;
22 |
23 | /**
24 | * Current read byte count.
25 | */
26 | private final AtomicLong count = new AtomicLong(0L);
27 |
28 | /**
29 | * Minimum number of bytes to read before failing.
30 | */
31 | private long minimumBytes;
32 |
33 | public FailingOutputStream(final OutputStream wrapped, int minimumBytes) {
34 | this.wrapped = wrapped;
35 | this.minimumBytes = minimumBytes;
36 | }
37 |
38 | @Override
39 | public void write(int b) throws IOException {
40 | failAfterMinimum(1);
41 | count.incrementAndGet();
42 | wrapped.write(b);
43 | }
44 |
45 | @Override
46 | public void write(byte[] bytes) throws IOException {
47 | write(bytes, 0, bytes.length);
48 | }
49 |
50 | @Override
51 | public void write(byte[] bytes, int off, int len) throws IOException {
52 | failAfterMinimum(bytes.length);
53 | count.addAndGet(bytes.length);
54 | wrapped.write(bytes, off, len);
55 | }
56 |
57 | public void setMinimumBytes(final int minimumBytes) {
58 | this.minimumBytes = minimumBytes;
59 | }
60 |
61 | private void failAfterMinimum(final int next) throws IOException {
62 | if (minimumBytes == NO_FAILURE) {
63 | return;
64 | }
65 |
66 | if (count.get() + next > minimumBytes) {
67 | throw new IOException("Write failure after byte " + minimumBytes);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/java-manta-client-unshaded/src/test/java/com/joyent/manta/util/FileInputStreamContinuator.java:
--------------------------------------------------------------------------------
1 | package com.joyent.manta.util;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 |
8 | public class FileInputStreamContinuator implements InputStreamContinuator {
9 |
10 | private final File file;
11 |
12 | public FileInputStreamContinuator(final File file) {
13 | this.file = file;
14 | }
15 |
16 | @Override
17 | public InputStream buildContinuation(final IOException ex, final long bytesRead) throws IOException {
18 |
19 | final FileInputStream fileStream = new FileInputStream(this.file);
20 |
21 | long skipRemaining = bytesRead;
22 | do {
23 | skipRemaining -= fileStream.skip(bytesRead);
24 | } while (0 < skipRemaining);
25 |
26 | return fileStream;
27 | }
28 |
29 | @Override
30 | public void close() {
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/java-manta-client-unshaded/src/test/java/com/joyent/manta/util/MantaVersionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016-2017, Joyent, Inc. All rights reserved.
3 | *
4 | * This Source Code Form is subject to the terms of the Mozilla Public
5 | * License, v. 2.0. If a copy of the MPL was not distributed with this
6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 | */
8 | package com.joyent.manta.util;
9 |
10 | import org.testng.Assert;
11 | import org.testng.annotations.Test;
12 |
13 | @Test
14 | public class MantaVersionTest {
15 | public void canLoadVersion() {
16 | Assert.assertNotNull(MantaVersion.VERSION);
17 | }
18 |
19 | public void canLoadVersionDate() {
20 | Assert.assertNotNull(MantaVersion.DATE);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/java-manta-client-unshaded/src/test/java/com/twmacinta/util/FastMD5DigestTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017, Joyent, Inc. All rights reserved.
3 | *
4 | * This Source Code Form is subject to the terms of the Mozilla Public
5 | * License, v. 2.0. If a copy of the MPL was not distributed with this
6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 | */
8 | package com.twmacinta.util;
9 |
10 | import org.testng.Assert;
11 | import org.testng.AssertJUnit;
12 | import org.testng.annotations.Test;
13 |
14 | /**
15 | * @author Elijah Zupancic
16 | * @since 3.0.0
17 | */
18 | @Test
19 | public class FastMD5DigestTest {
20 | public void canSaveAndLoadEncodedState() {
21 | final MD5State initialState = new MD5State();
22 | initialState.buffer = new byte[] { (byte)23, (byte)34, (byte)56 };
23 | initialState.state = new int[] { 23423, -5, -33, 232323, Integer.MAX_VALUE };
24 | initialState.count = 20123;
25 |
26 | final byte[] encodedState = FastMD5Digest.generateEncodedState(initialState);
27 |
28 | final MD5State decodedState = new MD5State();
29 | FastMD5Digest.updateStateFromEncodedState(decodedState, encodedState);
30 |
31 | AssertJUnit.assertArrayEquals("MD5State buffer arrays do not match",
32 | initialState.buffer, decodedState.buffer);
33 | AssertJUnit.assertArrayEquals("MD5State state arrays do not match",
34 | initialState.state, decodedState.state);
35 | Assert.assertEquals(decodedState.count, initialState.count,
36 | "MD5State count value was decoded correctly");
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/java-manta-client-unshaded/src/test/resources/logback-test.xml:
--------------------------------------------------------------------------------
1 |