Hi i try to implement in a tomcat container the

=> How can I make PUT or DELETE calls from browsers? i read the faq/mailing
list but there is not a lot informations about that.

So my goal it is pretty simple, a html page with a <form> which contain a
<input type="hidden"> with the name="method" and value="put" (or "delete"),
and call the associate method in a resource ( handlePut() or handleDelete()
).

My problem it is i never go to the handlePut() ! But always to the
handlePost().
When i debug the application in the request object, in the method attribute
it is always POST !!!


So here my code :

XHTML Page:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
    <head>
        <title>form.xhtml</title>
        <meta http-equiv="keywords" content="enter,your,keywords,here" />
        <meta http-equiv="description" content="A short description of this
page." />
        <meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1" />

        <base href="http://localhost:8080/restlet/"/>
    </head>
    <body>
        <form action="api/form" method="post" enctype="text/plain">
            <input type="text" name="text" />

            <input  type="hidden" name="method" value="PUT"/>
            <input  type="submit" name="ok" value="ok"/>
        </form>

    </body>
</html>


web.xml

<web-app>

  <!-- Your application class name -->
  <context-param>
     <param-name>org.restlet.application</param-name>
     <param-value>com.webmd.restlet.app.RestApplication</param-value>
  </context-param>

  <servlet>
     <servlet-name>RestServlet</servlet-name>
     
<servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class>

  </servlet>

  <servlet-mapping>
     <servlet-name>RestServlet</servlet-name>
     <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

</web-app>


com/webmd/restlet/app/RestApplication.java


public class RestApplication extends Application {

    public RestApplication(Context context) {
        //enable the tunnel service
        TunnelService tunnelService = new TunnelService(true, true, true);
        tunnelService.setMethodParameter("method");
        this.setTunnelService(tunnelService);

    }

    @Override
    public Restlet createRoot() {
        Router router = new Router(getContext());
        router.attach(/form, FormResource.getClass());

        TunnelFilter tunnelFilter = new TunnelFilter(this);
        tunnelFilter.setContext(getContext());
        tunnelFilter.setNext(router);

        return tunnelFilter;
    }

    public void handle(Request request, Response response) {
        try {
            start();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        super.handle(request, response);
    }

}

com/webmd/restlet/rest/FormResource .java;

public class FormResource extends Resource {


    @Override
    public void init(Context context, Request request, Response response) {

        System.out.println(" init()");
        Form form = request.getEntityAsForm();

        String value = form.getFirstValue("text");
        String method = form.getFirstValue("method");

        super.init(context, request, response);
    }

@Override
    public boolean allowDelete() {
        return true;
    }

    @Override
    public boolean allowGet() {
        return true;
    }


    @Override
    public boolean allowPost() {
        return true;
    }


    @Override
    public boolean allowPut() {
        return true;
    }


    @Override
    public void handleDelete() {
        System.out.println(" handleDelete()");
        super.handleDelete();
    }


    @Override
    public void handleGet() {
        System.out.println(" handleGet()");
        super.handleGet();
    }


    @Override
    public void handlePost() {
        System.out.println(" handlePost()");
        super.handlePost();
    }


    @Override
    public void handlePut() {
        System.out.println(" handlePut()");
        super.handlePut();
    }

    @Override
    public Representation getRepresentation(Variant arg0) {
        System.out.println("=> getRepresentation()");
        return super.getRepresentation(arg0);
    }


    @Override
    public void delete() {
        System.out.println(" delete()");
        super.delete();
    }



    @Override
    public void post(Representation arg0) {
        System.out.println(" post()");
        super.post(arg0);
    }



    @Override
    public void put(Representation arg0) {
        System.out.println(" put()");
        super.put(arg0);
    }

}

Thanks for the reply

Reply via email to