View Javadoc

1   /*
2    * Copyright  2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.apache.axis.message.addressing.handler;
18  
19  import org.apache.axis.AxisFault;
20  import org.apache.axis.Handler;
21  import org.apache.axis.Message;
22  import org.apache.axis.MessageContext;
23  import org.apache.axis.message.addressing.AddressingHeaders;
24  import org.apache.axis.message.addressing.Constants;
25  import org.apache.axis.message.addressing.EndpointReference;
26  import org.apache.ws.addressing.handler.ServerSideAddressingHandler;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  import javax.xml.namespace.QName;
31  import javax.xml.rpc.Call;
32  import javax.xml.rpc.JAXRPCException;
33  import javax.xml.rpc.Service;
34  import javax.xml.rpc.handler.soap.SOAPMessageContext;
35  import javax.xml.soap.SOAPBody;
36  import java.util.Hashtable;
37  import java.util.List;
38  
39  /***
40   * Axis-specific server-side WS-Addressing handler.
41   *
42   * @author Davanum Srinivas (dims@yahoo.com)
43   * @author Ian P. Springer
44   */
45  public class AxisServerSideAddressingHandler extends ServerSideAddressingHandler implements Handler
46  {
47  
48      private GenericAxisHandler axisHelperHandler = new GenericAxisHandler( this );
49  
50      /***
51       * @see org.apache.axis.Handler#init()
52       */
53      public void init()
54      {
55          axisHelperHandler.init();
56      }
57  
58      /***
59       * @see org.apache.axis.Handler#cleanup()
60       */
61      public void cleanup()
62      {
63          axisHelperHandler.cleanup();
64      }
65  
66      /***
67       * @see org.apache.axis.Handler#canHandleBlock(javax.xml.namespace.QName)
68       */
69      public boolean canHandleBlock( QName qname )
70      {
71          return ( axisHelperHandler.canHandleBlock( qname ) );
72      }
73  
74      /***
75       * @see org.apache.axis.Handler#setOption(java.lang.String, java.lang.Object)
76       */
77      public void setOption( String name,
78                             Object value )
79      {
80          axisHelperHandler.setOption( name, value );
81      }
82  
83      /***
84       * Set a default value for the given option:
85       * if the option is not already set, then set it.
86       * if the option is already set, then do not set it.
87       * <p/>
88       * If this is called multiple times, the first with a non-null value
89       * if 'value' will set the default, remaining calls will be ignored.
90       * <p/>
91       * Returns true if value set (by this call), otherwise false;
92       */
93      public boolean setOptionDefault( String name,
94                                       Object value )
95      {
96          return axisHelperHandler.setOptionDefault( name, value );
97      }
98  
99      /***
100      * @see org.apache.axis.Handler#getOption(java.lang.String)
101      */
102     public Object getOption( String name )
103     {
104         return axisHelperHandler.getOption( name );
105     }
106 
107     /***
108      * @see org.apache.axis.Handler#getOptions()
109      */
110     public Hashtable getOptions()
111     {
112         return axisHelperHandler.getOptions();
113     }
114 
115     /***
116      * @see org.apache.axis.Handler#setOptions(java.util.Hashtable)
117      */
118     public void setOptions( Hashtable opts )
119     {
120         axisHelperHandler.setOptions( opts );
121     }
122 
123     /***
124      * @see org.apache.axis.Handler#setName(java.lang.String)
125      */
126     public void setName( String name )
127     {
128         axisHelperHandler.setName( name );
129     }
130 
131     /***
132      * @see org.apache.axis.Handler#getName()
133      */
134     public String getName()
135     {
136         return axisHelperHandler.getName();
137     }
138 
139     /***
140      * @see org.apache.axis.Handler#getDeploymentData(org.w3c.dom.Document)
141      */
142     public Element getDeploymentData( Document doc )
143     {
144         return axisHelperHandler.getDeploymentData( doc );
145     }
146 
147     /***
148      * @see org.apache.axis.Handler#getUnderstoodHeaders()
149      */
150     public List getUnderstoodHeaders()
151     {
152         return axisHelperHandler.getUnderstoodHeaders();
153     }
154 
155     /***
156      * @see org.apache.axis.Handler#generateWSDL(org.apache.axis.MessageContext)
157      */
158     public void generateWSDL( MessageContext msgContext ) throws AxisFault
159     {
160         axisHelperHandler.generateWSDL( msgContext );
161     }
162 
163     /***
164      * @see org.apache.axis.Handler#invoke(org.apache.axis.MessageContext)
165      */
166     public void invoke( MessageContext msgContext ) throws AxisFault
167     {
168         axisHelperHandler.invoke( msgContext );
169     }
170 
171     /***
172      * @see org.apache.axis.Handler#onFault(org.apache.axis.MessageContext)
173      */
174     public void onFault( MessageContext msgContext )
175     {
176         axisHelperHandler.onFault( msgContext );
177     }
178 
179     /***
180      * Use the UUID generator that comes with Axis.
181      *
182      * @return a UUID
183      */
184     protected String generateUUId()
185     {
186         return axisHelperHandler.generateUUId();
187     }
188 
189     // TODO: not sure if overriding this method is even necessary.
190     protected void forwardMessage( SOAPMessageContext soapMsgContext, EndpointReference endpointRef )
191     {
192         try
193         {
194             Service service = getService( soapMsgContext );
195             Call call = service.createCall();
196             call.setTargetEndpointAddress( endpointRef.getAddress().toString() );
197             ( (org.apache.axis.client.Call) call ).setRequestMessage( (Message) soapMsgContext.getMessage() );
198             AddressingHeaders addrHeaders = buildAddressingHeadersForMessageForward( soapMsgContext, endpointRef );
199             call.setProperty( Constants.ENV_ADDRESSING_REQUEST_HEADERS, addrHeaders );
200             configureCall( call, soapMsgContext );
201             call.invoke( new Object[0] );
202             SOAPBody responseBody = soapMsgContext.getMessage().getSOAPPart().getEnvelope().getBody();
203             removeAllChildElements( responseBody );
204         }
205         catch ( Exception e )
206         {
207             throw new JAXRPCException( "Failed to forward SOAP message." );
208         }
209 
210     }
211 
212     protected void setTargetService( SOAPMessageContext soapMsgContext, AddressingHeaders headers )
213             throws Exception
214     {
215         org.apache.axis.MessageContext axisMsgContext = (org.apache.axis.MessageContext) soapMsgContext;
216         // TODO: why is the below code necessary? - add comments
217         if ( axisMsgContext.getService() == null &&
218                 axisMsgContext.getTargetService() == null )
219         {
220             axisMsgContext.setTargetService( getTargetServiceName( headers ) );
221             // This is to ensure the SOAPEnvelope is rebuilt with the right type mappings and
222             // MessageContext is set with the right properties.
223             if ( axisMsgContext.getTargetService() != null )
224             {
225                 ( (Message) axisMsgContext.getMessage() ).getSOAPPartAsString();
226             }
227         }
228     }
229 
230     private AddressingHeaders buildAddressingHeadersForMessageForward( SOAPMessageContext soapMsgContext, EndpointReference endpointRef )
231     {
232         AddressingHeaders headers = null;
233         if ( soapMsgContext != null )
234         {
235             headers = (AddressingHeaders) soapMsgContext.getProperty( Constants.ENV_ADDRESSING_RESPONSE_HEADERS );
236         }
237         if ( headers == null )
238         {
239             headers = new AddressingHeaders( endpointRef );
240         }
241         return headers;
242     }
243 
244 }