com.googlecode.webutilities.util
Class Utils

java.lang.Object
  extended by com.googlecode.webutilities.util.Utils

public final class Utils
extends Object

Common Utilities provider class

Version:
1.0
Author:
rpatil

Method Summary
static String addFingerPrint(String fingerPrint, String url)
           
static String buildETagForResource(String relativePath, javax.servlet.ServletContext context)
           
static String buildETagForResources(List<String> resourcesRelativePath, javax.servlet.ServletContext context)
           
static String buildProperPath(String parentPath, String relativePathFromParent)
           
static String detectExtension(String requestURI)
           
static List<String> findResourcesToMerge(String contextPath, String requestURI)
          Split multiple resources with comma eg.
static String forHeaderDate(long time)
           
static long getLastModifiedFor(List<String> resources, javax.servlet.ServletContext servletContext)
           
static String getParentPath(String path)
          Fast get parent directory using substring
static String hexDigestString(byte[] data)
           
static boolean isAnyResourceETagModified(List<String> resources, String requestETag, String actualETag, javax.servlet.ServletContext servletContext)
           
static boolean isAnyResourceModifiedSince(List<String> resources, long sinceTime, javax.servlet.ServletContext servletContext)
           
static boolean isProtocolURL(String url)
           
static boolean readBoolean(String string, boolean defaultValue)
           
static Date readDateFromHeader(String headerDateString)
           
static int readInt(String string, int defaultValue)
           
static long readLong(String string, long defaultValue)
           
static String readString(String string, String defaultValue)
           
static String removeFingerPrint(String url)
           
static String selectMimeByFile(String filePath)
           
static String selectMimeForExtension(String extensionOrFile)
           
static boolean updateReferenceMap(String cssFilePath, String imgFilePath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readInt

public static int readInt(String string,
                          int defaultValue)
Parameters:
string - string representation of a int which is to be parsed and read from
defaultValue - in case parsing fails or string is null, returns this default value
Returns:
int parsed value or the default value in case parsing failed

readLong

public static long readLong(String string,
                            long defaultValue)
Parameters:
string - string representation of a long which is to be parsed and read from
defaultValue - in case parsing fails or string is null, returns this default value
Returns:
long parsed value or the default value in case parsing failed

readBoolean

public static boolean readBoolean(String string,
                                  boolean defaultValue)
Parameters:
string - string representation of a boolean (true or false) which is to be parsed and read from
defaultValue - in case string is null or does not contain valid boolean, returns this default value
Returns:
boolean parsed value or the default value

readString

public static String readString(String string,
                                String defaultValue)
Parameters:
string - string
defaultValue - in case string is null or empty
Returns:
String parsed value or the default value

detectExtension

public static String detectExtension(String requestURI)
Parameters:
requestURI - the URL string
Returns:
extension .css or .js etc.

selectMimeByFile

public static String selectMimeByFile(String filePath)
Parameters:
filePath - - path of the file, whose mime is to be detected
Returns:
contentType - mime type of the file

selectMimeForExtension

public static String selectMimeForExtension(String extensionOrFile)
Parameters:
extensionOrFile - - .js or .css etc. of full file path in case of image files
Returns:
- mime like text/javascript or text/css etc.

buildProperPath

public static String buildProperPath(String parentPath,
                                     String relativePathFromParent)

findResourcesToMerge

public static List<String> findResourcesToMerge(String contextPath,
                                                String requestURI)
Split multiple resources with comma eg. if URL is http://server/context/js/a,b,c.js then a.js, b.js and c.js have to be processed and merged together.

b and c can be absolute paths or relative (relative to previous resource) too.

eg.

http://server/context/js/a,/js/libs/b,/js/yui/c.js - absolutes paths for all OR http://server/context/js/a,/js/libs/b,../yui/c.js - relative path used for c.js (relative to b) OR http://server/context/js/a,/js/libs/b,./c.js OR - b & c are in same directory /js/libs

Parameters:
contextPath - request Context Path
requestURI - requestURI
Returns:
Set of resources to be processed

isAnyResourceModifiedSince

public static boolean isAnyResourceModifiedSince(List<String> resources,
                                                 long sinceTime,
                                                 javax.servlet.ServletContext servletContext)
Parameters:
resources - - list of resources paths
sinceTime - - long value to compare against
servletContext - - servlet context
Returns:
true if any of the resources is modified since given time, false otherwise

getLastModifiedFor

public static long getLastModifiedFor(List<String> resources,
                                      javax.servlet.ServletContext servletContext)
Parameters:
resources - - list of resources paths
servletContext - - servlet context
Returns:
long - maximum of last modified values of the resources

isAnyResourceETagModified

public static boolean isAnyResourceETagModified(List<String> resources,
                                                String requestETag,
                                                String actualETag,
                                                javax.servlet.ServletContext servletContext)
Parameters:
resources - - list of resources
requestETag - - request ETag from If-None-Match header
actualETag - - current ETag of a resource (can be null)
servletContext - - servlet context
Returns:
true if any resource ETag is modified, false otherwise.

buildETagForResources

public static String buildETagForResources(List<String> resourcesRelativePath,
                                           javax.servlet.ServletContext context)
Parameters:
resourcesRelativePath - - list of resources
context - - servlet context
Returns:
- String as ETag calculated using simple hash based on size and last modified of all resources

updateReferenceMap

public static boolean updateReferenceMap(String cssFilePath,
                                         String imgFilePath)
Parameters:
cssFilePath - - css file path
imgFilePath - - img file path
Returns:
true if all goes well and paths are touched, false otherwise

isProtocolURL

public static boolean isProtocolURL(String url)

buildETagForResource

public static String buildETagForResource(String relativePath,
                                          javax.servlet.ServletContext context)
Parameters:
relativePath - - relative path of res
context - - servlet context
Returns:
ETag string

readDateFromHeader

public static Date readDateFromHeader(String headerDateString)
Parameters:
headerDateString - - from request header
Returns:
Date object after reading from header string

forHeaderDate

public static String forHeaderDate(long time)

hexDigestString

public static String hexDigestString(byte[] data)

addFingerPrint

public static String addFingerPrint(String fingerPrint,
                                    String url)
Parameters:
fingerPrint - hex digest
url - original url w/o fingerprint
Returns:
url with fingerprint

removeFingerPrint

public static String removeFingerPrint(String url)
Parameters:
url - Finger Printed URL
Returns:
Non Finger Printed URL

getParentPath

public static String getParentPath(String path)
Fast get parent directory using substring

Parameters:
path - path whose parent path has to be returned
Returns:
parent path of the argument


Copyright © 2011. All Rights Reserved.