Books I Like

Showing posts with label How to. Show all posts
Showing posts with label How to. Show all posts

Tuesday, October 13, 2009

How To : A Simple Generic SOAP Test Client written in Java

There are lots of libraries which are available to consume/generate a web service, like Spring Webservices, XFire, Apache Axis, to name a few.

While each one has its own pro/cons (that we can leave for a separate blog entry), many a times, one needs a simple (1 or 2) methods of a webservice calls to make. She may or may not care about the response, as long as the response is not leading to error.

For testing purposes, many use soapui, which is a wonderful tool. You may use it to point towards your soap end point, and it will generate request stub for you . Just fill in few params required by the method you are calling.

Here, I am ind of demonstrating the same thing, using a simple java program.
I am not using any WSDL2JAVA tool kind of stuff, to generate the stubs. [Please note, these stubs help you with the xml to java bindings, so if you are planning to use many methods, or you have sufficient time and want to do it right way, please follow that way].

Here is the code, you may use to call any end point. please note, it has some security headers just to show you. You may/may not need it.



package example;

import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.URL;


public class TestClient {
public static String wsURL =
"{YOUR_END_POINT}?WSDL";

public static String mySoapRequest =
new StringBuilder().
append("\n").
append(" \n").
#If your soap end point doesn't need security just remove these elements, or if it implements other sceurity standards, replace here
append(" \n").
append(" \n").
append(" \n").
append(" testusername\n").
append(" testpassword\n").
append("
\n").
append("
\n").
append("
\n").
append("
\n").
append(" \n").
#Here goes your request body
.......................
.......................
append("
\n").
append("
").toString();

public static void test() throws Exception {
InputStream input = new ByteArrayInputStream(mySoapRequest.getBytes());
Service service = new Service();
Call call = (Call) service.createCall();
SOAPEnvelope soapEnvelope = new SOAPEnvelope(input);
call.setTargetEndpointAddress(new URL(wsURL));
call.setUseSOAPAction(true);
System.out.println("Request:\n"+ mySoapRequest);
soapEnvelope = call.invoke(soapEnvelope);
System.out.println("Response:\n" + soapEnvelope.toString());
}

public static void main(String args[]) throws Exception {
TestClient callClient = new TestClient();
callClient.test();
}
}

Monday, October 5, 2009

Recipe : How to get property value in a Spring Application

If you work with Spring, I am sure you might have come across the scenario when you need to access the value of a property key in your application.

The class "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" is normally used to resolve the runtime key/value pairs. However, if you need to use a property value in your bean, it doesn't expose that to be read.

Here is a very simple work-around :-

1. Extend the PropertyPlaceholderConfigurer


public class OpenPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

Properties mergedProperties;

public Properties getMergedProperties() throws IOException {
if (mergedProperties == null) {

mergedProperties = mergeProperties();

}
return mergedProperties;

}
}



2. Configure it to be used instead of PropertyPlaceholderConfigurer

  
<bean id="propertyConfigurer" class="com.mypackage.OpenPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:${YOUR_ENV}.properties</value>
</list>
</property>
</bean>


3. Inject this "propertyConfigurer" in your defined bean.

4. Get the key value propertyConfigurer.getMergedProperties().getProperty("your.prop.key")

I am sure, there could be some better ways. Let me know, if you know one.

Credit goes to Mat for this recipe.

Get at Amazon