org.meshcms.core
Class WebUtils

java.lang.Object
  extended byorg.meshcms.core.WebUtils

public final class WebUtils
extends java.lang.Object

A collection of utilities related to a web application.


Field Summary
static java.util.regex.Pattern BODY_REGEX
           
static java.lang.String[] DEFAULT_WELCOME_FILES
          The default array of welcome file names.
static java.util.regex.Pattern EMAIL_REGEX
           
static java.util.Properties ENTITY_TO_NUMBER
           
static java.lang.String FN_CHARMAP
          Mapping of the ISO-8859-1 characters to characters included in FN_CHARS.
static java.lang.String FN_CHARS
          Characters allowed in a file name.
static java.lang.String FN_SPACERS
          Characters that are considered spacers in a file name.
static java.util.regex.Pattern HYPERLINK_REGEX
           
static java.util.Properties NUMBER_TO_ENTITY
           
static java.text.SimpleDateFormat numericDateFormatter
           
static java.util.regex.Pattern PRE_BR_REGEX
           
static java.util.regex.Pattern PRE_REGEX
           
 
Method Summary
static java.lang.String addToQueryString(java.lang.String url, java.lang.String name, java.lang.String value, boolean encodeValue)
           
static java.lang.String convertToHTMLEntities(java.lang.String s, boolean encodeTags)
           
static java.lang.String convertToHTMLEntities(java.lang.String s, java.lang.String charset, boolean encodeTags)
           
static java.lang.String createExcerpt(WebSite webSite, java.lang.String body, int length, java.lang.String contextPath, Path oldPage, Path newPage)
           
static java.lang.String findEmails(java.lang.String text)
           
static java.lang.String findLinks(java.lang.String text)
           
static java.lang.String fixFileName(java.lang.String name, boolean useSpacers)
          Returns a modified name that does not contain characters not recommended in a file name.
static java.lang.String fixLinks(WebSite webSite, java.lang.String body, java.lang.String contextPath, Path oldPage, Path newPage)
           
static java.lang.String fixRelativeURLs(java.lang.String html, Path root, boolean relative)
           
static java.lang.String formatFileLength(long length, java.util.Locale locale, java.util.ResourceBundle bundle)
          Returns a nicer representation of the number as a file length.
static java.lang.String get2ndLevelDomain(javax.servlet.http.HttpServletRequest request)
          Returns the 2nd level domain from which the request comes.
static java.lang.String get2ndLevelDomain(java.lang.String urlString)
          Returns the 2nd level domain from the URL.
static java.lang.String[] getAcceptedLanguages(javax.servlet.http.HttpServletRequest request)
           
static java.io.File getCacheFile(WebSite webSite, SiteMap siteMap, Path pagePath)
           
static java.lang.String getCharsetCanonicalName(java.lang.String charsetName)
           
static java.lang.StringBuffer getContextHomeURL(javax.servlet.http.HttpServletRequest request)
          Reconstructs the full URL of the context home.
static Path getCorrespondingPath(WebSite webSite, Path path, java.lang.String otherRoot)
           
static java.lang.String getFullMeshCSS(WebSite webSite, javax.servlet.http.HttpServletRequest request)
          Returns the complete path of the meshcms.css file (context path included).
