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
ConstructorsConstructorDescriptionRedirectService(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.voidInvoked when this service has been added to aServerwith the specified configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.linecorp.armeria.server.HttpService
decorate, decorate, exchangeTypeMethods inherited from interface com.linecorp.armeria.server.Service
as, shouldCachePath, unwrapMethods 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 specifiedlocationPatternis 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 specifiedlocationPatternis 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- aFunctionthat takes aServiceRequestContextand 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- aFunctionthat takes aServiceRequestContextand 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- theHttpStatusthat theHttpServicewill return.locationPattern- the location pattern that is used to generate a redirect location.- Throws:
IllegalArgumentException- if the specifiedlocationPatternis 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- theHttpStatusthat theHttpServicewill 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 specifiedlocationPatternis 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- theHttpStatusthat theHttpServicewill return.locationFunction- aFunctionthat takes aServiceRequestContextand 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- theHttpStatusthat theHttpServicewill return.locationFunction- aFunctionthat takes aServiceRequestContextand 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:
servein interfaceHttpService- Specified by:
servein interfaceService<HttpRequest,HttpResponse> - Parameters:
ctx- the context of the receivedRequestreq- the receivedRequest- Returns:
- the
Response - Throws:
Exception
-
serviceAdded
Description copied from interface:ServiceInvoked when this service has been added to aServerwith 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:
serviceAddedin interfaceService<HttpRequest,HttpResponse> - Throws:
Exception
-