Access denied to resource file from java servlet - google-app-engine

I am trying to access a resource file from a servlet but getting HTTP Error 500 - access denied:
File file = new File("//warChildFolder//myFile.txt");
InputStream is = new FileInputStream(file); // <--error on this line
I am on google-app-engine.
Any help appreciated!

Google App Engine docs talk about "white listing" a file. Is that in play here?
I also wonder about this:
File file = new File("//warChildFolder//myFile.txt");
Doesn't the leading slash make this an absolute path?
I'd try it like this:
File file = new File("WEB-INF/warChildFolder/myFile.txt");
Make the path relative to the WAR root and be explicit about WEB-INF.

I'm not sure about Google App Engine but in my experience the only solution that works across containers and platforms is to use ServletContext.getRealPath().
new File(servletContext.getRealPath("/WEB-INF/warChildFolder/myFile.txt"));
The spec says: use forward slashes and a leading slash. This gives you platform independence and you're not relying on the process' current directory.

Does it work if you use single path separators?
(updated to use relative paths):
File file = new File("warChildFolder/myFile.txt");
You need to escape the "\" character in Strings, so use "\", but a single "/" is all that is needed.
Update: It may be that the path being processed is not the same as you expect, you can try logging the absolute path of the file (with file.getAbsolutePath()) to check this.
Another thing to check is that the process has read permissions on the folder/file. If you're not on Windows this can be a problem.

Related

How to rename a file while using "move" in URL in apache camel

I have an URL like
url = "file:D:/inputFolder?move=D:/outputFolder". we are making this url dynamically.
I want to rename the file while moving, So I made it something like this
url = "file:D:/inputFolder?move=D:/outputFolder&fileName=abc.txt". But I think move and fileName do not work together, it is not renaming.
Is there any alternative to do it? Please remember I want with "move" only.
I cannot use .setHeader(..) also.
Thanks,
Hy,
as far as I understand you, your trying to move the file in one single uri.
That is not really how camel works.
The idea of camel is to have a "consumer" and a "producer", where the consumer loads data (e.g. your file) and the producer puts the data somewhere (e.g. save the file into a folder)
That being said, here is what worked for me with a java route:
from("file:/home/chris/temp/camel/in")
.to("file:/home/chris/temp/camel/out/?fileName=test.txt");
The from part configures the folder where camel looks for new files. A few notes on that:
The file component checks the folder each 0.5 sec for new files. This can be changed with the delay parameter
The option noop configures, if the file is being moved or copied. By default it is set to false, which means it is moved
In the to part you configure, where the file is supposed to be moved. Here you can use the fileName parameter to rename the file.
Be careful with this though, because setting an option in the uri directly does make it "static".
What I mean by that is, that the only way of changing the parameter is by completely reconfiguring the route or by restarting it, where neither is something you would want to do normally.
Note 1:
Moving all files that are put into one folder into the same file always overrides the previous file by default.
You could, for example, use the fileExists parameter to always just append the content of the file: fileExists=Append (See camel file docu for details)
Note 2:
There is an option in the file component to not "move" the file, but copy, rename and delete it, which sometimes is necessary, when you want to move it onto a different drive and a simple copy does not work.
Also see the docu for the camel file component for details on that.
Note 3:
You can have multiple to() statements in the same route to have the file moved to multiple locations. For example:
from("file:/home/chris/temp/camel/in")
.to("file:/home/chris/temp/camel/out/?fileName=test.txt")
.to("smtp:....");
Hope I could help you and answer you question.
Greets
Chris
Two possible ways to achieve your goal.
Use both "consumer" and "producer"
Using this way, you are free to control where and how your destination can be set and has great freedom to control filename with the use of a processor/bean.
from("file:D:/inputFolder")
.to("file:D:/outputFolder?fileName=abc.txt")
Use "consumer" only
Using this way, you are treating your work as source data control. This can be use when your file is going to move within same drive. The drawback is the filename rename pattern is limited (refer to camel file language)
from("file:D:/inputFolder?move=${file:parent}/../outputFolder/abc.txt")

JavaFX - `isDirectory` Not Returning TRUE

Why is this check not return true?
String dirpath = getClass().getResource("../util/assets/sounds/").toString();
File dir = new File(dirpath);
System.out.println(dirpath);
System.out.println(dir);
System.out.println(dir.isDirectory());
It returns these:
file:/D:/JAVA/exercises/FX/TasksList/build/classes/taskslist/util/assets/sounds
file:\D:\JAVA\exercises\FX\TasksList\build\classes\taskslist\util\assets\sounds
false
I run it using netbeans IDE as a source code.
getClass().getResource(...) returns a URL, not a file path string. A file path string is needed by the constructor of File.
You can try using the Paths constructor to construct a valid file path from a URI, which you can get from the resource using toUri().
So, this should work:
Files.isDirectory(
Paths.get(
getClass().getResource("../util/assets/sounds/").toUri()
)
)
Or
new File(
getClass().getResource("../util/assets/sounds/").toUri()
).isDirectory();
I haven't got a windows machine to try that on.
Note: If you start packaging your app as jar (as is recommended), then this technique will break. The resource would not be a file, but instead a jar resource. There is no concept of a file directory in relation to a jar resource.
It supposed to be :
String dirpath = getClass().getResource("../util/assets/sounds/").getPath();
instead of:
String dirpath = getClass().getResource("../util/assets/sounds/").toString();
And it will work.

Getting cannot find file exception in Play Framework

I have a file called product.csv in my application root. I want to read from the file. But I am getting file not found exception. Here is the code I have written.
val lines = scala.io.Source.fromFile("/product.csv").mkString
println(lines)
What am I doing wrong?
I am using Windows OS.
You need to use a relative filepath "product.csv" rather than "/product.csv" which will look at the root of your drive.

hadoop write file and put in Distributed cache

I have a requirement to create a dynamic file based on the content in hadoop job.properties and then put it in Distributed Cache.
When I create the file I see that it has been created with the path of "/tmp".
I create a symbolic name and refer to this file in the cache. Now, when I try to read the file in the Dis. cache I am not able to access it. I get th error caused by: java.io.FileNotFoundException: Requested file /tmp/myfile6425152127496245866.txt does not exist.
Can you please let me know If should I need to specify the path also while creating the file and also use that path while accessing/reading the file.
I only need the file to be available only till the job is running.
I don't really get your meaning of
I only need the file to be available only till the job is running
But, when I practice to use distributed cache , I use path like this :
final String NAME_NODE = "hdfs://sandbox.hortonworks.com:8020";
job.addCacheFile(new URI(NAME_NODE + "/user/hue/users/users.dat"));
hope this will help you .

How to convert from DOS path to file scheme URI in Batch file

I'm trying to write a batch file for svnsync, which needs urls to svn repositories. The rest of the batch file uses %~dp0 to get the path of the batch file, but that doesn't work with svnsync.
What is the best way to convert a path (say %~dp0repo, which gets expanded to c:\backup\repo) to a uri suitable for svnsync (file:///c:/backup/repo)?
Ideally it would be able to handle spaces and what not in the path too, so I'd prefer avoid having to use some explicit character replacement to convert from path to URL -- but if that's the only way, oh well.
Thanks!
From your recipe is seems you only need to:
Replace \ with /
Stick file:/// on the front
Here we go:
set DOSPATH=%~dp0repo
set URI=file:///%DOSPATH:\=/%

Resources