2

我正在使用 SOAP Web 服务,我需要将信息从作为我的主客户端类的应用程序级别传递到客户端处理程序级别以进行进一步评估。我在下面列出了我的客户和我的客户处理程序。我想扫描来自用户的值并将其存储在位于处理程序中的“discountVal”变量中,以便可以将文本节点添加到 SOAPHeader。

客户:

/*
 * 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 将是一种方法,但我不确定从哪里开始。

4

1 回答 1

6

你是对的,使用 MessageContext 是将信息从客户端实现传递到处理程序的一种方式。这是如何做到的:

1)向客户端类添加

((BindingProvider)port).getRequestContext().put("discountID", discount);

2)然后到处理程序

if(context.containsKey("discountID")){
     discountVal = context.get("discountID");
}

就这样。

于 2013-01-20T17:45:46.607 回答