使用NetBeans生成的Web Service类将服务Auth Header添加到SOAP消息

时间:2012-09-19 10:02:17

标签: java soap netbeans-7

我已经获得了一个需要使用的SOAP Web服务的wsdl。我使用wsdl在netbeans中创建webservice类。

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;
}

}

所以我可以使用它来成功调用服务:

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。

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分: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和Handler看起来像这样......

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;
}
}

需要对它进行一些细微处理,但它确实能够完成这项任务。