0

I have been looking for an answer for this for ages and can't find it anywhere. Really hope someone can help

My structure:

WebContent
 - resources
     - css
         - style.css
 - WEB-INF
     - web.xml
template.html

web.xml

 <servlet>
  <servlet-name>RestletServlet</servlet-name>
  <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>RestletServlet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>

Application.java

String ROOT_URI = "C:/projects/testsite/WebContent/"
    try {
        configuration.setDirectoryForTemplateLoading(new File(ROOT_URI));
        configuration.setObjectWrapper(new BeansWrapper());
    } catch (IOException e) {
        e.printStackTrace();
    }

    router.attach("/resources", new Directory(getContext(), LocalReference.createFileReference(ROOT_URI)));
    router.attach("/index", IndexResource.class);

When I go to URL: localhost:8080/testsite/index I get the template file with it populated with the correct data. However the CSS is not loaded. I can see in my eclipse console that restlet is trying to fetch it but I it get a 404

127.0.0.1   8080    GET /testsite/resources/css/style.css   -   404

As you can see above I am trying to use the Directory class to load my css directory but has no effect. Maybe this is wrong!? Is there a way in which css does not go through restlet? It would be good if ROOT_URI was relative instead of the absolute path. Is there an easier way to get the path of my location?

Decrypter
  • 2,784
  • 12
  • 38
  • 57

2 Answers2

2

I notice first that as your Directory is based on the "file" protocol, the container of the Application must declare this client connector. This is done inside the web.xml configuration file:

<servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.application</param-name>
        <param-value>test.MyApplication (actually the full name of your Application</param-value>
    </init-param>
    <init-param>
        <param-name>org.restlet.clients</param-name>
        <param-value>FILE</param-value>
    </init-param>
 </servlet>

Without this instruction, I think your log contains some entries that look like:

ATTENTION: The protocol used by this request is not declared in the list of client connectors. (FILE)

Having said that, using the "file" protocol to serve your static files ties your application to your file system. You can serve those files internally, that is to say let the container looks for your static resources inside the application WAR (which could be uncompressed or not). You can simply based the Directory on the "war" protocol, as follow:

router.attach("/resources", new Directory(getContext(), "war:///"));

I hope this will help you.

Best regards, Thierry Boileau

0

I got it working I had to put in this mapping into my web.xml file

 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>/resources/*</url-pattern>
</servlet-mapping>
Decrypter
  • 2,784
  • 12
  • 38
  • 57