Adding notification of ip/hostname of client (so we can get "source" of the conecction inside user services)

This commit is contained in:
Adolfo Gómez 2012-12-14 11:47:39 +00:00
parent ff609a39e8
commit de680eabb3
5 changed files with 113 additions and 6 deletions

Binary file not shown.

View File

@ -13,7 +13,7 @@ public class LinuxApplet implements OsApplet {
private Hashtable<String,String> params;
private String tmpDir = "";
// private String baseUrl = "";
private String baseUrl = "";
private String nxFileName = "";
private String scrWidth;
private String scrHeight;
@ -26,6 +26,9 @@ public class LinuxApplet implements OsApplet {
String height = params.get("height");
boolean fullScreen = false;
// Notifies to broker the hostname/ip
util.notifyHostname(baseUrl, params.get("is"));
if( width.equals("-1"))
{
width = scrWidth;
@ -96,7 +99,7 @@ public class LinuxApplet implements OsApplet {
public void setParameters(Hashtable<String, String> parameters, String urlBase,
int screenWidth, int screenHeight) {
params = parameters;
// baseUrl = urlBase;
baseUrl = urlBase;
scrWidth = Integer.toString(screenWidth);
scrHeight = Integer.toString(screenHeight);
}

View File

@ -13,7 +13,7 @@ public class MacApplet implements OsApplet {
private Hashtable<String,String> params;
private String tmpDir = "";
// private String baseUrl = "";
private String baseUrl = "";
private String nxFileName = "";
private String scrWidth;
private String scrHeight;
@ -26,6 +26,9 @@ public class MacApplet implements OsApplet {
String height = params.get("height");
boolean fullScreen = false;
// Notifies to broker the hostname/ip
util.notifyHostname(baseUrl, params.get("is"));
if( width.equals("-1"))
{
width = scrWidth;
@ -96,7 +99,7 @@ public class MacApplet implements OsApplet {
public void setParameters(Hashtable<String, String> parameters, String urlBase,
int screenWidth, int screenHeight) {
params = parameters;
// baseUrl = urlBase;
baseUrl = urlBase;
scrWidth = Integer.toString(screenWidth);
scrHeight = Integer.toString(screenHeight);
}

View File

@ -11,7 +11,7 @@ public class WindowsApplet implements OsApplet {
private Hashtable<String,String> params;
private String tmpDir = "";
// private String baseUrl = "";
private String baseUrl = "";
private String nxFileName = "";
private String scrWidth;
private String scrHeight;
@ -19,7 +19,7 @@ public class WindowsApplet implements OsApplet {
public void setParameters(Hashtable<String, String> parameters, String urlBase,
int screenWidth, int screenHeight) {
params = parameters;
// baseUrl = urlBase;
baseUrl = urlBase;
scrWidth = Integer.toString(screenWidth);
scrHeight = Integer.toString(screenHeight);
}
@ -34,6 +34,9 @@ public class WindowsApplet implements OsApplet {
String height = params.get("height");
boolean fullScreen = false;
// Notifies to broker the hostname/ip
util.notifyHostname(baseUrl, params.get("is"));
if( width.equals("-1"))
{
width = scrWidth;

View File

@ -0,0 +1,98 @@
package es.virtualcable.nx;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
public class util {
public static boolean download(String baseUrl, String id, String outputFileName)
{
try {
java.net.URL u = new java.net.URL(baseUrl + id);
java.net.URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentType.startsWith("text/") || contentLength == -1) {
throw new IOException("This is not a binary file.");
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
in.close();
if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}
java.io.FileOutputStream out = new java.io.FileOutputStream(outputFileName);
out.write(data);
out.flush();
out.close();
} catch(Exception e) {
System.out.println("Unable to download file, already present or network error? " + e.getMessage());
return false;
}
return true;
}
public static String getUrl(String url) {
try {
java.net.URL u = new java.net.URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
StringBuilder data = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
data.append(inputLine);
data.append("\n");
}
in.close();
return data.toString();
} catch(Exception e) {
System.out.println("Unable to get url. Network error? " + e.getMessage());
return null;
}
}
@SuppressWarnings("deprecation")
public static void notifyHostname(String baseUrl, String serviceId) {
String[] urlComponents = baseUrl.split("/");
String hostname;
String ip;
String url="";
try {
hostname = java.net.InetAddress.getLocalHost().getHostName();
ip = java.net.InetAddress.getLocalHost().getHostAddress();
} catch(Exception e) {
hostname = "unknown";
ip = "0.0.0.0";
}
try {
// An url is "http[s]://.....:/,
url = urlComponents[0] + "//" + urlComponents[2] + "/sernotify/" + serviceId + "/hostname?hostname="+URLEncoder.encode(hostname)+"&ip="+URLEncoder.encode(ip);
getUrl(url);
} catch(Exception e) {
System.out.println("Unable to get url? " + e.getMessage());
}
}
}