Signed Java Applet writing to a text file - java

I want to make an Applet write to a text file. I think have overcome the security problem with my own certificate, but the text file isn't receiving the output.
My Applet can be seen at tomrenn.com/fallball, and should ask if you trust the certificate.
I'm not sure an Applet can write to a text file within the web hosted contents, so I'm asking if this is possible; and if not, at least be able to do it on the local machine. I had been using a FileOutputStream, but my teacher recommended this very basic output method.
public void writeToFile()
{
try {
java.io.File file = new java.io.File("highscores.txt");
java.io.PrintWriter output = new java.io.PrintWriter(file);
output.print(name + " " +score);
output.close();
} catch (IOException e) {
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
This is not what I would be implementing, just trying to see if I get any output. Thanks for looking over my question!

From an Applet, you cannot directly write to the server's file system. You can issue a request to the server that causes the server to write to its own file system, but an Applet does not have a way to write to a file system on a remote machine. (Of course, unless it's mounted NFS or otherwise.) To issue such a request, you could use Apache HttpClient to issue HTTP requests, for example. This may be more heavyweight than you are looking for. You can also have the client issue a POST to the server to say, "This is my high score," and let the server manage high scores.
A signed Applet has every right to write to the local file system of the person running the Applet. If you are writing to the "current directory" (rather than an absolute full path), then make sure you know what directory the Applet is running in. Otherwise you may indeed create a file, but not be able to find it!

If you want an applet to store data on the local machine, from 6u10 the javax.jnlp.PersistenceService is available. Creating a secure "signed applet" is difficult, and I wouldn't recommend it to anyone.

Applets run on the client so cannot access the servers disk.
The code you posted will write to the clients local disk. I'd suggest changing it though to specify the directory you want to place the file. The users home directory would seem a good place for it
java.io.File file = new java.io.File(System.getProperty("user.home"), "highscores.txt");

just sign your applet.
it is easy using netbeans.
open the project in netbeans.
right click on your project and select properties now you will have new window.
go to > application > webstart on that window.
check enable webstart.
press customize button at signing and choose "self signed by a generated key".
check "applet descriptor" and target your applet.
press ok.
rebuild the project (now netbeans will create certificates with all privileges )
use "launch.html" at your projects dist dir to run the applet via jnlp.
That's all.
** used netbeans version = 7.0
** used JDK = 1.6
** certificates will expire in 6 months.
Thanks

Related

Java - Hosting a file via server/client architecture

I've been having trouble finding information on how I might go about hosting a .jar file with a simple Java server. Basically, I'm writing a 2D game in Java and I would like to create a launcher that, given internet access is present, will check for the latest version on launch and download the newest version if necessary. Now, I'm aware I could use a file sharing site, simply up the latest build and have the launcher download from there; I just happen to find the redundancy of hosting the file on a Java server that I will myself be running all the more satisfying.
So basically, my question is: how would I create a Java server that simply hosts the latest build's .jar file and when an incoming connection requests it, the server sends that entire file to the client PC?
Also, please omit any concerns regarding the possible inefficiency of downloading the entire game .jar, as the game will be extremely small. The only threat of an over-sized download I can foresee is with music; however, I could create a separate request for music files and only grab what is needed. That goes the same for graphical assets if they become an issue as well, but 16x16 textures don't strike me as having potential for getting in the way.
If you are willing to obtain a code signing certificate, you could use WebStart
Check Java WebStart - this covers all your needs regarding checking and downloading.
Build a simple webapp that contains / hosts two things: Your .jnlp (Web Start descriptor file) and your game .jar
Sign your jar using a valid code signing certificate (this is required for webstart)
Deploy your webapp or jnlp/jar combination to whatever webserver you can get a hold of. As you're only uploading two files, there are a lot of "free" webhosting sites where you can upload some files.
If you really, really like to run the server for the downloads yourself, you should possibly check out Tomcat or Jetty or run Java SE HttpServer like this:
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/game.jnlp", new FileHandler("game.jnlp"));
server.createContext("/game.jar", new FileHandler("game.jar"));
server.setExecutor(null);
server.start();
}
static class FileHandler implements HttpHandler {
String file = "";
public FileHandler(String string) {
file = string;
}
#Override
public void handle(HttpExchange t) throws IOException {
byte[] data = IOUtils.toByteArray(getClass().getResourceAsStream(file));
t.sendResponseHeaders(200, data.length);
OutputStream os = t.getResponseBody();
os.write(data);
os.close();
}
}
If you believe Webstart is overkill, which I'd understand in your situation, you would need two jar files in the client: One to check and do the download of the other and then start the newly loaded file up. Sequence of steps would be:
Check if .jar is reachable by network.
If it is, load it and store it in defined path
Start a new java Process (-> ProcessBuilder) to run that java-program
Wait for game to finish (downloader will finish right after)
In that case your server would only need to host the game.jar

