- import java.lang.reflect.Proxy;
- import net.BookServiceClient;
- import net.BookServicePortType;
- import org.codehaus.xfire.client.XFireProxy;
- public class XFireClient2 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- BookServiceClient client = new BookServiceClient();
- BookServicePortType portType = client.getBookServiceHttpPort();
- XFireProxy proxy = (XFireProxy) Proxy.getInvocationHandler(portType);
- TestOutHandler handler = new TestOutHandler();
- proxy.getClient().addOutHandler(handler);
- org.codehaus.xfire.demo.Book book = portType.getBooks();
- System.out.println(book.getTitle().getValue());
- }
- }
- import org.codehaus.xfire.MessageContext;
- import org.codehaus.xfire.handler.AbstractHandler;
- import org.jdom.Element;
- import org.jdom.Namespace;
- public class TestOutHandler extends AbstractHandler {
- private static final String VERSION_NS = "http://xfire.codehaus.org/Book";
- public void invoke(MessageContext context) throws Exception {
- // TODO Auto-generated method stub
- Namespace namespace = Namespace.getNamespace(VERSION_NS);
- Element
- header = new Element("Header", namespace);
- Element version = new Element("version", namespace);
- version.addContent("2.0");
- header.addContent(version);
- context.getOutMessage().setHeader(header);
- }
- }
服务器端的代码为:
- package org.codehaus.xfire.demo.handlers;
- import org.codehaus.xfire.MessageContext;
- import org.codehaus.xfire.XFireRuntimeException;
- import org.codehaus.xfire.handler.AbstractHandler;
- import org.jdom.Element;
- import org.jdom.Namespace;
- /**
- * @author <a href="mailto:tsztelak@gmail.com">Tomasz Sztelak</a>
- *
- * Retrive service version from SOAP header.
- */
- public class CheckVersionHandler extends AbstractHandler {
- private static final String VERSION_TAG = "version";
- private static final String VERSION_NS = "http://xfire.codehaus.org/Book";
- public void invoke(MessageContext ctx) throws Exception {
- Element header = ctx.getInMessage().getHeader();
- if (header == null) {
- throw new XFireRuntimeException("Missing SOAP header");
- }
- Element version = header.getChild(VERSION_TAG, Namespace
- .getNamespace(VERSION_NS));
- if (version == null) {
- throw new XFireRuntimeException("Missing version header");
- }
- ctx.setProperty("ServiceVersion", version.getText());
- }
- }