001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.rpc;
020
021import java.io.StringReader;
022import java.util.Iterator;
023import java.util.Vector;
024
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.net.rpc.client.WSClient;
028import lucee.runtime.text.xml.XMLCaster;
029import lucee.runtime.text.xml.XMLUtil;
030
031import org.apache.axis.AxisFault;
032import org.apache.axis.MessageContext;
033import org.apache.axis.client.Call;
034import org.apache.axis.message.SOAPEnvelope;
035import org.apache.axis.message.SOAPHeaderElement;
036import org.apache.axis.wsdl.symbolTable.ElementDecl;
037import org.apache.axis.wsdl.symbolTable.TypeEntry;
038import org.w3c.dom.Element;
039import org.w3c.dom.Node;
040import org.xml.sax.InputSource;
041
042public class AxisUtil {
043
044        public static boolean isSOAPRequest() {
045                MessageContext context = MessageContext.getCurrentContext();
046        return context != null && !context.isClient();
047        }
048        
049        public static Object getSOAPRequestHeader(PageContext pc, String namespace, String name, boolean asXML) throws Exception {
050                MessageContext context = MessageContext.getCurrentContext();
051        if(context==null || context.isClient()) throw new AxisFault("not inside a Soap Request");
052        
053        SOAPEnvelope env = context.getRequestMessage().getSOAPEnvelope();
054        SOAPHeaderElement header = env.getHeaderByName(namespace, name);
055        return toValue(header,asXML);
056        }
057
058        public static Object getSOAPResponseHeader(PageContext pc, WSClient client, String namespace, String name, boolean asXML) throws Exception {
059                MessageContext context = getMessageContext(client);
060            
061                SOAPEnvelope env = context.getResponseMessage().getSOAPEnvelope();
062            SOAPHeaderElement header = env.getHeaderByName(namespace, name);
063            return toValue(header,asXML);
064        }
065        
066        public static Node getSOAPRequest(WSClient client) throws Exception {
067                MessageContext context=getMessageContext(client);
068        SOAPEnvelope env = context.getRequestMessage().getSOAPEnvelope();
069        return XMLCaster.toXMLStruct(env.getAsDocument(),true);
070    }
071        
072        public static Node getSOAPResponse(WSClient client) throws Exception {
073                Call call = client.getLastCall();
074                if(call==null) throw new AxisFault("web service was not invoked yet");
075        SOAPEnvelope env = call.getResponseMessage().getSOAPEnvelope();
076                return XMLCaster.toXMLStruct(env.getAsDocument(),true);
077    }
078        
079        public static void addSOAPResponseHeader(String namespace, String name, Object value, boolean mustUnderstand) throws AxisFault {
080        MessageContext context = MessageContext.getCurrentContext();
081        if(context==null || context.isClient()) throw new AxisFault("not inside a Soap Request");
082        
083        SOAPEnvelope env = context.getResponseMessage().getSOAPEnvelope();
084        SOAPHeaderElement header=toSOAPHeaderElement(namespace,name,value);
085        header.setMustUnderstand(mustUnderstand);
086        env.addHeader(header);
087    }
088
089        public static void addSOAPRequestHeader(WSClient client, String namespace, String name, Object value, boolean mustUnderstand) throws PageException  {
090        SOAPHeaderElement header=toSOAPHeaderElement(namespace,name,value);
091        header.setMustUnderstand(mustUnderstand);
092        client.addHeader(header);
093    }
094        
095        
096        private static SOAPHeaderElement toSOAPHeaderElement(String namespace, String name, Object value) {
097                Element el=XMLCaster.toRawElement(value,null);
098        if(el!=null) return new SOAPHeaderElement(el);
099        return new SOAPHeaderElement(namespace, name, value);
100        }
101
102
103        
104        private static Object toValue(SOAPHeaderElement header, boolean asXML) throws Exception {
105                if(header==null) return "";
106        if(asXML) {
107                String strXML = header.toString();
108                        InputSource is = new InputSource(new StringReader(strXML.trim()));
109                        return XMLCaster.toXMLStruct(XMLUtil.parse(is,null,false),true);
110        } 
111        
112        Object value=header.getObjectValue();
113        if(value == null){
114                value = header.getObjectValue(String.class);
115        }
116        return value;
117        }
118        
119
120        
121        private static MessageContext getMessageContext(WSClient client) throws AxisFault, PageException {
122                if(client!=null) {
123                        Call call = client.getLastCall();
124                if(call==null) throw new AxisFault("web service was not invoked yet");
125                return call.getMessageContext();
126        } 
127        MessageContext context = MessageContext.getCurrentContext();
128        if(context == null) throw new AxisFault("not inside a Soap Request");
129        return context;
130        }
131
132        public static TypeEntry getContainedElement(TypeEntry type, String name, TypeEntry defaultValue) {
133                if(type==null) return defaultValue;
134                Vector v = type.getContainedElements();
135                if(v==null) return defaultValue;
136                Iterator it = v.iterator();
137                ElementDecl ed;
138                String tmp;
139                while(it.hasNext()){
140                        ed=(ElementDecl) it.next();
141                        if(ed.getQName()==null) continue;
142                        tmp=lucee.runtime.type.util.ListUtil.last(ed.getQName().getLocalPart(), '>');
143                
144                        
145                        if(tmp.equalsIgnoreCase(name))
146                                return ed.getType();
147                }
148                return defaultValue;
149        }
150        
151        
152}