2
我正在使用SOAP Web服務,我需要將信息從應用程序級別(這是我的主客戶機級別)傳遞到客戶機處理程序級別以供進一步評估。我在下面列出了我的客戶端和我的客戶端處理程序。我想掃描用戶的值並存儲在處理程序中的「discountVal」變量中,以便我可以將一個文本節點添加到SOAPHeader中。將信息從應用程序傳遞到處理程序級別SOAP
客戶:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
/**
*
* @author Jack
*/
public class Client
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
client.ItemPriceServiceService service = new client.ItemPriceServiceService();
service.setHandlerResolver(new ClientHandlerResolver());
client.ItemPriceService port = service.getItemPriceServicePort();
String discount = "25";
Scanner scan = new Scanner(System.in);
String item = scan.next();
while(!item.equals("bye"))
{
double price = getPrice(item);
System.out.println(price);
item = scan.next();
}
if(item.equals("bye"))
{
System.exit(0);
}
}
private static double getPrice(java.lang.String arg0) {
client.ItemPriceServiceService service = new client.ItemPriceServiceService();
client.ItemPriceService port = service.getItemPriceServicePort();
return port.getPrice(arg0);
}
private static class ClientHandlerResolver implements HandlerResolver {
public ClientHandlerResolver() {
}
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> hchain = new ArrayList<Handler>();
hchain.add(new clientHandler());
return hchain;
}
}
}
客戶端處理
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
import java.util.Collections;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/**
*
* @author Jack
*/
public class clientHandler implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext context)
{
Boolean outgoing = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if(outgoing){
try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
if (soapHeader == null){
soapHeader = soapEnv.addHeader();
}
//get discount value FROM CLIENT
String discountVal = "25";
QName qname = new QName("http://testOne", "discount");
SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);
soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
soapHeaderElement.addTextNode(discountVal);
soapMsg.saveChanges();
}catch(SOAPException e){
System.err.println(e);
}
}
//continue other handler chain
return true;
}
public Set<QName> getHeaders() {
return Collections.EMPTY_SET;
}
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
public void close(MessageContext context) {
}
}
任何幫助將不勝感激!我有一種感覺,使用MessageContext是一種方法,但我不確定從哪裏開始。