Spring WS 1.0 using Castor 1.1.1 marshalling (force UTF-8 encoding)

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;

   * 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();

    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();
              StringBuilder sm = new StringBuilder(os.toString());
              ByteArrayInputStream is = new ByteArrayInputStream(sm.toString().getBytes("UTF-8"));
              SaajSoapMessage wsm = (SaajSoapMessage)getWebServiceTemplate().getMessageFactory().createWebServiceMessage(is);
            } catch (Exception e) {

            // Do WSS signature
            SOAPMessage saajMessage = saajSoapMessage.getSaajMessage();
            try {
              ProcessingContext context = new ProcessingContext();
              SOAPMessage securedMessage = cprocessor.secureOutboundMessage(context);
            } 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) {

    // response
    return ret;



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s