My application attempts to set context attributes:
final Router router = new Router();
router.attachDefault(HttpListener.class);
org.restlet.Application myApp = new org.restlet.Application() {
@Override
public org.restlet.Restlet createInboundRoot() {
getContext().getAttributes().put("mysharedobj", new MySharedObj());
return router;
};
};
Component component = new Component();
component.getDefaultHost().attach("/", myApp);
new Server(Protocol.HTTP, port, component).start();
In my HttpListener I assert the context is not null:
public class HttpListener extends ServerResource {
public MySharedObj mysharedobj;
public HttpListener() { }
@java.lang.Override
public void init(Context context, Request request, Response response) {
assert context != null; // throws java.lang.AssertionError
// my end goal is to pass a shared object to my listener
mysharedobj = context.getAttributes().get("mysharedobj");
}
...
}
However, a java.lang.AssertionError is thrown because the context is null. My end goal is to pass a shared object to my listener. Is there another way I can do this?
Where am I going wrong? Note: I'm using Restlet 2.1.7. My application always run from an android app, so no server context is available.
Update:
I've also tried using the Application Context:
final Router router = new Router();
router.attachDefault(HttpListener.class);
Component component = new Component();
final Context ctx = component.getApplication().getContext().createChildContext();
ctx.getAttributes().put("mysharedobj", new MySharedObj());
org.restlet.Application myApp = new org.restlet.Application(ctx) {
@Override
public org.restlet.Restlet createInboundRoot() {
return router;
};
};
And ..
public HttpListener() {
Context ctx = getApplication().getContext();
assert ctx.getAttributes().size() > 0; // Throws AssertionError
...
}
In this approach, I am able to access the Application context, but the attributes are not set on it for some reason.
Cannot refer to the non-final local variable router defined in an enclosing scope
at the linereturn router;
– Diarmuid