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.Constants;
021: import org.apache.axis.Message;
022: import org.apache.axis.MessageContext;
023: import org.apache.axis.components.logger.LogFactory;
024: import org.apache.axis.handlers.BasicHandler;
025: import org.apache.axis.message.SOAPEnvelope;
026: import org.apache.axis.message.SOAPHeaderElement;
027: import org.apache.axis.utils.Messages;
028: import org.apache.commons.logging.Log;
029:
030: import javax.xml.namespace.QName;
031:
032: /** This handler processes the SOAP header "echoMeString" defined in the
033: * SOAPBuilder Round2C interop tests.
034: *
035: * <p>Essentially, you install it on both the request and response chains of
036: * your service, on the server side.</p>
037: *
038: * @author Simon Fell (simon@zaks.demon.co.uk)
039: */
040: public class echoHeaderStringHandler extends BasicHandler {
041: static Log log = LogFactory.getLog(echoHeaderStringHandler.class
042: .getName());
043:
044: public static final String ECHOHEADER_STRING_ID = "echoHeaderStringHandler.id";
045: public static final String HEADER_NS = "http://soapinterop.org/echoheader/";
046: public static final String HEADER_REQNAME = "echoMeStringRequest";
047: public static final String HEADER_RESNAME = "echoMeStringResponse";
048: public static final String ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next";
049:
050: public boolean canHandleBlock(QName qname) {
051: if (HEADER_NS.equals(qname.getNamespaceURI())
052: && HEADER_REQNAME.equals(qname.getLocalPart())) {
053: return true;
054: }
055:
056: return false;
057: }
058:
059: /**
060: * Process a MessageContext.
061: */
062: public void invoke(MessageContext context) throws AxisFault {
063: if (context.getPastPivot()) {
064: // This is a response. Add the response header, if we saw
065: // the requestHeader
066: String strVal = (String) context
067: .getProperty(ECHOHEADER_STRING_ID);
068: if (strVal == null)
069: return;
070:
071: Message msg = context.getResponseMessage();
072: if (msg == null)
073: return;
074: SOAPEnvelope env = msg.getSOAPEnvelope();
075: SOAPHeaderElement header = new SOAPHeaderElement(HEADER_NS,
076: HEADER_RESNAME, strVal);
077: env.addHeader(header);
078: } else {
079: // Request. look for the header
080: Message msg = context.getRequestMessage();
081: if (msg == null)
082: throw new AxisFault(Messages.getMessage("noRequest00"));
083:
084: SOAPEnvelope env = msg.getSOAPEnvelope();
085: SOAPHeaderElement header = env.getHeaderByName(HEADER_NS,
086: HEADER_REQNAME);
087:
088: if (header != null) {
089: // seems Axis has already ignored any headers not tageted
090: // at us
091: String strVal;
092: // header.getValue() doesn't seem to be connected to anything
093: // we always get null.
094: try {
095: strVal = (String) header
096: .getValueAsType(Constants.XSD_STRING);
097: } catch (Exception e) {
098: throw AxisFault.makeFault(e);
099: }
100: context.setProperty(ECHOHEADER_STRING_ID, strVal);
101: header.setProcessed(true);
102: }
103: }
104: }
105: }
|