1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.action;
17
18 import org.apache.commons.beanutils.ConvertUtils;
19 import org.apache.commons.beanutils.converters.BigDecimalConverter;
20 import org.apache.commons.beanutils.converters.BigIntegerConverter;
21 import org.apache.commons.beanutils.converters.BooleanConverter;
22 import org.apache.commons.beanutils.converters.ByteConverter;
23 import org.apache.commons.beanutils.converters.CharacterConverter;
24 import org.apache.commons.beanutils.converters.DoubleConverter;
25 import org.apache.commons.beanutils.converters.FloatConverter;
26 import org.apache.commons.beanutils.converters.IntegerConverter;
27 import org.apache.commons.beanutils.converters.LongConverter;
28 import org.apache.commons.beanutils.converters.ShortConverter;
29 import org.apache.commons.collections.iterators.IteratorEnumeration;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32 import org.apache.struts.action.ActionServlet;
33 import org.kuali.rice.core.api.config.ConfigurationException;
34 import org.kuali.rice.core.api.config.property.ConfigContext;
35 import org.kuali.rice.core.framework.config.module.ModuleConfigurer;
36 import org.kuali.rice.core.framework.config.module.WebModuleConfiguration;
37
38 import java.io.IOException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.ServletConfig;
42 import javax.servlet.ServletContext;
43 import javax.servlet.ServletException;
44 import java.math.BigDecimal;
45 import java.math.BigInteger;
46 import java.util.Collection;
47 import java.util.Enumeration;
48 import java.util.HashMap;
49 import java.util.Map;
50
51 public class KualiActionServlet extends ActionServlet {
52 private static final Logger LOG = Logger.getLogger(KualiActionServlet.class);
53
54
55 private String parameterEncoding = "";
56
57
58
59
60
61
62
63
64 @Override
65 protected void initOther() throws ServletException {
66
67 String value = null;
68 value = getServletConfig().getInitParameter("config");
69 if (value != null) {
70 config = value;
71 }
72
73
74
75 value = getServletConfig().getInitParameter("convertNull");
76 if ("true".equalsIgnoreCase(value)
77 || "yes".equalsIgnoreCase(value)
78 || "on".equalsIgnoreCase(value)
79 || "y".equalsIgnoreCase(value)
80 || "1".equalsIgnoreCase(value)) {
81
82 convertNull = true;
83 }
84
85 if (convertNull) {
86 ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
87 ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
88 ConvertUtils.register(new BooleanConverter(null), Boolean.class);
89 ConvertUtils.register(new ByteConverter(null), Byte.class);
90 ConvertUtils.register(new CharacterConverter(null), Character.class);
91 ConvertUtils.register(new DoubleConverter(null), Double.class);
92 ConvertUtils.register(new FloatConverter(null), Float.class);
93 ConvertUtils.register(new IntegerConverter(null), Integer.class);
94 ConvertUtils.register(new LongConverter(null), Long.class);
95 ConvertUtils.register(new ShortConverter(null), Short.class);
96 }
97
98
99 parameterEncoding = getServletConfig().getInitParameter("PARAMETER_ENCODING");
100 }
101
102 KualiActionServletConfig serverConfigOverride = null;
103
104 @Override
105 public ServletConfig getServletConfig() {
106 if ( serverConfigOverride == null ) {
107 ServletConfig sConfig = super.getServletConfig();
108
109 if ( sConfig == null ) {
110 return null;
111 }
112 serverConfigOverride = new KualiActionServletConfig(sConfig);
113 }
114 return serverConfigOverride;
115 }
116
117
118
119
120
121
122 private class KualiActionServletConfig implements ServletConfig {
123
124 private ServletConfig wrapped;
125 private Map<String,String> initParameters = new HashMap<String, String>();
126
127 public KualiActionServletConfig(ServletConfig wrapped) {
128 this.wrapped = wrapped;
129
130 @SuppressWarnings("unchecked")
131 final Enumeration<String> initParameterNames = wrapped.getInitParameterNames();
132 while ( initParameterNames.hasMoreElements() ) {
133 String paramName = initParameterNames.nextElement();
134 initParameters.put( paramName, wrapped.getInitParameter(paramName) );
135 }
136
137
138 final Collection<ModuleConfigurer> riceModules = ModuleConfigurer.getCurrentContextConfigurers();
139
140 if ( LOG.isInfoEnabled() ) {
141 LOG.info( "Configuring init parameters of the KualiActionServlet from riceModules: " + riceModules );
142 }
143 for ( ModuleConfigurer module : riceModules ) {
144
145
146
147 if ( module.shouldRenderWebInterface() ) {
148 WebModuleConfiguration webModuleConfiguration = module.getWebModuleConfiguration();
149 if (webModuleConfiguration == null) {
150 throw new ConfigurationException("Attempting to load WebModuleConfiguration for module '" + module.getModuleName() + "' but no configuration was provided!");
151 }
152 if ( LOG.isInfoEnabled() ) {
153 LOG.info( "Configuring Web Content for Module: " + webModuleConfiguration.getModuleName()
154 + " / " + webModuleConfiguration.getWebModuleStrutsConfigName()
155 + " / " + webModuleConfiguration.getWebModuleStrutsConfigurationFiles()
156 + " / Base URL: " + webModuleConfiguration.getWebModuleBaseUrl() );
157 }
158 if ( !initParameters.containsKey( webModuleConfiguration.getWebModuleStrutsConfigName() ) ) {
159 initParameters.put( webModuleConfiguration.getWebModuleStrutsConfigName(), webModuleConfiguration.getWebModuleStrutsConfigurationFiles() );
160 }
161 }
162 }
163 }
164
165 @Override
166 public String getInitParameter(String name) {
167 return initParameters.get(name);
168 }
169
170 @Override
171 @SuppressWarnings("unchecked")
172 public Enumeration<String> getInitParameterNames() {
173 return new IteratorEnumeration( initParameters.keySet().iterator() );
174 }
175
176 @Override
177 public ServletContext getServletContext() {
178 return wrapped.getServletContext();
179 }
180 @Override
181 public String getServletName() {
182 return wrapped.getServletName();
183 }
184 }
185
186
187
188
189
190
191 @Override
192 protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
193 if (StringUtils.isNotBlank(parameterEncoding)) {
194 request.setCharacterEncoding(parameterEncoding);
195 response.setCharacterEncoding(parameterEncoding);
196 }
197
198 super.process(request, response);
199 }
200 }