View Javadoc

1   /*
2    *  Copyright (c) 2008 Rodrigo Ruiz
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  package org.apache.axis.message.addressing.handler;
17  
18  import org.apache.axis.AxisFault;
19  import org.apache.axis.Handler;
20  import org.apache.axis.Message;
21  import org.apache.axis.MessageContext;
22  import org.apache.axis.message.addressing.AddressingHeaders;
23  import org.apache.axis.message.addressing.Constants;
24  import org.apache.axis.message.addressing.EndpointReference;
25  import org.apache.ws.addressing.handler.ServerSideAddressingHandler;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  import javax.xml.namespace.QName;
30  import javax.xml.rpc.Call;
31  import javax.xml.rpc.JAXRPCException;
32  import javax.xml.rpc.Service;
33  import javax.xml.rpc.ServiceException;
34  import javax.xml.rpc.handler.soap.SOAPMessageContext;
35  import javax.xml.soap.SOAPBody;
36  import javax.xml.soap.SOAPException;
37  
38  import java.io.IOException;
39  import java.util.Hashtable;
40  import java.util.List;
41  
42  /**
43   * Axis-specific server-side WS-Addressing handler.
44   *
45   * @author Davanum Srinivas
46   * @author Ian P. Springer
47   * @version $Revision: 14 $
48   */
49  public class AxisServerSideAddressingHandler extends ServerSideAddressingHandler
50    implements Handler {
51  
52    /**
53     * <code>serialVersionUID</code> attribute.
54     */
55    private static final long serialVersionUID = 7912643926095186786L;
56  
57    /**
58     * Wrapped handler.
59     */
60    private GenericAxisHandler axisHelperHandler = new GenericAxisHandler(this);
61  
62    /**
63     * {@inheritDoc}
64     */
65    public void init() {
66      axisHelperHandler.init();
67    }
68  
69    /**
70     * {@inheritDoc}
71     */
72    public void cleanup() {
73      axisHelperHandler.cleanup();
74    }
75  
76    /**
77     * {@inheritDoc}
78     */
79    public boolean canHandleBlock(QName qname) {
80      return (axisHelperHandler.canHandleBlock(qname));
81    }
82  
83    /**
84     * {@inheritDoc}
85     */
86    public void setOption(String name, Object value) {
87      axisHelperHandler.setOption(name, value);
88    }
89  
90    /**
91     * Set a default value for the given option:
92     * if the option is not already set, then set it.
93     * if the option is already set, then do not set it.
94     * <p>
95     * If this is called multiple times, the first with a non-null value
96     * if 'value' will set the default, remaining calls will be ignored.
97     *
98     * @param name  Option name
99     * @param value Default value
100    * @return true if value set (by this call), otherwise false;
101    */
102   public boolean setOptionDefault(String name, Object value) {
103     return axisHelperHandler.setOptionDefault(name, value);
104   }
105 
106   /**
107    * {@inheritDoc}
108    */
109   public Object getOption(String name) {
110     return axisHelperHandler.getOption(name);
111   }
112 
113   /**
114    * {@inheritDoc}
115    */
116   public Hashtable<?, ?> getOptions() {
117     return axisHelperHandler.getOptions();
118   }
119 
120   /**
121    * {@inheritDoc}
122    */
123   public void setOptions(Hashtable opts) {
124     axisHelperHandler.setOptions(opts);
125   }
126 
127   /**
128    * {@inheritDoc}
129    */
130   public void setName(String name) {
131     axisHelperHandler.setName(name);
132   }
133 
134   /**
135    * {@inheritDoc}
136    */
137   public String getName() {
138     return axisHelperHandler.getName();
139   }
140 
141   /**
142    * {@inheritDoc}
143    */
144   public Element getDeploymentData(Document doc) {
145     return axisHelperHandler.getDeploymentData(doc);
146   }
147 
148   /**
149    * {@inheritDoc}
150    */
151   public List<?> getUnderstoodHeaders() {
152     return axisHelperHandler.getUnderstoodHeaders();
153   }
154 
155   /**
156    * {@inheritDoc}
157    */
158   public void generateWSDL(MessageContext msgContext) throws AxisFault {
159     axisHelperHandler.generateWSDL(msgContext);
160   }
161 
162   /**
163    * {@inheritDoc}
164    */
165   public void invoke(MessageContext msgContext) throws AxisFault {
166     axisHelperHandler.invoke(msgContext);
167   }
168 
169   /**
170    * {@inheritDoc}
171    */
172   public void onFault(MessageContext msgContext) {
173     axisHelperHandler.onFault(msgContext);
174   }
175 
176   /**
177    * Use the UUID generator that comes with Axis.
178    *
179    * @return a UUID
180    */
181   protected String generateUUId() {
182     return axisHelperHandler.generateUUId();
183   }
184 
185   /**
186    * {@inheritDoc}
187    */
188   protected void forwardMessage(SOAPMessageContext soapMsgContext,
189       EndpointReference endpointRef) {
190     // TODO: not sure if overriding this method is even necessary.
191     try {
192       Service service = getService(soapMsgContext);
193       Call call = service.createCall();
194       call.setTargetEndpointAddress(endpointRef.getAddress().toString());
195       ((org.apache.axis.client.Call) call).setRequestMessage((Message) soapMsgContext
196           .getMessage());
197       AddressingHeaders addrHeaders = buildAddressingHeadersForMessageForward(
198           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()
203       .getBody();
204       removeAllChildElements(responseBody);
205     } catch (IOException e) {
206       throw new JAXRPCException("Failed to forward SOAP message", e);
207     } catch (ServiceException e) {
208       throw new JAXRPCException("Failed to forward SOAP message", e);
209     } catch (SOAPException e) {
210       throw new JAXRPCException("Failed to forward SOAP message", e);
211     }
212 
213   }
214 
215   /**
216    * {@inheritDoc}
217    */
218   @Override
219   protected void setTargetService(SOAPMessageContext soapMsgContext,
220     AddressingHeaders headers) throws Exception {
221     org.apache.axis.MessageContext axisMsgContext
222       = (org.apache.axis.MessageContext) soapMsgContext;
223 
224     // TODO: why is the below code necessary? - add comments
225     if (axisMsgContext.getService() == null
226         && axisMsgContext.getTargetService() == null) {
227       axisMsgContext.setTargetService(getTargetServiceName(headers));
228       // This is to ensure the SOAPEnvelope is rebuilt with the right type mappings and
229       // MessageContext is set with the right properties.
230       if (axisMsgContext.getTargetService() != null) {
231         ((Message) axisMsgContext.getMessage()).getSOAPPartAsString();
232       }
233     }
234   }
235 
236   /**
237    * AddressingHeaders factory method.
238    *
239    * @param soapMsgContext Context information
240    * @param endpointRef    EPR
241    * @return Headers container instance
242    */
243   private AddressingHeaders buildAddressingHeadersForMessageForward(
244       SOAPMessageContext soapMsgContext, EndpointReference endpointRef) {
245     AddressingHeaders headers = null;
246     if (soapMsgContext != null) {
247       headers = (AddressingHeaders) soapMsgContext
248       .getProperty(Constants.ENV_ADDRESSING_RESPONSE_HEADERS);
249     }
250     if (headers == null) {
251       headers = new AddressingHeaders(endpointRef);
252     }
253     return headers;
254   }
255 
256 }