GenericURL Class

com.bea.portlet
GenericURL Class

public class GenericURL

    extends Object
    implements Cloneable

The GenericURL helps portal applications create URLs to resources within the portal.

To obtain an instance of GenericURL an application must supply valid HttpServletRequest and HttpServletResponse objects. This class derives certain default attributes from the request/response.

A GenericURL could be populated with the following attributes:

Attribute Purpose Nodes
Scheme Scheme. Valid values are "http" and "https". If not specified, uses the scheme that was used to make the request.
Domain Server domain. If not specified, uses the domain that was used to make the request. If requests are made via a proxy, specify the domain of the proxy server.
Port Server port. If not specified, uses the port from the request. If requests are made via a proxy, specify the port of the proxy server for the scheme used.
Secure port Secure server port. If not specified, uses the default secure server port. If requests are made via a proxy, specify the port of the proxy server for the scheme used.
Path prefix Path prefix. No default.

path path is the relative path to the resource (typically used for static/dynamic resources such as files, images, etc.). If the webapp name is needed in the path use contextualPath.
contextualPath contextualPath is the relative path to the resource, The contextual path includes the webAppName (typically used for static/dynamic resources such as files, images, etc.). No default.

Encode session Encodes the URL to include session ID. Defaults to true

Parameter Query parameters.

These parameters will be appended in the query string of the URL. This implementation preserves the order of parameters. If a parameter has multiple values, all values will be added at the first occurrence.

In order to specify web-app wide defaults for the above attributes, named URL templates may be specified. A URL template is a parameterized fully-qualified URL string. Each URL template has a name. URL templates should be specified in "url-template-config.xml" file in WEB-INF directory of the webapp.

An example of a URL template (with name "mytemplate") is "http://my.domain.com/{url:prefix}/{url:path}?{url:queryString}&myParam1=myValue1

In this templates, parameters are enclosed in {}s, and can be replaced by attributes set.

The parameters "url:path" and "url:queryString" are reserved and should be present in every template.

A URL with the above template as the basis could be created as follows: GenericURL url = GenericURL.createGenericURL(request, response); url.setTemplate("mytemplate"); url.setPathPrefix("myproxypath");

In this example, since scheme, domain and port are not parameterized, it is illegal to set these attributes on the URL.

This mechanism recognizes the following parameters in URL templates:

url:scheme Scheme Usage
url:domain Domain May be set
url:port Port May be set
url:securePort Secure port May be set
url:prefix Purpose May be set
url:path Extra path information Reserved, must be present in a template.
url:contextualPath Relative path to the resource No default.

url:queryString Reserved query parameters Reserved, must be present in a template.
url:currentPage Current page parameter May be set

If one or more parameters of a template are not set at runtime, the implementation will use defaults if appropriate.


Hierarchy
Object
  GenericURL
All Implemented Interfaces

Cloneable
Direct Known Subclasses

PostbackURL, ResourceURL

Field Summary

public static final String
CONTROL_STATE_PARAM

Control state parameter (reserved).

public static final String
CURRENT_PAGE
String
public static final String
DOMAIN

Template parameter: domain.

protected HttpServletRequest
httpRequest

HTTP request.

protected HttpServletResponse
httpResponse

HTTP response<.

public static final String
LOADSTATE_PARAM

Load state request query parameter (reserved).

public static final String
MODE_PARAM

Window mode query parameter (reserved).

public static final String
PAGE_LABEL_PARAM

Page label query parameter (reserved).

protected LinkedHashMap
params

Query parameters

public static final String
PATH

Template parameter: path.

public static final String
PORT

Template parameter: port.

public static final String
POSTBACK_PARAM

Postback request query parameter (reserved).

public static final String
PREFIX

Template parameter: prefix.

public static final String
QUERY_STRING

Template parameter: queryString.

public static final String
SCHEME

Template parameter: scheme.

public static final String
SECURE_PORT

Template parameter: securePort.

public static final String
STATE_PARAM

Window state query parameter (reserved).

public static final String
TREE_OPTIMIZATION_PARAM

Tree Optimization request query parameter (reserved).

public static final String
WINDOW_LABEL_PARAM

Window label query parameter (reserved).

 

Constructor Summary

GenericURL(HttpServletRequest request, HttpServletResponse response)

Constructs a GenericURL.

 

