com.basho.riak.client.http.util
Class ClientUtils

java.lang.Object
  extended by com.basho.riak.client.http.util.ClientUtils

public class ClientUtils
extends Object

Utility functions.


Constructor Summary
ClientUtils()
           
 
Method Summary
static Map<String,String> asHeaderMap(org.apache.http.Header[] headers)
          Convert a header array returned from HttpClient to a map
static byte[] bufferStream(InputStream valueStream)
          Buffers an input stream into a byte array
static void copyStream(InputStream in, OutputStream out)
          Copies data from an InputStream to an OutputStream in blocks
static String encodeClientId(byte[] clientId)
          Base64 encodes the first 4 bytes of clientId into a value acceptable for the X-Riak-ClientId header.
static String encodeClientId(String clientId)
           
static String getPathFromUrl(String url)
          Return just the path portion of the given URL
static String join(String[] arr, String delimiter)
          Join the elements in arr in to a single string separated by delimiter.
static List<String> jsonArrayAsList(JSONArray json)
          Convert a JSONArray to a list
static Map<String,String> jsonObjectAsMap(JSONObject json)
          Convert a JSONObject to a map
static String makeURI(RiakConfig config, String bucket)
          Return a URL to the given bucket
static String makeURI(RiakConfig config, String bucket, String key)
          Return a URL to the given object
static String makeURI(RiakConfig config, String bucket, String index, int[] values)
          Return a URL to the given index
static String makeURI(RiakConfig config, String bucket, String key, String extra)
          Return a URL to the given object
static String makeURI(RiakConfig config, String bucket, String index, String[] values)
          Return a URL to the given index
static org.apache.http.client.HttpClient newHttpClient(RiakConfig config)
          Construct a new HttpClient instance given a RiakConfig.
static List<RiakIndex> parseIndexHeaders(Map<String,String> headers)
          Extract X-Riak-Index-* headers and create List of RiakIndexes from them
static List<RiakLink> parseLinkHeader(String header)
          Parse a link header into a RiakLink.
static List<RiakObject> parseMultipart(RiakClient riak, String bucket, String key, Map<String,String> docHeaders, byte[] docBody)
          Convert a multipart/mixed document to a list of RiakObjects.
static Map<String,String> parseUsermeta(Map<String,String> headers)
          Extract only the user-specified metadata headers from a header set: all headers prefixed with X-Riak-Meta-.
static String randomClientId()
          Returns a random X-Riak-ClientId header value.
static void throwChecked(Throwable exception)
          Throws a checked Exception not declared in the method signature, which can be particularly useful for throwing checked exceptions within a RiakExceptionHandler.
static String unquoteString(String s)
          Unquote and unescape an HTTP quoted-string: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 Does nothing if s is not quoted.
static String urlDecode(String s)
          Decodes a UTF-8 encoded string
static String urlEncode(String s)
          UTF-8 encode the string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClientUtils

public ClientUtils()
Method Detail

newHttpClient

public static org.apache.http.client.HttpClient newHttpClient(RiakConfig config)
Construct a new HttpClient instance given a RiakConfig.

Parameters:
config - RiakConfig containing HttpClient configuration specifics.
Returns:
A new HttpClient

makeURI

public static String makeURI(RiakConfig config,
                             String bucket)
Return a URL to the given bucket

Parameters:
config - RiakConfig containing the base URL to Riak
bucket - Bucket whose URL to retrieving
Returns:
URL to the bucket

makeURI

public static String makeURI(RiakConfig config,
                             String bucket,
                             String key)
Return a URL to the given object

Parameters:
config - RiakConfig containing the base URL to Riak
bucket - Bucket of the object
key - Key of the object
Returns:
URL to the object

makeURI

public static String makeURI(RiakConfig config,
                             String bucket,
                             String key,
                             String extra)
Return a URL to the given object

Parameters:
config - RiakConfig containing the base URL to Riak
bucket - Bucket of the object
key - Key of the object
extra - Extra path information beyond the bucket and key (e.g. for link walking or query parameters)
Returns:
URL to the object

makeURI

public static String makeURI(RiakConfig config,
                             String bucket,
                             String index,
                             String[] values)
Return a URL to the given index

