Wednesday, October 10, 2007

Retrieve All Http Request Parms


Enumeration enumeration = req.getParameterNames();
for (; enumeration.hasMoreElements();) {
String name = (String) enumeration.nextElement();
if (!name.startsWith("rtn_")) {
continue;
}
String safld = req.getParameter(name);
String tafld = name.substring(4);
returnData.add(new ReturnDataField(safld,tafld));
}

Access SSL Content in Java Code

Call this method before access:

/***************************************************************************
* Get web contents using HttpURLConnection class.
**************************************************************************/
public static void handleSSL(String urlString) {
if (urlString == null || urlString.indexOf("https:") < 0) {
return;
}
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
} };

// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}

HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
System.out.println("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};

HttpsURLConnection.setDefaultHostnameVerifier(hv);
}


Ussage:

/*********************************************************************
* Get web contents using HttpURLConnection class (no cookie).
*********************************************************************/
public static String httpContentNoCookie(String urlString) {
log.info("Get content of " + urlString);
String incoming = "";
DataInputStream dis;

handleSSL(urlString);


try {
URL url = new URL(urlString);
HttpURLConnection uconn = (HttpURLConnection) url.openConnection();


uconn.setDoOutput(true);
uconn.setDoInput(true);
dis = new DataInputStream(uconn.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dis));

String line = "";
while ((line = in.readLine()) != null) {
incoming += line;
}
in.close();
uconn.disconnect();
} catch (Exception e) {
e.printStackTrace();
incoming = "error: " + e.getMessage();
log.error(e);
}
return incoming;
}

Wednesday, October 03, 2007

TortoiseSVN

Downloaded TortoiseSVN-1.4.5.10425-win32-svn-1.4.5.msi (from http://tortoisesvn.net/) and installed on my PC.

Followed instruction here to download iText to Eclipse.