Monday, June 14, 2010

Using Eclipse WTP for SOAP based Web Services

I have started using Apache Axis2 as a standard engine for SOAP based web-services. To make my job easy(super easy) I am utilizing the WTP project for Eclipse IDE.
While I am still at a learning stage, I came across this tutorial that gave me a good understanding of the platform and explained on a quick example of building and deploying a SOAP based web-service. It also created a small test client for the Axis2 web service runtime.

I had a couple of issues while setting up the service, few libraries were missing and an entry in the web.xml had to be changed. Here are my notes:
1. If the server throws NoClassDef error for "AxisAdminServlet", go to web.xml and change the entry to point to org.apache.axis2.webapp.AxisAdminServlet.
2. You need to add httpcore-x.x.jar, XMLBean's jars and JAX-WS's jars if they are not already present in your CLASSPATH.

To create a Axis web service runtime, you can follow this tutorial.

Notice the difference in the stub and skeleton classes that both the runtimes create.

Friday, June 4, 2010

Passing Objects in Javascript methods

I recently came across a requirement where I needed to call Javascript methods embedded in a flash module. The flash module was a chart from Fusion charts, and I wanted to perform certain client side actions when user clicked on these charts. Based on certain conditions and where the user clicked, I had to vary the number of parameters I pass to the javascript method.
I was looking for possible solutions and I figured out that the best way to achieve this was passing JSON objects as parameters. Here is how i did it:

link='javascript:jsMethod(%26apos;{\"param1\":\"param1Val\",\"param2\":\"param2Val\",\"param3\":\"param3Val\",\"param4\":\"param4Val\"}%26apos;)'

