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.spooler;
020
021import java.io.IOException;
022import java.nio.charset.Charset;
023import java.util.HashMap;
024import java.util.Map;
025
026import lucee.commons.lang.StringUtil;
027import lucee.commons.net.HTTPUtil;
028import lucee.commons.net.http.HTTPResponse;
029import lucee.commons.net.http.httpclient4.HTTPEngine4Impl;
030import lucee.runtime.PageContext;
031import lucee.runtime.PageContextImpl;
032import lucee.runtime.config.Config;
033import lucee.runtime.config.RemoteClient;
034import lucee.runtime.converter.ConverterException;
035import lucee.runtime.converter.JSONConverter;
036import lucee.runtime.engine.ThreadLocalPageContext;
037import lucee.runtime.exp.PageException;
038import lucee.runtime.interpreter.JSONExpressionInterpreter;
039import lucee.runtime.op.Caster;
040import lucee.runtime.type.Struct;
041import lucee.runtime.type.StructImpl;
042import lucee.runtime.type.util.KeyConstants;
043
044public abstract class SpoolerTaskHTTPCall extends SpoolerTaskSupport {
045        
046        private static final long serialVersionUID = -1994776413696459993L;
047        
048        private RemoteClient client;
049    
050    
051        public SpoolerTaskHTTPCall(ExecutionPlan[] plans,RemoteClient client) {
052                super(plans);
053                this.client=client;
054        }
055
056        /**
057         * @return 
058         * @see lucee.runtime.spooler.SpoolerTask#execute()
059         */
060        public final Object execute(Config config) throws PageException {
061                return execute(client, config, getMethodName(), getArguments());
062        }
063        
064        public static final Object execute(RemoteClient client, Config config, String methodName, Struct args) throws PageException {
065                //return rpc.callWithNamedValues(config, getMethodName(), getArguments());
066                PageContext pc = ThreadLocalPageContext.get();
067                
068                
069                // remove wsdl if necessary
070                String url=client.getUrl();
071                if(StringUtil.endsWithIgnoreCase(url,"?wsdl"))
072                        url=url.substring(0,url.length()-5);
073                
074                // Params
075                Map<String, String> params=new HashMap<String, String>();
076                params.put("method",methodName);
077                params.put("returnFormat","json");
078                try {
079                        Charset cs = ((PageContextImpl)pc).getWebCharset();
080                        params.put("argumentCollection",new JSONConverter(true,cs).serialize(pc, args, false));
081                
082                
083                        HTTPResponse res = HTTPEngine4Impl.post(
084                                HTTPUtil.toURL(url,true), 
085                                client.getServerUsername(), 
086                                client.getServerPassword(), -1L, -1, ((PageContextImpl)pc).getWebCharset().name(), "Lucee Remote Invocation", client.getProxyData(), null,params);
087                
088                        return new JSONExpressionInterpreter().interpret(pc, res.getContentAsString());
089                        
090                }
091                catch (IOException ioe) {
092                        throw Caster.toPageException(ioe); 
093                }
094                catch (ConverterException ce) {
095                        throw Caster.toPageException(ce); 
096                }
097                
098        }
099        
100        
101        /**
102         * @see lucee.runtime.spooler.SpoolerTask#subject()
103         */
104        public String subject() {
105                return client.getLabel();
106        }
107
108        /**
109         * @see lucee.runtime.spooler.SpoolerTask#detail()
110         */
111        public Struct detail() {
112                Struct sct=new StructImpl();
113                sct.setEL(KeyConstants._label, client.getLabel());
114                sct.setEL(KeyConstants._url, client.getUrl());
115                
116                return sct;
117        }
118        
119
120
121        protected abstract String getMethodName();
122        protected abstract Struct getArguments();
123}