Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restlet, CLAP, Ajax, and chunk timeouts

We're using RESTlet to do a small little REST server for a project we have. We set up a bunch of routes in a class inheriting from Application:

public static void createRestServer(ApplicationContext appCtx, String propertiesPath) throws Exception {    // Create a component   Component component = new Component();   component.getServers().add(Protocol.HTTP, 8081);   component.getClients().add(Protocol.FILE);   component.getClients().add(Protocol.CLAP);    Context context = component.getContext().createChildContext();   RestServer application = new RestServer(context);    application.getContext().getParameters().add("useForwardedForHeader", "true");    application.getContext().getAttributes().put("appCtx", appCtx);   application.getContext().getAttributes().put("file", propertiesPath);    // Attach the application to the component and start it   component.getDefaultHost().attach(application);   component.start(); }  private RestServer(Context context) {   super(context); }  public synchronized Restlet createInboundRoot() {   Router router = new Router(getContext());    // we then have a bunch of these   router.attach("/accounts/{accountId}", AccountFetcher.class); //LIST Account level   // blah blah blah    // finally some stuff for static files:   //   Directory directory = new Directory(getContext(),      LocalReference.createClapReference(LocalReference.CLAP_CLASS, "/"));   directory.setIndexName("index.html");   router.attach("/", directory);    return router; } 

The problem: If I request a .js file in the JAR via Ajax from a web page (also loaded via CLAP from this JAR), it'll only return the first 7737 bytes of that file and then hang. I can't get it to return the rest of the file. It always hangs after exactly the same number of bytes. 1 in 50 times it works.

Any ideas why it's hanging? Can I just turn off chunked encoding for CLAP and static files (all ours are quite small).

This is driving us nuts.

like image 435
MarcWan Avatar asked Jan 24 '14 18:01

MarcWan


1 Answers

I don't know which server connector you use for your application but it seems that it's the default one.

Restlet is pluggable and extensible at different levels. I recommend you to use the Jetty one. To do that simply add the JAR file for the Jetty extension (org.restlet.ext.jetty.jar) within your classpath. The connector will be automatically registered and use instead of the default one.

I also recommend you to upgrade to the latest version (2.3).

To see which connectors are registered in the Restlet engine, you can use the following code:

List<ConnectorHelper<Server>> serverConnectors        = Engine.getInstance().getRegisteredServers(); for (ConnectorHelper<Server> connectorHelper : serverConnectors) {     System.out.println("Server connector: "+connectorHelper); } 

You shouldn't have such problems after doing this.

Hope it helps you, Thierry

like image 69
Thierry Templier Avatar answered Sep 22 '22 08:09

Thierry Templier