public final class RedirectService extends AbstractHttpService
HttpService that sends a redirect response such as "307 Temporary Redirect".
You have to specify a template or a Function that generates the value of the "Location"
header.
You can choose one of the following template styles where the path parameters are substituted with
the values retrieved from ServiceRequestContext.pathParam(String):
/new (no path parameters)
ServerBuilder sb = Server.builder();
// e.g. /old -> /new
sb.service("/old", new RedirectService("/new");
/new/{var} (curly-brace style parameters)
// e.g. /old/foo -> /new/foo
sb.service("/old/{var}", new RedirectService("/new/{var}");
/new/:var1/:var2 (colon style parameters)
// e.g. /old/foo/bar -> /new/foo/bar
sb.service("/old/:var1/:var2", new RedirectService("/new/:var1/:var2"));
http://host/new (full URL without path parameters)
// e.g. /old -> http://host/new
sb.service("/old", new RedirectService("http://host/new"));
http://host/new/{var} (full URL with curly-brace style parameters)
// e.g. /old/foo -> http://host/new/foo
sb.service("/old/{var}", new RedirectService("http://host/new/{var}"));
http://host/new/:var1/:var2 (full URL with colon style parameters)
// e.g. /old/foo/bar -> http://host/new/foo/bar
sb.service("/old/:var1/:var2", new RedirectService("http://host/new/:var1/:var2"));
You can also specify a custom function to generate a location which cannot be generated with a location template:
ServerBuilder sb = Server.builder();
// e.g. /foo/bar -> /NNNNNN/foo_bar
sb.service("/:var1/:var2", new RedirectService(ctx -> {
String name = ctx.pathParam("var1") + "_" + ctx.pathParam("var2");
return String.format("/%d/%s", name.hashCode(), name);
});
By default, RedirectService responds with 307 Temporary
Redirect status. You can specify alternative status such as 301 Moved
Permanently when calling the constructor.
By default, RedirectService preserves the query string in the request URI when generating
a new location. For example, if you bound new RedirectService("/new") at "/old",
a request to "/old?foo=bar" will be redirected to "/new?foo=bar". You can disable
this behavior by specifying false for the preserveQueryString parameter
when constructing the service.
Note that RedirectService will not append the query string if the generated location already
contains a query string, regardless of the preserveQueryString parameter value. For example,
the following location function never preserves the original query string:
ServiceBuilder sb = Server.builder();
// /old?foo=bar -> /new?redirected=1 (?foo=bar is ignored.)
sb.service("/old", new RedirectService("/new?redirected=1"));
| Constructor and Description |
|---|
RedirectService(Function<? super ServiceRequestContext,String> locationFunction)
Creates a new instance that redirects to the location returned by
locationFunction,
preserving the query string in the request URI. |
RedirectService(Function<? super ServiceRequestContext,String> locationFunction,
boolean preserveQueryString)
Creates a new instance that redirects to the location returned by
locationFunction. |
RedirectService(HttpStatus redirectStatus,
Function<? super ServiceRequestContext,String> locationFunction)
Creates a new instance that redirects to the location returned by
locationFunction,
preserving the query string in the request URI. |
RedirectService(HttpStatus redirectStatus,
Function<? super ServiceRequestContext,String> locationFunction,
boolean preserveQueryString)
Creates a new instance that redirects to the location returned by
locationFunction. |
RedirectService(HttpStatus redirectStatus,
String locationPattern)
Creates a new instance that redirects to the location constructed with the specified
locationPattern, preserving the query string in the request URI. |
RedirectService(HttpStatus redirectStatus,
String locationPattern,
boolean preserveQueryString)
Creates a new instance that redirects to the location constructed with the specified
locationPattern. |
RedirectService(String locationPattern)
Creates a new instance that redirects to the location constructed with the specified
locationPattern, preserving the query string in the request URI. |
RedirectService(String locationPattern,
boolean preserveQueryString)
Creates a new instance that redirects to the location constructed with the specified
locationPattern. |
| Modifier and Type | Method and Description |
|---|---|
HttpResponse |
serve(ServiceRequestContext ctx,
HttpRequest req)
NB: For now we redirect all methods.
|
void |
serviceAdded(ServiceConfig cfg)
Invoked when this service has been added to a
Server with the specified
configuration. |
doDelete, doDelete, doGet, doGet, doHead, doHead, doOptions, doOptions, doPatch, doPatch, doPost, doPost, doPut, doPut, doTrace, doTraceclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdecorate, decorateas, decorate, shouldCachePathpublic RedirectService(String locationPattern)
locationPattern, preserving the query string in the request URI.locationPattern - the location pattern that is used to generate a redirect location.IllegalArgumentException - if the specified locationPattern is unsupported or invalidpublic RedirectService(String locationPattern, boolean preserveQueryString)
locationPattern.locationPattern - the location pattern that is used to generate a redirect location.preserveQueryString - whether to preserve the query string in the generated redirect location.IllegalArgumentException - if the specified locationPattern is unsupported or invalidpublic RedirectService(Function<? super ServiceRequestContext,String> locationFunction)
locationFunction,
preserving the query string in the request URI.locationFunction - a Function that takes a ServiceRequestContext
and returns a new location.public RedirectService(Function<? super ServiceRequestContext,String> locationFunction, boolean preserveQueryString)
locationFunction.locationFunction - a Function that takes a ServiceRequestContext
and returns a new location.preserveQueryString - whether to preserve the query string in the generated redirect location.public RedirectService(HttpStatus redirectStatus, String locationPattern)
locationPattern, preserving the query string in the request URI.redirectStatus - the HttpStatus that the HttpService will return.locationPattern - the location pattern that is used to generate a redirect location.IllegalArgumentException - if the specified locationPattern is unsupported or invalidpublic RedirectService(HttpStatus redirectStatus, String locationPattern, boolean preserveQueryString)
locationPattern.redirectStatus - the HttpStatus that the HttpService will return.locationPattern - the location pattern that is used to generate a redirect location.preserveQueryString - whether to preserve the query string in the generated redirect location.IllegalArgumentException - if the specified locationPattern is unsupported or invalidpublic RedirectService(HttpStatus redirectStatus, Function<? super ServiceRequestContext,String> locationFunction)
locationFunction,
preserving the query string in the request URI.redirectStatus - the HttpStatus that the HttpService will return.locationFunction - a Function that takes a ServiceRequestContext
and returns a new location.public RedirectService(HttpStatus redirectStatus, Function<? super ServiceRequestContext,String> locationFunction, boolean preserveQueryString)
locationFunction.redirectStatus - the HttpStatus that the HttpService will return.locationFunction - a Function that takes a ServiceRequestContext
and returns a new location.preserveQueryString - whether to preserve the query string in the generated redirect location.public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception
serve in interface HttpServiceserve in interface Service<HttpRequest,HttpResponse>serve in class AbstractHttpServicectx - the context of the received Requestreq - the received RequestResponseExceptionpublic void serviceAdded(ServiceConfig cfg) throws Exception
ServiceServer with the specified
configuration. Please note that this method can be invoked more than once if this service
has been added more than once.ExceptionCopyright © 2020 LeanCloud. All rights reserved.