org.restlet
Class Guard

java.lang.Object
  extended by org.restlet.Uniform
      extended by org.restlet.Restlet
          extended by org.restlet.Filter
              extended by org.restlet.Guard

public class Guard
extends Filter

Filter guarding the access to an attached Restlet.

Author:
Jerome Louvel (contact@noelios.com)
See Also:
Tutorial: Guarding access to sensitive resources

Constructor Summary
Guard(Context context, ChallengeScheme scheme, String realm)
          Constructor.
 
Method Summary
 void accept(Request request, Response response)
          Accepts the call.
 int authenticate(Request request)
          Indicates if the call is properly authenticated.
 boolean authorize(Request request)
          Indicates if the request is authorized to pass through the Guard.
 void challenge(Response response)
          Challenges the client by adding a challenge request to the response and by setting the status to CLIENT_ERROR_UNAUTHORIZED.
protected  boolean checkSecret(String identifier, char[] secret)
          Indicates if the secret is valid for the given identifier.
 void doHandle(Request request, Response response)
          Handles the call by distributing it to the next Restlet.
protected  char[] findSecret(String identifier)
          Finds the secret associated to a given identifier.
 void forbid(Response response)
          Rejects the call due to a failed authentication or authorization.
 Map<String,char[]> getSecrets()
          Returns the map of identifiers and secrets.
 
Methods inherited from class org.restlet.Filter
afterHandle, beforeHandle, getNext, handle, hasNext, setNext, setNext
 
Methods inherited from class org.restlet.Restlet
getContext, getLogger, init, isStarted, isStopped, setContext, start, stop
 
Methods inherited from class org.restlet.Uniform
delete, delete, get, get, handle, head, head, options, options, post, post, put, put
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Guard

public Guard(Context context,
             ChallengeScheme scheme,
             String realm)
Constructor.

Parameters:
context - The context.
scheme - The authentication scheme to use.
realm - The authentication realm.
Method Detail

accept

public void accept(Request request,
                   Response response)
Accepts the call. By default, it is invoked it the request is authenticated and authorized, and asks the attached Restlet to handle the call.

Parameters:
request - The request to accept.
response - The response to accept.

authenticate

public int authenticate(Request request)
Indicates if the call is properly authenticated. By default, this delegates credential checking to authenticate().

Parameters:
request - The request to authenticate.
Returns:
-1 if the given credentials were invalid, 0 if no credentials were found and 1 otherwise.
See Also:
checkSecret(String, char[])

checkSecret

protected boolean checkSecret(String identifier,
                              char[] secret)
Indicates if the secret is valid for the given identifier. By default, this returns true given the correct login/password couple as verified via the findSecret() method.

Parameters:
identifier - the identifier
secret - the identifier's secret
Returns:
true if the secret is valid for the given identifier

authorize

public boolean authorize(Request request)
Indicates if the request is authorized to pass through the Guard. This method is only called if the call was sucessfully authenticated. It always returns true by default. If specific checks are required, they could be added by overriding this method.

Parameters:
request - The request to authorize.
Returns:
True if the request is authorized.

challenge

public void challenge(Response response)
Challenges the client by adding a challenge request to the response and by setting the status to CLIENT_ERROR_UNAUTHORIZED.

Parameters:
response - The response to update.

doHandle

public void doHandle(Request request,
                     Response response)
Handles the call by distributing it to the next Restlet.

Overrides:
doHandle in class Filter
Parameters:
request - The request to handle.
response - The response to update.

findSecret

protected char[] findSecret(String identifier)
Finds the secret associated to a given identifier. By default it looks up into the secrets map, but this behavior can be overriden.

Parameters:
identifier - The identifier to lookup.
Returns:
The secret associated to the identifier or null.

forbid

public void forbid(Response response)
Rejects the call due to a failed authentication or authorization. This can be overriden to change the defaut behavior, for example to display an error page. By default, if authentication is required, the challenge method is invoked, otherwise the call status is set to CLIENT_ERROR_FORBIDDEN.

Parameters:
response - The reject response.

getSecrets

public Map<String,char[]> getSecrets()
Returns the map of identifiers and secrets.

Returns:
The map of identifiers and secrets.


Copyright © 2005-2007 Noelios Consulting.