View Javadoc

1   /**
2    * This file Copyright (c) 2009-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.groovy.console;
35  
36  import groovy.lang.Binding;
37  import groovy.lang.GroovySystem;
38  import groovy.lang.MissingPropertyException;
39  import info.magnolia.cms.i18n.MessagesUtil;
40  import info.magnolia.cms.security.User;
41  import info.magnolia.cms.util.NodeDataUtil;
42  import info.magnolia.context.Context;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.module.admininterface.TemplatedMVCHandler;
45  
46  import java.io.ByteArrayInputStream;
47  import java.io.FileInputStream;
48  import java.io.FileNotFoundException;
49  import java.io.InputStream;
50  import java.io.PrintWriter;
51  import java.io.Serializable;
52  import java.util.Collection;
53  
54  import javax.jcr.RepositoryException;
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.apache.commons.io.IOUtils;
59  import org.apache.commons.lang.StringUtils;
60  import org.codehaus.groovy.control.CompilationFailedException;
61  import org.codehaus.groovy.runtime.InvokerInvocationException;
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  
65  
66  /**
67   * The server-side implementation of the *nix-like Magnolia Groovy console.
68   * @author fgrilli
69   * @version $Id$
70   */
71  public class MgnlGroovyInteractiveConsole extends TemplatedMVCHandler {
72  
73      public MgnlGroovyInteractiveConsole(String name, HttpServletRequest request, HttpServletResponse response) {
74          super(name, request, response);
75      }
76  
77      private static final Logger log = LoggerFactory.getLogger(MgnlGroovyInteractiveConsole.class);
78  
79      private static final String MSG_BASENAME = "info.magnolia.module.groovy.messages";
80  
81      private static final String BINDING_SESSION_ATTRIBUTE = "info.magnolia.module.groovy.console.binding.session.attribute";
82  
83      private static final String GROOVY_VERSION = GroovySystem.getVersion();
84  
85      public String clean() throws Exception {
86          log.debug("calling command {} and cleaning groovy context", getCommand());
87          request.getSession().setAttribute(BINDING_SESSION_ATTRIBUTE, new SerializableBinding());
88          return VIEW_SHOW;
89      }
90  
91      public String evaluateGroovy() throws Exception {
92          User currentUser = MgnlContext.getUser();
93          if(!isAuthorized(currentUser)){
94              String msg = "User " + currentUser.getName() + " is trying to use the Magnolia Groovy Interactive Console but is not authorized.";
95              log.warn(msg);
96              response.getWriter().println(msg);
97              return StringUtils.EMPTY;
98          }
99          String code = request.getParameter("code");
100         log.debug("calling command {} with code {}", getCommand(), code);
101         response.setContentType("text/xml");
102 
103         PrintWriter out = response.getWriter();
104 
105         if (handleCommand(code, out)) {
106             return StringUtils.EMPTY;
107         }
108 
109         evaluate(new ByteArrayInputStream(code.getBytes()), out);
110 
111         return StringUtils.EMPTY;
112     }
113 
114     public String getGroovyVersion() {
115         return GROOVY_VERSION;
116     }
117 
118     /**
119      * @param code
120      * @param out
121      * @return
122      */
123     private void evaluate(final InputStream is, final PrintWriter out) {
124         Binding binding = (Binding) request.getSession().getAttribute(BINDING_SESSION_ATTRIBUTE);
125         MgnlGroovyConsole console = new MgnlGroovyConsole(binding != null ? binding : new SerializableBinding());
126 
127         Object lastResult = null;
128         Context originalCtx = MgnlContext.getInstance();
129         MgnlGroovyConsoleContext groovyCtx = new MgnlGroovyConsoleContext(originalCtx);
130         MgnlContext.setInstance(groovyCtx);
131         try {
132             lastResult = console.evaluate(is, console.generateScriptName(), out);
133             // Shows the result of the evaluated code
134             out.print("===> \n");
135             out.println(lastResult);
136         }
137         catch (CompilationFailedException e) {
138             out.println(e);
139         }
140         catch (Throwable e) {
141             // Unroll invoker exceptions
142             if (e instanceof InvokerInvocationException) {
143                 e = e.getCause();
144             }
145             if(e instanceof MissingPropertyException){
146                 out.println(e.getMessage());
147             } else {
148                 log.error("Error while evaluating script: ", e);
149                 out.println(e.getClass().getSimpleName() + ": " +e.getMessage());
150             }
151         } finally {
152             MgnlContext.setInstance(originalCtx);
153         }
154     }
155 
156     // need this otherwise adding Binding to the session fails with:
157     // java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute
158     // at org.apache.catalina.session.StandardSession.setAttribute(..)
159     private static final class SerializableBinding extends Binding implements Serializable {
160 
161         public static final long serialVersionUID = 42L;
162     }
163 
164     /**
165      * @param command
166      * @param out
167      * @return <code>true</code> if the command could be processed, <code>false</code> otherwise
168      * @throws RepositoryException
169      */
170     private boolean handleCommand(final String command, final PrintWriter out) throws RepositoryException {
171         if (StringUtils.isEmpty(command)) {
172             // just ignore it
173             return true;
174         }
175         String[] tokens = command.trim().split("\\s+");
176         if ("help".equals(tokens[0]) || "?".equals(tokens[0])) {
177             out.println(MessagesUtil.get("console.help", MSG_BASENAME));
178             return true;
179         }
180         else if ("clear".equals(tokens[0])) {
181             return true;
182         }
183         else if ("clean".equals(tokens[0])) {
184             request.getSession().setAttribute(BINDING_SESSION_ATTRIBUTE, new SerializableBinding());
185             return true;
186         }
187         /*
188          * TODO should we use something like apache
189          * commons cli to parse commands and arguments?
190          */
191         else if ("run".equals(tokens[0])) {
192             if (tokens.length != 2) {
193                 out.println(MessagesUtil.get("console.commands.run.usage", MSG_BASENAME));
194                 return true;
195             }
196             InputStream is = null;
197             String path = tokens[1];
198             // first try to locate the script in the scripts workspace
199             String source = NodeDataUtil.getString("scripts", path.endsWith("/") ? path + "text" : path + "/text");
200             try {
201                 if (StringUtils.isNotEmpty(source)) {
202                     is = new ByteArrayInputStream(source.getBytes());
203                 }
204                 else {
205                     // if not found in the workspace, try with the file system
206                     is = new FileInputStream(path);
207                 }
208                 evaluate(is, out);
209                 return true;
210             }
211             catch (FileNotFoundException e) {
212                 out.println(e.getMessage());
213                 return true;
214             }
215             finally {
216                 IOUtils.closeQuietly(is);
217             }
218         }
219         /*else if ("cd".equals(tokens[0])) {
220             if (tokens.length != 2) {
221                 out.println("Please, specify valid a path in the repository.");
222                 return true;
223             }
224             Binding binding = (Binding) request.getSession().getAttribute(BINDING_SESSION_ATTRIBUTE);
225             Object hm;
226             try {
227                 hm = binding.getVariable("hm");
228             }
229             catch (MissingPropertyException e) {
230                 hm = MgnlContext.getHierarchyManager("website");
231                 binding.setVariable("hm", hm);
232             }
233 
234             MgnlGroovyNode node = MgnlGroovySupport.getContent((HierarchyManager) hm, tokens[1]);
235             binding.setVariable("pwd", node);
236             request.getSession().setAttribute(BINDING_SESSION_ATTRIBUTE, binding);
237             return true;
238         }
239         else if ("cdtab".equals(tokens[0])) {
240             Binding binding = (Binding) request.getSession().getAttribute(BINDING_SESSION_ATTRIBUTE);
241             Object hm;
242             try {
243                 hm = binding.getVariable("hm");
244             }
245             catch (MissingPropertyException e) {
246                 hm = MgnlContext.getHierarchyManager("website");
247                 binding.setVariable("hm", hm);
248             }
249             Object pwd;
250             try {
251                 pwd = binding.getVariable("pwd");
252             }
253             catch (MissingPropertyException e) {
254                 pwd = MgnlGroovySupport.getContent((HierarchyManager) hm, "/");
255                 binding.setVariable("pwd", pwd);
256             }
257             out.println(ContentUtil.collectAllChildren((MgnlGroovyNode) pwd, ItemType.CONTENT));
258             return true;
259         }*/
260         return false;
261     }
262 
263     protected boolean isAuthorized(User currentUser) {
264         final Collection<String> roles = currentUser.getRoles();
265         return roles.contains("superuser") || roles.contains("scripter");
266     }
267 }