XFire header 代码

来源:互联网 发布:免费快递打印软件 编辑:程序博客网 时间:2024/05/20 16:45
  1. import java.lang.reflect.Proxy;
  2. import net.BookServiceClient;
  3. import net.BookServicePortType;
  4. import org.codehaus.xfire.client.XFireProxy;
  5. public class XFireClient2 {
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         BookServiceClient client = new BookServiceClient();
  11.         BookServicePortType portType = client.getBookServiceHttpPort();
  12.         XFireProxy proxy = (XFireProxy) Proxy.getInvocationHandler(portType);
  13.         TestOutHandler handler = new TestOutHandler();
  14.         proxy.getClient().addOutHandler(handler);
  15.         org.codehaus.xfire.demo.Book book = portType.getBooks();
  16.         System.out.println(book.getTitle().getValue());
  17.         
  18.     }
  19. }

 

 

 

  1. import org.codehaus.xfire.MessageContext;
  2. import org.codehaus.xfire.handler.AbstractHandler;
  3. import org.jdom.Element;
  4. import org.jdom.Namespace;
  5. public class TestOutHandler extends AbstractHandler {
  6.     private static final String VERSION_NS = "http://xfire.codehaus.org/Book";
  7.     public void invoke(MessageContext context) throws Exception {
  8.         // TODO Auto-generated method stub
  9.         Namespace namespace = Namespace.getNamespace(VERSION_NS);
  10.         Element
  11.         header = new Element("Header", namespace);
  12.         Element version = new Element("version", namespace);
  13.         version.addContent("2.0");
  14.         header.addContent(version);
  15.         context.getOutMessage().setHeader(header);
  16.         
  17.         
  18.     }
  19. }

 

 

服务器端的代码为:

 

 

  1. package org.codehaus.xfire.demo.handlers;
  2. import org.codehaus.xfire.MessageContext;
  3. import org.codehaus.xfire.XFireRuntimeException;
  4. import org.codehaus.xfire.handler.AbstractHandler;
  5. import org.jdom.Element;
  6. import org.jdom.Namespace;
  7. /**
  8.  * @author <a href="mailto:tsztelak@gmail.com">Tomasz Sztelak</a>
  9.  * 
  10.  * Retrive service version from SOAP header.
  11.  */
  12. public class CheckVersionHandler extends AbstractHandler {
  13.     private static final String VERSION_TAG = "version";
  14.     private static final String VERSION_NS = "http://xfire.codehaus.org/Book";
  15. public void invoke(MessageContext ctx) throws Exception {
  16.         
  17.         
  18.     Element header = ctx.getInMessage().getHeader();
  19.     if (header == null) {
  20.         throw new XFireRuntimeException("Missing SOAP header");
  21.         }
  22.             Element version = header.getChild(VERSION_TAG, Namespace
  23.                 .getNamespace(VERSION_NS));
  24.         if (version == null) {
  25.             throw new XFireRuntimeException("Missing version header");
  26.         }
  27.         ctx.setProperty("ServiceVersion", version.getText());
  28.         
  29.         
  30.     }
  31. }

 

 

 

原创粉丝点击