user583066
user583066

Reputation: 123

Read static XML file on Google App Engine

I have a static XML file in my App Engine app that uploads just fine and I am trying to read it for some rules based execution logic, but the below error is thrown at me:

Caused by: java.security.AccessControlException: access denied (java.io.FilePermission /war/WEB-INF/StaticContent.xml read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:393)
    at java.security.AccessController.checkPermission(AccessController.java:553)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:166)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at java.io.FileInputStream.<init>(FileInputStream.java:96)

I tried placing the file both directly in the war and in the war/WEB-INF directories, the problem persists. The on the server attempts to read the file is as simple as this:

final FileInputStream fis = new FileInputStream("/war/WEB-INF/StaticContent.xml");

According to this article, I am doing everything correctly: http://code.google.com/appengine/kb/java.html#readfile

Any help will be much appreciated.

Upvotes: 9

Views: 4258

Answers (4)

Tad
Tad

Reputation: 4784

I found that the following worked for me:

InputStream feedStream = new FileInputStream("WEB-INF/" + fileName);

Upvotes: -1

Parveen Verma
Parveen Verma

Reputation: 17520

If your file is stored in war Directory then you can access that without specifying project name in filepath:

project- war- sample.json

then access file with path as- FileReader fr=new FileReader("war/sample.json");

:)

Upvotes: 0

Kal
Kal

Reputation: 24910

Use getResourceAsStream instead of directly opening a FileInputStream.

The location you specify in FileInputStream is taken as an absolute location which is why you are getting hte access denied exception.

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/StaticContent.xml");

Upvotes: 11

Chris Bunch
Chris Bunch

Reputation: 89873

Have you tried reading from war/WEB-INF/StaticContent.xml instead of /war/WEB-INF/StaticContent.xml? It may be that the latter is interpreted as an absolute path, when in fact you don't know what the absolute path is and thus want a relative path.

Upvotes: 1

Related Questions