Search code examples
javaservletsrestletrestlet-2.0

Reading config files within Web-INF in RESTLET


I'm trying to read a config file placed inside the root path of WEB-INF. The application is using RESTLET framework. I read in the official RESTLET doc that its possible to read files via a WAR connector ("war:///WEB-INF/web.xml") using Context.getClientDispatcher().

However i was not able to figure out as to how this can be achieved. Kindly let me know about this or any other ways a file can be read using RESTLET


Solution

  • Found the solution for this. I had to access the ServletContext to get to the configuration text file. Here is the code sample -

    // Extract the ServletContext from the attributes of RestletContext
        ServletContext servlet = (ServletContext) context.getAttributes().get("org.restlet.ext.servlet.ServletContext");
    // Get the path of the config file relative to the WAR
        String rootPath = servlet.getRealPath("/WEB-INF/configuration.txt");
        Path path = Paths.get(rootPath);
        File configFile = new File(path.toString());
        FileRepresentation file = new FileRepresentation(configFile, MediaType.TEXT_PLAIN);