Method Summary

public void
addParameter(String name, String value)

Add a parameter.

public void
addParameter(String name, String value, boolean encode)

Add a parameter.

public void
addParameter(String name, String value, boolean encodeName, boolean encodeValue)

Add a parameter.

protected static StringBuffer
appendEnsureSeparator(StringBuffer buf, String token)
public Object
clone()

Returns a deep copy.

protected StringBuffer
createQueryString()

Creates a query string with the parameters set so far.

public String
getContextualPath()

Returns the contextualPath.

public String
getDomain()

Returns the domain.

public boolean
getEncodeSession()

Returns true if URL rewriting is enabled.

public String
getParameter(String name)

Returns the value of the parameter.

public List
getParameters(String name)

Returns the values of the given parameter.

public String
getPathPrefix()

Returns the path prefix.

public int
getPort()

Returns the port.

protected String
getResolvedTemplate()

Returns the template used for generating the URL string.

public String
getScheme()

Returns the protocol scheme.

public int
getSecurePort()

Returns the port.

public String
getTemplate()

Returns the current template.

public String
getUrlType()

Returns the type this URL.

public void
removeParameter(String name)

Removes the given parameter.

public void
setContextualPath(String contextualPath)

Sets contextual path.

public void
setDomain(String domain)

Sets the domain.

public void
setEncodeSession(boolean encodeSession)

Enables URL rewriting for encoding a session ID in the URL.

public void
setPath(String path)

Sets path.

public void
setPathPrefix(String pathPrefix)

Sets a path prefix.

public void
setPort(int port)

Sets a port.

public void
setScheme(String scheme)

Sets the protocol scheme.

public void
setSecurePort(int securePort)

Sets a port.

public void
setTemplate(String templateName)

Sets a URL template for this URL.

public String
toString()

Returns a string form (fully-quanlfied) of this URL removing any unset template parameters.

public String
toString(boolean removeUnsetParams)

Returns a string form (fully-qualified) of this URL.

 
Methods from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
   

Field Detail

CONTROL_STATE_PARAM

public static final String CONTROL_STATE_PARAM

Control state parameter (reserved).


CURRENT_PAGE

public static final String CURRENT_PAGE


DOMAIN

public static final String DOMAIN

Template parameter: domain.


httpRequest

protected HttpServletRequest httpRequest

HTTP request.


httpResponse

protected HttpServletResponse httpResponse

HTTP response<./p>


LOADSTATE_PARAM

public static final String LOADSTATE_PARAM

Load state request query parameter (reserved).


MODE_PARAM

public static final String MODE_PARAM

Window mode query parameter (reserved).


PAGE_LABEL_PARAM

public static final String PAGE_LABEL_PARAM

Page label query parameter (reserved).


params

protected LinkedHashMap params

Query parameters


PATH

public static final String PATH

Template parameter: path.


PORT

public static final String PORT

Template parameter: port.


POSTBACK_PARAM

public static final String POSTBACK_PARAM

Postback request query parameter (reserved).


PREFIX

public static final String PREFIX

Template parameter: prefix.


QUERY_STRING

public static final String QUERY_STRING

Template parameter: queryString.


SCHEME

public static final String SCHEME

Template parameter: scheme.


SECURE_PORT

public static final String SECURE_PORT

Template parameter: securePort.


STATE_PARAM

public static final String STATE_PARAM

Window state query parameter (reserved).


TREE_OPTIMIZATION_PARAM

public static final String TREE_OPTIMIZATION_PARAM

Tree Optimization request query parameter (reserved).


WINDOW_LABEL_PARAM

public static final String WINDOW_LABEL_PARAM

Window label query parameter (reserved).

 

Constructor Detail

GenericURL

protected GenericURL(HttpServletRequest request, 
                     HttpServletResponse response)

Constructs a GenericURL.

 

Method Detail

addParameter(String, String) Method

public void addParameter(String name, 
                         String value)

Add a parameter. Multiple values for the same parameter can be set by calling this method multiple times with the same name.

This method URL encodes the parameter name and value value.

Parameters

name
name
value
value

addParameter(String, String, boolean) Method

public void addParameter(String name, 
                         String value, 
                         boolean encode)

Add a parameter. Multiple values for the same parameter can be set by calling this method multiple times with the same name.

