Package org.takes.facets.forward

Forward.

See: Description

Package org.takes.facets.forward Description

Forward.

Classes in this package help you to automate forwarding operations, through 30x HTTP responses. For example, when a web form is submitted through POST request, it is a good practice to return a 303 response which will instruct the browser to change the web page immediately. This technique is used mostly in order to prevent duplicate form submissions by hitting "Refresh" button in the browser. This is how you implement it in your "take":

 public class TkSaveFile implements Take {
   @Override
   public Response act(final Request req) {
     final InputStream content =
       new RqMultipart.Base(req).part("file").body();
     // save content to whenever you want
     return new RsForward(new RqHref(req).href());
   }
 }

When the file is saved, this "take" will return an instance of RsForward, which will contain the URI to be placed into Location header. The browser will use this URI as a destination point of the next page to render.

Sometimes it is convenient to throw an exception instead of returning a response, especially in input checking situations, for example:

 public class TkLoadFile implements Take {
   @Override
   public Response act(final Request req) {
     final Iterable<String> param =
       new RqHref(req).href().param("name");
     if (!param.iterator().hasNext()) {
       throw RsForward(
         new RsFlash("query param NAME is mandatory"),
         HttpURLConnection.HTTP_SEE_OTHER,
         "/files"
       );
     }
     // continue normal operations
   }
 }

This example will work only if you wrap your entire "take" into TkForward decorator:

Take take = new TkForward(take);

This TkForward decorator will catch all exceptions of type RsForward and convert them to responses.

Since:
0.1
Version:
$Id$
Author:
Yegor Bugayenko ([email protected])

Copyright © 2015 Take. All rights reserved.