In this blog I will show how you can make a java callout from the OSB. This might be required if you want some basic functionality which is not provided by the OSB. In this example we are making a CryptService which hashes a string.First we need to make a class which does the hashing. We are going to make use of our favourite build tool Maven. So our pom.xml should look something like this…very basic:
4.0.0 nl.redrock CryptService 1.0 junit junit 4.10 jar test
Next we are going to write our class with a static method in it. We are going to keep it quite simple as I think java callouts are not ment to do very intricate things. So our service will only take two parameters which will be used to hash a string with a certain security digest.
package nl.redrock.util; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.MessageDigest; public class CryptService { /** * Public static class which can be used to Hash strings using an algoritme * @param aDigest the digest to use....SHA-1 for example * @param aString The string to hash * @return the hased string * @throws GeneralSecurityException * @throws UnsupportedEncodingException */ public static String crypt(String aDigest,String aString) throws GeneralSecurityException, UnsupportedEncodingException { String result = null; MessageDigest md = MessageDigest.getInstance(aDigest); md.update(aString.getBytes()); byte byteData[] = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } result = sb.toString(); return result; } }
Notice that it is a static method so we don't need an instance of the class to call this method.
Next as a good programmer does....write your unit test! Something like this:
package nl.redrock.util; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import junit.framework.TestCase; public class CryptServiceTester extends TestCase { public void testCrypto() throws UnsupportedEncodingException, GeneralSecurityException{ String result = CryptService.crypt("SHA-1", "string2hash"); assertNotNull(result); } }
Run your unit test to see if works.....remember! Keep the bar green!
Ok...now we are ready to create a jar file which we can use in the OSB. Run maven by using the target mvn clean package. This should make a nice CryptService-1.0.jar for you.
Now moving to the OSB. Make a directory under your OSB project say 'Jars' for example. Copy your CryptService-1.0.jar into this directory. Now lets make use of it. Insert a 'Java Callout' action into your ProxyService flow.Select the CryptService-1.0.jar. This should now show in your bottom window the 2 parameter you can input and enter a variable name in which you want to have the result placed.
Clean up the service by returning a result conformthe datamodel, and some validation and deploy it. Let get Soap-UI out and test it....
And there you have it! I would like to emphesize though that this functionality is not recommended to call big methods with lots of business functionality behind it. This will make tracing, errorhandling and managing transaction much more difficult and untransparant.
Hi,
Thanks for sharing this !
Can you, please, provide the information in the “replace” step. I exactly need the same functionality (“replace all”) and know nothing about xpath…
Thanks in advance
As you can see in the Callout, I store my result from the Java callout in the variable cryptResult. In the xpath step you can then for example replace the body with this result in there. Something like
[code]
<CryptServiceResponse>
<Result>{data($cryptResult)}</Result>
</CryptServiceResponse>
[/code]