org.w3c.mwi.mobileok.basic
Class Resource

java.lang.Object
  extended by org.w3c.mwi.mobileok.basic.Resource

public class Resource
extends java.lang.Object

Encapsulates information and basic functionality to represent Web resources.

The information includes the absolute URI of the resource and the list of exchanges that were needed to retrieve the resource along with the representation of the resource in itself.

Version:
$Revision: 1.1 $
Author:
Francois Daoust

Nested Class Summary
static class Resource.LoadType
          Describes the different ways that a typical browser would handle a resource.
 
Field Summary
private  TesterConfiguration config
          Since the processing will be done in separate threads, we need some local reference to the configuration in use for current processing.
private  DecodedContent decodedContent
          Decoded content.
private  boolean decodedFlag
          Flag that asserts whether the resource has been decoded.
private  int depth
          Depth of the resource in the Web created by the initial resource.
private  Resource.LoadType loadType
          Browsing rendering type of the resource.
private  boolean parsedFlag
          Flag that asserts whether the resource has been parsed to extract further resources.
private  ResourceContext resourceContext
          Context that gave birth to the resource.
private  java.util.List<RetrievalElement> retrievedElements
          Retrieved resource.
private  boolean retrievedFlag
          Flag that asserts whether the resource has been retrieved.
private static java.text.DateFormat[] RFC_2616_DATE_FORMATS
          Possible date formats for HTTP header fields that contain a date.
private  java.net.URI uri
          Absolute URI of the resource.
 
Constructor Summary
Resource(java.net.URI uri, int depth, ResourceContext resourceContext, Resource.LoadType loadType)
          Creates a class instance bound to a URI, a depth and a context.
 
Method Summary
static int compareUrisWithoutFragments(java.net.URI uri1, java.net.URI uri2)
          Compares two absolute URIs, leaving fragment parts aside
 void decode()
          Decodes the content that has been previously retrieved.
 java.util.List<Resource> extractResources(java.util.List<Resource> knownResources)
          Extract additional resources from the current resource.
 java.util.List<RetrievalElement> getAllRedirects()
          Returns the complete list of exchanges, including the resource itself.
 DecodedContent getContent()
          Returns the resource's decoded content.
 int getDepth()
          Returns the depth of the resource in the web created by the initial resource.
 RetrievalElement getLastRedirect()
          Returns the exchange that contains the resource itself.
 Resource.LoadType getLoadType()
          Returns the load type of the resource.
 RetrievalElement getRedirect(int redirect)
          Returns the exchange of the intermediate redirection identified by its index.
 int getRedirectCount()
          Returns the number of redirections that were required to retrieve the resource.
 ResourceContext getResourceContext()
          Returns the context that gave birth to the resource.
 java.net.URI getUri()
          Returns the absolute URI of the resource.
 java.lang.String getURI(int redirect)
          Returns the absolute URI of the intermediate redirection identified by its index.
 boolean hasBeenDecoded()
          Returns true when the resource has been decoded.
 boolean hasBeenParsed()
          Returns true when the resource has been parsed in search of new resources.
 boolean hasBeenRetrieved()
          Returns true when the resource has been retrieved.
static boolean isValidDateHeader(java.lang.String dateHeader)
          Checks that the date is valid according to RFC2616.
static boolean isValidExpirationDate(java.lang.String currentDateStr, java.lang.String expirationDateStr)
          Checks that the expiration date lies in the future of given date.
static java.net.URI massageURI(java.net.URI uri)
          Makes sure that the URI is absolute, and automatically adds "http://" when it is not.
private static java.util.Date parseDateHeader(java.lang.String dateHeader)
          Parses a string representing a date in one of the formats defined in RFC 2616 as formats that must be parsed by an HTTP/1.1 server.
static java.net.URI parseUri(java.net.URI baseUri, java.lang.String uriString)
          Checks that a URI string is a valid URI, and resolves it against a base URI.
 void retrieve()
          Retrieves the resource.
 void setLoadType(Resource.LoadType type)
          Sets the load type of the resource.
 org.w3c.dom.Node toMokiNode(org.w3c.dom.Document document)
          Serializes the resource to a moki node.
 void useRetrievalInformation(java.util.List<RetrievalElement> retrieved)
          Use the given retrieval material as the retrieved information of the resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RFC_2616_DATE_FORMATS

