com.bradmcevoy.http
Class SimpleResource

java.lang.Object
  extended by com.bradmcevoy.http.SimpleResource
All Implemented Interfaces:
GetableResource, PostableResource, Resource
Direct Known Subclasses:
SimpleDigestResource

public class SimpleResource
extends java.lang.Object
implements GetableResource, PostableResource

A very simple resource implementation, which simply takes parameters in the constructor to define the resource, which includes the content Can be useful for resources defined in by code, where the content is a classpath item


Constructor Summary
SimpleResource(java.lang.String name, java.util.Date modDate, byte[] content, java.lang.String contentType, java.lang.String uniqueId, Resource secureResource)
           
SimpleResource(java.lang.String name, java.util.Date modDate, byte[] content, java.lang.String contentType, java.lang.String uniqueId, java.lang.String realm)
           
 
Method Summary
 java.lang.Object authenticate(java.lang.String user, java.lang.String password)
          Check the given credentials, and return a relevant object if accepted.
 boolean authorise(Request request, Request.Method method, Auth auth)
          Return true if the current user is permitted to access this resource using the specified method.
 java.lang.String checkRedirect(Request request)
          Determine if a redirect is required for this request, and if so return the URL to redirect to.
 java.lang.Long getContentLength()
          The length of the content in this resource.
 java.lang.String getContentType(java.lang.String accepts)
          Given a comma separated listed of preferred content types acceptable for a client, return one content type which is the best.
 java.lang.Long getMaxAgeSeconds(Auth auth)
          How many seconds to allow the content to be cached for, or null if caching is not allowed The provided auth object allows this method to determine an appropriate caching time depending on authenticated context.
 java.util.Date getModifiedDate()
          The date and time that this resource, or any part of this resource, was last modified.
 java.lang.String getName()
          Note that this name MUST be consistent with URL resolution in your ResourceFactory

If they aren't consistent Milton will generate a different href in PropFind responses then what clients have request and this will cause either an error or no resources to be displayed

 java.lang.String getRealm()
          Return the security realm for this resource.
 java.lang.String getUniqueId()
          Returning a null value is allowed, and disables the ETag field.
 java.lang.String processForm(java.util.Map<java.lang.String,java.lang.String> parameters, java.util.Map<java.lang.String,FileItem> files)
           
 void sendContent(java.io.OutputStream out, Range range, java.util.Map<java.lang.String,java.lang.String> params, java.lang.String contentType)
          Send the resource's content using the given output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleResource

public SimpleResource(java.lang.String name,
                      java.util.Date modDate,
                      byte[] content,
                      java.lang.String contentType,
                      java.lang.String uniqueId,
                      java.lang.String realm)

SimpleResource

public SimpleResource(java.lang.String name,
                      java.util.Date modDate,
                      byte[] content,
                      java.lang.String contentType,
                      java.lang.String uniqueId,
                      Resource secureResource)
Method Detail

sendContent

public void sendContent(java.io.OutputStream out,
                        Range range,
                        java.util.Map<java.lang.String,java.lang.String> params,
                        java.lang.String contentType)
                 throws java.io.IOException
Description copied from interface: GetableResource
Send the resource's content using the given output stream. Implementations should assume that bytes are being physically transmitted and that headers have already been committed, although this might not be the case with all web containers.

This method will be used to serve GET requests, and also to generate content following POST requests (if they have not redirected)

The Range argument is not-null for partial content requests. In this case implementations should (but are not required) to only send the data range requested.

The contentType argument is that which was resolved by negotiation in the getContentType method. HTTP allows a given resource to have multiple representations on the same URL. For example, a data series could be retrieved as a chart as SVG, PNG, JPEG, or as text as CSV or XML. When the user agent requests the resource is specified what content types it can accept. These are matched against those that can be provided by the server and a preferred representation is selected. That contentType is set in the response header and is provided here so that the resource implementation can render itself appropriately.

Specified by:
sendContent in interface GetableResource
Parameters:
out - - the output stream to send the content to
range - - null for normal GET's, not null for partial GET's. May be ignored
params - - request parameters
contentType - - the contentType selected by negotiation
Throws:
java.io.IOException - - if there is an exception writing content to the output stream. This indicates that the client has disconnected (as frequently occurs with http transfers). DO NOT throw an IOException if there was an internal error generating the response (eg if reading from a database)

getMaxAgeSeconds

