View Javadoc

1   /*
2    * $Id: AxisServiceProxy.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.soap.axis;
12  
13  import org.mule.config.ExceptionHelper;
14  import org.mule.config.MuleProperties;
15  import org.mule.impl.MuleMessage;
16  import org.mule.impl.RequestContext;
17  import org.mule.providers.AbstractMessageReceiver;
18  import org.mule.providers.soap.ServiceProxy;
19  import org.mule.providers.soap.axis.extras.AxisCleanAndAddProperties;
20  import org.mule.umo.UMOException;
21  import org.mule.umo.UMOExceptionPayload;
22  import org.mule.umo.UMOMessage;
23  import org.mule.umo.provider.UMOMessageAdapter;
24  
25  import java.lang.reflect.InvocationHandler;
26  import java.lang.reflect.Method;
27  import java.lang.reflect.Proxy;
28  
29  /**
30   * <code>ServiceProxy</code> is a proxy that wraps a soap endpointUri to look like
31   * a Web service. Also provides helper methods for building and describing web
32   * service interfaces in Mule.
33   */
34  
35  public class AxisServiceProxy extends ServiceProxy
36  {
37  
38      public static Object createProxy(AbstractMessageReceiver receiver, boolean synchronous, Class[] classes)
39      {
40          final ClassLoader cl = Thread.currentThread().getContextClassLoader();
41          return Proxy.newProxyInstance(cl, classes, createServiceHandler(receiver, synchronous));
42      }
43  
44      public static InvocationHandler createServiceHandler(AbstractMessageReceiver receiver, boolean synchronous)
45      {
46          return new AxisServiceHandler(receiver, synchronous);
47      }
48  
49      private static class AxisServiceHandler implements InvocationHandler
50      {
51          private AbstractMessageReceiver receiver;
52          private boolean synchronous = true;
53  
54          public AxisServiceHandler(AbstractMessageReceiver receiver, boolean synchronous)
55          {
56              this.receiver = receiver;
57              this.synchronous = synchronous;
58          }
59  
60          public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
61          {
62              UMOMessageAdapter messageAdapter = receiver.getConnector().getMessageAdapter(args);
63              messageAdapter.setProperty(MuleProperties.MULE_METHOD_PROPERTY, method);
64              
65              // add all custom headers, filter out all mule headers (such as
66              // MULE_SESSION) except
67              // for MULE_USER header. Filter out other headers like "soapMethods" and
68              // MuleProperties.MULE_METHOD_PROPERTY and "soapAction"
69              // and also filter out any http related header
70              messageAdapter.addProperties(AxisCleanAndAddProperties.cleanAndAdd(RequestContext.getEventContext()));                        
71                                     
72              UMOMessage message = receiver.routeMessage(new MuleMessage(messageAdapter), synchronous);
73              
74              if (message != null)
75              {
76                  UMOExceptionPayload wsException = message.getExceptionPayload();
77  
78                  if (wsException != null)
79                  {
80                      UMOException umoException = ExceptionHelper.getRootMuleException(wsException.getException());
81                      // if the exception has a cause, then throw only the cause
82                      if (umoException.getCause() != null)
83                      {
84                          throw umoException.getCause();
85                      }
86                      else
87                      {
88                          throw umoException;
89                      }
90                  }
91  
92                  return message.getPayload();
93              }
94              else
95              {
96                  return null;
97              }
98          }
99      }
100 }