Note: I needed to escape single quote(') with %26apos; and double quotes(") with \". One can also use ' for single quote, and \x22 for double.

This approach of using JSON to pass variable number of parameters can be used in a variety of ways and keeps the method signature consistent.

This works for me, in case you have a better solution, please do write a comment.

Tuesday, June 1, 2010

Ext 2.2 RadioGroup getValue()/setValue()

There seems to be issues with the getValue() and setValue() methods of the Ext.form.RadioGroup class in the 2.2 API version. The methods do not seem to work the way they are supposed to. If you have to use these methods, you need to override them. Here's the override script and some sample code on how to use the RadioGroup class.

Override code:
Ext.override(Ext.form.RadioGroup, {
  getName: function(){
    return this.items.first().name;
  },
  getValue: function(){
    var v;
    if (this.rendered) {
      this.items.each(function(item){
      if (!item.getValue())
        return true;
      v = item.getRawValue();
      return false;
      });
    }
    else {
      for (var k in this.items) {
        if (this.items[k].checked) {
          v = this.items[k].inputValue;
          break;
        }
      }
    }
    return v;
  },
  setValue: function(v){
    if (this.rendered){
      this.items.each(function(item){
        item.setValue(item.getRawValue() == v);
      });
    }
    else {
      for (var k in this.items) {
        this.items[k].checked = this.items[k].inputValue == v;
      }
    }
  }
});

RadioGroup:
{
  xtype: 'radiogroup',
  fieldLabel: 'Male/Female',
  id:'rg1',
  items: [
    {boxLabel: 'Male', name: 'gender', inputValue: 1},
    {boxLabel: 'Female', name: 'gender' ,inputValue: 2, checked: true}
  ]
}

Ext.getCmp('rg1').setValue(1);
Ext.getCmp('rg1').getValue();

Hope it helps!!

Thursday, May 27, 2010

Using Oracle to search text documents

We can use Oracle*Text utility to search through large amounts of text stored in documents like MS-Word, MS-xls , PDF, XML, HTML, RTF or txt.
Oracle Text(also known as interMedia Text and ConText) is an extensive full text indexing technology allowing you to parse through a large text column and efficiently query free text.

Oracle Text has several index types. However to search large amounts of text, we need to use the CONTEXT Index.

To achieve the search capability, I store the documents in a BLOB column. Using a CLOB is preferable if only plain text documents are being used. Lets assume our table is named "USER_DOCUMENTS" and it has a BLOB column "DOC" that stores the actual file. To create the CONTEXT type index on the "DOC" column we need to:

CREATE INDEX user_documents_index ON user_documents(doc) INDEXTYPE IS CTXSYS.CONTEXT;

Now to perform free text search on the documents we need to use the CONTAINS clause. The Oracle's basic syntax is:

CONTAINS(
[schema.]column,
text_query VARCHAR2
[,label NUMBER])
RETURN NUMBER;

[schema.]column:
Specify the text column to be searched on. This column must have a Text index associated with it.

text_query:
the query expression that defines your search in column.

label:
Optionally specify the label that identifies the score generated by the CONTAINS operator.

Returns:
For each row selected, CONTAINS returns a number between 0 and 100 that indicates how relevant the document row is to the query. The number 0 means that Oracle Text found no matches in the row.

Note:
The CONTAINS operator must be followed by an expression such as > 0, which specifies that the score value calculated must be greater than zero for the row to be selected.

For example, to search for 'oracle' in all the docs of the user_documents table, we will fire:

SELECT SCORE(1), ud.* from user_documents ud WHERE CONTAINS(doc, 'oracle', 1) > 0 ORDER BY SCORE(1) DESC;

The query will return a list of all the docs having the keyword 'oracle' and sort them based on their relevance.

Also, remember to rebuild the index everytime you add docs to the table during development and testing phase. For production systems, based on your load and usage, decide an optimal build schedule.

Hope this helps!

Friday, May 21, 2010

Java Keytool - Self-Signed SSL Certificate

Keytool is a key and certificate management utility.
In this post, I list down a few useful commands that will help you generate self-signed certificates for development purposes. For production systems, do not use keytool to generate certificates. Use those provided by CAs like VeriSign or thawte. Self-signed certificates are challenged by browsers and that creates a poor user interaction every time they go to your site.

Definitions:
Keystore - A keystore is a database (usually a file) that can contain trusted certificates and combinations of private keys with their corresponding certficiates.
Alias - All keystore entries (key and trusted certificate entries) are accessed via unique aliases
cacerts - The "cacerts" file represents a system-wide keystore with CA certificates. It resides in the security properties directory, $JAVA_HOME/jre/lib/security
Certificate - A certificate (also known as a public-key certificate) is a digitally signed statement from one entity (the issuer), saying that the public key (and some other information) of another entity (the subject) has some specific value.

Prerequisites:
-> JDK 1.3+ installed and JAVA_HOME set to the directory where you have installed JDK

Notes:
-> For this example, lets call our alias "my_alias"
-> For this example, lets call our certificate "my_cert.crt"

Go to $JAVA_HOME/bin directory

# Generate the keystore file (the following command will ask few questions, at the end it will generate a .keystore file - changeit is the password, you can put whatever you want to, just dont forget it :))
> keytool -genkey -alias my_alias -keypass changeit -keyalg RSA

# Export the .keystore file to generate the certificate (the following command will ask for the password and then generate a my_cert.crt file)
> keytool -export -alias my_alias -keypass changeit -file my_cert.crt

At this stage we have the certificate file ready, we can use this certificate file and point our server's trustedFile source to it. However for certain services like CAS, the certificate needs to be imported in JDK trusted certificate file - cacerts.

# Import the certificate file to the cacerts file (the following command will ask for the password and confirm the certificate you are trying to import)
> keytool -import -file my_cert.crt -keypass changeit -keystore $JAVA_HOME/jre/lib/security/cacerts

Other useful keytool commands

# List all .keystore certificates
> keytool -list -v

# List one .keystore certificate
> keytool -list -v -alias my_alias

# List all .keystore certificates in a specific keystore
> keytool -list -keystore

# Remove certificate from cacerts file
> keytool -delete -alias my_alias -keystore $JAVA_HOME/jre/lib/security/cacerts

#Remove a certificate from the default .keystore
> keytool -delete -alias my_alias

As always there is "man" help available!

Hope this helps!

Thursday, May 20, 2010

Javascript Cookies

In one of my earlier posts I had handled cookies in JSP. Here are a few methods to handle cookies using Javascript. I have written three methods that will do all that you need to manage cookies
1. Write/Create cookie
2. Read Cookie
3. Delete Cookie (Write the cookie with a prior date)





Hopefully, this helps.

Monday, April 19, 2010

Parse File Using Scanner - Java Implementation

Code snippet to parse a file using java.util.Scanner class.





Usage: Call the tokenizeUsingScanner method and pass it the File object and the delimiter for the file.
Note: The pipe symbol has special meaning in a regex -- so you need to escape it.