View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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   * @author gjoseph
47   * @version $Revision: $ ($Author: $)
48   */
49  public class WebXmlConditionsUtil {
50      private final WebXmlUtil webXmlUtil;
51      private final List<Condition> conditions;
52  
53      public WebXmlConditionsUtil(List<Condition> conditions) {
54          this(new WebXmlUtil(), conditions);
55      }
56  
57      WebXmlConditionsUtil(WebXmlUtil webXmlUtil, List<Condition> conditions) {
58          this.webXmlUtil = webXmlUtil;
59          this.conditions = conditions;
60      }
61  
62      public void servletIsNowWrapped(final String servletName) {
63          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
64              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."));
65          }
66      }
67  
68      public void servletIsDeprecated(final String servletName) {
69          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
70              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."));
71          }
72      }
73  
74      public void servletIsRemoved(final String servletName) {
75          if (webXmlUtil.isServletOrMappingRegistered(servletName)) {
76              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."));
77          }
78      }
79      
80      public void servletIsPresent(String servletName) {
81          if (!webXmlUtil.isServletRegistered(servletName)) {
82              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."));
83          }
84      }
85  
86      public void filterMustBeRegisteredWithCorrectDispatchers(final String filterClass) {
87          final String conditionName = "web.xml updates";
88          final String message = "Since Magnolia 3.5, the main Magnolia filter is " + filterClass + ", and it must be mapped with dispatchers REQUEST, FORWARD and, optionally, ERROR. The INCLUDE dispatcher is not supported.";
89          final String additionalMessage = " Please add \n"
90                  + " <dispatcher>REQUEST</dispatcher>\n"
91                  + " <dispatcher>FORWARD</dispatcher>\n"
92                  + " <dispatcher>ERROR</dispatcher>\n"
93                  + " to the filter-mapping element in your web.xml file.";
94  
95          if (!webXmlUtil.isFilterRegistered(filterClass)) {
96              conditions.add(new FalseCondition(conditionName, message));
97          } else {
98              final int result = webXmlUtil.checkFilterDispatchersConfiguration(filterClass, Arrays.asList(new String[]{"REQUEST", "FORWARD"}), Collections.singletonList("ERROR"));
99              if (result > 0) {
100                 conditions.add(new TrueCondition(conditionName, message));
101             } else if (result == 0) {
102                 conditions.add(new WarnCondition(conditionName, message));
103             } else if (result < 0) {
104                 conditions.add(new FalseCondition(conditionName + additionalMessage, message));
105             }
106         }
107     }
108 
109     public void filterIsDeprecated(final String deprecatedFilterClass, final String replacementFilterClass) {
110         if (webXmlUtil.isFilterRegistered(deprecatedFilterClass)) {
111             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."));
112         }
113     }
114 
115     public void listenerIsDeprecated(final String deprecatedListenerClass, final String replacementListenerClass) {
116         if (webXmlUtil.isListenerRegistered(deprecatedListenerClass)) {
117             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."));
118         }
119     }
120 
121     public void listenerMustBeRegistered(String listenerClass) {
122         if (!webXmlUtil.isListenerRegistered(listenerClass)) {
123             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."));
124         }
125     }
126 }