001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.rendering;
023
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.webengine.WebEngine;
030import org.nuxeo.ecm.webengine.model.WebContext;
031
032import freemarker.template.AdapterTemplateModel;
033import freemarker.template.SimpleScalar;
034import freemarker.template.TemplateCollectionModel;
035import freemarker.template.TemplateHashModelEx;
036import freemarker.template.TemplateMethodModelEx;
037import freemarker.template.TemplateModel;
038import freemarker.template.TemplateModelException;
039import freemarker.template.TemplateModelIterator;
040
041/**
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class ScriptMethod implements TemplateMethodModelEx {
045
046    @Override
047    public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
048        int size = arguments.size();
049        if (size < 1) {
050            throw new TemplateModelException("Invalid number of arguments for script(...) method");
051        }
052
053        SimpleScalar val = (SimpleScalar) arguments.get(0);
054        if (val == null) {
055            throw new TemplateModelException("src attribute is required");
056        }
057        String src = val.getAsString();
058
059        Map<String, Object> args = new HashMap<>();
060        if (arguments.size() > 1) {
061            Object o = arguments.get(1);
062            if (o instanceof SimpleScalar) {
063                String arg = ((SimpleScalar) o).getAsString();
064                args.put("_args", new String[] { arg });
065            } else if (!(o instanceof TemplateHashModelEx)) {
066                throw new TemplateModelException("second argument should be a map");
067            } else {
068                TemplateHashModelEx t = (TemplateHashModelEx) o;
069                TemplateCollectionModel keys = t.keys();
070                TemplateModelIterator it = keys.iterator();
071
072                while (it.hasNext()) {
073                    TemplateModel k = it.next();
074                    String kk = k.toString();
075                    TemplateModel v = t.get(kk);
076                    Object vv = null;
077                    if (v instanceof AdapterTemplateModel) {
078                        vv = ((AdapterTemplateModel) v).getAdaptedObject(null);
079                    } else {
080                        vv = v.toString();
081                    }
082                    args.put(kk, vv);
083                }
084            }
085        }
086
087        WebContext ctx = WebEngine.getActiveContext();
088        if (ctx != null) {
089            try {
090                return ctx.runScript(src, args);
091            } catch (NuxeoException e) {
092                throw new TemplateModelException("Failed to run script: " + src, e);
093            }
094        } else {
095            throw new IllegalStateException("Not In a Web Context");
096        }
097    }
098
099}