0

I have a problem when trying to use the org.restlet.Server Component to serve static content when using the Restlet Framework.

When I ran the following code, I get: HTTP ERROR: 404

Problem accessing /. Reason:

Not Found

Restlet throws the following exception: Unable to start the Restlet

java.lang.IllegalArgumentException: Relative references are only usable when a base reference is set. at org.restlet.data.Reference.getTargetRef(Reference.java:1954) at org.restlet.resource.Directory.(Directory.java:125)

My code is as below. I am using Restlet 2.3.9 JSE on Java 1.8JDK, Linux 64

public static final String ROOT_URI = "/home/user/templates/"; 
public class myApplication extends Application{

    Server app = new Server (Protocol.HTTP, 8110);
    app.getProtocols().add(Protocol.FILE);

    app.setNext(new myApplication());
    app.start();
}

@Override
 public Restlet createInboundRoot(){
      Router router = new Router(getContext());
      System.out.println("Inbound");

    Directory directory = new Directory(getContext(), ROOT_URI);
    directory.setListingAllowed(true);
    directory.setDeeplyAccessible(true);        
    router.attach("/",directory);       
    return router;
   }
}
NgigiW
  • 1
  • 1
  • Try to change `ROOT_URI` to `"file:///home/user/templates/"`. – putu Jun 30 '17 at 06:51
  • Hi, that by itself now shows " Not Found The server has not found anything matching the request URI" on the browser but it does not throw any errors at the Server. If I however change router.attach("/", directory) to router.attach(serverURL+"/", directory) I get the following error on the browser... "Internal Server Error No client dispatcher is available." On the server, exception is: "No client dispatcher is available. Can't get the target URI: null org.restlet.resource.ResourceException: Internal Server Error (500) - No client dispatcher is available." – NgigiW Jun 30 '17 at 16:49
  • How if you change it to `"file:///home/user/templates"` (without slash)? Does the directory `/home/user/templates` exist? Is it accessible from the application you've built, i.e. does the application has enough permission to access the directory? How did you access the server from the browser? Take a look at [Restlet User Guide](https://restlet.com/open-source/documentation/user-guide/2.3/core/routing/static-files). – putu Jul 01 '17 at 06:03
  • Hi, Yes, the directory exists and works externally. After hitting a brickwall on this, I used Component instead of Server and voila, everything works. I think Restlet Documentation should be clear on what to use, when and how between those two components. – NgigiW Jul 03 '17 at 06:48

0 Answers0