private static final java.text.DateFormat[] RFC_2616_DATE_FORMATS
Possible date formats for HTTP header fields that contain a date.


uri

private final java.net.URI uri
Absolute URI of the resource.


depth

private final int depth
Depth of the resource in the Web created by the initial resource. The inital resource comes with a depth of 0.


resourceContext

private final ResourceContext resourceContext
Context that gave birth to the resource.


loadType

private Resource.LoadType loadType
Browsing rendering type of the resource.


retrievedElements

private java.util.List<RetrievalElement> retrievedElements
Retrieved resource.


decodedContent

private DecodedContent decodedContent
Decoded content.


retrievedFlag

private boolean retrievedFlag
Flag that asserts whether the resource has been retrieved.


decodedFlag

private boolean decodedFlag
Flag that asserts whether the resource has been decoded.


parsedFlag

private boolean parsedFlag
Flag that asserts whether the resource has been parsed to extract further resources.


config

private TesterConfiguration config
Since the processing will be done in separate threads, we need some local reference to the configuration in use for current processing.

Constructor Detail

Resource

public Resource(java.net.URI uri,
                int depth,
                ResourceContext resourceContext,
                Resource.LoadType loadType)
Creates a class instance bound to a URI, a depth and a context.

Parameters:
uri - absolute URI of the resource.
depth - the depth of the resource.
resourceContext - the context that gave birth to the resource.
loadType - browser rendering type for the resource. The load type may be set afterwards through a call to setLoadType(LoadType)
Method Detail

massageURI

public static java.net.URI massageURI(java.net.URI uri)
Makes sure that the URI is absolute, and automatically adds "http://" when it is not.

Parameters:
uri - URI to massage.
Returns:
a copy of the URI, with a defined scheme.

retrieve

public final void retrieve()
Retrieves the resource.

The ResourceRetriever that is used to retrieve the resource depends on the URI scheme. The ResourceRetrieverFactory instance of the current TesterConfiguration will be used to select the dedicated one.

The function sets the retrievedFlag flag.


useRetrievalInformation

public final void useRetrievalInformation(java.util.List<RetrievalElement> retrieved)
Use the given retrieval material as the retrieved information of the resource.

The function should be used whenever a resource that has already been retrieved needs to be created for another context, not to have to retrieve the resource twice.

The function sets the retrievedFlag flag.

Parameters:
retrieved - list of exchanges to use as retrieval information

decode

public final void decode()
                  throws TestException
Decodes the content that has been previously retrieved.

Decoding typically depends on the content-type of the retrieved content. The

The function sets the decodedFlag flag, except when the resource has not been retrieved yet through a call to retrieve().

Throws:
TestException

extractResources

public final java.util.List<Resource> extractResources(java.util.List<Resource> knownResources)
Extract additional resources from the current resource.

All ResourceExtractors defined in the current TesterConfiguration are applied to the current resource.

The function sets the parsedFlag flag, except when the resource has not been retrieved or decoded yet.

Parameters:
knownResources - list of resources that are already known.
Returns:
the list of extracted resources, null when no resources were extracted.

getUri

public final java.net.URI getUri()
Returns the absolute URI of the resource.

Returns:
the absolute URI of the resource.

getDepth

public final int getDepth()
Returns the depth of the resource in the web created by the initial resource.

Returns:
the depth of the resource, 0 when the resource is the initial one.

getResourceContext

public final ResourceContext getResourceContext()
Returns the context that gave birth to the resource.

Bear in mind that the ResourceContext enumeration may be extended to suit anyone's needs.

Returns:
the resource context.

getURI

public final java.lang.String getURI(int redirect)
Returns the absolute URI of the intermediate redirection identified by its index.

See getRedirectCount() for more details on the meaning of the term redirection.

Parameters:
redirect - index of the redirection, starting at 0.
Returns:
the absolute URI of the given redirect.