static java.lang.String getFullParameter(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Returns a request parameter in the form &name=value.
static java.lang.String getFullThemeCSS(javax.servlet.http.HttpServletRequest request)
          Returns the complete path of the main.css file (context path included).
static java.lang.String getFullThemeFile(javax.servlet.http.HttpServletRequest request)
          Returns the complete path of the main.jsp file (context path included).
static java.lang.String getFullThemeFolder(javax.servlet.http.HttpServletRequest request)
          Returns the complete path of the folder of current theme (context path included).
static java.util.regex.Pattern getJSPBlockPattern()
           
static long getLastModifiedTime(javax.servlet.http.HttpServletRequest request)
           
static javax.mail.Session getMailSession(WebSite webSite)
           
static int getMenuCode(java.lang.Object o)
          Returns a numeric code for an object.
static Path getMeshCSSPath(WebSite webSite, javax.servlet.http.HttpServletRequest request, Path pageDir)
          Returns the path of the meshcms.css file, relative to page path.
static java.util.Locale getPageLocale(javax.servlet.jsp.PageContext pageContext)
          Tries to locate a Locale stored in the Page scope or in the Request scope.
static java.util.ResourceBundle getPageResourceBundle(javax.servlet.jsp.PageContext pageContext)
          Tries to locate a ResourceBundle stored in the Page scope or in the Request scope using the JSTL.
static java.lang.String getPathInContext(javax.servlet.http.HttpServletRequest request, Path path)
          Returns the full context path of the given path.
static Path getThemeCSSPath(javax.servlet.http.HttpServletRequest request, Path pageDir)
          Returns the complete path of the main.css file, relative to page path.
static Path getThemeFilePath(javax.servlet.http.HttpServletRequest request, Path pageDir)
          Returns the path of the main.jsp file, relative to page path.
static Path getThemeFolderPath(javax.servlet.http.HttpServletRequest request, Path pageDir)
          Returns the path of the folder of current theme, relative to page path.
static java.lang.String[] getWelcomeFiles(javax.servlet.ServletContext sc)
          Parses the web.xml configuration file and returns an array of welcome file names.
static boolean isCacheBlocked(javax.servlet.http.HttpServletRequest request)
          Checks the current Request scope to see if some class has called the setBlockCache(javax.servlet.http.HttpServletRequest) method.
static boolean isCached(WebSite webSite, SiteMap siteMap, Path pagePath)
           
static java.lang.String parseCharset(java.lang.String fullValue)
           
static void removeFromCache(WebSite webSite, SiteMap siteMap, Path pagePath)
           
static java.lang.String replaceThumbnails(WebSite webSite, java.lang.String body, java.lang.String contextPath, Path pagePath)
           
static void setBlockCache(javax.servlet.http.HttpServletRequest request)
          This method must be called to avoid the current page to be cached.
static java.lang.String tidyHTML(WebSite webSite, java.lang.String html)
           
static void updateLastModifiedTime(javax.servlet.http.HttpServletRequest request, java.io.File file)
           
static void updateLastModifiedTime(javax.servlet.http.HttpServletRequest request, long time)
           
static boolean verifyJSP(WebSite webSite, Path jspPath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numericDateFormatter

public static final java.text.SimpleDateFormat numericDateFormatter

FN_CHARS

public static final java.lang.String FN_CHARS
Characters allowed in a file name.

See Also:
Constant Field Values

FN_CHARMAP

public static final java.lang.String FN_CHARMAP
Mapping of the ISO-8859-1 characters to characters included in FN_CHARS.

See Also:
Constant Field Values

FN_SPACERS

public static final java.lang.String FN_SPACERS
Characters that are considered spacers in a file name.

See Also:
Constant Field Values

DEFAULT_WELCOME_FILES

public static final java.lang.String[] DEFAULT_WELCOME_FILES
The default array of welcome file names. This array is used if the welcome file names can't be found in the web.xml configuration file.


NUMBER_TO_ENTITY

public static java.util.Properties NUMBER_TO_ENTITY

ENTITY_TO_NUMBER

public static java.util.Properties ENTITY_TO_NUMBER

BODY_REGEX

public static final java.util.regex.Pattern BODY_REGEX

PRE_REGEX

public static final java.util.regex.Pattern PRE_REGEX

PRE_BR_REGEX

public static final java.util.regex.Pattern PRE_BR_REGEX

HYPERLINK_REGEX

public static final java.util.regex.Pattern HYPERLINK_REGEX

EMAIL_REGEX

public static final java.util.regex.Pattern EMAIL_REGEX
Method Detail

convertToHTMLEntities

public static java.lang.String convertToHTMLEntities(java.lang.String s,
                                                     boolean encodeTags)

convertToHTMLEntities

public static java.lang.String convertToHTMLEntities(java.lang.String s,
                                                     java.lang.String charset,
                                                     boolean encodeTags)

getWelcomeFiles

public static java.lang.String[] getWelcomeFiles(javax.servlet.ServletContext sc)
Parses the web.xml configuration file and returns an array of welcome file names. If the names can't be found, DEFAULT_WELCOME_FILES is returned.

Parameters:
sc - ServletContext required to access the web.xml file
Returns:
an array of welcome file names

getFullParameter

public static java.lang.String getFullParameter(javax.servlet.http.HttpServletRequest request,
                                                java.lang.String name)
Returns a request parameter in the form &name=value. Returns null if the value has not been found.


getPathInContext

public static java.lang.String getPathInContext(javax.servlet.http.HttpServletRequest request,
                                                Path path)
Returns the full context path of the given path.


getContextHomeURL

public static java.lang.StringBuffer getContextHomeURL(javax.servlet.http.HttpServletRequest request)
Reconstructs the full URL of the context home. The URL is returned as a StringBuffer so other elements can be added easily.


getFullThemeFolder

public static java.lang.String getFullThemeFolder(javax.servlet.http.HttpServletRequest request)
Returns the complete path of the folder of current theme (context path included).

Parameters:
request - used to get the theme name

getFullThemeFile

public static java.lang.String getFullThemeFile(javax.servlet.http.HttpServletRequest request)
Returns the complete path of the main.jsp file (context path included).

Parameters:
request - used to get the theme name

getFullThemeCSS

public static java.lang.String getFullThemeCSS(javax.servlet.http.HttpServletRequest request)
Returns the complete path of the main.css file (context path included).

Parameters:
request - used to get the theme name

getFullMeshCSS

public static java.lang.String getFullMeshCSS(WebSite webSite,
                                              javax.servlet.http.HttpServletRequest request)
Returns the complete path of the meshcms.css file (context path included). If meshcms.css is not found in the theme folder, the default from the admin theme is returned.

Parameters:
request - used to get the theme name

getThemeFolderPath

public static Path getThemeFolderPath(javax.servlet.http.HttpServletRequest request,
                                      Path pageDir)
Returns the path of the folder of current theme, relative to page path.

Parameters:
request - used to get the theme name
pageDir - path of the requested page folder

getThemeFilePath

public static Path getThemeFilePath(javax.servlet.http.HttpServletRequest request,
                                    Path pageDir)
Returns the path of the main.jsp file, relative to page path.

Parameters:
request - used to get the theme name
pageDir - path of the requested page folder

getThemeCSSPath

public static Path getThemeCSSPath(javax.servlet.http.HttpServletRequest request,
                                   Path pageDir)
Returns the complete path of the main.css file, relative to page path.

Parameters:
request - used to get the theme name
pageDir - path of the requested page folder

getMeshCSSPath

public static Path getMeshCSSPath(WebSite webSite,
                                  javax.servlet.http.HttpServletRequest request,
                                  Path pageDir)
Returns the path of the meshcms.css file, relative to page path. If meshcms.css is not found in the theme folder, the default from the admin theme is returned.

Parameters:
request - used to get the theme name
pageDir - path of the requested page folder

getMenuCode

public static int getMenuCode(java.lang.Object o)
Returns a numeric code for an object. This code is used in the menu, but can be used elsewhere if needed. It is equal to the hash code of the object, but never negative, since the minus sign creates problems when converted to a string and used as part of a variable name in JavaScript.


get2ndLevelDomain

public static java.lang.String get2ndLevelDomain(javax.servlet.http.HttpServletRequest request)
Returns the 2nd level domain from which the request comes.


get2ndLevelDomain

public static java.lang.String get2ndLevelDomain(java.lang.String urlString)
Returns the 2nd level domain from the URL.


setBlockCache

public static void setBlockCache(javax.servlet.http.HttpServletRequest request)
This method must be called to avoid the current page to be cached. For example, some modules will call this method to be sure that they are parsed again when the page is called another time.


isCacheBlocked

public static boolean isCacheBlocked(javax.servlet.http.HttpServletRequest request)
Checks the current Request scope to see if some class has called the setBlockCache(javax.servlet.http.HttpServletRequest) method.


getPageLocale

public static java.util.Locale getPageLocale(javax.servlet.jsp.PageContext pageContext)
Tries to locate a Locale stored in the Page scope or in the Request scope. If none is found, returns the Locale of the request, or at least the system default Locale.


getPageResourceBundle

public static java.util.ResourceBundle getPageResourceBundle(javax.servlet.jsp.PageContext pageContext)
Tries to locate a ResourceBundle stored in the Page scope or in the Request scope using the JSTL.

Returns:
the ResourceBundle, or null if not found

fixFileName

public static java.lang.String fixFileName(java.lang.String name,
                                           boolean useSpacers)
Returns a modified name that does not contain characters not recommended in a file name.


formatFileLength

public static java.lang.String formatFileLength(long length,
                                                java.util.Locale locale,
                                                java.util.ResourceBundle bundle)
Returns a nicer representation of the number as a file length. The number is returned as bytes, kilobytes or megabytes, with the unit attached.


getCharsetCanonicalName

public static java.lang.String getCharsetCanonicalName(java.lang.String charsetName)

parseCharset

public static java.lang.String parseCharset(java.lang.String fullValue)

updateLastModifiedTime

public static void updateLastModifiedTime(javax.servlet.http.HttpServletRequest request,
                                          java.io.File file)

updateLastModifiedTime

public static void updateLastModifiedTime(javax.servlet.http.HttpServletRequest request,
                                          long time)

getLastModifiedTime

public static long getLastModifiedTime(javax.servlet.http.HttpServletRequest request)

getMailSession

public static javax.mail.Session getMailSession(WebSite webSite)

getCacheFile

public static java.io.File getCacheFile(WebSite webSite,
                                        SiteMap siteMap,
                                        Path pagePath)

isCached

public static boolean isCached(WebSite webSite,
                               SiteMap siteMap,
                               Path pagePath)

removeFromCache

public static void removeFromCache(WebSite webSite,
                                   SiteMap siteMap,
                                   Path pagePath)

getCorrespondingPath

public static Path getCorrespondingPath(WebSite webSite,
                                        Path path,
                                        java.lang.String otherRoot)

getAcceptedLanguages

public static java.lang.String[] getAcceptedLanguages(javax.servlet.http.HttpServletRequest request)

fixRelativeURLs

public static java.lang.String fixRelativeURLs(java.lang.String html,
                                               Path root,
                                               boolean relative)

getJSPBlockPattern

public static java.util.regex.Pattern getJSPBlockPattern()

verifyJSP

public static boolean verifyJSP(WebSite webSite,
                                Path jspPath)

tidyHTML

public static java.lang.String tidyHTML(WebSite webSite,
                                        java.lang.String html)

createExcerpt

public static java.lang.String createExcerpt(WebSite webSite,
                                             java.lang.String body,
                                             int length,
                                             java.lang.String contextPath,
                                             Path oldPage,
                                             Path newPage)

fixLinks

public static java.lang.String fixLinks(WebSite webSite,
                                        java.lang.String body,
                                        java.lang.String contextPath,
                                        Path oldPage,
                                        Path newPage)

replaceThumbnails

public static java.lang.String replaceThumbnails(WebSite webSite,
                                                 java.lang.String body,
                                                 java.lang.String contextPath,
                                                 Path pagePath)

addToQueryString

public static java.lang.String addToQueryString(java.lang.String url,
                                                java.lang.String name,
                                                java.lang.String value,
                                                boolean encodeValue)

findLinks

public static java.lang.String findLinks(java.lang.String text)

findEmails

public static java.lang.String findEmails(java.lang.String text)