001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.wsrp;
023:
024: import java.util.Iterator;
025:
026: import javax.xml.namespace.QName;
027: import javax.xml.rpc.handler.GenericHandler;
028: import javax.xml.rpc.handler.HandlerInfo;
029: import javax.xml.rpc.handler.MessageContext;
030: import javax.xml.rpc.handler.soap.SOAPMessageContext;
031: import javax.xml.soap.Node;
032: import javax.xml.soap.SOAPBody;
033: import javax.xml.soap.SOAPHeader;
034: import javax.xml.soap.SOAPMessage;
035:
036: import org.jboss.logging.Logger;
037: import org.jboss.util.xml.DOMUtils;
038: import org.w3c.dom.Element;
039:
040: //$Id: WSRPExtensionHandler.java 57211 2006-09-26 12:39:46Z dimitris@jboss.org $
041:
042: /**
043: * JAX-RPC Handler that strips the SOAP Message off the wsrp extensions
044: *
045: * Checks for the existence of a SOAP Header element "jboss_wsrp_remove_extension"
046: * which when present, will strip the wsrp extensions.
047: * If there the handler has a init param set to remove the extension, it will.
048: *
049: * @author <a href="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
050: * @since Apr 26, 2006
051: * @version $Revision: 57211 $
052: */
053: public class WSRPExtensionHandler extends GenericHandler {
054: private static Logger log = Logger
055: .getLogger(WSRPExtensionHandler.class);
056: private boolean debug = false;
057: private boolean removeExtensions = false;
058:
059: public void init(HandlerInfo info) {
060: // this parameter configured in 'webservices.xml'
061: String debugStr = (String) info.getHandlerConfig().get("debug");
062: debug = "true".equalsIgnoreCase(debugStr);
063: String rem = (String) info.getHandlerConfig().get(
064: "removeExtensions");
065: removeExtensions = "true".equalsIgnoreCase(rem);
066: }
067:
068: public QName[] getHeaders() {
069: return null;
070: }
071:
072: public boolean handleRequest(MessageContext msgContext) {
073: SOAPMessageContext soapMessageContext = (SOAPMessageContext) msgContext;
074: SOAPMessage soapMessage = soapMessageContext.getMessage();
075: try {
076: if (debug)
077: soapMessage.writeTo(System.out);
078: SOAPHeader soapHeader = soapMessage.getSOAPHeader();
079: if (shouldRemoveWSRPExtensions(soapHeader)
080: || removeExtensions) {
081: SOAPBody soapBody = soapMessage.getSOAPBody();
082: Element firstEl = DOMUtils
083: .getFirstChildElement(soapBody);
084: Iterator iter = DOMUtils.getChildElements(firstEl,
085: "runtimeContext");
086: if (iter.hasNext()) {
087: Element runtimeCtx = (Element) iter.next();
088: iter = DOMUtils.getChildElements(runtimeCtx,
089: "extensions");
090: log.error("extensions exist:" + iter.hasNext());
091: if (iter.hasNext()) {
092: Element exts = (Element) iter.next();
093: iter = DOMUtils.getChildElements(exts);
094: while (iter.hasNext()) {
095: Node node = (Node) iter.next();
096: exts.removeChild(node);
097: }
098: if (debug)
099: soapMessage.writeTo(System.out);
100: }
101: }
102: }
103: } catch (Exception e) {
104: log.error("Error in handleRequest:", e);
105: }
106: return super .handleRequest(msgContext);
107: }
108:
109: private boolean shouldRemoveWSRPExtensions(SOAPHeader soapHeader) {
110: boolean result = false;
111: if (soapHeader != null) {
112: Iterator iter = DOMUtils.getChildElements(soapHeader,
113: "jboss_wsrp_remove_extension");
114: if (iter.hasNext())
115: result = true;
116: }
117: return result;
118: }
119: }
|