SOA / Web Services / Java

A Technology Blog

Archive for February, 2010

Access User-Defined SOAP Header Elements in Service

Posted by Vivek on February 13, 2010

There could always be a requirement that some data be sent only in SOAP Header. Usually SOAP Header contains data that is system specific and is dissimilar to the message payload that is contained in the SOAP Body. The information that a SOAP Header carries could be user-defined and generally used for tracking and logging. This data could be required by your service but is lost when a SOAP engine unmarshals the SOAP Request and provides just payload to the service i.e., applies the binding mechanism to convert an XML into a VO for the service to access elements using accessors and mutators.  To make this user-defined data available to the service, use of ThreadLocal can be advocated.

ThreadLocal allows you to have copy of variable per thread, which are subject to garbage collection when the thread goes away. Before the binding framework of SOAP engine performs any logic, a SOAP Handler can be placed to access the original SOAP request message and let the handler set the data in a threadlocal variable and use this data, which remains alive till the thread is active.

Here is the sample code which allows a bean injection and makes it available for setting values in the handler class.

<bean id=”myUserDefinedInfoBean”
class=”org.springframework.aop.framework.ProxyFactoryBean”>
<property name=”targetSource” ref=”threadLocalUserInfo” />
</bean>

<bean id=”threadLocalUserInfo”
class=”org.springframework.aop.target.ThreadLocalTargetSource”
destroy-method=”destroy”>
<property name=”targetBeanName” value=”userInfoVO” />
</bean>
<bean id=”userInfoVO”
class=”mypackage.vo.UserInfoVO” scope=”prototype” />

UserInfoVO defines setters and getters for the user defined elements . The values can be set in the handler class and can be obtained anytime till the thread remains active.

For ex, the following code snippet will set the values in SOAP Handler

private UserInfoVO userInfoBean;

….

public UserInfoVO getUserInfoBean() {
return userInfoBean;
}

public void setUserInfoBean(UserInfoVO userInfoBean) {
this.userInfoBean = userInfoBean;
}

….

getUserInfoBean()
.setUserElementValue(myVal);

….

Once the above code is executed, value set in myVal can be accessed anywhere using getUserInfoBean()
.getUserElementValue();

Posted in SOA, Web Services | Tagged: , , , , | 2 Comments »