Package org.restlet.routing
Class Redirector
java.lang.Object
org.restlet.Restlet
org.restlet.routing.Redirector
- All Implemented Interfaces:
Uniform
Rewrites URIs then redirects the call or the client to a new destination.
There are various redirection modes that you can choose from: client-side
redirections (
When setting the redirection URIs, you can also used special URI variables to reuse most properties from the original request as well as URI template variables. For a complete list of properties, please see the
To create a reverse proxy, a typically configuration will use the
Concurrency note: instances of this class or its subclasses can be invoked by several threads at the same time and therefore must be thread-safe. You should be especially careful when storing state in member variables.
MODE_CLIENT_FOUND, MODE_CLIENT_PERMANENT,
MODE_CLIENT_SEE_OTHER, MODE_CLIENT_TEMPORARY) or
server-side redirections, similar to a reverse proxy (
MODE_SERVER_OUTBOUND and MODE_SERVER_INBOUND).When setting the redirection URIs, you can also used special URI variables to reuse most properties from the original request as well as URI template variables. For a complete list of properties, please see the
Resolver
class. For example "/target?referer={fi}" would redirect to the relative URI,
inserting the referrer URI as a query parameter.To create a reverse proxy, a typically configuration will use the
MODE_SERVER_OUTBOUND constant and a target URI like
"http://targetHost/targetRootPath/{rr}" to ensure that all child URIs are
properly redirected as well, "rr" appending the remaining part of the current
request URI that hasn't been routed yet.Concurrency note: instances of this class or its subclasses can be invoked by several threads at the same time and therefore must be thread-safe. You should be especially careful when storing state in member variables.
- Author:
- Jerome Louvel
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanIndicates if all headers of HTTP requests stored in the request's attributes, must be removed before the redirection.protected intThe redirection mode.static final intIn this mode, the client is simply redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_FOUNDstatus.static final intIn this mode, the client is permanently redirected to the URI generated from the target URI pattern, using theStatus.REDIRECTION_PERMANENTstatus.static final intIn this mode, the client is simply redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_SEE_OTHERstatus.static final intIn this mode, the client is temporarily redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_TEMPORARYstatus.static final intIn this mode, the call is sent toContext.getServerDispatcher().static final intIn this mode, the call is sent toApplication.getOutboundRoot()or if null toContext.getClientDispatcher().protected StringThe target URI pattern. -
Constructor Summary
ConstructorsConstructorDescriptionRedirector(Context context, String targetTemplate) Constructor for the client dispatcher mode.Redirector(Context context, String targetPattern, int mode) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionintgetMode()Returns the redirection mode.protected ReferencegetTargetRef(Request request, Response response) Returns the target reference to redirect to by automatically resolving URI template variables found using theTemplateclass using the request and response as data models.Returns the target URI pattern.voidHandles a call by redirecting using the selected redirection mode.protected voidinboundServerRedirect(Reference targetRef, Request request, Response response) Redirects a given call to a target reference.booleanIndicates if the headers must be cleaned.protected voidoutboundServerRedirect(Reference targetRef, Request request, Response response) Redirects a given call to a target reference.protected Representationrewrite(Representation initialEntity) Optionally rewrites the response entity returned in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes.protected voidOptionally updates the request sent in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes.protected voidOptionally updates the response sent in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes.voidrewriteLocation(Request request, Response response) Rewrite the location of the response, and the Location of the entity, if any.protected voidserverRedirect(Restlet next, Reference targetRef, Request request, Response response) Redirects a given call on the server-side to a next Restlet with a given target reference.voidsetHeadersCleaning(boolean headersCleaning) Indicates if the headers must be cleaned.voidsetMode(int mode) Sets the redirection mode.voidsetTargetTemplate(String targetTemplate) Sets the target URI pattern.Methods inherited from class org.restlet.Restlet
createFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwner, start, stop
-
Field Details
-
MODE_CLIENT_FOUND
public static final int MODE_CLIENT_FOUNDIn this mode, the client is simply redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_FOUNDstatus. Note: this is a client-side redirection. -
MODE_CLIENT_PERMANENT
public static final int MODE_CLIENT_PERMANENTIn this mode, the client is permanently redirected to the URI generated from the target URI pattern, using theStatus.REDIRECTION_PERMANENTstatus. Note: this is a client-side redirection. -
MODE_CLIENT_SEE_OTHER
public static final int MODE_CLIENT_SEE_OTHERIn this mode, the client is simply redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_SEE_OTHERstatus. Note: this is a client-side redirection. -
MODE_CLIENT_TEMPORARY
public static final int MODE_CLIENT_TEMPORARYIn this mode, the client is temporarily redirected to the URI generated from the target URI pattern using theStatus.REDIRECTION_TEMPORARYstatus. Note: this is a client-side redirection. -
MODE_SERVER_INBOUND
public static final int MODE_SERVER_INBOUNDIn this mode, the call is sent toContext.getServerDispatcher(). Once the selected client connector has completed the request handling, the response is normally returned to the client. In this case, you can view the Redirector as acting as a transparent proxy Restlet. Note: this is a server-side redirection.
Warning: remember to add the required connectors to the parentComponentand to declare them in the list of required connectors on theApplication.getConnectorService()property.
Note that in this mode, the headers of HTTP requests, stored in the request's attributes, are removed before dispatching. Also, when a HTTP response comes back the headers are also removed. You can control this behavior by setting theheadersCleaningattribute or by overriding therewrite(Request)orrewrite(Response). -
MODE_SERVER_OUTBOUND
public static final int MODE_SERVER_OUTBOUNDIn this mode, the call is sent toApplication.getOutboundRoot()or if null toContext.getClientDispatcher(). Once the selected client connector has completed the request handling, the response is normally returned to the client. In this case, you can view theRedirectoras acting as a transparent server-side proxy. Note: this is a server-side redirection.
Warning: remember to add the required connectors to the parentComponentand to declare them in the list of required connectors on theApplication.getConnectorService()property.
Note that in this mode, the headers of HTTP requests, stored in the request's attributes, are removed before dispatching. Also, when a HTTP response comes back the headers are also removed. You can control this behavior by setting theheadersCleaningattribute or by overriding therewrite(Request)orrewrite(Response). -
headersCleaning
protected volatile boolean headersCleaningIndicates if all headers of HTTP requests stored in the request's attributes, must be removed before the redirection. If set to true, it removes all headers, otherwise it keeps only the extension (or non HTTP standard) headers -
mode
protected volatile int modeThe redirection mode. -
targetTemplate
The target URI pattern.
-
-
Constructor Details
-
Redirector
Constructor for the client dispatcher mode.- Parameters:
context- The context.targetTemplate- The template to build the target URI.- See Also:
-
Redirector
Constructor.- Parameters:
context- The context.targetPattern- The pattern to build the target URI (using StringTemplate syntax and the CallModel for variables).mode- The redirection mode.
-
-
Method Details
-
getMode
public int getMode()Returns the redirection mode.- Returns:
- The redirection mode.
-
getTargetRef
Returns the target reference to redirect to by automatically resolving URI template variables found using theTemplateclass using the request and response as data models.- Parameters:
request- The request to handle.response- The response to update.- Returns:
- The target reference to redirect to.
-
getTargetTemplate
Returns the target URI pattern.- Returns:
- The target URI pattern.
-
handle
Handles a call by redirecting using the selected redirection mode. -
inboundServerRedirect
Redirects a given call to a target reference. In the default implementation, the request HTTP headers, stored in the request's attributes, are removed before dispatching. After dispatching, the response HTTP headers are also removed to prevent conflicts with the main call.- Parameters:
targetRef- The target reference with URI variables resolved.request- The request to handle.response- The response to update.
-
isHeadersCleaning
public boolean isHeadersCleaning()Indicates if the headers must be cleaned.- Returns:
- True if the headers must be cleaned.
-
outboundServerRedirect
Redirects a given call to a target reference. In the default implementation, the request HTTP headers, stored in the request's attributes, are removed before dispatching. After dispatching, the response HTTP headers are also removed to prevent conflicts with the main call.- Parameters:
targetRef- The target reference with URI variables resolved.request- The request to handle.response- The response to update.
-
rewrite
Optionally rewrites the response entity returned in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes. By default, it just returns the initial entity without any modification.- Parameters:
initialEntity- The initial entity returned.- Returns:
- The rewritten entity.
-
rewrite
Optionally updates the request sent in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes. By default, it leverages theheadersCleaningattribute in order to clean the headers: if set to true, it removes all headers, otherwise it keeps only the extension (or non HTTP standard) headers- Parameters:
initialRequest- The initial request returned.
-
rewrite
Optionally updates the response sent in theMODE_SERVER_INBOUNDandMODE_SERVER_OUTBOUNDmodes. By default, it leverages theheadersCleaningattribute in order to clean the headers: if set to true, it removes all headers, otherwise it keeps only the extension (or non HTTP standard) headers- Parameters:
initialResponse- The initial response returned.
-
rewriteLocation
Rewrite the location of the response, and the Location of the entity, if any.- Parameters:
request- The request to handle.response- The response to update.
-
serverRedirect
protected void serverRedirect(Restlet next, Reference targetRef, Request request, Response response) Redirects a given call on the server-side to a next Restlet with a given target reference. In the default implementation, the request HTTP headers, stored in the request's attributes, are removed before dispatching. After dispatching, the response HTTP headers are also removed to prevent conflicts with the main call.- Parameters:
next- The next Restlet to forward the call to.targetRef- The target reference with URI variables resolved.request- The request to handle.response- The response to update.
-
setHeadersCleaning
public void setHeadersCleaning(boolean headersCleaning) Indicates if the headers must be cleaned.- Parameters:
headersCleaning- True if the headers must be cleaned.
-
setMode
public void setMode(int mode) Sets the redirection mode.- Parameters:
mode- The redirection mode.
-
setTargetTemplate
Sets the target URI pattern.- Parameters:
targetTemplate- The target URI pattern.
-