Class RedirectService
- All Implemented Interfaces:
Unwrappable
,HttpService
,Service<HttpRequest,
HttpResponse>
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.
Using a location template
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"));
Using a location function
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);
});
Specifying an alternative status code
By default, RedirectService
responds with 307 Temporary
Redirect
status. You can specify alternative status such as 301 Moved
Permanently
when calling the constructor.
Preserving a query string (or not)
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 Summary
ConstructorDescriptionRedirectService
(HttpStatus redirectStatus, String locationPattern) Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
, 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 specifiedlocationPattern
.RedirectService
(HttpStatus redirectStatus, Function<? super ServiceRequestContext, String> locationFunction) Creates a new instance that redirects to the location returned bylocationFunction
, 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 bylocationFunction
.RedirectService
(String locationPattern) Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
, 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 specifiedlocationPattern
.RedirectService
(Function<? super ServiceRequestContext, String> locationFunction) Creates a new instance that redirects to the location returned bylocationFunction
, 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 bylocationFunction
. -
Method Summary
Modifier and TypeMethodDescriptionserve
(ServiceRequestContext ctx, HttpRequest req) NB: For now we redirect all methods.void
Invoked when this service has been added to aServer
with the specified configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.server.HttpService
decorate, decorate, exchangeType
Methods inherited from interface com.linecorp.armeria.server.Service
as, shouldCachePath, unwrap
Methods inherited from interface com.linecorp.armeria.common.util.Unwrappable
equalsIgnoreWrapper, unwrapAll
-
Constructor Details
-
RedirectService
Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
, preserving the query string in the request URI.- Parameters:
locationPattern
- the location pattern that is used to generate a redirect location.- Throws:
IllegalArgumentException
- if the specifiedlocationPattern
is unsupported or invalid
-
RedirectService
Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
.- Parameters:
locationPattern
- the location pattern that is used to generate a redirect location.preserveQueryString
- whether to preserve the query string in the generated redirect location.- Throws:
IllegalArgumentException
- if the specifiedlocationPattern
is unsupported or invalid
-
RedirectService
Creates a new instance that redirects to the location returned bylocationFunction
, preserving the query string in the request URI.- Parameters:
locationFunction
- aFunction
that takes aServiceRequestContext
and returns a new location.
-
RedirectService
public RedirectService(Function<? super ServiceRequestContext, String> locationFunction, boolean preserveQueryString) Creates a new instance that redirects to the location returned bylocationFunction
.- Parameters:
locationFunction
- aFunction
that takes aServiceRequestContext
and returns a new location.preserveQueryString
- whether to preserve the query string in the generated redirect location.
-
RedirectService
Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
, preserving the query string in the request URI.- Parameters:
redirectStatus
- theHttpStatus
that theHttpService
will return.locationPattern
- the location pattern that is used to generate a redirect location.- Throws:
IllegalArgumentException
- if the specifiedlocationPattern
is unsupported or invalid
-
RedirectService
public RedirectService(HttpStatus redirectStatus, String locationPattern, boolean preserveQueryString) Creates a new instance that redirects to the location constructed with the specifiedlocationPattern
.- Parameters:
redirectStatus
- theHttpStatus
that theHttpService
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.- Throws:
IllegalArgumentException
- if the specifiedlocationPattern
is unsupported or invalid
-
RedirectService
public RedirectService(HttpStatus redirectStatus, Function<? super ServiceRequestContext, String> locationFunction) Creates a new instance that redirects to the location returned bylocationFunction
, preserving the query string in the request URI.- Parameters:
redirectStatus
- theHttpStatus
that theHttpService
will return.locationFunction
- aFunction
that takes aServiceRequestContext
and returns a new location.
-
RedirectService
public RedirectService(HttpStatus redirectStatus, Function<? super ServiceRequestContext, String> locationFunction, boolean preserveQueryString) Creates a new instance that redirects to the location returned bylocationFunction
.- Parameters:
redirectStatus
- theHttpStatus
that theHttpService
will return.locationFunction
- aFunction
that takes aServiceRequestContext
and returns a new location.preserveQueryString
- whether to preserve the query string in the generated redirect location.
-
-
Method Details
-
serve
NB: For now we redirect all methods.- Specified by:
serve
in interfaceHttpService
- Specified by:
serve
in interfaceService<HttpRequest,
HttpResponse> - Parameters:
ctx
- the context of the receivedRequest
req
- the receivedRequest
- Returns:
- the
Response
- Throws:
Exception
-
serviceAdded
Description copied from interface:Service
Invoked when this service has been added to aServer
with the specified configuration. Please note that this method can be invoked more than once if this service has been added more than once.- Specified by:
serviceAdded
in interfaceService<HttpRequest,
HttpResponse> - Throws:
Exception
-