Java applet socket server permissions - java

I want to make a Java applet, which functions as a socket server (local network ips, or 127 range is fine).
I want this applet to run in a webpage.
the browser can ideally load the webpage from the file system (file://...), but I could install an HTTP server if required.
The applet will need to be able to interact with JavaScript.
I'm having trouble wading through all the docs. Is the above possible? I'm not familiar with java's security model. If I need to sign the applet, or set some special security or configuration settings, that's fine.
edit-
In case anyones curious, the applet does very little. I just want to use it as a bridge so that another program running on the local computer can communicate via sockets. That program supplies values, which javascript will use to update the ui in real time. The ui is complicated, and building it in html/javascript will be a huge time saver. I'd prefer the applet function as the socket server opposed to a socket client, because otherwise then I need to write a seperate program to act as a server in between the applet and the other program.

Is the above possible?
Sure. If you control the security/policy files for that applet & the firewall, it is a single browser deal, it should be doable. You might need to wrap some of the applet methods in a PrivilegedAction if called from JS.

Related

JNLP and FileOutputStream

I haven't done any program that utilizes Java Web Start. However, since I need to develop a program that I can automatically update remotely, I am thinking of delivering it via JNLP.
My question is, will it run given that the program uses FileOutputStream?
Before answering, I must indicate here that I cannot use an Open Dialog because I am outputting to a printer on a shared location (example: //computer/epsonlx300). Also, not very familiar with sandbox permissions, etc.
Thanks.
Unless you electronically sign your jar, it will not be able to write anywhere outside the JWS sandbox (unless the user gives permission through an open dialog). This is mostly for security reasons, but makes developing a Java Web Start app fairly frustrating.
Unfortunately since you need to write to a location outside of the sandbox, you can't use JWS unless you sign it.
For future reference, there is a JNLP service called PersistenceService that will allow you to write to the sandbox. Think of it like writing cookies, except that it's for Java Web Start apps only.

How to run Java Applet in browser without activating plugin?

I know this may sound like a silly question, but I was wondering if there was a way to run Java Applets on my server and embed them to a page in a way that the user would not have to "allow access" to the applet, because it wouldn't have direct connection with their computer. In other words, make an embedded java applet which wouldn't download to the client side? I understand that the lifetime of an applet is only in the web browser when a user activates a plugin and all of that.. So how can I run Java APplets on my server?
Theoretically everything is possible. Applet is a special java application that extends Applet or JApplet. Applet just extends Panel. You can simulate applet container by implementing your own dummy appet context and run applet on server side.
Now, you can capture its UI and stream it to browser using AJAX or even web sockets. You can catch mouse and keyboard events in browser, send them using ajax to server where your applet is running and emulate the same events on your applets.
But may I ask you "why?" I think these efforts can be reasonable only if you have a huge applet and do not want to re-implement it using other technologies. But IMHO it is easier to re-write your applet using ExtJs or GWT. In GWT it is probably the better way because GWT is a java that is compiled to javascript, so you will probably be able to reuse most of your code.

Interactive terminal emulation in JSF application

We are developing (basically data-mining) web application backed by JSF(Primefaces)/Spring/Hibernate. For next step, we have to create some sort of terminal emulation so the web application can SSH user to requested host.
Purpose of this question is to find out right technology for our requirement. Currently I am doing proof of concepts with java applets and jnlp, but don't understand them deep enough to see their limitation/possibilities in advance.
Use case: (simplified from the project specification):
Let's assume we do the emulation in java applet.
Imagine simple web page with p:selectOne menu (hostMenu), p:dataTable (scriptTable) and our applet. HostMenu items are backed by object that contains server URL, login and password. Selection change trigger event that will copy the data to applet and make the SSH connection.
Now in the scriptTable, there will be simple, predefined script defined by user. Think of the script as something really simple (such as ls -l on linux machine). Doubleclick event on the table will execute the script in the applet's ssh session.
Some other requirements and contract limitations
ssh connection must be done on client computer (not on server)
the emulation must be fully interactive (especially, on linux
machines, TAB key autocompletes)
on the web page, AJAX request does not reset the applet (it stays
connected to the machine before the request)
Any response is appretiated. So are other technologies than applets/jnlp. For example, if you know possiblities hot to intercact from JSF with external emulator (Putty, linux console) we can use them.
For the frontend, check out Primefaces terminal component:
http://www.primefaces.org/showcase-labs/ui/terminalHome.jsf
Well, I think we have figured out at last using JCraft's JCterm. It required some changes to code, but it appears to work well (so far ;))

Triggering a web page print from server side

This question might be very basic.
Till now I thought a command to print a webpage can only be initiated at the client side.
(window.print when using javascript)
But I came across http://juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/ which states about printing using Java. I think this seems to be related to some desktop client and the same may not be possible in a web client. Can anyone confirm and explain this?
You can't execute server side code on the client, so the only way to do it in browser is through javascript or using plugins/flash/java applets.
You could print using java, but for that java needs to run on the client.
A website can ask the browser to open its print dialog (Google Maps does this on the "print directions" page, for example), but it can't actually force the browser to print anything. (If it could, you can be sure that advertisers would use it to print ads on your printer.)
A Java application running locally with sufficient permissions can print, just like any other desktop application. That has nothing to do with web pages.
Don't confuse Java and JavaScript. When trying to use Java within a browser, you'd have to look into using applets. A Java applet could definitely be used to do the kind of work you'd normally have a rich client do from within a browser.
Java applets could also receive events sent out from a server via sockets or some other mechanism, although I'm not certain if security constraints would allow it. Also seems a bit of a roundabout way to do things.
Remember that web browsing is a client-side-driven affair. There's some push models in certain infrastructures (I believe it's possible using JavaServer Faces). But those are probably just a sort of polling mechanism initiated by the client that is abstracted away to look like a server-side push.

Is it possible to get the default gateway IP and MAC addresses in java?

I'm trying to find a way to access the IP and MAC addresses for the default gateway from a java applet to be run on a website, is this possible?
You can't do that kind of thing in Java, like getting the SSID on a Wifi network. This low-level hardware information can not be obtained in Java in a cross-platform way and Java only allow you to work in the Transport (TCP) level, aka use sockets.
If you need this kind of information, try invoking a OS command tool using Runtime.getRuntime().exec(...) and parsing the output. But you have to deal with different OS that means different commands tools and outputs.
But in the Applet world, you can't invoke OS command tools, nor opening files, you are in a sandbox that prevents Applets for doing some low-levels tasks that can put in risk the user computer.
If you sign your applet, you have privileges to perform same operations like regular desktop applications, but this is out of scope.
Try to focus you problem in another direction.
You can get the Mac Address of an interface in Java using the method getHardwareAddress in the class java.net.NetworkInterface (http://download.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()). I am not sure if that will work inside an Applet, and I have never tested that method myself, but if something does it, this method is that thing.
The IP Address? sure thing. Just have the applet connect back to your server. An applet can open an http connection to the server that hosts it.
The MAC and Default GW are system specific things you could get via external programs, but I'm not 100% sure those are supported in the applet sandbox (in fact, they're probably not supported).

Categories

Resources