org.xlightweb
Class HttpUtils

Package class diagram package HttpUtils
java.lang.Object
  extended by org.xlightweb.HttpUtils

public final class HttpUtils
extends Object

A HTTP utility class


Method Summary
static void clearCaches()
          clears the internal caches

This is a xSocket internal method and subject to change
static BlockingBodyDataSource compress(BlockingBodyDataSource dataSource)
          Deprecated.  
static BodyDataSource compress(BodyDataSource dataSource)
          compresses a body (using the GZIP compression)
static byte[] compress(byte[] data)
          compresses a byte array (using the GZIP compression)
static byte[] computeRFC2104HMAC(byte[] data, byte[] key)
          computes a HMAC-SHA1 Signature
static IHttpRequest copy(IHttpRequest request)
          Copies a request.
static IHttpResponse copy(IHttpResponse response)
          Copies a response.
static byte[] decodeBase64(byte[] bytes)
          decodes the given byte array
static BlockingBodyDataSource decompress(BlockingBodyDataSource dataSource)
          Deprecated.  
static BodyDataSource decompress(BodyDataSource dataSource)
          decompresses a body (using the GZIP decompression)
static byte[] decompress(byte[] compressedData)
          decompresses a byte array (using the GZIP decompression)
static byte[] encodeBase64(byte[] bytes)
          encodes the given byte array
static void establishTcpTunnel(IHttpConnection httpConnection, String target)
          establishs a tcp tunnel
static void establishTcpTunnel(IHttpConnection httpConnection, String targetHost, int targetPort)
          establishs a tcp tunnel
static String getImplementationDate()
          get the implementation date
static String getImplementationVersion()
          get the implementation version
static Map<String,String> getMimeTypeMapping()
          get the mime type file to extension map
static String getReason(int statusCode)
          returns the reason text
static boolean isAcceptEncdoingGzip(IHttpRequestHeader requestHeader)
           
static boolean isAfter(String dateString, long referenceTime)
           
static boolean isCompressableMimeType(String mimeType)
           
static boolean isConnectHandlerWarningIsSuppressed()
          returns if the connect handler warning is suppressed

This is a xSocket internal method and subject to change
static boolean isContainExpect100ContinueHeader(IHttpMessageHeader header)
          returns true if the header contains a Expect: 100-continue header
static boolean isContainsExpect100ContinueHeader(IHttpRequest request)
           
static boolean isMethodMultithreaded(Class clazz, String methodname, boolean dflt, Class... paramClass)
          returns if the handler method is multi threaded

This is a xSocket internal method and subject to change
static boolean isShowDetailedError()
          returns true, if detailed error messages should been shown.
static Date parseDateTimeString(String dateTimeString)
           
static String parseEncoding(String contentType)
          parse extracts the encoding parameter of the content type
static String parseEncodingWithDefault(String contentType, String dflt)
          parse extracts the encoding parameter of the content type
static Date parseHttpDateString(String dateString)
           
static long parseLong(String longString, long dflt)
          

This is a xSocket internal method and subject to change
static String removeMediaTypeParameters(String mediaType)
          returns the given media type string without parameters
static String removeSurroundingSlashes(String path)
           
static String resolveContentTypeByFileExtension(File file)
          returns the content type based on the file extension
static void setExpireHeaders(IHttpResponseHeader header, int expireSec)
          set the caching expires headers of a response
static void setLastModifiedHeader(IHttpResponseHeader header, long timeMillis)
          set a last modified header of the response
static String toDateTimeString(Date date)
           
static IOException toIOException(Throwable t)
          wraps a exception with an io exception
static String toRFC1123DateString(Date date)
           
static int validateSufficientDatasizeByIntLengthField(NonBlockingBodyDataSource stream)
          validate, based on a leading int length field.
static int validateSufficientDatasizeByIntLengthField(NonBlockingBodyDataSource stream, boolean removeLengthField)
          validate, based on a leading int length field, that enough data (getNumberOfAvailableBytes() >= length) is available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isShowDetailedError

