View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.delta;
35  
36  import info.magnolia.cms.util.WebXmlUtil;
37  
38  import java.util.Arrays;
39  import java.util.Collections;
40  import java.util.List;
41  
42  /**
43   * A utility class for web.xml related conditions, which will add
44   * conditions to a given list of tasks based on some conditions.
45   */
46  public class WebXmlConditionsUtil {
47      private final WebXmlUtil webXmlUtil;
48      private final List<Condition> conditions;
49  
50      public WebXmlConditionsUtil(List<Condition> conditions) {
51          this(new WebXmlUtil(), conditions);
52      }
53  
54      WebXmlConditionsUtil(WebXmlUtil webXmlUtil, List<Condition> conditions) {
55          this.webXmlUtil = webXmlUtil;
56          this.conditions = conditions;
57      }
58  
59      public void servletIsNowWrapped(final String servletName) {
60          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
61              conditions.add(new FalseCondition("web.xml updates", "Since Magnolia 3.5, servlets are wrapped in ServletDispatchingFilter; please remove the <servlet> and <servlet-mapping> elements for " + servletName + " from your web.xml file."));
62          }
63      }
64  
65      public void servletIsDeprecated(final String servletName) {
66          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
67              conditions.add(new FalseCondition("web.xml updates", "The " + servletName + " servlet is deprecated and not in use; please remove the corresponding <servlet> and <servlet-mapping> elements from your web.xml file."));
68          }
69      }
70  
71      public void servletIsRemoved(final String servletName) {
72          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
73              conditions.add(new FalseCondition("web.xml updates", "The " + servletName + " servlet does not exist anymore; please remove the corresponding <servlet> and <servlet-mapping> elements from your web.xml file."));
74          }
75      }
76  
77      public void servletIsPresent(String servletName) {
78          if (!webXmlUtil.isServletRegistered(servletName)) {
79              conditions.add(new FalseCondition("web.xml updates", "The " + servletName + " servlet is not installed. Please configure a <servlet> and an appropriate mapping with name " + servletName + " in your web.xml file."));
80          }
81      }
82  
83      public void filterMustBeRegisteredWithCorrectDispatchers(final String filterClass) {
84          final String conditionName = "web.xml updates";
85          final String message = "Since Magnolia 4.4, the main Magnolia filter is " + filterClass + ", and it must be mapped with dispatchers REQUEST, FORWARD, INCLUDE and, optionally, ERROR.";
86          final String additionalMessage = " Please add \n"
87                  + " <dispatcher>REQUEST</dispatcher>\n"
88                  + " <dispatcher>FORWARD</dispatcher>\n"
89                  + " <dispatcher>INCLUDE</dispatcher>\n"
90                  + " <dispatcher>ERROR</dispatcher>\n"
91                  + " to the filter-mapping element in your web.xml file.";
92  
93          if (!webXmlUtil.isFilterRegistered(filterClass)) {
94              conditions.add(new FalseCondition(conditionName, message));
95          } else {
96              final int result = webXmlUtil.checkFilterDispatchersConfiguration(filterClass, Arrays.asList("REQUEST", "FORWARD", "INCLUDE"), Collections.singletonList("ERROR"));
97              if (result > 0) {
98                  conditions.add(new TrueCondition(conditionName, message));
99              } else if (result == 0) {
100                 conditions.add(new WarnCondition(conditionName, message));
101             } else if (result < 0) {
102                 conditions.add(new FalseCondition(conditionName, message + additionalMessage));
103             }
104         }
105     }
106 
107     public void filterIsDeprecated(final String deprecatedFilterClass, final String replacementFilterClass) {
108         if (webXmlUtil.isFilterRegistered(deprecatedFilterClass)) {
109             conditions.add(new FalseCondition("web.xml updates", "The " + deprecatedFilterClass + " filter class is now deprecated. Please replace it with " + replacementFilterClass + ": please update the corresponding <filter-class> element in your web.xml file."));
110         }
111     }
112 
113     public void listenerIsDeprecated(final String deprecatedListenerClass, final String replacementListenerClass) {
114         if (webXmlUtil.isListenerRegistered(deprecatedListenerClass)) {
115             conditions.add(new FalseCondition("web.xml updates", "The " + deprecatedListenerClass + " listener class is now deprecated. Please replace it with " + replacementListenerClass + ": please update the corresponding <listener> element in your web.xml file."));
116         }
117     }
118 
119     public void listenerMustBeRegistered(String listenerClass) {
120         if (!webXmlUtil.isListenerRegistered(listenerClass)) {
121             conditions.add(new FalseCondition("web.xml updates", "The " + listenerClass + " listener is not installed. Please configure a <listener> with class " + listenerClass + " in your web.xml file."));
122         }
123     }
124 }