001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.echo;
018:
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.Message;
021: import org.apache.axis.MessageContext;
022: import org.apache.axis.components.logger.LogFactory;
023: import org.apache.axis.handlers.BasicHandler;
024: import org.apache.axis.message.SOAPEnvelope;
025: import org.apache.axis.message.SOAPHeaderElement;
026: import org.apache.axis.utils.Messages;
027: import org.apache.commons.logging.Log;
028:
029: import javax.xml.namespace.QName;
030:
031: /** This handler processes the SOAP header "echoMeStruct" defined in the
032: * SOAPBuilder Round2C interop tests.
033: *
034: * <p>Essentially, you install it on both the request and response chains of
035: * your service, on the server side.</p>
036: *
037: * @author Simon Fell (simon@zaks.demon.co.uk)
038: */
039: public class echoHeaderStructHandler extends BasicHandler {
040: static Log log = LogFactory.getLog(echoHeaderStringHandler.class
041: .getName());
042:
043: public static final String ECHOHEADER_STRUCT_ID = "echoHeaderStructHandler.id";
044: public static final String HEADER_NS = "http://soapinterop.org/echoheader/";
045: public static final String HEADER_REQNAME = "echoMeStructRequest";
046: public static final String HEADER_RESNAME = "echoMeStructResponse";
047: public static final String ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next";
048: public static final String STRUCT_NS = "http://soapinterop.org/xsd";
049: public static final String STRUCT_NAME = "SOAPStruct";
050: public static final QName SOAPStructType = new QName(STRUCT_NS,
051: STRUCT_NAME);
052:
053: public boolean canHandleBlock(QName qname) {
054: if (HEADER_NS.equals(qname.getNamespaceURI())
055: && HEADER_REQNAME.equals(qname.getLocalPart())) {
056: return true;
057: }
058:
059: return false;
060: }
061:
062: /**
063: * Process a MessageContext.
064: */
065: public void invoke(MessageContext context) throws AxisFault {
066: if (context.getPastPivot()) {
067: // This is a response. Add the response header, if we saw
068: // the requestHeader
069: SOAPStruct hdrVal = (SOAPStruct) context
070: .getProperty(ECHOHEADER_STRUCT_ID);
071: if (hdrVal == null)
072: return;
073:
074: Message msg = context.getResponseMessage();
075: if (msg == null)
076: return;
077: SOAPEnvelope env = msg.getSOAPEnvelope();
078: SOAPHeaderElement header = new SOAPHeaderElement(HEADER_NS,
079: HEADER_RESNAME, hdrVal);
080: env.addHeader(header);
081: } else {
082: // Request. look for the header
083: Message msg = context.getRequestMessage();
084: if (msg == null)
085: throw new AxisFault(Messages.getMessage("noRequest00"));
086:
087: SOAPEnvelope env = msg.getSOAPEnvelope();
088: SOAPHeaderElement header = env.getHeaderByName(HEADER_NS,
089: HEADER_REQNAME);
090:
091: if (header != null) {
092: // seems Axis has already ignored any headers not tageted
093: // at us
094: SOAPStruct hdrVal;
095: // header.getValue() doesn't seem to be connected to anything
096: // we always get null.
097: try {
098: hdrVal = (SOAPStruct) header
099: .getValueAsType(SOAPStructType);
100: } catch (Exception e) {
101: throw AxisFault.makeFault(e);
102: }
103: context.setProperty(ECHOHEADER_STRUCT_ID, hdrVal);
104: header.setProcessed(true);
105: }
106: }
107: }
108: }
|