public static boolean isShowDetailedError()
returns true, if detailed error messages should been shown. See IHttpExchange.SHOW_DETAILED_ERROR_KEY

Returns:
true, if detailed error messages should been shown

clearCaches

public static void clearCaches()
clears the internal caches

This is a xSocket internal method and subject to change


getReason

public static String getReason(int statusCode)
returns the reason text

Parameters:
statusCode - the status code
Returns:
the reason text

encodeBase64

public static byte[] encodeBase64(byte[] bytes)
                           throws IOException
encodes the given byte array

Parameters:
bytes - byte array to encode
Returns:
the encoded byte array
Throws:
IOException - if an exception occurs

decodeBase64

public static byte[] decodeBase64(byte[] bytes)
                           throws IOException
decodes the given byte array

Parameters:
bytes - byte array to decode
Returns:
the decoded byte array
Throws:
IOException - if an exception occurs

validateSufficientDatasizeByIntLengthField

public static int validateSufficientDatasizeByIntLengthField(NonBlockingBodyDataSource stream)
                                                      throws IOException,
                                                             BufferUnderflowException
validate, based on a leading int length field. The length field will be removed

Parameters:
connection - the connection
Returns:
the length
Throws:
IOException - if an exception occurs
BufferUnderflowException - if not enough data is available

validateSufficientDatasizeByIntLengthField

public static int validateSufficientDatasizeByIntLengthField(NonBlockingBodyDataSource stream,
                                                             boolean removeLengthField)
                                                      throws IOException,
                                                             BufferUnderflowException
validate, based on a leading int length field, that enough data (getNumberOfAvailableBytes() >= length) is available. If not, an BufferUnderflowException will been thrown.

Parameters:
connection - the connection
removeLengthField - true, if length field should be removed
Returns:
the length
Throws:
IOException - if an exception occurs
BufferUnderflowException - if not enough data is available

getMimeTypeMapping

public static Map<String,String> getMimeTypeMapping()
get the mime type file to extension map

Returns:
the mime type file to extension map

isAfter

public static boolean isAfter(String dateString,
                              long referenceTime)

removeMediaTypeParameters

public static String removeMediaTypeParameters(String mediaType)
returns the given media type string without parameters

Parameters:
mediaType - the media type string
Returns:
the media type string without paramaters

parseEncoding

public static String parseEncoding(String contentType)
parse extracts the encoding parameter of the content type

Parameters:
contentType - the content type
Returns:
the encoding or

parseEncodingWithDefault

public static String parseEncodingWithDefault(String contentType,
                                              String dflt)
parse extracts the encoding parameter of the content type

Parameters:
contentType - the content type
Returns:
the encoding or

copy

public static IHttpRequest copy(IHttpRequest request)
                         throws IOException
Copies a request. Only complete received message are supported

Parameters:
request - the request
Returns:
the copy
Throws:
IOException - if an exception occurs

copy

public static IHttpResponse copy(IHttpResponse response)
                          throws IOException
Copies a response. Only complete received message are supported

Parameters:
response - the response
Returns:
the copy
Throws:
IOException - if an exception occurs

toIOException

public static IOException toIOException(Throwable t)
wraps a exception with an io exception

Parameters:
t - the exception
Returns:
the ioe exception

compress

public static BlockingBodyDataSource compress(BlockingBodyDataSource dataSource)
                                       throws IOException
Deprecated. 

Throws:
IOException

compress

public static BodyDataSource compress(BodyDataSource dataSource)
                               throws IOException
compresses a body (using the GZIP compression)

Parameters:
dataSource - the body to compress
Returns:
the compressed body
Throws:
IOException - if an excption occurs

compress

public static byte[] compress(byte[] data)
                       throws IOException
compresses a byte array (using the GZIP compression)

Parameters:
dataSource - the byte array to compress
Returns:
the compressed byte array
Throws:
IOException - if an excption occurs

decompress

public static BlockingBodyDataSource decompress(BlockingBodyDataSource dataSource)
                                         throws IOException