public java.lang.Long getMaxAgeSeconds(Auth auth)
Description copied from interface: GetableResource
How many seconds to allow the content to be cached for, or null if caching is not allowed The provided auth object allows this method to determine an appropriate caching time depending on authenticated context. For example, in a CMS in might be appropriate to have a short expiry time for logged in users who might be editing content, as opposed to non-logged in users who are just viewing the site.

Specified by:
getMaxAgeSeconds in interface GetableResource

getContentType

public java.lang.String getContentType(java.lang.String accepts)
Description copied from interface: GetableResource
Given a comma separated listed of preferred content types acceptable for a client, return one content type which is the best.

Returns the most preferred MIME type. E.g. text/html, image/jpeg, etc

Must be IANA registered

accepts is the accepts header. Eg: Accept: text/*, text/html, text/html;level=1

See - http://www.iana.org/assignments/media-types/ for a list of content types See - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for details about the accept header

If you can't handle accepts interpretation, just return a single content type - E.g. text/html

But typically you should do something like this:

   String mime = ContentTypeUtils.findContentTypes( this.file );
   return ContentTypeUtils.findAcceptableContentType( mime, preferredList );
 

Specified by:
getContentType in interface GetableResource
See Also:
ContentTypeUtils

getContentLength

public java.lang.Long getContentLength()
Description copied from interface: GetableResource
The length of the content in this resource. If unknown return NULL

Specified by:
getContentLength in interface GetableResource

getUniqueId

public java.lang.String getUniqueId()
Description copied from interface: Resource
Returning a null value is allowed, and disables the ETag field.

If a unique id is returned it will be combined with the modified date (if available) to produce an ETag which identifies this version of this resource.

Specified by:
getUniqueId in interface Resource
Returns:
- a string which uniquely identifies this resource. This will be used in the ETag header field, and affects caching of resources.

getName

public java.lang.String getName()
Description copied from interface: Resource
Note that this name MUST be consistent with URL resolution in your ResourceFactory

If they aren't consistent Milton will generate a different href in PropFind responses then what clients have request and this will cause either an error or no resources to be displayed

Specified by:
getName in interface Resource
Returns:
- the name of this resource. Ie just the local name, within its folder

authenticate

public java.lang.Object authenticate(java.lang.String user,
                                     java.lang.String password)
Description copied from interface: Resource
Check the given credentials, and return a relevant object if accepted.

Returning null indicates credentials were not accepted

Specified by:
authenticate in interface Resource
Parameters:
user - - the user name provided by the user's agent
password - - the password provided by the user's agent
Returns:
- if credentials are accepted, some object to attach to the Auth object. otherwise null

authorise

public boolean authorise(Request request,
                         Request.Method method,
                         Auth auth)
Description copied from interface: Resource
Return true if the current user is permitted to access this resource using the specified method.

Note that the current user may be determined by the Auth associated with the request, or by a separate, application specific, login mechanism such as a session variable or cookie based system. This method should correctly interpret all such mechanisms

The auth given as a parameter will be null if authentication failed. The auth associated with the request will still exist

Specified by:
authorise in interface Resource

getRealm

public java.lang.String getRealm()
Description copied from interface: Resource
Return the security realm for this resource. Just any string identifier.

This will be used to construct authorization challenges and will be used on Digest authentication to construct the expected response.

Specified by:
getRealm in interface Resource

getModifiedDate

public java.util.Date getModifiedDate()
Description copied from interface: Resource
The date and time that this resource, or any part of this resource, was last modified. For dynamic rendered resources this should consider everything which will influence its output.

Resources for which no such date can be calculated should return null.

This field, if not null, is used to reply to conditional GETs (ie GET with if-modified-since). If the modified-since argument is later then the modified date then we return a 304 - Not Modified.

Although nulls are explicitly allowed by milton, certain client applications might require modified dates for file browsing. For example, the command line client on Vista doesn't work properly if this is null.

Specified by:
getModifiedDate in interface Resource

checkRedirect

public java.lang.String checkRedirect(Request request)
Description copied from interface: Resource
Determine if a redirect is required for this request, and if so return the URL to redirect to. May be absolute or relative.

Called after authorization check but before any method specific processing

Return null for no redirect

Specified by:
checkRedirect in interface Resource

processForm

public java.lang.String processForm(java.util.Map<java.lang.String,java.lang.String> parameters,
                                    java.util.Map<java.lang.String,FileItem> files)
                             throws BadRequestException,
                                    NotAuthorizedException,
                                    ConflictException
Specified by:
processForm in interface PostableResource
Throws:
BadRequestException
NotAuthorizedException
ConflictException


Copyright © 2011. All Rights Reserved.