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