View Javadoc
1   /**
2    * This file Copyright (c) 2015 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.ui;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.cms.filters.AbstractMgnlFilter;
38  import info.magnolia.cms.filters.FilterManager;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.cms.util.ServletUtil;
41  import info.magnolia.context.Context;
42  import info.magnolia.context.MgnlContext;
43  import info.magnolia.context.WebContextImpl;
44  import info.magnolia.module.ModuleManagementException;
45  import info.magnolia.module.ModuleManager;
46  
47  import java.io.IOException;
48  import java.io.Writer;
49  import java.util.Locale;
50  
51  import javax.servlet.FilterChain;
52  import javax.servlet.FilterConfig;
53  import javax.servlet.ServletContext;
54  import javax.servlet.ServletException;
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.apache.commons.lang3.StringUtils;
59  
60  /**
61   * Filter responsible for executing the update/install mechanism.
62   */
63  public class InstallationFilter extends AbstractMgnlFilter {
64      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(InstallationFilter.class);
65  
66      private final ModuleManager moduleManager;
67      private final FilterManager filterManager;
68      private ServletContext servletContext;
69  
70      public InstallationFilter(ModuleManager moduleManager, FilterManager filterManager) {
71          this.moduleManager = moduleManager;
72          this.filterManager = filterManager;
73      }
74  
75      @Override
76      public void init(FilterConfig filterConfig) throws ServletException {
77          super.init(filterConfig);
78          servletContext = filterConfig.getServletContext();
79      }
80  
81      @Override
82      public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
83          // this isn't the cleanest thing, but we're basically tricking FreemarkerHelper into using a Context, while avoiding using WebContextImpl and its depedencies on the repository
84          final Context originalContext = MgnlContext.hasInstance() ? MgnlContext.getInstance() : null;
85          final InstallWebContext ctx = new InstallWebContext();
86          ctx.init(request, response, servletContext);
87          MgnlContext.setInstance(ctx);
88  
89          try {
90              final String contextPath = request.getContextPath();
91              // TODO : this will be invalid the day we allow other resources (css, images) to be served through the installer
92              response.setContentType("text/html");
93              final Writer out = response.getWriter();
94              final String uri = ServletUtil.stripPathParameters(request.getRequestURI());
95              final ModuleManagerUI ui = moduleManager.getUI();
96  
97              final String prefix = contextPath + ModuleManagerWebUI.INSTALLER_PATH;
98              if (uri.startsWith(prefix)) {
99                  final String command = StringUtils.defaultIfEmpty(StringUtils.substringAfter(uri, prefix + "/"), null);
100                 final boolean installDone = ui.execute(out, command);
101                 if (installDone) {
102                     filterManager.startUsingConfiguredFilters();
103                     // invalidate session: MAGNOLIA-2611
104                     request.getSession().invalidate();
105                     // redirect to root
106                     response.sendRedirect(contextPath + "/");
107                 }
108             } else {
109                 // redirect to /.magnolia/installer - even if it has no concrete effect, the change in the browser's url bar makes this more explicit
110                 response.sendRedirect(prefix);
111             }
112         } catch (ModuleManagementException e) {
113             log.error(e.getMessage(), e);
114             throw new RuntimeException(e); // TODO
115         } finally {
116             MgnlContext.release();
117             MgnlContext.setInstance(originalContext);
118         }
119     }
120 
121     private final static class InstallWebContext extends WebContextImpl {
122         @Override
123         public User getUser() {
124             return null;
125         }
126 
127         @Override
128         public Locale getLocale() {
129             return MgnlContext.getSystemContext().getLocale();
130         }
131 
132         @Override
133         protected AggregationState newAggregationState() {
134             throw new IllegalStateException("AggregationState is not meant to be used during the installation process.");
135         }
136     }
137 }