How to check if a Internet enabled java program is the original one i made?

I am writing a program which communicates with a server. But i want the server to only work with the original jar.
Is there anyway to do this? certificates etc?
Sign your jar file
And verify the signature of the jar file on runtime.

Is it possible to connect an OutputStream to the download window of a browser fron a java applet?

see, i am developing a web application that downloads files from a server via http requests, but in a case the file isn't in the server but in the applet itself, i mean, some binary content is dynamically generated inside the applet and it must be downloaded. Of course i can use the java libraries to save the file in the client file system (if the applet is a signed one), but i was wondering if it can be done connecting the java OutputStream to the browser's download window, in other words, start a download from an applet.
Am i a crazy person ?
by the way, is it possible to do something similar from javascript ?
No, it is not possible to get around security by attaching the output of an applet to the standard file download mechanisms of a browser.
OTOH, since the Next Generation Java Plug-In, it is no longer necessary to have a signed and trusted applet in order to save files(/bytes) to the local file-system. Chase the links in the Applet info. page for more details. For a demo. of using the JNLP API services (that the plug-in2 hooks into for this functionality) see the File service demo..
You can if e.g. you upload the file to the server and then force browser (via LiveConnect or otherwise) to open that file from the server.
As far as I'm aware, there's no cross-browser way to emulate downloading from within an applet. So you should create that download yourself, and let browser do what it does best.
Obviously, it might well make sense to move the actual creation of the stream to your server side.

Force Download on client machine

One of my requirements is, on load of page, a file is to be created dynamically and downloaded at a particular location on the client's machine.
In case the file is already present, it has to be over written.
is there any way where we can access the client's system and store the file at the required folder?
I feel one cannot access the client machine when the code is being executed on the server..
Senorio:
1-User click on generate document then it took template stream data ,req. data file and then save two file into client machine.
2-After that template open and it fetch the data file from same directory.
Please help me on this. This is an SOS!!
There are probably other solutions, I use a signed applet for this purpose.
As always, there are a few caveats though:
You can't "force" anything against the will of the user. Applets may be disabled in the client's browser, or they may not even have Java installed. Or the target directory might not be writeable by the user.Your server should handle cases where the client doesn't have the correct version of the file gracefully.
You can't do this from the server side obviously but you also really can't do this from a client script either. Browser security will prevent a page script from modifying contents of the file system.
Your only options will be to run a third-party browser plugin software that has elevated permissions.
Examples of such are
Java Applets
Java WebStart
Microsoft Silverlight
ActiveX
Each one is different and most require some level of user interaction to confirm that they allow plugins to run with elevated security.

Zip File on a web server to extract in to local machine

We have a web application that allows user to download a zip file from a web server. We just provide dummy iframe source to the full URL of zip file on web server. This approach would allow end user to use browser controls which allows the user to open or save the zip to user's local machine.
We have a requirement that the zip file is automatically extracted and save to a specific location on user's machine. Any thoughts on how this can be achieved?
Thanks.
I highly doubt that you'll be able to do that. The closest you're likely to get is to generate a self-extracting executable file (which would be OS-dependent, of course).
I certainly wouldn't want a zip file to be automatically extracted - and I wouldn't want my browser to be able to force that decision upon me.
Short answer is I don't believe this is possible using the simple URL link you've implemented.
Fundamentally the problem you have is that you have no control over what the user does on their end, since you've ceded control to the browser.
If you do want to do this, then you'll need some client-side code that downloads the zipfile and unzips it.
I suspect Java is the way to go for this - Javascript and Flash both have problems writing files to the local drive. Of course if you want to be Windows only then a COM object could work.
Instead of sending a zip file why don't u instruct the web server to compress all the web traffic and just send the files directly?
See http://articles.sitepoint.com/article/web-output-mod_gzip-apache# for example.

Categories