While XMLBeans performs marshalling operations using UTF-8 encoding by default, Castor XML relies on implicit Stream encoding. So, it’s mandatory to set UTF-8 encoding when using Castor marshalling on a Web Services client.
Below a Spring WS web service client sample using XWSS 3.0 is exposed. Spring’s application context (applicationContext.xml) and XWSS security policy file (wss-client-config.xml) are required (Spring WS Client).
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.xml.soap.SOAPMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.oxm.castor.CastorMarshaller; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.client.core.WebServiceMessageCallback; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; import org.springframework.ws.soap.saaj.SaajSoapMessage; import org.springframework.ws.soap.security.xwss.XwsSecuritySecurementException; import org.springframework.ws.soap.security.xwss.callback.KeyStoreCallbackHandler; import com.sun.xml.wss.ProcessingContext; import com.sun.xml.wss.XWSSProcessor; import com.sun.xml.wss.XWSSProcessorFactory; import com.sun.xml.wss.XWSSecurityException; public class WSCastorClient extends WebServiceGatewaySupport { // XWSS supporting objects private static XWSSProcessor cprocessor; private KeyStoreCallbackHandler ksch; /** * Get the keystore * @return */ public KeyStoreCallbackHandler getKsch() { return ksch; } /** * Set the keystore * @param ksch */ public void setKsch(KeyStoreCallbackHandler ksch) { this.ksch = ksch; } /** * Default constructor. * * @param ksch Keystore * @param castorMarshaller Castor marshaller * @param castorUnmarshaller Castor unmarshaller */ public WSCastorClient(KeyStoreCallbackHandler ksch, CastorMarshaller castorMarshaller, CastorMarshaller castorUnmarshaller) { this.ksch = ksch; this.setMarshaller(castorMarshaller); this.setUnmarshaller(castorUnmarshaller); } /** * Invokes a web service using defaultUri * * @param the unnmarshalled message payload as object * * @return the object to be marshalled as response */ private Object callWs(Object input) { WebServiceTemplate wst = getWebServiceTemplate(); wst.setMarshaller(getMarshaller()); wst.setUnmarshaller(getUnmarshaller()); Object result = wst.marshalSendAndReceive(input, new WebServiceMessageCallback() { public void doWithMessage(WebServiceMessage message) throws IOException { SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message; // UTF-8 encoding try { ByteArrayOutputStream os = new ByteArrayOutputStream(); message.writeTo(os); StringBuilder sm = new StringBuilder(os.toString()); os.close(); ByteArrayInputStream is = new ByteArrayInputStream(sm.toString().getBytes("UTF-8")); SaajSoapMessage wsm = (SaajSoapMessage)getWebServiceTemplate().getMessageFactory().createWebServiceMessage(is); saajSoapMessage.setSaajMessage(wsm.getSaajMessage()); is.close(); } catch (Exception e) { e.printStackTrace(); } // Do WSS signature SOAPMessage saajMessage = saajSoapMessage.getSaajMessage(); try { ProcessingContext context = new ProcessingContext(); context.setSOAPMessage(saajMessage); SOAPMessage securedMessage = cprocessor.secureOutboundMessage(context); saajSoapMessage.setSaajMessage(securedMessage); } catch (XWSSecurityException e) { throw new XwsSecuritySecurementException(e.getMessage()); } } }); return result; } /** * Invoke web service * @param opeRequest * @param invoker * @return */ public static Object invoke(Object opeRequest, Class invoker) { // Application Context load ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml", invoker); // XWSS parameters Resource xwssConfig = new ClassPathResource("wss-client-config.xml", invoker); // XWSS client WSCastorClient xwssClient = (WSCastorClient) applicationContext.getBean("wsCastorClient"); Object ret = null; try { // XWSS initializacion XWSSProcessorFactory factory = XWSSProcessorFactory.newInstance(); cprocessor = factory.createProcessorForSecurityConfiguration(xwssConfig.getInputStream(), xwssClient.getKsch()); // Web service invocation ret = xwssClient.callWs(opeRequest); } catch (Exception e) { e.printStackTrace(); } // response return ret; } }