KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > axis2 > listener > soap > Axis2ReceiverUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: Axis2ReceiverUtil.java 154 19 mai 2006 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.axis2.listener.soap;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.jbi.messaging.DeliveryChannel;
28 import javax.jbi.messaging.InOnly;
29 import javax.jbi.messaging.InOut;
30 import javax.jbi.messaging.MessageExchange;
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35
36 import org.apache.axiom.om.OMElement;
37 import org.apache.axiom.om.OMNamespace;
38 import org.apache.axiom.soap.SOAPBody;
39 import org.apache.axiom.soap.SOAPEnvelope;
40 import org.apache.axis2.context.MessageContext;
41 import org.apache.axis2.description.AxisService;
42
43 import org.objectweb.petals.binding.axis2.SOAPHelper;
44 import org.objectweb.petals.component.common.util.SourceHelper;
45
46 /**
47  *
48  * @author ofabre - EBM Websourcing
49  *
50  */

51 public class Axis2ReceiverUtil {
52
53     /**
54      * Create a new Source object containing the axis message Soap envelope
55      *
56      * @param message
57      * the axis message
58      * @return the source content
59      */

60     public Source JavaDoc createSourceContent(MessageContext message) throws Exception JavaDoc {
61         String JavaDoc source = getBodyElement(message);
62         return SourceHelper.createSource(source);
63     }
64
65     /**
66      * Create a new String containing the normalized message content
67      *
68      * @param message
69      * the normalized message
70      * @return the String content
71      * @throws Exception
72      */

73     public String JavaDoc createStringContent(NormalizedMessage message)
74         throws Exception JavaDoc {
75         return SourceHelper.createString(message.getContent());
76     }
77
78     @SuppressWarnings JavaDoc("unchecked")
79     public String JavaDoc getBodyElement(MessageContext message) throws Exception JavaDoc {
80         String JavaDoc result = new String JavaDoc();
81         SOAPEnvelope env = message.getEnvelope();
82         SOAPBody body = env.getBody();
83         OMNamespace namespace = env.getNamespace();
84         Iterator JavaDoc<OMNamespace> envNS = env.getAllDeclaredNamespaces();
85         Iterator JavaDoc<OMNamespace> bodyNS = body.getAllDeclaredNamespaces();
86         OMElement element = body.getFirstElement();
87         if (element != null) {
88             element.declareNamespace(namespace);
89             while (envNS.hasNext()) {
90                 element.declareNamespace(envNS.next());
91             }
92             while (bodyNS.hasNext()) {
93                 element.declareNamespace(bodyNS.next());
94             }
95             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
96             element.serialize(os);
97             result = os.toString();
98         }
99         return result;
100     }
101
102    
103     /**
104      * Return the operation QName of the requested axis operation
105      *
106      * @param message
107      * the Axis message
108      * @return the operation QName, null if there's no Operation setted in the
109      * message
110      */

111     public QName JavaDoc getOperationName(MessageContext message) {
112         return message.getOperationContext().getAxisOperation().getName();
113     }
114
115     /**
116      * Return the service name of the requested axis service
117      *
118      * @param message
119      * the Axis message
120      * @return the service name, null if there's no service setted in the
121      * message
122      */

123     public QName JavaDoc getServiceName(MessageContext message) {
124         QName JavaDoc name = null;
125         AxisService axisService = message.getServiceContext().getAxisService();
126         name = SOAPHelper.getServiceName(axisService);
127         return name;
128     }
129
130    
131     /**
132      * Create a new MessageExchange with the given Operation, Service and
133      * content. MEP is InOnly if inOnly boolean is set to true, InOut if it's
134      * set to false
135      *
136      * @param channel
137      * the deliveryChannel to use
138      * @param operationName
139      * the targeted operation QName
140      * @param serviceName
141      * the targeted service QName
142      * @param content
143      * the Source content
144      * @param inOnly
145      * boolean true --> MEP=InOnly, false --> MEP=InOut
146      * @return a new MessageExchange, must be non null
147      * @throws MessagingException
148      * if MessageExchange and NormalizedMessage can't be created or
149      * filled or if delivery channel can't be retrieved
150      */

151     protected MessageExchange createMessageExchange(DeliveryChannel channel, QName JavaDoc operationName,
152             QName JavaDoc serviceName, Source JavaDoc content, boolean inOnly)
153         throws MessagingException {
154         MessageExchange messageExchange = null;
155
156         if (channel != null) {
157             if (inOnly) {
158                 messageExchange = channel.createExchangeFactory()
159                         .createInOnlyExchange();
160             } else {
161                 messageExchange = channel.createExchangeFactory()
162                         .createInOutExchange();
163             }
164             NormalizedMessage nm = messageExchange.createMessage();
165             nm.setContent(content);
166             if (inOnly) {
167                 ((InOnly) messageExchange).setInMessage(nm);
168             } else {
169                 ((InOut) messageExchange).setInMessage(nm);
170             }
171             messageExchange.setService(serviceName);
172             messageExchange.setOperation(operationName);
173         } else {
174             throw new MessagingException(
175                     "Can't retrieve the delivery channel of the component");
176         }
177
178         return messageExchange;
179     }
180 }
181
Popular Tags