View Javadoc

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