Reading config files within Web-INF in RESTLET

故事扮演 提交于 2019-12-11 01:09:25

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/27043399/reading-config-files-within-web-inf-in-restlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!