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.client;
020
021import java.net.URL;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import javax.wsdl.Binding;
028import javax.wsdl.BindingOperation;
029import javax.wsdl.Definition;
030import javax.wsdl.Input;
031import javax.wsdl.Message;
032import javax.wsdl.Operation;
033import javax.wsdl.Part;
034import javax.wsdl.Port;
035import javax.wsdl.Service;
036import javax.wsdl.Types;
037import javax.wsdl.WSDLException;
038import javax.wsdl.factory.WSDLFactory;
039import javax.wsdl.xml.WSDLReader;
040import javax.xml.namespace.QName;
041
042import lucee.commons.net.HTTPUtil;
043import lucee.runtime.PageContext;
044import lucee.runtime.config.Config;
045import lucee.runtime.dump.DumpData;
046import lucee.runtime.dump.DumpProperties;
047import lucee.runtime.dump.DumpTable;
048import lucee.runtime.dump.SimpleDumpData;
049import lucee.runtime.exp.ApplicationException;
050import lucee.runtime.exp.PageException;
051import lucee.runtime.exp.PageRuntimeException;
052import lucee.runtime.net.proxy.ProxyData;
053import lucee.runtime.net.rpc.RPCException;
054import lucee.runtime.op.Caster;
055import lucee.runtime.type.Collection.Key;
056import lucee.runtime.type.Struct;
057import lucee.runtime.type.dt.DateTime;
058
059import org.apache.axis.client.Call;
060import org.apache.axis.message.SOAPHeaderElement;
061
062public class JaxWSClient extends WSClient {
063        
064        private URL url;
065        private Definition wsdl;
066        private Service service;
067        private String wsdlUrl;
068        
069        public static void main(String[] args) throws PageException {
070                JaxWSClient client = new JaxWSClient("http://www.lucee.org/lucee/Admin.cfc?wsdl", null, null, null);
071        }
072        
073        public JaxWSClient( String wsdlUrl, String username, String password, ProxyData proxyData) throws PageException {
074                try {
075                        this.wsdlUrl=wsdlUrl;
076                        url = HTTPUtil.toURL(wsdlUrl, true);
077                        wsdl=loadWSDL(url);
078                        
079                        
080                        // get service
081                        {
082                        Iterator<Service> it = wsdl.getServices().values().iterator();
083                        while(it.hasNext()){
084                                if(service!=null)throw new ApplicationException("cannot handle more than one service");
085                                service=it.next();
086                        }
087                        }
088                        
089                        
090                        //Port port = WSUtil.getWSDLPort(service);
091                //Binding binding = port.getBinding();
092                //List<BindingOperation> operations = binding.getBindingOperations();
093                
094                }
095                catch (Exception e) {
096                        throw Caster.toPageException(e);
097                }
098    }
099
100        private static Definition loadWSDL(URL url) throws WSDLException { 
101                WSDLFactory factory = WSDLFactory.newInstance();
102                
103                // create an object to read the WSDL file
104            WSDLReader reader = factory.newWSDLReader();
105
106            // pass the URL to the reader for parsing and get back a WSDL definiton
107            return reader.readWSDL(url.toExternalForm());
108           
109        }
110
111        @Override
112        public Object get(PageContext pc, Key key, Object defaultValue) {
113                // TODO Auto-generated method stub
114                return null;
115        }
116
117        @Override
118        public Object get(PageContext pc, Key key) throws PageException {
119                // TODO Auto-generated method stub
120                return null;
121        }
122
123        @Override
124        public Object set(PageContext pc, Key propertyName, Object value) throws PageException {
125                // TODO Auto-generated method stub
126                return null;
127        }
128
129        @Override
130        public Object setEL(PageContext pc, Key propertyName, Object value) {
131                // TODO Auto-generated method stub
132                return null;
133        }
134
135        @Override
136        public Object call(PageContext pc, Key methodName, Object[] arguments) throws PageException {
137                // TODO Auto-generated method stub
138                return null;
139        }
140
141
142        @Override
143        public String castToString() throws PageException {
144                // TODO Auto-generated method stub
145                return null;
146        }
147
148        @Override
149        public String castToString(String defaultValue) {
150                // TODO Auto-generated method stub
151                return null;
152        }
153
154        @Override
155        public boolean castToBooleanValue() throws PageException {
156                // TODO Auto-generated method stub
157                return false;
158        }
159
160        @Override
161        public Boolean castToBoolean(Boolean defaultValue) {
162                // TODO Auto-generated method stub
163                return null;
164        }
165
166        @Override
167        public double castToDoubleValue() throws PageException {
168                // TODO Auto-generated method stub
169                return 0;
170        }
171
172        @Override
173        public double castToDoubleValue(double defaultValue) {
174                // TODO Auto-generated method stub
175                return 0;
176        }
177
178        @Override
179        public DateTime castToDateTime() throws PageException {
180                // TODO Auto-generated method stub
181                return null;
182        }
183
184        @Override
185        public DateTime castToDateTime(DateTime defaultValue) {
186                // TODO Auto-generated method stub
187                return null;
188        }
189
190        @Override
191        public int compareTo(String str) throws PageException {
192                // TODO Auto-generated method stub
193                return 0;
194        }
195
196        @Override
197        public int compareTo(boolean b) throws PageException {
198                // TODO Auto-generated method stub
199                return 0;
200        }
201
202        @Override
203        public int compareTo(double d) throws PageException {
204                // TODO Auto-generated method stub
205                return 0;
206        }
207
208        @Override
209        public int compareTo(DateTime dt) throws PageException {
210                // TODO Auto-generated method stub
211                return 0;
212        }
213
214        @Override
215        public Iterator<Key> keyIterator() {
216                // TODO Auto-generated method stub
217                return null;
218        }
219
220        @Override
221        public Iterator<String> keysAsStringIterator() {
222                // TODO Auto-generated method stub
223                return null;
224        }
225
226        @Override
227        public Iterator<Object> valueIterator() {
228                // TODO Auto-generated method stub
229                return null;
230        }
231
232        @Override
233        public Iterator<Entry<Key, Object>> entryIterator() {
234                // TODO Auto-generated method stub
235                return null;
236        }
237
238        @Override
239        public void addHeader(SOAPHeaderElement header) throws PageException {
240                // TODO Auto-generated method stub
241
242        }
243
244        @Override
245        public Call getLastCall() throws PageException {
246                // TODO Auto-generated method stub
247                return null;
248        }
249
250        @Override
251        public Object callWithNamedValues(Config config, Key methodName, Struct arguments) throws PageException {
252                // TODO Auto-generated method stub
253                return null;
254        }
255
256        @Override
257        public Object callWithNamedValues(PageContext pc, Key methodName, Struct arguments) throws PageException {
258                // TODO Auto-generated method stub
259                return null;
260        }
261        
262        @Override
263    public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
264        
265            try {
266                                return _toDumpData(pageContext,maxlevel,dp);
267                        }
268                        catch (RPCException e) {
269                                throw new PageRuntimeException(Caster.toPageException(e));
270                        }
271        /*try {} catch (Exception e) {
272            DumpTable table = new DumpTable("webservice","#99cc99","#ccffcc","#000000");
273            table.appendRow(1,new SimpleDumpData("webservice"),new SimpleDumpData(wsdlUrl));
274            return table;
275        }*/
276    }
277    private DumpData _toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) throws RPCException {
278                
279        DumpTable functions = new DumpTable("webservice","#99cc99","#ccffcc","#000000");
280        functions.setTitle("Web Service (JAX WS)");
281        if(dp.getMetainfo())functions.setComment(wsdlUrl);
282        
283        Port port = WSUtil.getSoapPort(service);
284        Binding binding = port.getBinding();
285        List<BindingOperation> operations = binding.getBindingOperations();
286        
287        Iterator<BindingOperation> it = operations.iterator();
288        BindingOperation bo;
289        while(it.hasNext()){
290                bo=it.next();
291                functions.appendRow(1, new SimpleDumpData(bo.getName()), toDumpData(bo));
292        }
293        
294        return functions;
295    }
296    
297    private DumpData toDumpData(BindingOperation bo) {
298        Map<QName,Message> messages = wsdl.getMessages();
299        
300        DumpTable table = new DumpTable("#99cc99","#ccffcc","#000000");
301        DumpTable attributes = new DumpTable("#99cc99","#ccffcc","#000000");
302        String returns = "void";
303        attributes.appendRow(3,new SimpleDumpData("name"),new SimpleDumpData("type"));
304        
305        Operation op = bo.getOperation();
306        
307        // attributes
308        Input in = op.getInput();
309        Message msg = in.getMessage();
310        
311        
312        //msg=WSUtil.getMessageByLocalName(messages,bo.getBindingInput().getName());
313        //print.e(msg.getQName());
314        
315        List<Part> parts = msg.getOrderedParts(null);
316        Iterator<Part> it = parts.iterator();
317        Part p;
318        QName en;
319        QName type;
320        while(it.hasNext()){
321                p=it.next();
322                en=p.getElementName();
323                
324                if(en!=null) {
325                        type=en;
326                        Types types = wsdl.getTypes();
327                }
328                else 
329                        type= p.getTypeName();
330                
331                attributes.appendRow(0,
332                        new SimpleDumpData(en+":"+p.getName()),
333                        new SimpleDumpData(toLuceeType(type)));
334            
335        }
336        
337        // return
338        msg = bo.getOperation().getOutput().getMessage();
339        msg=wsdl.getMessage(msg.getQName());
340        parts = msg.getOrderedParts(null);
341        it = parts.iterator();
342        while(it.hasNext()){
343                p=it.next();
344                returns=toLuceeType(p.getTypeName());
345            
346        }
347        
348        // TODO msg.getDocumentationElement();
349        
350        
351        
352        /*
353        for(int j = 0; j < parameters.list.size(); j++) {
354            Parameter p = (Parameter)parameters.list.get(j);
355            
356            QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
357            String strType=paramType.getLocalPart();
358                        
359            switch(p.getMode()) {
360            case Parameter.IN:
361                attributes.appendRow(0,new SimpleDumpData(p.getName()),new SimpleDumpData(toLuceeType(strType)));
362            break;
363            case Parameter.OUT:
364                returns=toLuceeType(strType);
365            break;
366            case Parameter.INOUT:
367                attributes.appendRow(0,new SimpleDumpData(p.getName()),new SimpleDumpData(toLuceeType(strType)));
368                returns=toLuceeType(strType);
369                
370            break;
371            }
372        }
373        Parameter rtn = parameters.returnParam;
374        if(rtn!=null) {
375            QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(rtn);
376            String strType=paramType.getLocalPart();
377            returns=toLuceeType(strType);
378        }
379        table.appendRow(1,new SimpleDumpData("arguments"),attributes);
380        table.appendRow(1,new SimpleDumpData("return type"),new SimpleDumpData(returns));
381        if(doc.length()>0)table.appendRow(1,new SimpleDumpData("hint"),new SimpleDumpData(doc));
382        
383        */
384        
385        table.appendRow(1,new SimpleDumpData("arguments"),attributes);
386        table.appendRow(1,new SimpleDumpData("return type"),new SimpleDumpData(returns));
387        // if(doc.length()>0)table.appendRow(1,new SimpleDumpData("hint"),new SimpleDumpData(doc));
388        
389        
390        return table;
391        
392    }
393
394    private String toLuceeType(QName typeName) {
395        if(typeName==null) return "";
396        
397        String strType = typeName.getLocalPart();
398        strType=strType.toLowerCase();
399        
400        if(strType.startsWith("array"))strType="array";
401        else if(strType.equals("map"))strType="struct";
402        else if(strType.startsWith("query"))strType="query";
403        else if(strType.equals("double"))strType="numeric";
404        else if(strType.startsWith("any"))strType="any";
405        else if(strType.equals("date"))strType="date";
406        return strType;
407        }
408}