KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > webservice > jaxrpc > HeaderHandler


1 /*
2  * Copyright 2005 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  * $Header:$
17  */

18 package org.apache.beehive.controls.system.webservice.jaxrpc;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
24 import javax.xml.rpc.handler.Handler JavaDoc;
25 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
26 import javax.xml.rpc.handler.MessageContext JavaDoc;
27 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
28 import javax.xml.soap.SOAPElement JavaDoc;
29 import javax.xml.soap.SOAPEnvelope JavaDoc;
30 import javax.xml.soap.SOAPException JavaDoc;
31 import javax.xml.soap.SOAPHeader JavaDoc;
32 import javax.xml.soap.SOAPHeaderElement JavaDoc;
33 import javax.xml.soap.SOAPMessage JavaDoc;
34 import javax.xml.soap.SOAPPart JavaDoc;
35
36 import org.apache.log4j.Logger;
37 import org.w3c.dom.Element JavaDoc;
38
39 public class HeaderHandler implements Handler JavaDoc {
40
41     static Logger logger = Logger.getLogger(HeaderHandler.class);
42
43     static ThreadLocal JavaDoc localInHeaders = new ThreadLocal JavaDoc();
44
45     static ThreadLocal JavaDoc localOutHeaders = new ThreadLocal JavaDoc();
46
47     /**
48      * Sign outgoing request message.
49      *
50      * @throws SOAPException
51      */

52     public boolean handleRequest(MessageContext JavaDoc mc) {
53         System.out.println("In HeaderHandler's handleRequest myOutHeaders: "
54                 + localOutHeaders.get());
55
56         try {
57             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) mc;
58             SOAPMessage JavaDoc msg = smc.getMessage();
59             logger.debug("Request message body: "
60                     + msg.getSOAPBody().toString() + "\n header: "
61                     + msg.getSOAPHeader().toString());
62            SOAPPart JavaDoc part = msg.getSOAPPart();
63             SOAPEnvelope JavaDoc envelope = part.getEnvelope();
64             SOAPHeader JavaDoc header = envelope.getHeader();
65
66             /**
67              * Create new header element. We don't specify a role on this header
68              * element, meaning the target role is the "ultimate destination".
69              */

70             Element JavaDoc[] elemsToAdd = (Element JavaDoc[]) localOutHeaders.get();
71
72             if (null == elemsToAdd) {
73                 logger.debug("no header to send");
74                 return true;
75             }
76             for (Element JavaDoc nxtElement : elemsToAdd) {
77                 header
78                         .addChildElement(new org.apache.axis.message.SOAPHeaderElement(
79                                 nxtElement)); // NOTE: Axis specific
80
}
81
82         } catch (SOAPException JavaDoc e) {
83             e.printStackTrace();
84             logger.error("Failed to add header.", e);
85         }
86
87         return true;
88     }
89
90     /**
91      *
92      */

93     static public void reset() {
94         localInHeaders = new ThreadLocal JavaDoc();
95         localOutHeaders = new ThreadLocal JavaDoc();
96
97     }
98
99     /**
100      * Check incoming response message.
101      */

102     public boolean handleResponse(MessageContext JavaDoc mc) {
103         System.out.println("In HeaderHandler's handleResponse");
104
105         try {
106             SOAPMessageContext JavaDoc smc = (SOAPMessageContext JavaDoc) mc;
107             SOAPMessage JavaDoc msg = smc.getMessage();
108             logger.debug("Response message body: "
109                     + msg.getSOAPBody().toString() + "\n header: "
110                     + msg.getSOAPHeader().toString());
111             SOAPPart JavaDoc part = msg.getSOAPPart();
112             SOAPEnvelope JavaDoc envelope = part.getEnvelope();
113             SOAPHeader JavaDoc header = envelope.getHeader();
114             if (header == null)
115                 return true;
116
117             Iterator JavaDoc it = header.extractAllHeaderElements();
118             ArrayList JavaDoc<Element JavaDoc> list = new ArrayList JavaDoc<Element JavaDoc>();
119             while (it.hasNext()) {
120                 SOAPElement JavaDoc nxtSoapElement = (SOAPElement JavaDoc) it.next();
121                 list.add((Element JavaDoc) nxtSoapElement.cloneNode(true));
122             }
123
124             Element JavaDoc[] elems = (Element JavaDoc[]) list
125                     .toArray(new Element JavaDoc[list.size()]); // Use generics
126
localInHeaders.set(elems);
127
128         } catch (SOAPException JavaDoc e) {
129             e.printStackTrace();
130             logger.error("Error extracting the header.", e);
131         }
132
133         logger.debug("myInHeaders: " + localInHeaders.get());
134         return true;
135     }
136
137     /**
138      * Check incoming response message.
139      */

140     public boolean handleFault(MessageContext JavaDoc mc) {
141         System.out.println("In HeaderHandler's handleFault");
142         return true;
143     }
144
145     /*
146      * (non-Javadoc)
147      *
148      * @see javax.xml.rpc.handler.Handler#init(javax.xml.rpc.handler.HandlerInfo)
149      */

150     public void init(HandlerInfo JavaDoc arg0) {
151         System.out.println("In HeaderHandler's init");
152
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see javax.xml.rpc.handler.Handler#destroy()
159      */

160     public void destroy() {
161         logger.debug("In HeaderHandler's destroy");
162
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see javax.xml.rpc.handler.Handler#getHeaders()
169      */

170     public QName JavaDoc[] getHeaders() {
171         // TODO Auto-generated method stub
172
return null;
173     }
174
175     /**
176      * @return Returns the inHeaders.
177      */

178     static public Element JavaDoc[] getInHeaders() {
179         return (Element JavaDoc[]) localInHeaders.get();
180     }
181
182     /**
183      * @param outHeaders
184      * The outHeaders to set.
185      */

186     static public void setOutHeaders(Element JavaDoc[] outHeaders) {
187         localOutHeaders.set(outHeaders);
188     }
189 }
190
Popular Tags