I have in my app Restlet server and If I open the server address in the browser then the server should return list of files in some folder and If the user click on the file then it should download the file.
I looked at this tutorial. Part 6 Serving static file. It´s this code:
public static final String ROOT_URI = "file:///c:/restlet/docs/api/";
[...]
// Create a component
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getClients().add(Protocol.FILE);
// Create an application
Application application = new Application() {
@Override
public Restlet createInboundRoot() {
return new Directory(getContext(), ROOT_URI);
}
};
// Attach the application to the component and start it
component.getDefaultHost().attach(application);
component.start();
but if I use for example file:///sdcard/
in URI, then if I open the address in the browser I get
Not Found
The server has not found anything matching the request URI
You can get technical details here.
Please continue your visit at our home page.
What´s wrong? How can I return list of the files with Restlet?
My solution:
mWebServerComponent = new Component();
mWebServerComponent.getClients().add(Protocol.FILE);
mWebServerComponent.getServers().add(Protocol.HTTP, 8182);
mWebServerComponent.getDefaultHost().attach(new FileApplication());
try {
mWebServerComponent.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And the FileApplication class:
public class FileApplication extends Application {
// for example: public static final String ROOT_URI =
// "file:///C:/restlet-jee-2.0.6/docs/api/";
@Override
public synchronized Restlet createInboundRoot() {
String ROOT_URI = "file:///"
+ Environment.getExternalStorageDirectory() + "/";
Directory directory = new Directory(getContext(),
LocalReference.localizePath(ROOT_URI));
directory.setListingAllowed(true);
Router router = new Router(getContext());
router.attach("/files", directory);
return router;
}
}