net.netheos.pcsapi.utils
Class PcsUtils

java.lang.Object
  extended by net.netheos.pcsapi.utils.PcsUtils

public final class PcsUtils
extends Object

Utility class for the project


Field Summary
static boolean ANDROID
           
static Charset UTF8
           
 
Method Summary
static String abbreviate(String source, int maxLen)
          Abbreviate a string if longer than maxLen.
static CStorageException buildCStorageException(CResponse response, String message, CPath path)
          Some common code between providers.
static void closeQuietly(Closeable c)
           
static void downloadDataToSink(CResponse response, ByteSink byteSink)
          Server has answered OK with a file to download as stream.
static void ensureContentTypeIsJson(CResponse response, boolean isRetriable)
          Extract content type from response headers, and ensure it is application/json or text/javascript.
static void ensureContentTypeIsXml(CResponse response, boolean isRetriable)
          Extract content type from response headers, and ensure it is application/xml.
static String randomString(char[] values, int len)
          Generate a random String
static String randomString(int len)
          Generate a random String
static void releaseHttpClient(org.apache.http.client.HttpClient httpClient)
           
static String shortenUrl(URI uri)
          Removes query parameters from url (only for logging, as query parameters may contain sensible informations)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF8

public static final Charset UTF8

ANDROID

public static final boolean ANDROID
Method Detail

ensureContentTypeIsJson

public static void ensureContentTypeIsJson(CResponse response,
                                           boolean isRetriable)
                                    throws CStorageException
Extract content type from response headers, and ensure it is application/json or text/javascript. If no content-type is defined, or content-type is not json, raises a CHttpError.

Parameters:
response - the response to check
isRetriable - if True, raised exception is wrapped into a CRetriable
Throws:
CStorageException

ensureContentTypeIsXml

public static void ensureContentTypeIsXml(CResponse response,
                                          boolean isRetriable)
                                   throws CStorageException
Extract content type from response headers, and ensure it is application/xml. If no content-type is defined, or content-type is not xml, raises a CHttpError.

Parameters:
response - the response to check
isRetriable - if True, raised exception is wrapped into a CRetriable
Throws:
CStorageException

buildCStorageException

public static CStorageException buildCStorageException(CResponse response,
                                                       String message,
                                                       CPath path)
Some common code between providers. Handles the different status codes, and generates a nice exception

Parameters:
response - The wrapped HTTP response
message - The error message (provided by the server or by the application)
path - The file requested (which failed)
Returns:
The exception

shortenUrl

public static String shortenUrl(URI uri)
Removes query parameters from url (only for logging, as query parameters may contain sensible informations)

Parameters:
uri -
Returns:
URI without parameters

abbreviate

public static String abbreviate(String source,
                                int maxLen)
Abbreviate a string if longer than maxLen.

Parameters:
source - may be null
maxLen - must be >= 0. Up to this length, string is not truncated; otherwise truncated to maxLen and three dots as allipsis are added.
Returns:
original or truncated string (up tomaxLen+3 characters).

downloadDataToSink

public static void downloadDataToSink(CResponse response,
                                      ByteSink byteSink)
Server has answered OK with a file to download as stream. Open byte sink stream, and copy data from server stream to sink stream

Parameters:
response - server response to read from
byteSink - destination

closeQuietly

public static void closeQuietly(Closeable c)

randomString

public static String randomString(int len)
Generate a random String

Parameters:
len - The number of characters in the output String
Returns:
The randomized String

randomString

public static String randomString(char[] values,
                                  int len)
Generate a random String

Parameters:
values - The characters list to use in the randomization
len - The number of characters in the output String
Returns:
The randomized String

releaseHttpClient

public static void releaseHttpClient(org.apache.http.client.HttpClient httpClient)


Copyright © 2014. All Rights Reserved.