hasBeenRetrieved

public final boolean hasBeenRetrieved()
Returns true when the resource has been retrieved.

Please note that a true value does not entail that the retrieval of the resource was a success.

The flag is set by the retrieve() function.

Returns:
true when the resource has already been retrieved, false otherwise.

hasBeenDecoded

public final boolean hasBeenDecoded()
Returns true when the resource has been decoded.

Please note that a true value does not entail that the the resource could be successfully decoded.

The flag is set by the decode() function.

Returns:
true when the resource has already been decoded, false otherwise.

hasBeenParsed

public final boolean hasBeenParsed()
Returns true when the resource has been parsed in search of new resources.

The flag is set by the extractResources(List) function.

Returns:
true when the resource has already been parsed, false otherwise.

getLoadType

public final Resource.LoadType getLoadType()
Returns the load type of the resource.

Returns:
the load type of the resource.

setLoadType

public final void setLoadType(Resource.LoadType type)
Sets the load type of the resource.

Parameters:
type - load type to set.

getContent

public final DecodedContent getContent()
Returns the resource's decoded content.

Returns:
the decoded content, null when content was not or could not be decoded.

getRedirectCount

public final int getRedirectCount()
Returns the number of redirections that were required to retrieve the resource.

In the HTTP/HTTPS context, the term redirections represents the number of 302 redirect responses received before the final resource was reached. The term may have other meanings in other schemes.

Returns:
number of redirections, final exchange not included.

getRedirect

public final RetrievalElement getRedirect(int redirect)
Returns the exchange of the intermediate redirection identified by its index.

See getRedirectCount() for more details on the meaning of the term redirection.

Parameters:
redirect - index of the redirection, starting at 0.
Returns:
the exchange of the given redirect.

getLastRedirect

public final RetrievalElement getLastRedirect()
Returns the exchange that contains the resource itself.

Returns:
the last exchange.

getAllRedirects

public final java.util.List<RetrievalElement> getAllRedirects()
Returns the complete list of exchanges, including the resource itself.

Returns:
the complete list of exchanges.

toMokiNode

public final org.w3c.dom.Node toMokiNode(org.w3c.dom.Document document)
Serializes the resource to a moki node.

The precise format of the moki node depends on the resource content.

Parameters:
document - DOM document the created node should belong to
Returns:
the moki representation of the resource as a DOM node. Note the returned node only belongs to the document, but still needs to be inserted in the document where appropriate

parseUri

public static java.net.URI parseUri(java.net.URI baseUri,
                                    java.lang.String uriString)
Checks that a URI string is a valid URI, and resolves it against a base URI.

Parameters:
baseUri - base URI against which the URI should be resolved
uriString - URI string to check
Returns:
the absolute URI, null when the URI is not valid.

compareUrisWithoutFragments

public static int compareUrisWithoutFragments(java.net.URI uri1,
                                              java.net.URI uri2)
Compares two absolute URIs, leaving fragment parts aside

Parameters:
uri1 - First URI to compare
uri2 - Second URI to compare
Returns:
A negative integer, zero, or a positive integer as the first URI is less than, equal to, or greater than the second URI

isValidExpirationDate

public static boolean isValidExpirationDate(java.lang.String currentDateStr,
                                            java.lang.String expirationDateStr)
Checks that the expiration date lies in the future of given date.

Parameters:
currentDateStr - current date
expirationDateStr - expiration date
Returns:
true when current date is in the past of expiration date, false otherwise. Returns true when the expiration date is invalid.

isValidDateHeader

public static boolean isValidDateHeader(java.lang.String dateHeader)
Checks that the date is valid according to RFC2616.

Parameters:
dateHeader - date to parse.
Returns:
true when the date is valid, false otherwise.
See Also:
parseDateHeader(String)

parseDateHeader

private static java.util.Date parseDateHeader(java.lang.String dateHeader)
                                       throws java.text.ParseException
Parses a string representing a date in one of the formats defined in RFC 2616 as formats that must be parsed by an HTTP/1.1 server.

The formats are:

Throws:
java.text.ParseException