If the boolean argument is true, this method URL encodes the parameter name and value.

Parameters

name
name
value
value
encode

addParameter(String, String, boolean, boolean) Method

public void addParameter(String name, 
                         String value, 
                         boolean encodeName, 
                         boolean encodeValue)

Add a parameter. Multiple values for the same parameter can be set by calling this method multiple times with the same name.

If the encodeName boolean argument is true, this method URL encodes the parameter name. If the encodeValue boolean argument is true, this method URL encodes the parameter value.

Parameters

name
name
value
value
encodeName
encodeValue

appendEnsureSeparator(StringBuffer, String) Method

protected static StringBuffer appendEnsureSeparator(StringBuffer buf, 
                                                    String token)

clone() Method

public Object clone()

Returns a deep copy.

Overrides
Object.clone()

Returns

clone

createQueryString() Method

protected StringBuffer createQueryString()

Creates a query string with the parameters set so far.

Returns

query string

getContextualPath() Method

public String getContextualPath()

Returns the contextualPath.

Returns

contextualPath

getDomain() Method

public String getDomain()

Returns the domain. If no domain was previously set, returns the domain that was used to make the current request.

Returns

domain

getEncodeSession() Method

public boolean getEncodeSession()

Returns true if URL rewriting is enabled.

Returns

boolean

getParameter(String) Method

public String getParameter(String name)

Returns the value of the parameter. If the parameter has several values, returns the first value.

Parameters

name
name of the parameter

Returns

value value of the parameter

getParameters(String) Method

public List getParameters(String name)

Returns the values of the given parameter.

Parameters

name
name of the parameter

Returns

values of the parameter

getPathPrefix() Method

public String getPathPrefix()

Returns the path prefix.

Returns

path prefix

getPort() Method

public int getPort()

Returns the port. If no port was previously set, returns the port from the request.

Returns

port

getResolvedTemplate() Method

protected String getResolvedTemplate()

Returns the template used for generating the URL string.

This method returns the same value as does the getTemplate() method if the set template exists. If not, this method returns the default template named "default".


getScheme() Method

public String getScheme()

Returns the protocol scheme. If no protocol was previously set, returns the protocol that was used to make the current request.

Returns

protocol scheme

getSecurePort() Method

public int getSecurePort()

Returns the port. If no port was previously set, returns the default secure server port.

Returns

secure port

getTemplate() Method

public String getTemplate()

Returns the current template.

Returns

URL template

getUrlType() Method

public String getUrlType()

Returns the type this URL.


removeParameter(String) Method

public void removeParameter(String name)

Removes the given parameter.

Parameters

name
name

setContextualPath(String) Method

public void setContextualPath(String contextualPath)

Sets contextual path.


setDomain(String) Method

public void setDomain(String domain)

Sets the domain. If a domain is not specified, the domain that was used to make the current request will be used.

Parameters

domain
domain

setEncodeSession(boolean) Method

public void setEncodeSession(boolean encodeSession)

Enables URL rewriting for encoding a session ID in the URL. If not set to true, does not encode the URL with the session ID. Default is true.

Parameters

encodeSession
boolean

setPath(String) Method

public void setPath(String path)

Sets path.

Parameters

path
path

setPathPrefix(String) Method

public void setPathPrefix(String pathPrefix)

Sets a path prefix.

Parameters

pathPrefix
path prefix

setPort(int) Method

public void setPort(int port)

Sets a port. If a port is not specified, the port from the request will be used.

Parameters

port
port

setScheme(String) Method

public void setScheme(String scheme)

Sets the protocol scheme. Should be one of "HTTP" or "HTTPS". If a protocol is not specified, the protocol that was used to make the current request will be used.

Parameters

scheme
protocol scheme

setSecurePort(int) Method

public void setSecurePort(int securePort)

Sets a port. If a port is not specified, the default server secure port will be used.

Parameters

securePort
secure port

setTemplate(String) Method

public void setTemplate(String templateName)

Sets a URL template for this URL.

Parameters

templateName
name of the url template

toString() Method

public String toString()

Returns a string form (fully-quanlfied) of this URL removing any unset template parameters.

Overrides
Object.toString()

Returns

string

toString(boolean) Method

public String toString(boolean removeUnsetParams)

Returns a string form (fully-qualified) of this URL. If the boolean argument is true, removes unset template parameters.