Thursday, December 3, 2009

Configuring an Axis2 client

I am using Axis2 (version 1.4) for performing SOAP requests.
A customer came up with a requirement to set the HTTP client to use a proxy.

I found the property I need to change in axis2 configuration in the axis2 documentation but I couldn't find documentation on where to place these configuration elements, only some obscure references to axis2.xml.

I configured Axis2 logging to get a better clue of what is used, using commons-logging and setting the log4j.properties to output everything (level ALL).
In the log I understood where Axis2 get the configuration from – it reads it from org/apache/axis2/deployment/axis2_default.xml
I found this file inside axis2-kernel-1.2.jar.
I extracted the file and configured a proxy for the "http" transportSender elements. Then I put the modified file in the classpath and this time the HTTP request went through my proxy.

Great, problem solved, but can I change the location of the file?

I started reading the code in http://grepcode.com (very useful, having all the sources online).
You can set the name and location of the configuration file using JVM properties, if you don’t do so then the default is used.
The JVM property "axis2.repo" is used to set the repository, and "axis2.xml" is used to set the configuration file name.
If you set the repository and not the file name then the expected file name is "axis2.xml".
See http://grepcode.com/file/repo1.maven.org/maven2/org.apache.axis2/axis2-kernel/1.4/org/apache/axis2/deployment/FileSystemConfigurator.java#FileSystemConfigurator.%3Cinit%3E%28java.lang.String,java.lang.String%29

Finally, if the JVM properties are not set then the default configuration resource is used org/apache/axis2/deployment/axis2_default.xml, see
http://grepcode.com/file/repo1.maven.org/maven2/org.apache.axis2/axis2-kernel/1.4/org/apache/axis2/deployment/FileSystemConfigurator.java#FileSystemConfigurator.getAxisConfiguration%28%29

Oded

Tuesday, November 3, 2009

How to run an external processes from a Java program

A QA engineers recently asked for my help invoking an external process from a Java program.
I told him "use java.lang.Runtime.exec" (link opens in a new tab).
It's been a long time since I used it, the opportunity rarely rises.
He struggled with the API, and couldn't understand how to write the output of the process to his Java console.
So I took a longer look in the API and sent this back as an example:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessRunner {

public static void main( String[] args ) throws Exception {
execute( "cmd.exe /c dir" );
execute( "cmd.exe /c java1" );
execute( "java -version" );
execute( "java -versiond" );
}

public static void execute( String cmd ) throws Exception {
Process p = Runtime.getRuntime().exec( cmd );

OutputReader stdout = new OutputReader( p.getInputStream() );
Thread t1 = new Thread( stdout );
t1.start();

OutputReader stderr = new OutputReader( p.getErrorStream() );
Thread t2 = new Thread( stderr );
t2.start();

int status = p.waitFor();
System.out.println( "status " + status );
System.out.println();
}

private static class OutputReader implements Runnable {
InputStream is;
public OutputReader( InputStream is ) {
this.is = is;
}

public void run() {
BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
try {
String line = null;
while ( ( line = reader.readLine() ) != null ) {
System.out.println( line );
}
} catch ( IOException e ) {
e.printStackTrace();
}
try
{
reader.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
}

Looks cumbersome, but then many mundane operations in Java feel the same (e.g. read from a file).

Wednesday, October 21, 2009

Using Spring LDAP to authenticate a user and verify group membership in Active Directory

Today I needed to authenticate a user and verify he belongs to a specific group in one step (in Active Directory).
I am using Spring LDAP, Spring Security 2.0.4.
After a few hours of trial and error I understood the meaning of the "searchFilter" in FilterBasedLdapUserSearch.
I can verify the sAMAccountName and the group membership using the filter like this:

FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch(
"OU=Users,DC=mycompany,DC=com",
"(&(objectCategory=user)(objectClass=person)(sAMAccountName={0})" +
"(memberof:=CN=MyGroup,OU=Users,DC=mycompany,DC=com)" +
")", ctx );

Thursday, August 27, 2009

Why are my HTTP cookies rejected by IE?

One of our customers had a problem with HTTP session management on his Java Web Server when using IE. It worked fine with FireFox.

We saw that the server returned a session ID cookie, but the session ID was not sent by IE in the following HTTP requests.

I found out that cookies are blocked in IE if the server name contains characters that are not supported by Domain Name System (DNS). For example, you cannot use underscore characters (_) in the server name. This behavior is by design.

This issue is mostly relevant for Intranet sites; most DNS systems will prohibit use of underscores in hostnames due to the LDH rule.