Question

I got:

wordsDictionary.xml

in:

/WEB-INF/xml/

and I'm trying to read it with SAXReader using code:

...
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setValidating(false);
saxFactory.setNamespaceAware(false);
XMLReader reader = saxFactory.getXMLReader();
reader.setContentHandler(new WordsDictionarySAXHandler(this,lettersMapping));
reader.parse(new InputSource("/WEB-INF/xml/wordsDictionary.xml"));
...

On Dev server everything goes fine. When deploying it to production I'm getting an error:

com.google.api.server.spi.SystemService invokeServiceMethod: access denied
("java.io.FilePermission" "/WEB-INF/xml/wordsDictionary.xml" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission"  
"/WEBINF/xml/wordsDictionary.xml" "read")

Before you say that I need to use: context.getResourceAsStream you need to know I'm using Google Endpoints. So I don't have access to any directly Servlet (nothing is passed to endpoint method - HttpRequest).

My question is: Do you know how to load xml file right on GAE Endpoints?

EDIT: Just read https://developers.google.com/appengine/kb/java?csw=1#readfile:

If the file location is not the issue, the problem may be that the method you are using to read from the file is not whitelisted. Your application can use any IO classes that are useful for reading from the file system, such as File, FileInputStream, FileReader, or RandomAccessFile. For a full list of whitelisted classes, please see the JRE Class White List.

I've changed part of code to:

FileInputStream stream = new FileInputStream("/WEB-INF/xml/wordsDictionary.xml");
reader.parse(new InputSource(new InputStreamReader(stream)));

Unfortunately, nothing changed.

Was it helpful?

Solution

Reason of error was "/" before WEB-INF. Remember:

access denied ("java.io.FilePermission"  
"/WEBINF/xml/wordsDictionary.xml" "read")

This error can be thrown even when you're trying to access a bad directory.

Also remember to set proper encoding for your XML files:

FileInputStream stream = new FileInputStream("WEB-INF/xml/wordsDictionary.xml");
InputSource is = new InputSource(new InputStreamReader(stream, "UTF-8"));
is.setEncoding("UTF-8");
reader.parse(is);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top