org.opensubsystems.core.www
Class WebUtils

java.lang.Object
  extended by org.opensubsystems.core.www.WebUtils

public final class WebUtils
extends java.lang.Object

Collection of useful methods for Web environment.

Version:
$Id: WebUtils.java,v 1.18 2007/02/20 04:12:33 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.13 2006/05/01 23:41:09 jlegeny

Field Summary
protected static int s_iConfiguredWebPort
          HTTP server port the web application was originally configred to run on.
protected static int s_iConfiguredWebPortSecure
          HTTP server port the web application was originally configred to accept SSL requests.
protected static int s_iWebFileBufferSize
          Size of the buffer used to send files.
protected static int s_iWebPort
          HTTP server port the web application is running on.
protected static int s_iWebPortSecure
          HTTPS server port the web application is running on (SSL port).
static int WEBFILE_BUFFER_DEFAULT_SIZE
          Default value for the WEBUTILS_WEBFILE_BUFFER_SIZE.
static java.lang.String WEBSERVER_PORT
          Name of the property specifying what port the embedded web server should start on.
static java.lang.String WEBSERVER_PORT_SECURE
          Name of the property specifying what port sthe embedded web server should accepts SSL requests.
static java.lang.String WEBUTILS_WEBFILE_BUFFER_SIZE
          Name of the property for size of the buffer used to serve files.
 
Method Summary
static void adjust(javax.servlet.http.HttpServletRequest hsrqRequest)
          Adjust non secure server port number the web application is currently running on.
static java.lang.String debug(javax.servlet.http.HttpServletRequest hsrqRequest)
          Create debug string containing all parameter names and their values from the request.
static java.lang.String debug(javax.servlet.ServletConfig scConfig)
          Create debug string containing all parameter names and their values from the config.
static java.lang.String debug(javax.servlet.ServletContext scContext)
          Create debug string containing all parameter names and their values from the context.
static java.lang.String encode(java.lang.String strInput)
          Encode URL string by method URLEncoder.encode() - after this encoding all spaces will be encoded as + character.
static void forwardToOrigin(javax.servlet.http.HttpServletRequest hsrqRequest, javax.servlet.http.HttpServletResponse hsrpResponse)
          Forward request back to the same page where it came from.
static int getActualPort()
          Get value of port, which actually accepts non SSL requests on.
static int getActualSSLPort()
          Get value of port, which was configured to accepts SSL requests on.
static int getConfiguredPort()
          Get value of port, which was configured to accepts non SSL requests on.
static int getConfiguredSSLPort()
          Get value of port, which was configured to accepts SSL requests on.
static java.lang.String getFullRequestPath(javax.servlet.http.HttpServletRequest hsrqRequest)
          Get full path from HTTP request consisting of the servlet path and the path info
static java.lang.String getFullRequestURL(javax.servlet.http.HttpServletRequest hsrqRequest)
          Reconstruct full URL from HTTP request with protocol, full path and query strings.
static java.lang.String getRequestURLWithoutQuery(javax.servlet.http.HttpServletRequest hsrqRequest)
          Reconstruct full URL from HTTP request with protocol, full path but without query strings.
static boolean isIndexPage(javax.servlet.http.HttpServletRequest hsrqRequest)
          Test if the requested path is path for the index page.
static boolean isMainIndexPage(javax.servlet.http.HttpServletRequest hsrqRequest)
          Test if the real path is path for the main index page.
static boolean isStaticWebPage(javax.servlet.http.HttpServletRequest hsrqRequest)
          Test if the requested path is path for the regular (.html) page.
static java.lang.String readProperty(javax.servlet.ServletConfig scConfig, java.lang.String strPropertyName, java.lang.String strDefaultValue, boolean bEmptyAllowed)
          Read property from from different configuration locations.
static java.lang.String readProperty(javax.servlet.ServletContext scContext, java.lang.String strPropertyName, java.lang.String strDefaultValue, boolean bEmptyAllowed)
          Read property from from different configuration locations.
static void serveFile(javax.servlet.http.HttpServletResponse hsrpResponse, java.lang.String strRealPath)
          Serve files to the Internet.
static java.lang.String toggleSecure(javax.servlet.http.HttpServletRequest hsrqRequest, java.lang.String strURL, boolean bIsSecure)
          Switch URL to HTTP or HTTPS and also particular ports.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WEBSERVER_PORT

