com.basho.riak.client.util
Class ClientUtils

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

public class ClientUtils
extends java.lang.Object

Utility functions.


Constructor Summary
ClientUtils()
           
 
Method Summary
static java.util.Map<java.lang.String,java.lang.String> asHeaderMap(org.apache.commons.httpclient.Header[] headers)
          Convert a header array returned from HttpClient to a map
static void copyStream(java.io.InputStream in, java.io.OutputStream out)
          Copies data from an InputStream to an OutputStream in blocks
static java.lang.String encodeClientId(byte[] clientId)
          Base64 encodes the first 4 bytes of clientId into a value acceptable for the X-Riak-ClientId header.
static java.lang.String encodeClientId(java.lang.String clientId)
           
static java.lang.String getPathFromUrl(java.lang.String url)
          Return just the path portion of the given URL
static java.lang.String join(java.lang.String[] arr, java.lang.String delimiter)
          Join the elements in arr in to a single string separated by delimiter.
static java.util.List<java.lang.String> jsonArrayAsList(JSONArray json)
          Convert a JSONArray to a list
static java.util.Map<java.lang.String,java.lang.String> jsonObjectAsMap(JSONObject json)
          Convert a JSONObject to a map
static java.lang.String makeURI(RiakConfig config, java.lang.String bucket)
          Return a URL to the given bucket
static java.lang.String makeURI(RiakConfig config, java.lang.String bucket, java.lang.String key)
          Return a URL to the given object
static java.lang.String makeURI(RiakConfig config, java.lang.String bucket, java.lang.String key, java.lang.String extra)
          Return a URL to the given object
static org.apache.commons.httpclient.HttpClient newHttpClient(RiakConfig config)
          Construct a new HttpClient instance given a RiakConfig.
static java.util.List<RiakLink> parseLinkHeader(java.lang.String header)
          Parse a link header into a RiakLink.
static java.util.List<RiakObject> parseMultipart(RiakClient riak, java.lang.String bucket, java.lang.String key, java.util.Map<java.lang.String,java.lang.String> docHeaders, java.lang.String docBody)
          Convert a multipart/mixed document to a list of RiakObjects.
static java.util.Map<java.lang.String,java.lang.String> parseUsermeta(java.util.Map<java.lang.String,java.lang.String> headers)
          Extract only the user-specified metadata headers from a header set: all headers prefixed with X-Riak-Meta-.
static java.lang.String randomClientId()
          Returns a random X-Riak-ClientId header value.
static void throwChecked(java.lang.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 java.lang.String unquoteString(java.lang.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 java.lang.String urlEncode(java.lang.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.commons.httpclient.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 java.lang.String makeURI(RiakConfig config,
                                       java.lang.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 java.lang.String makeURI(RiakConfig config,
                                       java.lang.String bucket,
                                       java.lang.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 java.lang.String makeURI(RiakConfig config,
                                       java.lang.String bucket,
                                       java.lang.String key,
                                       java.lang.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

getPathFromUrl

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


urlEncode

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


encodeClientId

public static java.lang.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 java.lang.String encodeClientId(java.lang.String clientId)

randomClientId

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


unquoteString

public static java.lang.String unquoteString(java.lang.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 java.util.Map<java.lang.String,java.lang.String> asHeaderMap(org.apache.commons.httpclient.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 java.util.Map<java.lang.String,java.lang.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 java.util.List<java.lang.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 java.lang.String join(java.lang.String[] arr,
                                    java.lang.String delimiter)
Join the elements in arr in to a single string separated by delimiter.


copyStream

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

Parameters:
in - InputStream to copy
out - OutputStream to copy to
Throws:
java.io.IOException

parseLinkHeader

public static java.util.List<RiakLink> parseLinkHeader(java.lang.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.

parseUsermeta

public static java.util.Map<java.lang.String,java.lang.String> parseUsermeta(java.util.Map<java.lang.String,java.lang.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 java.util.List<RiakObject> parseMultipart(RiakClient riak,
                                                        java.lang.String bucket,
                                                        java.lang.String key,
                                                        java.util.Map<java.lang.String,java.lang.String> docHeaders,
                                                        java.lang.String 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(java.lang.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.


Copyright © 2010. All Rights Reserved.