public class ServiceUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
HASH_SHA256 |
protected static java.text.SimpleDateFormat |
iso8601DateParser |
protected static java.text.SimpleDateFormat |
iso8601DateParser_Walrus |
protected static java.text.SimpleDateFormat |
rfc822DateParser |
Constructor and Description |
---|
ServiceUtils() |
Modifier and Type | Method and Description |
---|---|
static S3Object |
buildObjectFromUrl(java.lang.String host,
java.lang.String urlPath,
java.lang.String s3Endpoint)
Builds an object based on the bucket name and object key information
available in the components of a URL.
|
static java.util.Map<java.lang.String,java.lang.Object> |
cleanRestMetadataMap(java.util.Map<java.lang.String,java.lang.Object> metadata,
java.lang.String serviceMetadataPrefix,
java.lang.String userMetadataPrefix)
From a map of metadata returned from a REST GET or HEAD request, returns a map
of metadata with the HTTP-connection-specific metadata items removed.
|
static byte[] |
computeMD5Hash(byte[] data)
Computes the MD5 hash of the given data and returns it as a hex string.
|
static byte[] |
computeMD5Hash(java.io.InputStream is)
Computes the MD5 hash of the data in the given input stream and returns it as a hex string.
|
static long |
countBytesInObjects(S3Object[] objects)
Counts the total number of bytes in a set of S3Objects by summing the
content length of each.
|
static java.lang.String |
findBucketNameInHostname(java.lang.String host,
java.lang.String s3Endpoint)
Identifies the name of a bucket from a given host name, if available.
|
static java.lang.String |
findBucketNameInHostOrPath(java.net.URI uri,
java.lang.String s3Endpoint)
Find the name of a bucket referred to by a HTTP request's Host or
Path URI components.
|
static java.lang.String |
formatIso8601Date(java.util.Date date) |
static java.lang.String |
formatRfc822Date(java.util.Date date) |
static byte[] |
fromBase64(java.lang.String b64Data)
Converts a Base64-encoded string to the original byte data.
|
static byte[] |
fromHex(java.lang.String hexData)
Converts a Hex-encoded data string to the original byte data.
|
static java.lang.String |
generateS3HostnameForBucket(java.lang.String bucketName,
boolean isDnsBucketNamingDisabled,
java.lang.String s3Endpoint) |
static java.lang.String |
getUserAgentDescription(java.lang.String applicationDescription)
Returns a user agent string describing the jets3t library, and optionally the application
using it, to server-side services.
|
static byte[] |
hash(byte[] data,
java.lang.String cryptoHash) |
static byte[] |
hash(java.io.InputStream dataIS,
java.lang.String cryptoHash,
boolean resetInsteadOfClose) |
static byte[] |
hash(java.lang.String data,
java.lang.String cryptoHash) |
static byte[] |
hashSHA256(byte[] data) |
static byte[] |
hashSHA256(java.io.InputStream dataIS) |
static byte[] |
hashSHA256(java.io.InputStream dataIS,
boolean resetInsteadOfClose) |
static byte[] |
hmac(byte[] key,
byte[] data,
java.lang.String cryptoAlgorithm) |
static byte[] |
hmacSHA256(byte[] key,
byte[] data) |
static byte[] |
hmacSHA256(java.lang.String key,
java.lang.String data)
Return lowercase hex-encoded HMAC message digest of given data using the
given key, using a crypto hash like "SHA256".
|
static boolean |
isBucketNameValidDNSName(java.lang.String bucketName)
Returns true if the given bucket name can be used as a component of a valid
DNS name.
|
static boolean |
isEtagAlsoAnMD5Hash(java.lang.String etag)
Guess whether the given ETag value is also an MD5 hash of an underlying object
in a storage service, as opposed to being some other kind of opaque hash.
|
static java.lang.String |
join(int[] ints,
java.lang.String delimiter)
Joins a list of ints into a delimiter-separated string.
|
static java.lang.String |
join(java.util.List<?> items,
java.lang.String delimiter)
Joins a list of items into a delimiter-separated string.
|
static java.lang.String |
join(java.lang.Object[] items,
java.lang.String delimiter)
Joins a list of items into a delimiter-separated string.
|
static org.xml.sax.XMLReader |
loadXMLReader()
Find a SAX XMLReader by hook or by crook, with work-arounds for
non-standard platforms.
|
static java.util.Date |
parseIso8601Date(java.lang.String dateString) |
static java.util.Date |
parseRfc822Date(java.lang.String dateString) |
static java.lang.String |
readInputStreamLineToString(java.io.InputStream is,
java.lang.String encoding)
Reads from an input stream until a newline character or the end of the stream is reached.
|
static byte[] |
readInputStreamToBytes(java.io.InputStream is)
Reads binary data from an input stream and returns it as a byte array.
|
static java.lang.String |
readInputStreamToString(java.io.InputStream is,
java.lang.String encoding)
Reads text data from an input stream and returns it as a String.
|
static java.lang.String |
signWithHmacSha1(java.lang.String awsSecretKey,
java.lang.String canonicalString)
Calculate the HMAC/SHA1 on a string.
|
static java.lang.String[] |
splitIgnoreEmpty(java.lang.String str,
java.lang.String regexp) |
static byte[] |
stringToBytes(java.lang.String str) |
static java.lang.String |
toBase64(byte[] data)
Converts byte data to a Base64-encoded string.
|
static java.lang.String |
toHex(byte[] data)
Converts byte data to a Hex-encoded string.
|
static java.lang.String |
wrapString(java.lang.String p_Input,
java.lang.String p_Prefix,
int p_Len)
Take the input we're given and wrap at the user-defined intervals
|
static java.lang.String |
wrapString(java.lang.String p_Input,
java.lang.String p_Prefix,
int p_Len,
java.lang.String p_Delims)
Take the input we're given and wrap at the user-defined intervals
|
public static java.lang.String HASH_SHA256
protected static final java.text.SimpleDateFormat iso8601DateParser
protected static final java.text.SimpleDateFormat iso8601DateParser_Walrus
protected static final java.text.SimpleDateFormat rfc822DateParser
public static java.util.Date parseIso8601Date(java.lang.String dateString) throws java.text.ParseException
java.text.ParseException
public static java.lang.String formatIso8601Date(java.util.Date date)
public static java.util.Date parseRfc822Date(java.lang.String dateString) throws java.text.ParseException
java.text.ParseException
public static java.lang.String formatRfc822Date(java.util.Date date)
public static java.lang.String signWithHmacSha1(java.lang.String awsSecretKey, java.lang.String canonicalString)
awsSecretKey
- AWS secret key.canonicalString
- canonical string representing the request to sign.public static byte[] stringToBytes(java.lang.String str)
str
- public static byte[] hash(byte[] data, java.lang.String cryptoHash)
data
- cryptoHash
- public static byte[] hash(java.io.InputStream dataIS, java.lang.String cryptoHash, boolean resetInsteadOfClose) throws java.io.IOException
dataIS
- cryptoHash
- resetInsteadOfClose
- if true, input stream is reset instead of closed after hash is generated.java.io.IOException
public static byte[] hash(java.lang.String data, java.lang.String cryptoHash)
data
- cryptoHash
- public static byte[] hashSHA256(byte[] data)
public static byte[] hashSHA256(java.io.InputStream dataIS, boolean resetInsteadOfClose) throws java.io.IOException
java.io.IOException
public static byte[] hashSHA256(java.io.InputStream dataIS) throws java.io.IOException
java.io.IOException
public static byte[] hmac(byte[] key, byte[] data, java.lang.String cryptoAlgorithm)
key
- key for HMACdata
- data to be HMAC'dcryptoAlgorithm
- cryptographic algorithm to use for HMAC, e.g. "SHA-256"public static byte[] hmacSHA256(java.lang.String key, java.lang.String data)
key
- data
- public static byte[] hmacSHA256(byte[] key, byte[] data)
key
- data
- public static java.lang.String readInputStreamToString(java.io.InputStream is, java.lang.String encoding) throws java.io.IOException
is
- input stream from which text data is read.encoding
- the character encoding of the textual data in the input stream. If this
parameter is null, the default system encoding will be used.java.io.IOException
public static java.lang.String readInputStreamLineToString(java.io.InputStream is, java.lang.String encoding) throws java.io.IOException
is
- java.io.IOException
public static byte[] readInputStreamToBytes(java.io.InputStream is) throws java.io.IOException
is
- input stream from which data is read.java.io.IOException
public static long countBytesInObjects(S3Object[] objects)
objects
- public static java.util.Map<java.lang.String,java.lang.Object> cleanRestMetadataMap(java.util.Map<java.lang.String,java.lang.Object> metadata, java.lang.String serviceMetadataPrefix, java.lang.String userMetadataPrefix)
metadata
- metadata map to be cleanedserviceMetadataPrefix
- prefix denoting service-specific "header" HTTP header values (case insensitive)userMetadataPrefix
- prefix denoting service-specific "user metadata" HTTP header values (case insensitive)public static java.lang.String toHex(byte[] data)
data
- data to hex encode.public static byte[] fromHex(java.lang.String hexData)
hexData
- hex-encoded data to decode.public static java.lang.String toBase64(byte[] data)
data
- data to Base64 encode.public static java.lang.String join(java.util.List<?> items, java.lang.String delimiter)
items
- the items to include in a delimited stringdelimiter
- the delimiter character or string to insert between each item in the listpublic static java.lang.String join(java.lang.Object[] items, java.lang.String delimiter)
items
- the items to include in a delimited stringdelimiter
- the delimiter character or string to insert between each item in the listpublic static java.lang.String join(int[] ints, java.lang.String delimiter)
ints
- the ints to include in a delimited stringdelimiter
- the delimiter character or string to insert between each item in the listpublic static java.lang.String[] splitIgnoreEmpty(java.lang.String str, java.lang.String regexp)
str
- regexp
- String.split(String)
with empty
strings removed.public static byte[] fromBase64(java.lang.String b64Data)
b64Data
- a Base64-encoded string to decode.public static byte[] computeMD5Hash(java.io.InputStream is) throws java.security.NoSuchAlgorithmException, java.io.IOException
is
- java.security.NoSuchAlgorithmException
java.io.IOException
public static byte[] computeMD5Hash(byte[] data) throws java.security.NoSuchAlgorithmException, java.io.IOException
data
- java.security.NoSuchAlgorithmException
java.io.IOException
public static boolean isEtagAlsoAnMD5Hash(java.lang.String etag)
This test was made necessary by Amazon S3's multipart upload feature, where the ETag value returned after a re-assembled multipart upload is completed is no longer the same as an MD5 hash of the assembled data.
An ETag is considered also an MD5 when:
These rules are drawn from the post by Carl@AWS on Nov 11, 2010 10:40 AM here: https://forums.aws.amazon.com/thread.jspa?messageID=222158&tstart=0
public static java.lang.String findBucketNameInHostname(java.lang.String host, java.lang.String s3Endpoint)
host
- the host name component of a URL that may include the bucket name,
if an alternative host name is in use.public static java.lang.String findBucketNameInHostOrPath(java.net.URI uri, java.lang.String s3Endpoint)
uri
- URI containing the request's Host domain name target
(via URI.getHost()
) and Path component (via URI.getPath()
)s3Endpoint
- Host name of default S3 endpoint, generally "s3.amazonaws.com" but may
differ when using alternative services such as Eucalyptus Walrus etc.public static S3Object buildObjectFromUrl(java.lang.String host, java.lang.String urlPath, java.lang.String s3Endpoint) throws java.io.UnsupportedEncodingException
host
- the host name component of a URL that may include the bucket name,
if an alternative host name is in use.urlPath
- the path of a URL that references an S3 object, and which may or may
not include the bucket name.java.io.UnsupportedEncodingException
public static boolean isBucketNameValidDNSName(java.lang.String bucketName)
bucketName
- the name of the bucket to test for DNS compatibility.public static java.lang.String generateS3HostnameForBucket(java.lang.String bucketName, boolean isDnsBucketNamingDisabled, java.lang.String s3Endpoint)
public static java.lang.String getUserAgentDescription(java.lang.String applicationDescription)
applicationDescription
- a description of the application using the jets3t toolkit, included at the end of the
user agent string. This value may be null.Constants.JETS3T_VERSION
(os.name/os.version; os.arch; user.region;
user.region; user.language) applicationDescriptionpublic static org.xml.sax.XMLReader loadXMLReader() throws ServiceException
ServiceException
public static java.lang.String wrapString(java.lang.String p_Input, java.lang.String p_Prefix, int p_Len)
p_Input
- The string to be modified by the line wrap.p_Prefix
- a prefix to prebend to the output stringp_Len
- The maximum number of characters per linepublic static java.lang.String wrapString(java.lang.String p_Input, java.lang.String p_Prefix, int p_Len, java.lang.String p_Delims)
p_Input
- The string to be modified by the line wrap.p_Prefix
- a prefix to prebend to the output stringp_Len
- The maximum number of characters per linep_Delims
- are the characters on which wrapping is allowed