KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > echo > echoHeaderStringHandler


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

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

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