Advertisement
tpeierls

Accepting forms in Restlet

Aug 8th, 2011
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.26 KB | None | 0 0
  1. package net.peierls.example.restlet.formpost;
  2.  
  3. import org.restlet.*;
  4. import org.restlet.data.*;
  5. import org.restlet.resource.*;
  6. import org.restlet.routing.*;
  7.  
  8.  
  9. /**
  10.  * Demonstrates automatic conversion of entity to Form.
  11.  * Test with something like:
  12.  * <pre>
  13.  *   curl -v --data param=value1 --data otherparam=value2 localhost:8182/myresource
  14.  * </pre>
  15.  */
  16. public class Main {
  17.  
  18.     public static void main(String... args) throws Exception {
  19.         Component comp = new Component();
  20.         App app = new App();
  21.         comp.getServers().add(Protocol.HTTP, 8182);
  22.         comp.getDefaultHost().attachDefault(app);
  23.         comp.start();
  24.     }
  25.  
  26.     public static class App extends Application {
  27.         @Override public Restlet createInboundRoot() {
  28.             Router router = new Router(getContext());
  29.             router.attach("/myresource", MyResource.class);
  30.             return router;
  31.         }
  32.     }
  33.  
  34.     public static class MyResource extends ServerResource {
  35.         @Post public String doPost(Form form) {
  36.             return String.format(
  37.                 "received POST with param=%s and otherparam=%s",
  38.                 form.getFirstValue("param", ""),
  39.                 form.getFirstValue("otherparam", "")
  40.             );
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement