2012-09-19 55 views
0

我已經爲我需要使用的SOAP Web服務提供了wsdl。我已經使用wsdl在netbeans中創建webservice類。使用NetBeans生成的WebService類將ServiceAuthHeader添加到SOAP消息中

SOAP Header需要一個包含用戶名和密碼的ServiceAuthHeader。

NetBeans確實生成了一個ServiceAuthHeader類,但我不知道如何將它添加到使用生成的類發送的SOAP消息中。

我知道如何在較低層次上做到這一點,即創建一個SOAPMEssage,添加頭,連接到服務併發送它,但我從來沒有使用jws之前,在哪裏完成了基本的任務,我正在努力找出我在任何文檔或教程中添加它的位置。

的ServiceAuthHeader產生是這樣的:

package com.theservice.webservice; 

import java.util.HashMap; 
import java.util.Map; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAnyAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.namespace.QName; 


/** 
* <p>Java class for ServiceAuthHeader complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType name="ServiceAuthHeader"> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="Username" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> 
*   &lt;element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> 
*  &lt;/sequence> 
*  &lt;anyAttribute/> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "ServiceAuthHeader", propOrder = { 
"username", 
"password" 
}) 
public class ServiceAuthHeader { 

@XmlElement(name = "Username") 
protected String username; 
@XmlElement(name = "Password") 
protected String password; 
@XmlAnyAttribute 
private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

/** 
* Gets the value of the username property. 
* 
* @return 
*  possible object is 
*  {@link String } 
*  
*/ 
public String getUsername() { 
    return username; 
} 

/** 
* Sets the value of the username property. 
* 
* @param value 
*  allowed object is 
*  {@link String } 
*  
*/ 
public void setUsername(String value) { 
    this.username = value; 
} 

/** 
* Gets the value of the password property. 
* 
* @return 
*  possible object is 
*  {@link String } 
*  
*/ 
public String getPassword() { 
    return password; 
} 

/** 
* Sets the value of the password property. 
* 
* @param value 
*  allowed object is 
*  {@link String } 
*  
*/ 
public void setPassword(String value) { 
    this.password = value; 
} 

/** 
* Gets a map that contains attributes that aren't bound to any typed property on this class. 
* 
* <p> 
* the map is keyed by the name of the attribute and 
* the value is the string value of the attribute. 
* 
* the map returned by this method is live, and you can add new attribute 
* by updating the map directly. Because of this design, there's no setter. 
* 
* 
* @return 
*  always non-null 
*/ 
public Map<QName, String> getOtherAttributes() { 
    return otherAttributes; 
} 

} 

這樣我就可以使用這個succcesfully撥打服務:

private static PriceDetailRetunValue priceDetail(PriceDetailInputValue inputValue) { 
      com.theservice.webservice.WebService service = new com.theservice.webservice.WebService(); 
     com.theservice.webservice.WebServiceSoap port = service.getWebServiceSoap12(); 
     return port.priceDetail(inputValue); 
} 

,我可以解析響應,這當然會告訴我,我需要給證書。

那麼我怎樣才能得到實際SOAP Header消息的句柄,以便能夠添加ServiceAuthHeader?我一直在研究創建的WebService的方法,並發現您可以獲取請求上下文,並且我已經看到如何將憑證添加到http請求標頭中,但是我沒有找到可以添加到的任何位置SOAPMEssage。

任何幫助,將不勝感激。謝謝。

+0

找到答案http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client – iainmac999

+0

你可以[回答你自己的問題問題](http://meta.stackexchange.com/q/17463/163188); [tag:javawebstart]是一個部署框架。 – trashgod

回答

1

我在這裏找到了答案http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client

您需要創建一個處理程序,然後告訴你的服務才能使用它。

所以我原來的方法只具有幾行添加

private static PriceDetailRetunValue priceDetail(PriceDetailInputValue inputValue) { 
     com.theservice.webservice.WebService service = new com.theservice.webservice.WebService(); 

    HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver(); 
    service.setHandlerResolver(handlerResolver); 

    com.theservice.webservice.WebServiceSoap port = service.getWebServiceSoap12(); 
    return port.priceDetail(inputValue); 
} 

和HandlerResolver和處理程序是這樣的......

package com.la.feed.xml.theservice; 

import java.util.*; 
import javax.xml.soap.Name; 
import javax.xml.soap.SOAPElement; 
import javax.xml.soap.SOAPEnvelope; 
import javax.xml.soap.SOAPFactory; 
import javax.xml.soap.SOAPHeader; 
import javax.xml.soap.SOAPHeaderElement; 
import javax.xml.ws.handler.*; 
import javax.xml.ws.handler.soap.SOAPHandler; 
import javax.xml.ws.handler.soap.SOAPMessageContext; 

public class HeaderHandler implements SOAPHandler<SOAPMessageContext> { 

@Override 
public boolean handleMessage(SOAPMessageContext smc) { 

    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

    if (outboundProperty.booleanValue()) { 

     try { 

      SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope(); 
      SOAPHeader header = envelope.addHeader(); 

      SOAPFactory soapFactory = SOAPFactory.newInstance(); 
      Name headerName = soapFactory.createName("ServiceAuthHeader", "", "http://www.interhome.com/webservice"); 
      SOAPHeaderElement headerElement = header.addHeaderElement(headerName); 

      Name username = soapFactory.createName("Username"); 
      SOAPElement usernameElement = headerElement.addChildElement(username); 
      usernameElement.addTextNode("GB1009688"); 

      Name password = soapFactory.createName("Password"); 
      SOAPElement passwordElement = headerElement.addChildElement(password); 
      passwordElement.addTextNode("verbier"); 


     } catch (Exception e) { 
     } 

    } 
    return outboundProperty; 

} 

@Override 
public Set getHeaders() { 
    return null; 
} 

@Override 
public boolean handleFault(SOAPMessageContext context) { 
    return true; 
} 

@Override 
public void close(MessageContext context) { 
} 
} 


package com.la.feed.xml.theservice; 

import java.util.*; 
import javax.xml.ws.handler.*; 

public class HeaderHandlerResolver implements HandlerResolver { 

@Override 
public List<Handler> getHandlerChain(PortInfo portInfo) { 
    List<Handler> handlerChain = new ArrayList<Handler>(); 

    HeaderHandler hh = new HeaderHandler(); 

    handlerChain.add(hh); 

    return handlerChain; 
} 
} 

它需要巧妙一點,但它做這項工作。

相關問題