Deprecated. 

Throws:
IOException

decompress

public static BodyDataSource decompress(BodyDataSource dataSource)
                                 throws IOException
decompresses a body (using the GZIP decompression)

Parameters:
dataSource - the body to decompress
Returns:
the decompressed body
Throws:
IOException - if an excption occurs

decompress

public static byte[] decompress(byte[] compressedData)
                         throws IOException
decompresses a byte array (using the GZIP decompression)

Parameters:
compressedData -
Returns:
the deconmpressed byte array
Throws:
IOException

isCompressableMimeType

public static boolean isCompressableMimeType(String mimeType)

setExpireHeaders

public static void setExpireHeaders(IHttpResponseHeader header,
                                    int expireSec)
set the caching expires headers of a response

Parameters:
header - the header
expireSec - the expire time or 0 to set no-cache headers

setLastModifiedHeader

public static void setLastModifiedHeader(IHttpResponseHeader header,
                                         long timeMillis)
set a last modified header of the response

Parameters:
header - the header
timeMillis - the last modified time in millis

toDateTimeString

public static String toDateTimeString(Date date)

parseDateTimeString

public static Date parseDateTimeString(String dateTimeString)
                                throws ParseException
Throws:
ParseException

computeRFC2104HMAC

public static byte[] computeRFC2104HMAC(byte[] data,
                                        byte[] key)
                                 throws IOException
computes a HMAC-SHA1 Signature

Parameters:
data - the data
key - the key
Returns:
the signature
Throws:
IOException - if an error occurs

toRFC1123DateString

public static String toRFC1123DateString(Date date)

parseHttpDateString

public static Date parseHttpDateString(String dateString)

isContainExpect100ContinueHeader

public static boolean isContainExpect100ContinueHeader(IHttpMessageHeader header)
returns true if the header contains a Expect: 100-continue header

Parameters:
header - the request header
Returns:
true if the header contains a Expect: 100-continue header

isContainsExpect100ContinueHeader

public static boolean isContainsExpect100ContinueHeader(IHttpRequest request)

removeSurroundingSlashes

public static String removeSurroundingSlashes(String path)

resolveContentTypeByFileExtension

public static final String resolveContentTypeByFileExtension(File file)
returns the content type based on the file extension

Parameters:
file - the file

getImplementationVersion

public static String getImplementationVersion()
get the implementation version

Returns:
the implementation version

parseLong

public static long parseLong(String longString,
                             long dflt)


This is a xSocket internal method and subject to change


getImplementationDate

public static String getImplementationDate()
get the implementation date

Returns:
the implementation date

isMethodMultithreaded

public static boolean isMethodMultithreaded(Class clazz,
                                            String methodname,
                                            boolean dflt,
                                            Class... paramClass)
returns if the handler method is multi threaded

This is a xSocket internal method and subject to change

Parameters:
clazz - the handler class
methodname - the method name
dflt - the default value
paramClass - the method parameter classes
Returns:
true, if multi threaded

isConnectHandlerWarningIsSuppressed

public static boolean isConnectHandlerWarningIsSuppressed()
returns if the connect handler warning is suppressed

This is a xSocket internal method and subject to change

Returns:
true, if the connect handler warning is suppressed

isAcceptEncdoingGzip

public static boolean isAcceptEncdoingGzip(IHttpRequestHeader requestHeader)

establishTcpTunnel

public static void establishTcpTunnel(IHttpConnection httpConnection,
                                      String target)
                               throws IOException
establishs a tcp tunnel

Parameters:
httpConnection - the http connection
target - the target address (e.g. www.gmx.com, www.gmx.com:443)
Throws:
IOException - if an exception occurs

establishTcpTunnel

public static void establishTcpTunnel(IHttpConnection httpConnection,
                                      String targetHost,
                                      int targetPort)
                               throws IOException
establishs a tcp tunnel

Parameters:
httpConnection - the http connection
targetHost - the target host
targetPort - the target port
Throws:
IOException - if an exception occurs