Pages

Thursday, July 31, 2008

Java Properties File - Some less known facts

I came across a problem using java properties file, and I think, its a good piece to go here. I am sure, majority of the developer who are new to java(even some oldies) may encounter this issue.

The properties file contents:

a0=impl1
a1:sol1=impl2

The java program is below:

public static void main(String[] args) {
        Properties properties = new Properties();
        try {//Assume, no problemo finding properties file
            properties.load(Tester.class.getClassLoader().getResourceAsStream("test.properties"));
            System.out.println(properties.getProperty("a0"));
            System.out.println(properties.getProperty("a1:sol1"));            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

Surprisingly the output of the following program is:

a0=impl1
null

When you do properties.list(System.out), I was little surprised to see:

a0=impl1
a1=sol1=impl2

The ':' has been converted to '='.

Finally, with the help of wiki I found that, the name value pairs in the java properties file can be of formats:

a. name=value (This all us know definitely know)
b. name:value c. name value

And, the solution is to escape the special character using backslash. That is in the properties file,

a0=impl1
a1\:sol1=impl2

Hope this helps someone who is surprised to see 'un-expected' output.

Tuesday, July 22, 2008

j2ssh : On authentication, removing user prompt

 

While doing an SFTP connection with j2ssh client in java, it is possible that you get a message as in:

Do you want to allow this host key? [Yes|No|Always]:

To remove this message, though not recommended, we have to give a custom implementation of AbstractKnownHostsKeyVerification class. The best possible way is to extend ConsoleKnownHostsKeyVerification and override the methods onUnknownHost or onHostKeyMismatch. And use this AlwaysAllowingConsoleKnownHostsKeyVerification instance while connecting, as in:

SshClient ssh = new SshClient();

ssh.connect(hostname,new AlwaysAllowingConsoleKnownHostsKeyVerification());

The class AlwaysAllowingConsoleHostsKeyVerification is defined below. I understand that this is like a hack in security, but, sometimes, you may like to know it anyway's.

 

import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;
import com.sshtools.j2ssh.transport.InvalidHostFileException;
import com.sshtools.j2ssh.transport.publickey.SshPublicKey;

public class AlwaysAllowingConsoleKnownHostsKeyVerification extends
        ConsoleKnownHostsKeyVerification {

    public AlwaysAllowingConsoleKnownHostsKeyVerification()
            throws InvalidHostFileException {
        super();
        // Don't not do anything else
    }

    @Override
    public void onHostKeyMismatch(String s, SshPublicKey sshpublickey,
            SshPublicKey sshpublickey1) {
        try
        {
            System.out.println("The host key supplied by " + s + " is: " + sshpublickey1.getFingerprint());
            System.out.println("The current allowed key for " + s + " is: " + sshpublickey.getFingerprint());
            System.out.println("~~~Using Custom Key verification, allowing to pass through~~~");
            allowHost(s, sshpublickey, false);
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
    }

    @Override
    public void onUnknownHost(String s, SshPublicKey sshpublickey) {
        try
        {
            System.out.println("The host " + s + " is currently unknown to the system");
            System.out.println("The host key fingerprint is: " + sshpublickey.getFingerprint());
            System.out.println("~~~Using Custom Key verification, allowing to pass through~~~");
            allowHost(s, sshpublickey, false);
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
    }

}

Thursday, July 03, 2008

Convert XML file to XML String in java

I had to find out a way to read an XML file, and convert it to java String. Below is the code snippet I used.

public String convertXMLFileToString(String fileName)
        {
          try{
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            InputStream inputStream = new FileInputStream(new File(fileName));
            org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
            StringWriter stw = new StringWriter();
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.transform(new DOMSource(doc), new StreamResult(stw));
            return stw.toString();
          }
          catch (Exception e) {
            e.printStackTrace();
          }
            return null;
        }