2010-10-28 4 views
11

Come posso aggiungere intestazioni SOAP al client Spring Jax-WS?Come aggiungere intestazioni SOAP al client Spring Jax-WS?

In particolare, ho un oggetto Jaxb che vorrei aggiungere all'intestazione ma gli esempi xml sarebbero apprezzati.

Sto usando Spring's JaxWsPortProxyFactoryBean descritto here. Inoltre, sto generando il mio cliente come descritto here che sta lavorando meno le intestazioni che ho bisogno di aggiungere.

Grazie.

risposta

6

Sto ancora cercando di trovare un modo elegante per aggiungere intestazioni, ma quello che faccio come suggerito da altri è quello di utilizzare un Transformer sul WebServiceMessageCallBack(). Ecco un codice di esempio:

Non è molto elegante, considerando che questo è il WS di primavera. Non è intuitivo.

14

Un po 'più elegante (ancora è richiesto un cast di classe):

public void doWithMessage(WebServiceMessage message) { 
    try { 
     SOAPMessage soapMessage = ((SaajSoapMessage)message).getSaajMessage(); 
     SOAPHeader header = soapMessage.getSOAPHeader(); 
     SOAPHeaderElement security = header.addHeaderElement(new QName("http://schemas.xmlsoap.org/ws/2003/06/secext", "Security", "wsse")); 
     SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse"); 
     SOAPElement username = usernameToken.addChildElement("Username", "wsse"); 
     SOAPElement password = usernameToken.addChildElement("Password", "wsse"); 

     username.setTextContent(someUsername); 
     password.setTextContent(somePassword); 
    } catch (Exception e) { 
     //... handle appropriately 
    } 
} 

Nota: Questo esempio è stato testicoli con la Primavera WS 2.1.4.

5

Dopo un po 'di ricerca, ho trovato una soluzione leggermente diversa. Sto usando JAXB per il marshalling del mio payload e le possibili classi di intestazione sono state generate con JAXB dal WSDL. Nel mio caso mi rivolgo a Microsoft Reporting Services e ho passato un ExecutionID come intestazione SOAP.

public class ReportExecution2005Client extends WebServiceGatewaySupport { 

    private static final String SET_EXECUTION_PARAMETERS_ACTION = "http://schemas.microsoft.com/sqlserver/2005/06/30/reporting/reportingservices/SetExecutionParameters"; 

    private final class SoapActionExecutionIdCallback implements WebServiceMessageCallback { 

     private final String soapAction; 
     private final String executionId; 

     public SoapActionExecutionIdCallback(String soapAction, String executionId) { 
      super(); 
      this.soapAction = soapAction; 
      this.executionId = executionId; 
     } 

     @Override 
     public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException { 
      SoapMessage soapMessage = (SoapMessage) message; 
      soapMessage.setSoapAction(soapAction); 
      ExecutionHeader executionHeader = new ExecutionHeader(); 
      executionHeader.setExecutionID(executionId); 
      getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult()); 
     } 
    } 

    public void setExecutionParameters(String executionId){ 
     SetExecutionParameters request = new SetExecutionParameters(); 
     request.setParameters(new ArrayOfParameterValue()); 

     SetExecutionParametersResponse response = (SetExecutionParametersResponse) getWebServiceTemplate().marshalSendAndReceive(request, 
       new SoapActionExecutionIdCallback(
         SET_EXECUTION_PARAMETERS_ACTION, 
         executionId)); 
    } 
} 

Fondamentalmente il WebServiceGatewaySupport conosce già il Marshaller per convertire JAXB Pojos. Sto usando questo per collegare le mie classi di intestazione al SoapHeader con questa linea:

getMarshaller().marshal(executionHeader, soapMessage.getSoapHeader().getResult()); 

nel mio WebServiceMessageCallback nidificato.