Class RedirectService

java.lang.Object
com.linecorp.armeria.server.RedirectService
All Implemented Interfaces:
Unwrappable, HttpService, Service<HttpRequest,HttpResponse>

public final class RedirectService extends Object implements HttpService
An 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 Details

    • RedirectService

      public 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.
      Parameters:
      locationPattern - the location pattern that is used to generate a redirect location.
      Throws:
      IllegalArgumentException - if the specified locationPattern is unsupported or invalid
    • RedirectService

      public RedirectService(String locationPattern, boolean preserveQueryString)
      Creates a new instance that redirects to the location constructed with the specified locationPattern.
      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 specified locationPattern is unsupported or invalid
    • RedirectService

      public 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.
      Parameters:
      locationFunction - a Function that takes a ServiceRequestContext 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 by locationFunction.
      Parameters:
      locationFunction - a Function that takes a ServiceRequestContext and returns a new location.
      preserveQueryString - whether to preserve the query string in the generated redirect location.
    • RedirectService

      public 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.
      Parameters:
      redirectStatus - the HttpStatus that the HttpService will return.
      locationPattern - the location pattern that is used to generate a redirect location.
      Throws:
      IllegalArgumentException - if the specified locationPattern 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 specified locationPattern.
      Parameters:
      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.
      Throws:
      IllegalArgumentException - if the specified locationPattern is unsupported or invalid
    • RedirectService

      public 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.
      Parameters:
      redirectStatus - the HttpStatus that the HttpService will return.
      locationFunction - a Function that takes a ServiceRequestContext 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 by locationFunction.
      Parameters:
      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.
  • Method Details