I would like to implement a Java application which runs on a webpage and allows:
User A (from its browser) can request a file to User B (with its browser opened). Of course User A knows User B's IP and port.
User B can respond this request from its browser.
Which options (Java libraries, technologies,...) do I have?
THANKS!
If you want Java in the browser, the only way to go is applets.
But a normal (untrusted) applet:
can not access files on the local system
can not listen at any ports
can only open sockets on the server it came from.
This is for security reasons: an applet should not be able to harm the user whose computer it runs on.
So, if you want to do what you said, you need the user to trust you and give you more permissions. For the file access, the way to go would be the JNLP API (i.e. start your applet with a jnlp file, and then use the API in javax.jnlp, specially FileOpenService and FileSaveService. The user then needs to confirm the access before choosing a file with a file chooser.
This still does not help for the network access - your applets need to have suitable SocketPermissions there, if you don't want to proxy everything on your server (which would not be peer-to-peer). For this, you need to sign your applet, and request all permissions from the user (there is no finer-grained way to give only the necessary SocketPermissions, I think). You can do this in the jnlp-file.
Related
I want to create an application in java that is monitoring logged users activities(create, delete, update folders/files).
The problem is that I didn't found how to get the OS of the logged user (java app is running on a windows server and users have windows on their machine, I want to know if there is a way to get the windows version of the logged users).
BRs,
Mihai
You would use JNI and call a native (windows-specific DLL) method to get the information. You would have to create this DLL yourself in (likely in C/C++)
System.getProperty("os.version")
If it's a web application you can use user-agent header. It can change easily, but worth to try. Check https://stackoverflow.com/a/1328393/5684110.
You're asking for a Windows-specific feature. I doubt Java would support that, so you will need a native module (written in C/C++ or something) to read that information and pass it into your Java application via JNI or a local socket connection. Maybe you could poll that data from Active Directory.
Another idea is, you could get the info through another Java app running on the client PC at startup. That way you would be able to monitor changes in the file system and some basic system properties like OS name and version (see Mustafa's answer). The app would be silently downloaded into the workstation and run automatically through Active Directory, sending the data to your server app via socket, web or a webservice.
Hope this helps you.
I have 2 parts to my question. First is just a general question and second is more into app development and coding.
How does Starbucks/Coffee Bean/McDonalds pushes out notification whenever we as customers connect to their WIFI AP? They will push out notifications like "Sign In to proceed using the internet" in the status bar, and then redirects us to their website before we can start using the internet.
I am trying to create an app that does just that by creating a personal hotspot(that will have internet access), and whenever customers are logged into my hotspot, they will receive that same notification. My intention is to just direct them to a 'sign in' page in their browser, and once they click sign in, they will again be directed to my personal website. After that, they are free to surf any websites they want to.
So is it possible to do just that? I only want the app to be installed in my phone, and not in my customers phone.
I'm deducing a lot of this, but I don't see how else it could work.
The wifi hotspots you are referring to are themselves little web servers, in addition to being participants in the world wide web. When a browser sends a URL out, it eventually gets to a web server (we won't go into the DNS system here), and the web server -- a machine listening on one or two ports, ready to accept a URL via socket and return something in response -- responds to it.
In the case you're talking about, the little wifi web server stores the URL sent and, regardless of what it is, sends back the "login to continue" page, and ensures that it is responded to. Once it is, then it does its normal thing with the original URL, which in this case means it sends it out to the internet cloud for interpretation like a normal wifi hotspot would.
As future URLs are sent, the wifi hotspot ensures that the login has occurred -- perhaps it just uses the URL of the machine as a key to information about when it logged in last, or a cookie, there are a couple of ways -- and if there is a current login session, then it again passes the URL out to the internet cloud.
In order to do this in Java on your phone, you would need control of the socket input/output of the hotspot; you would have to be able to read the input, determine whether the login had occurred, and only then either pass along the incoming URL or store it so the login could take place. I don't know how you would do that.
What you describe is a "captive portal" - all new users connecting through the hotspot will have their HTTP requests redirected to this portal. Once you register, your IP-address is known and the redirect no longer occurs.
How this works on mobile devices is typically that when you connect to a hotspot your phone will try to access a well known address and when the redirection occurs to a HTTP-AUTH response it will then open the captive portal as part of the setup process. For instance in the case of the iPhone apple will try to access 'phobos.apple.com' which is why you'll often see this in the pop-up when you are trying to access a "other" website on an non-authenticated access point.
As regards doing this in Java, I think you will have a few problems on your hands. Typically Java only provides network access at TCP/UDP layers and above. Now what you could do is have your application acting as a "proxy" such that it relays all network activity through it, but this would be a heavy load for your application. It also would not have the transparency of the type of solutions you would be used to. Your users may also have to be told to access your site directly.
Your typical captive portal has a few different actors involved and a number of different protocols beneath the application layer. The Access Point needs to "know" whether a user is authorised or not (and thus is redirected to the portal, or gets to access the wider internet); The portal needs to be able to notify the portal upon authentication; 802.1x is used for the initial handshaking; DHCP to assign the IP-address based on MAC address.
I have embedded a JSCH SSH Java applet in a web page and need to know if it's possible to run a script (of any language like PHP) to automate logging in and running commands. I have heard of expect4j and java robot but cannot see any way to implement it. Keep in mind, I'm not great with Java so I don't know everything about it. Any help is appreciated.
JSch is an SSH client library, and by itself only allows programmatically steered connections to another server. The user interaction has to be build around it by users of the library.
The JCTerm applet provided on the website also contains a terminal emulator in form of a Java GUI. If you only want to automatically execute some command (and maybe show its output in the web page), you could do everything on the server side, and don't need the applet with its terminal emulator. (You would need either some PHP-Java bridge on the server side or some Java-enabled webserver with a Servlet or similar, though.)
(If the web server would be the same machine as the server you'll run the command on you wouldn't even need the SSH connection, but could execute the stuff directly.)
If the server can't do anything (i.e. a "static server"), an applet is the way to go, yes. You can either modify JCTerm or create a new applet from scratch (using JCTerm's connection code as an example on how to connect to to the server).
If you don't have to fear any malicious users in your LAN (i.e. between web server and user, the SSH server doesn't matter), you can embedd the password (or preferably a private key for public-key authentication) into the applet's jar file, and pass it to the library for connection. (You should also include the server's public key for easier checking.)
Provide the command(s) to a ChannelExec (instead of a ChannelShell), this makes it easier to provide input (if necessary) and capture the output. Pipe the output in a text area, or simply use a green/red label saying if the command was successfully executed.
(I might have a look at this in the next days and try to do it. No promise, though.)
I have created an applet which creates a file on running it. But when I run my applet via server, it fails.
Is there any possible way to create a file on server with applet?
EDIT:I am creating a sound record applet which works fine when I run the applet in browser locally.It actually creates a file of recorded sound,but when I run the same applet on server,it does not create file.Is it because the server does not allow you to do so?
Is there any possible solution so that the file can be created?
File objects always point to a location (that may not exist) on the client machine.
To store something on a server, it would require some server side functionality to accept the bytes and create a (server-side) File. That might be done with PHP, servlets/JSP, ASP etc. Once the server-side is organized to accept the bytes, the applet can connect to it and push the sound recording through.
Java Applets are run on the client machine. Once you invoke the page containing an applet, the applet gets downloaded to the client's machine and runs. Hence it will not get access to the server.
I want to write a yahoo! messenger and I create 2 java applications .one is for "server" and the other one is for" client" .at first I run the Server application and then I will run my GUI frame which is in my client application(So I will run my client application):it will show a frame that gets user name and password from a client . IF the user name and password were correct I will call the method which I create it in my client class which is in my ClientNetwork package(this package is in Client application).
Is this a correct way to run this program?
You will need to determine SOME protocol that will be used to communicate between the two programs. Also you most likely need it to be working over TCP/IP.
I would suggest you look into client/server programming. Here is the relevant Sun Java Tutorial page - http://java.sun.com/docs/books/tutorial/networking/sockets/
Also note that the username and password check should be done at the server. It wasn't quite clear from your question where the credentials are checked, but it's crucial not to do it at the client.
Anything that's done at the client can be potentially compromised, and you certainly don't want a malicious user to be able to log in with someone else's name.