KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > echo > echoHeaderStructHandler


1 /*
2  * Copyright 2001-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 samples.echo;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.handlers.BasicHandler;
24 import org.apache.axis.message.SOAPEnvelope;
25 import org.apache.axis.message.SOAPHeaderElement;
26 import org.apache.axis.utils.Messages;
27 import org.apache.commons.logging.Log;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31
32 /** This handler processes the SOAP header "echoMeStruct" defined in the
33  * SOAPBuilder Round2C interop tests.
34  *
35  * <p>Essentially, you install it on both the request and response chains of
36  * your service, on the server side.</p>
37  *
38  * @author Simon Fell (simon@zaks.demon.co.uk)
39  */

40 public class echoHeaderStructHandler extends BasicHandler
41 {
42     static Log log =
43             LogFactory.getLog(echoHeaderStringHandler.class.getName());
44
45     public static final String JavaDoc ECHOHEADER_STRUCT_ID = "echoHeaderStructHandler.id";
46     public static final String JavaDoc HEADER_NS = "http://soapinterop.org/echoheader/";
47     public static final String JavaDoc HEADER_REQNAME = "echoMeStructRequest";
48     public static final String JavaDoc HEADER_RESNAME = "echoMeStructResponse";
49     public static final String JavaDoc ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next";
50     public static final String JavaDoc STRUCT_NS = "http://soapinterop.org/xsd" ;
51     public static final String JavaDoc STRUCT_NAME = "SOAPStruct";
52     public static final QName JavaDoc SOAPStructType = new QName JavaDoc(STRUCT_NS, STRUCT_NAME);
53     
54     public boolean canHandleBlock(QName JavaDoc qname) {
55         if (HEADER_NS.equals(qname.getNamespaceURI()) &&
56                 HEADER_REQNAME.equals(qname.getLocalPart())) {
57             return true;
58         }
59         
60         return false;
61     }
62     
63     /**
64      * Process a MessageContext.
65      */

66     public void invoke(MessageContext context) throws AxisFault
67     {
68         if (context.getPastPivot()) {
69             // This is a response. Add the response header, if we saw
70
// the requestHeader
71
SOAPStruct hdrVal= (SOAPStruct)context.getProperty(ECHOHEADER_STRUCT_ID);
72             if (hdrVal == null)
73                 return;
74             
75             Message msg = context.getResponseMessage();
76             if (msg == null)
77                 return;
78             SOAPEnvelope env = msg.getSOAPEnvelope();
79             SOAPHeaderElement header = new SOAPHeaderElement(HEADER_NS,
80                                                              HEADER_RESNAME,
81                                                              hdrVal);
82             env.addHeader(header);
83         } else {
84             // Request. look for the header
85
Message msg = context.getRequestMessage();
86             if (msg == null)
87                 throw new AxisFault(Messages.getMessage("noRequest00"));
88             
89             SOAPEnvelope env = msg.getSOAPEnvelope();
90             SOAPHeaderElement header = env.getHeaderByName(HEADER_NS,
91                                                            HEADER_REQNAME);
92             
93             if (header != null) {
94                 // seems Axis has already ignored any headers not tageted
95
// at us
96
SOAPStruct hdrVal ;
97                 // header.getValue() doesn't seem to be connected to anything
98
// we always get null.
99
try {
100                     hdrVal = (SOAPStruct)header.getValueAsType(SOAPStructType);
101                 } catch (Exception JavaDoc e) {
102                     throw AxisFault.makeFault(e);
103                 }
104                 context.setProperty(ECHOHEADER_STRUCT_ID, hdrVal) ;
105                 header.setProcessed(true);
106             }
107         }
108     }
109 }
110
Popular Tags