public static final java.lang.String WEBSERVER_PORT
Name of the property specifying what port the embedded web server should start on.

See Also:
s_iConfiguredWebPort, Constant Field Values

WEBSERVER_PORT_SECURE

public static final java.lang.String WEBSERVER_PORT_SECURE
Name of the property specifying what port sthe embedded web server should accepts SSL requests.

See Also:
s_iConfiguredWebPortSecure, Constant Field Values

WEBUTILS_WEBFILE_BUFFER_SIZE

public static final java.lang.String WEBUTILS_WEBFILE_BUFFER_SIZE
Name of the property for size of the buffer used to serve files.

See Also:
s_iWebFileBufferSize, WEBFILE_BUFFER_DEFAULT_SIZE, Constant Field Values

WEBFILE_BUFFER_DEFAULT_SIZE

public static final int WEBFILE_BUFFER_DEFAULT_SIZE
Default value for the WEBUTILS_WEBFILE_BUFFER_SIZE.

See Also:
s_iWebFileBufferSize, WEBUTILS_WEBFILE_BUFFER_SIZE, Constant Field Values

s_iWebFileBufferSize

protected static int s_iWebFileBufferSize
Size of the buffer used to send files.

See Also:
WEBUTILS_WEBFILE_BUFFER_SIZE, WEBFILE_BUFFER_DEFAULT_SIZE

s_iConfiguredWebPort

protected static int s_iConfiguredWebPort
HTTP server port the web application was originally configred to run on. This port may differ from the actual port in case the server was not able to start on the configured port and it started on a different one and it adjusted the configured port.


s_iWebPort

protected static int s_iWebPort
HTTP server port the web application is running on.


s_iConfiguredWebPortSecure

protected static int s_iConfiguredWebPortSecure
HTTP server port the web application was originally configred to accept SSL requests. This port may differ from the actual port in case the server was not able to start on the configured port and it started on a different 1one and it adjusted the configured port.


s_iWebPortSecure

protected static int s_iWebPortSecure
HTTPS server port the web application is running on (SSL port).

Method Detail

getConfiguredPort

public static int getConfiguredPort()
Get value of port, which was configured to accepts non SSL requests on.

Returns:
int - port configured to accepts non SSL requests

getActualPort

public static int getActualPort()
Get value of port, which actually accepts non SSL requests on.

Returns:
int - port to accepts non SSL requests

getConfiguredSSLPort

public static int getConfiguredSSLPort()
Get value of port, which was configured to accepts SSL requests on.

Returns:
int - port to accepts SSL requests

getActualSSLPort

public static int getActualSSLPort()
Get value of port, which was configured to accepts SSL requests on.

Returns:
int - port to accepts SSL requests

serveFile

public static void serveFile(javax.servlet.http.HttpServletResponse hsrpResponse,
                             java.lang.String strRealPath)
                      throws java.io.IOException
Serve files to the Internet.

Parameters:
hsrpResponse - - the servlet response.
strRealPath - - real path to the file to server
Throws:
java.io.IOException - - an error has occured while accessing the file or writing response

getFullRequestURL

public static java.lang.String getFullRequestURL(javax.servlet.http.HttpServletRequest hsrqRequest)
Reconstruct full URL from HTTP request with protocol, full path and query strings.

Parameters:
hsrqRequest - - the servlet request.
Returns:
String - full URL

getRequestURLWithoutQuery

public static java.lang.String getRequestURLWithoutQuery(javax.servlet.http.HttpServletRequest hsrqRequest)
Reconstruct full URL from HTTP request with protocol, full path but without query strings.

Parameters:
hsrqRequest - - the servlet request.
Returns:
String - full URL

getFullRequestPath

public static java.lang.String getFullRequestPath(javax.servlet.http.HttpServletRequest hsrqRequest)
Get full path from HTTP request consisting of the servlet path and the path info

Parameters:
hsrqRequest - - the servlet request
Returns:
- full URL

adjust

public static void adjust(javax.servlet.http.HttpServletRequest hsrqRequest)
Adjust non secure server port number the web application is currently running on.

Parameters:
hsrqRequest - - the servlet request.

toggleSecure

public static java.lang.String toggleSecure(javax.servlet.http.HttpServletRequest hsrqRequest,
                                            java.lang.String strURL,
                                            boolean bIsSecure)
Switch URL to HTTP or HTTPS and also particular ports.

