View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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.rescue;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.cms.security.RescueSecuritySupport;
38  import info.magnolia.cms.security.SecuritySupport;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.SingleJCRSessionSystemContext;
41  import info.magnolia.i18nsystem.SimpleTranslator;
42  import info.magnolia.i18nsystem.TranslationService;
43  import info.magnolia.module.groovy.terminal.Terminal;
44  import info.magnolia.objectfactory.Components;
45  import info.magnolia.ui.framework.message.MessagesManager;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  import com.vaadin.annotations.Title;
51  import com.vaadin.server.VaadinRequest;
52  import com.vaadin.ui.CssLayout;
53  import com.vaadin.ui.UI;
54  
55  /**
56   * A special app which makes at your disposal the Magnolia's Groovy interactive console bypassing the
57   * normal Magnolia's filters chain. This can be useful, for instance, to perform rescue operations on a corrupted Magnolia instance.
58   * <strong>Warning:</strong> All operations are executed in system context, meaning that no security restrictions are enforced which
59   * might expose your data to risk of irreversible damages if you are not aware of what you are doing. In other words, <strong>use it at your own risk</strong>. <br>
60   * To use it, in your <code>web.xml</code> you must explicitly comment out the Magnolia filter chain part and add or uncomment the Vaadin servlet.
61   * For a sample configuration, see the <code>web.xml</code> fragment below:
62   *
63   * <pre>
64   *   [...]
65   *   &lt;!--
66   *   &lt;filter&gt;
67   *     &lt;display-name&gt;Magnolia global filters&lt;/display-name&gt;
68   *     &lt;filter-name&gt;magnoliaFilterChain&lt;/filter-name&gt;
69   *     &lt;filter-class&gt;info.magnolia.cms.filters.MgnlMainFilter&lt;/filter-class&gt;
70   *   &lt;/filter&gt;
71   *   &lt;filter-mapping&gt;
72   *     &lt;filter-name&gt;magnoliaFilterChain&lt;/filter-name&gt;
73   *     &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
74   *     &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
75   *     &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
76   *     &lt;dispatcher&gt;INCLUDE&lt;/dispatcher&gt;
77   *     &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt;
78   *   &lt;/filter-mapping&gt;
79   *   --&gt;
80   *   &lt;servlet&gt;
81   *     &lt;servlet-name&gt;Vaadin&lt;/servlet-name&gt;
82   *     &lt;servlet-class&gt;com.vaadin.server.VaadinServlet&lt;/servlet-class&gt;
83   *       &lt;init-param&gt;
84   *        &lt;description&gt;Groovy Rescue App&lt;/description&gt;
85   *        &lt;param-name&gt;UI&lt;/param-name&gt;
86   *        &lt;param-value&gt;info.magnolia.module.groovy.rescue.MgnlGroovyRescueApp&lt;/param-value&gt;
87   *       &lt;/init-param&gt;
88   *   &lt;/servlet&gt;
89   *   &lt;servlet-mapping&gt;
90   *      &lt;servlet-name&gt;Vaadin&lt;/servlet-name&gt;
91   *      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
92   *   &lt;/servlet-mapping&gt;
93   *  [...]
94   * </pre>
95   *
96   * Once configured and having restarted the web container, you can access the console like this
97   * http://server[:port]/contextname
98   * <p>
99   * If you need to restart the app, you can append the Vaadin <code>?restartApplication</code> to the URL.
100  */
101 
102 @Title("Magnolia Groovy Rescue App")
103 public class MgnlGroovyRescueApp extends UI {
104     private static final long serialVersionUID = 222L;
105 
106     private static final Logger log = LoggerFactory.getLogger(MgnlGroovyRescueApp.class);
107 
108     private CssLayout layout = new CssLayout();
109 
110     @Override
111     protected void init(VaadinRequest request) {
112         log.info("Initializing MgnlGroovyRescueApp...");
113         log.warn("Setting value of {} to {}", SecuritySupport.class.getName(), RescueSecuritySupport.class.getName());
114         // following call will implicitly register this implementation.
115         Components.newInstance(RescueSecuritySupport.class);
116         SystemProperty.setProperty(SecuritySupport.class.getName(), RescueSecuritySupport.class.getName());
117 
118         MgnlContext.setInstance(new SingleJCRSessionSystemContext());
119 
120         layout.setSizeFull();
121         TranslationService translationService = Components.getComponent(TranslationService.class);
122         translationService.reloadMessageBundles();
123 
124         // no notification to send to pulse here
125         Terminal terminal = new Terminal(Components.getComponent(SimpleTranslator.class), true, false, Components.getComponent(MessagesManager.class));
126 
127         layout.addComponent(terminal);
128         setContent(layout);
129     }
130 }