Hello Roger,

If I understand well, you want to make some unit tests of the framework. Your code seems ok, but we would like you to use the Application class which is at a higher level than Router but at the same time is the base of all developments with the framework.
A sample code class with your code is attached to this mail.

Best reards,
Thierry Boileau


Hi,

At the moment i would like to use restlet without the transport 'attached'. I think I'm not using the correct terminology. An example follows which maybe makes it clearer ...

So, actually, everything works fine, and it can do what I want it to do on the basis of this simple test.

My question is, am I missing something in the framework here ... ?

regards,

Roger



public class XXX2
{

 public static void main(String[] args) throws Exception
 {
   Router router = new Router();
   router.attach("/xxx/{x}", XXX4.class);

   Request req = new Request(Method.GET, "/xxx/fv");
   Response res = router.handle(req);

   System.out.println(res.getEntity().getText());
 }


 public static class XXX4 extends Resource
 {

   public XXX4(Context context, Request req, Response res)
   {
     super(context, req, res);
     getVariants().add(new Variant(MediaType.TEXT_PLAIN));
         System.out.println( req.getAttributes().get("x") );
   }
     @Override
   public Representation getRepresentation(Variant variant)
   {
     return new StringRepresentation("hello");
   }
 }

}



import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

public class MyTest {
    public static void main(String[] args) throws Exception {

        // Create an application
        Application application = new Application() {
            @Override
            public Restlet createRoot() {
                Router router = new Router();
                router.attach("/xxx/{x}", MyResource.class);
                return router;
            }
        };

        Request req = new Request(Method.GET, "/xxx/fv");
        Response res = application.handle(req);

        System.out.println(res.getEntity().getText());
    }

    public static class MyResource extends Resource {

        public MyResource(Context context, Request req, Response res) {
            super(context, req, res);
            getVariants().add(new Variant(MediaType.TEXT_PLAIN));
            System.out.println(req.getAttributes().get("x"));
        }

        @Override
        public Representation getRepresentation(Variant variant) {
            return new StringRepresentation("hello");
        }
    }

}

Reply via email to