Parameters:
config - RiakConfig containing the base URL to Riak
bucket - Bucket of the object
index - index name
values - the index value (or values for a range)
Returns:
URL for an index query

makeURI

public static String makeURI(RiakConfig config,
                             String bucket,
                             String index,
                             int[] values)
Return a URL to the given index

Parameters:
config - RiakConfig containing the base URL to Riak
bucket - Bucket of the object
index - index name
values - the index value (or values for a range)
Returns:
URL for an index query

getPathFromUrl

public static String getPathFromUrl(String url)
Return just the path portion of the given URL


urlEncode

public static String urlEncode(String s)
UTF-8 encode the string


urlDecode

public static String urlDecode(String s)
Decodes a UTF-8 encoded string


encodeClientId

public static String encodeClientId(byte[] clientId)
Base64 encodes the first 4 bytes of clientId into a value acceptable for the X-Riak-ClientId header.


encodeClientId

public static String encodeClientId(String clientId)

randomClientId

public static String randomClientId()
Returns a random X-Riak-ClientId header value.


unquoteString

public static String unquoteString(String s)
Unquote and unescape an HTTP quoted-string: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 Does nothing if s is not quoted.

Parameters:
s - quoted-string to unquote
Returns:
s with quotes and backslash-escaped characters unescaped

asHeaderMap

public static Map<String,String> asHeaderMap(org.apache.http.Header[] headers)
Convert a header array returned from HttpClient to a map

Parameters:
headers - Header array returned from HttpClient
Returns:
Map of the header names to values

jsonObjectAsMap

public static Map<String,String> jsonObjectAsMap(JSONObject json)
Convert a JSONObject to a map

Parameters:
json - JSONObject to convert
Returns:
Map of the field names to string representations of the values

jsonArrayAsList

public static List<String> jsonArrayAsList(JSONArray json)
Convert a JSONArray to a list

Parameters:
json - JSONArray to convert
Returns:
List of string representations of the elements

join

public static String join(String[] arr,
                          String delimiter)
Join the elements in arr in to a single string separated by delimiter.


copyStream

public static void copyStream(InputStream in,
                              OutputStream out)
                       throws IOException
Copies data from an InputStream to an OutputStream in blocks

Parameters:
in - InputStream to copy
out - OutputStream to copy to
Throws:
IOException

parseLinkHeader

public static List<RiakLink> parseLinkHeader(String header)
Parse a link header into a RiakLink. See LinkHeader.

Parameters:
header - The HTTP Link header value.
Returns:
List of RiakLink objects constructed from the links in header in order.

parseIndexHeaders

public static List<RiakIndex> parseIndexHeaders(Map<String,String> headers)
Extract X-Riak-Index-* headers and create List of RiakIndexes from them

Parameters:
headers - The full HTTP headers from the response
Returns:
a List of RiakIndexs

parseUsermeta

public static Map<String,String> parseUsermeta(Map<String,String> headers)
Extract only the user-specified metadata headers from a header set: all headers prefixed with X-Riak-Meta-. The prefix is removed before returning.

Parameters:
headers - The full HTTP header set from the response
Returns:
Map of all headers prefixed with X-Riak-Meta- with prefix removed.

parseMultipart

public static List<RiakObject> parseMultipart(RiakClient riak,
                                              String bucket,
                                              String key,
                                              Map<String,String> docHeaders,
                                              byte[] docBody)
Convert a multipart/mixed document to a list of RiakObjects.

Parameters:
riak - RiakClient this object should be associate with, or null if none
bucket - original object's bucket
key - original object's key
docHeaders - original document's headers
docBody - original document's body
Returns:
List of RiakObjects represented by the multipart document

throwChecked

public static void throwChecked(Throwable exception)
Throws a checked Exception not declared in the method signature, which can be particularly useful for throwing checked exceptions within a RiakExceptionHandler. Clearly, this circumvents compiler safeguards, so use with caution. You've been warned.

Parameters:
exception - A checked (or unchecked) exception to be thrown.

bufferStream

public static byte[] bufferStream(InputStream valueStream)
Buffers an input stream into a byte array

Parameters:
valueStream - the stream to read into an array
Returns:
the byte array of the consumed stream


Copyright © 2012. All Rights Reserved.