Wednesday, June 8, 2011

Import a .js file in another js file

function inc(filename)
{
var body = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
body.appendChild(script);
}

inc("jscripts/HelpWindow.js");
inc("jscripts/shortcut.js");

Thursday, April 21, 2011

Https Configuration in Tomcat

How to configure SSL on Tomcat
Steps to create keystore file.
1. >cd $CATALINA_HOME
2. > $JAVA_HOME/keytool -genkey -alias tomcat -keyalg RSA -keystore mycert.jks
3. Enter keystore password: changeit
4. What is your first and last name? [Unknown]: Company
5. What is the name of your organizational unit? [Unknown]: IT
6. What is the name of your organization? [Unknown]: My Comp.
7. What is the name of your City or Locality? [Unknown]: KL
8. What is the name of your State or Province? [Unknown]: KL
9. What is the two-letter country code for this unit? [Unknown]: MY
10. US Is CN= Company, OU=IT, O=”My Comp.”, L=KL, ST=KL, C=MY correct? [no]: yes
11. Enter key password for (RETURN if same as keystore password): Hit Enter.
Tomcat will assume the password is “changeit” by default so it’s advised to leave it that way.
Steps to configure Tomcat to use the keystore file.
1,cd $CATALINA_HOME/conf/
2,vi server.xml
3,Look for “ Define a SSL HTTP/1.1 Connector on port 8443 ”. Remove the comments indicator and add the keystore info.

Define a SSL HTTP/1.1 Connector on port 8443
Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" keystoreFile="/app/interface/ui/mycert.jks" sslProtocol="TLS"

4,Make sure redirect port is set as 443 as below in server.xml.
Connector port="9090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443"

5,In Application’s web.xml following lines need to be added.
security-constraint




6,Restart Tomcat server and test.

Wednesday, April 20, 2011

Eclipse Issue "An internal error occurred during: "Items filtering". Class file name must end with .class"

Deleting the corrupted search index which is explained in Eclipse bug’s #269820 comment.

How to delete the search index:

1. Close Eclipse
2. Delete workspace/.metadata/.plugins/org.eclipse.jdt.core/*.index
3. Delete workspace/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
4. Start Eclipse again

This fixed the issue for me.

Thursday, March 24, 2011

POST Method request Using JavaScript



How to simulate the below in javascript ?

action=http://example.com/ method=POST
input type=hidden name=q value=a
How to call the javascript function :

post_to_url
('http://example.com/', {'q':'a'});

function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.

// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);

form.appendChild(hiddenField);
}

document.body.appendChild(form); // Not entirely sure if this is necessary
form.submit();
}


Hope this helped someone.



Thursday, December 2, 2010

Daemon Threads

Daemon Threads are generally taken to consideration in Application development when there is some task to be done, however of not much priority. Say like U have an GUI application whose task is to get User Name & Password & validate it....however, U would like to put up few ads/JPGS on the screen .... In such a case we will associating a main thread to validate UID/PWD & daemon threads to show or change the ads.... In the above example there's no use of the GUI with just ads & without the UID/PWD screen & validator. So JVM can make the daemon thread exit. Generally Daemon threads are used when there are some not-so-important task to done along with the main task.

Timer scheduler = new Timer(isDaemon); //for creating domean threads isDaemon = true

scheduler.cancel(); //cancel the assigned tasks
scheduler.purge(); //remove the tasks in queue so that it can be garbage collected.

Friday, May 21, 2010

HttpsUrlConnection Example Code

/**
* JavaHttpsUrlConnectionReader
*
*
*@author Palaniappan S
*/
import java.io.*;
import java.net.*;
import java.security.cert.X509Certificate;
import java.util.Properties;
import javax.net.ssl.*;


public class JavaHttpsUrlConnectionReader implements javax.net.ssl.X509TrustManager {

public static void main(String[] args) throws Exception {

String output = new JavaHttpsUrlConnectionReader().doHttpsUrlConnectionAction("https://somesecuresite.com");
System.out.println("Output : "+output);

}

public String doHttpsUrlConnectionAction(String desiredUrl) throws Exception {

URL url;
int responseCode=0;
StringBuffer responseData = new StringBuffer( );
//Uncomment below lines of code if any proxy enabled in the network you are using
//Properties props = System.getProperties();
//props.put("https.proxyHost", "127.0.0.1");
//props.put("https.proxyPort", "8080");

// ###########
SSLContext sc = SSLContext.getInstance("SSLv3");
TrustManager[] tma = { new JavaHttpsUrlConnectionReader() };
sc.init(null, tma, null);
SSLSocketFactory ssf = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
// ###########
HttpsURLConnection connection = null;

String nurl = desiredUrl;
HostnameVerifier hv = new HostnameVerifier()
{
public boolean verify(String urlHostName, SSLSession session)
{
System.out.println("Warning: URL Host: " + urlHostName + " vs. "
+ session.getPeerHost());
return true;
}
};
// Now you are telling the JRE to trust any https server.
// If you know the URL that you are connecting to then this should not be a problem
// trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);

try {
url = new URL(nurl);
connection = (HttpsURLConnection) url.openConnection();
System.out.println("Conn established "+connection);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
responseCode = connection.getResponseCode();
System.out.println("response code : "+responseCode);
connection.connect();
} catch(Exception e) {
System.err.println(e);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String _ResData = null;
while ((_ResData = reader.readLine()) != null)
{
responseData.append(_ResData);
//_ResData = sckStream.readLine();
}
System.out.println("Received Data: "+ responseData.toString());

} catch (Exception e) {
System.err.println(e);
}
finally
{
// close the reader; this can throw an exception too, so
// wrap it in another try/catch block.
if (reader != null)
{
try
{
connection.disconnect();
reader.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
return responseData.toString();
}

// TrustManager Methods
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}

}

Hope the above code helped you......

Best Regards,
Palaniappan