Parameters:
hsrqRequest - - the servlet request
strURL - - relative URL
bIsSecure - - flag signaling if url should be switched to secure - true = switch to secure (use HTTPS) - false = switch to unsecure (use HTTP)
Returns:
String - absolute URL

debug

public static java.lang.String debug(javax.servlet.http.HttpServletRequest hsrqRequest)
Create debug string containing all parameter names and their values from the request.

Parameters:
hsrqRequest - - the servlet request.
Returns:
String - debug string containing all parameter names and their values from the request

debug

public static java.lang.String debug(javax.servlet.ServletConfig scConfig)
Create debug string containing all parameter names and their values from the config.

Parameters:
scConfig - - config to print out
Returns:
String - debug string containing all parameter names and their values from the config

debug

public static java.lang.String debug(javax.servlet.ServletContext scContext)
Create debug string containing all parameter names and their values from the context.

Parameters:
scContext - - context to print out
Returns:
String - debug string containing all parameter names and their values from the context

forwardToOrigin

public static void forwardToOrigin(javax.servlet.http.HttpServletRequest hsrqRequest,
                                   javax.servlet.http.HttpServletResponse hsrpResponse)
                            throws java.io.IOException,
                                   javax.servlet.ServletException
Forward request back to the same page where it came from.

Parameters:
hsrqRequest - - the servlet request.
hsrpResponse - - the servlet response.
Throws:
java.io.IOException - - an error while writing response
javax.servlet.ServletException - - an error while serving the request

isMainIndexPage

public static boolean isMainIndexPage(javax.servlet.http.HttpServletRequest hsrqRequest)
Test if the real path is path for the main index page. It either doesn't contain any path e.g. http://www.bastafidli.com or just a root directory http://www.bastafidli.com/.

Parameters:
hsrqRequest - - the servlet request.
Returns:
boolean - true if the page is main index page

isIndexPage

public static boolean isIndexPage(javax.servlet.http.HttpServletRequest hsrqRequest)
Test if the requested path is path for the index page. It either points just to a directory http://www.bastafidli.com/directory/ or it ends with index.html e.g. http://www.bastafidli.com/directory.html.

Parameters:
hsrqRequest - - the servlet request.
Returns:
boolean - true if the page is index page for the folder false otherwise

isStaticWebPage

public static boolean isStaticWebPage(javax.servlet.http.HttpServletRequest hsrqRequest)
Test if the requested path is path for the regular (.html) page.

Parameters:
hsrqRequest - - the servlet request.
Returns:
boolean - true if the page is regular web page (ends with .html)

readProperty

public static java.lang.String readProperty(javax.servlet.ServletConfig scConfig,
                                            java.lang.String strPropertyName,
                                            java.lang.String strDefaultValue,
                                            boolean bEmptyAllowed)
Read property from from different configuration locations. 1st try read from external configuration file 2nd try to read from servlet config (init-param within web.xml) 3rd try to read from servlet context (context-param within web.xml)

Parameters:
scConfig - - servlet config to search (application configuration file is known by other mean)
strPropertyName - - name of the property to read
strDefaultValue - - default value of the property returned in case the value is not found, null is allows
bEmptyAllowed - - is empty string valid value of the property
Returns:
String - value of the property

readProperty

public static java.lang.String readProperty(javax.servlet.ServletContext scContext,
                                            java.lang.String strPropertyName,
                                            java.lang.String strDefaultValue,
                                            boolean bEmptyAllowed)
Read property from from different configuration locations. 1st try read from external configuration file 2nd try to read from servlet context (context-param within web.xml)

Parameters:
scContext - - servlet context to search (application configuration file is known by other mean)
strPropertyName - - name of the property to read
strDefaultValue - - default value of the property returned in case the value is not found, null is allows
bEmptyAllowed - - is empty string valid value of the property
Returns:
String - value of the property

encode

public static java.lang.String encode(java.lang.String strInput)
                               throws java.io.UnsupportedEncodingException
Encode URL string by method URLEncoder.encode() - after this encoding all spaces will be encoded as + character. But there is also problem with decoding this + character - it is decoded again as character + . It means after 1st encode process we will replace all occurences of + by code for space. E.g. input string = "abc def" after encoded = "abc+def" after replace = "abc%20def"

Parameters:
strInput - - input string that has to be encoded
Returns:
- encoded string
Throws:
java.io.UnsupportedEncodingException - - error while encode process


Copyright © 2003 - 2006 OpenSubsystems s.r.o.