1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.webapp.filter;
20
21 import javax.faces.context.ExternalContext;
22 import javax.servlet.ServletContext;
23
24 import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
25
26
27
28
29
30
31
32
33 class MultipartRequestWrapperConfig
34 {
35
36 private int _uploadMaxSize = 100 * 1024 * 1024;
37 private int _uploadMaxFileSize = 100 * 1024 * 1024;
38 private int _uploadThresholdSize = 1 * 1024 * 1024;
39 private String _uploadRepositoryPath = null;
40 private boolean _cacheFileSizeErrors = false;
41
42 private static final String UPLOAD_MAX_SIZE = "org.apache.myfaces.UPLOAD_MAX_SIZE";
43 private static final String UPLOAD_MAX_FILE_SIZE = "org.apache.myfaces.UPLOAD_MAX_FILE_SIZE";
44 private static final String UPLOAD_THRESHOLD_SIZE = "org.apache.myfaces.UPLOAD_THRESHOLD_SIZE";
45 private static final String UPLOAD_MAX_REPOSITORY_PATH = "org.apache.myfaces.UPLOAD_MAX_REPOSITORY_PATH";
46 private static final String UPLOAD_CACHE_FILE_SIZE_ERRORS = "org.apache.myfaces.UPLOAD_CACHE_FILE_SIZE_ERRORS";
47 private static final String MULTIPART_REQUEST_WRAPPER_CONFIG = MultipartRequestWrapperConfig.class.getName();
48
49 private MultipartRequestWrapperConfig() {}
50
51 private static int resolveSize(String param, int defaultValue)
52 {
53 int numberParam = defaultValue;
54
55 if (param != null)
56 {
57 param = param.toLowerCase();
58 int factor = 1;
59 String number = param;
60
61 if (param.endsWith("g"))
62 {
63 factor = 1024 * 1024 * 1024;
64 number = param.substring(0, param.length() - 1);
65 }
66 else if (param.endsWith("m"))
67 {
68 factor = 1024 * 1024;
69 number = param.substring(0, param.length() - 1);
70 }
71 else if (param.endsWith("k"))
72 {
73 factor = 1024;
74 number = param.substring(0, param.length() - 1);
75 }
76
77 numberParam = Integer.parseInt(number) * factor;
78 }
79 return numberParam;
80 }
81
82 private static boolean getBooleanValue(String initParameter, boolean defaultVal)
83 {
84 if(initParameter == null || initParameter.trim().length()==0)
85 return defaultVal;
86
87 return (initParameter.equalsIgnoreCase("on") || initParameter.equals("1") || initParameter.equalsIgnoreCase("true"));
88 }
89
90 public int getUploadMaxSize()
91 {
92 return _uploadMaxSize;
93 }
94
95 public void setUploadMaxSize(int uploadMaxSize)
96 {
97 this._uploadMaxSize = uploadMaxSize;
98 }
99
100 public int getUploadMaxFileSize()
101 {
102 return _uploadMaxFileSize;
103 }
104
105 public void setUploadMaxFileSize(int uploadMaxFileSize)
106 {
107 this._uploadMaxFileSize = uploadMaxFileSize;
108 }
109
110 public int getUploadThresholdSize()
111 {
112 return _uploadThresholdSize;
113 }
114
115 public void setUploadThresholdSize(int uploadThresholdSize)
116 {
117 this._uploadThresholdSize = uploadThresholdSize;
118 }
119
120 public String getUploadRepositoryPath()
121 {
122 return _uploadRepositoryPath;
123 }
124
125 public void setUploadRepositoryPath(String uploadRepositoryPath)
126 {
127 this._uploadRepositoryPath = uploadRepositoryPath;
128 }
129
130 public boolean isCacheFileSizeErrors()
131 {
132 return _cacheFileSizeErrors;
133 }
134
135 public void setCacheFileSizeErrors(boolean cacheFileSizeErrors)
136 {
137 this._cacheFileSizeErrors = cacheFileSizeErrors;
138 }
139
140 public static MultipartRequestWrapperConfig getMultipartRequestWrapperConfig(
141 ExternalContext context)
142 {
143
144 MultipartRequestWrapperConfig config = (MultipartRequestWrapperConfig) context
145 .getApplicationMap().get(MULTIPART_REQUEST_WRAPPER_CONFIG);
146
147 if (config == null)
148 {
149 config = new MultipartRequestWrapperConfig();
150
151 if(!ExternalContextUtils.getRequestType(context).isPortlet())
152 {
153 ServletContext servletContext = (ServletContext) context
154 .getContext();
155
156 String param = servletContext
157 .getInitParameter(UPLOAD_MAX_FILE_SIZE);
158
159 config._uploadMaxFileSize = resolveSize(param,
160 config._uploadMaxFileSize);
161
162 param = servletContext
163 .getInitParameter(UPLOAD_MAX_SIZE);
164
165 if (param != null)
166 {
167 config._uploadMaxSize = resolveSize(param,
168 config._uploadMaxSize);
169 }
170 else
171 {
172
173 config._uploadMaxSize = resolveSize(param,
174 config._uploadMaxFileSize);
175 }
176
177 param = servletContext.getInitParameter(UPLOAD_THRESHOLD_SIZE);
178
179 config._uploadThresholdSize = resolveSize(param,
180 config._uploadThresholdSize);
181
182 config._uploadRepositoryPath = servletContext
183 .getInitParameter(UPLOAD_MAX_REPOSITORY_PATH);
184
185 config._cacheFileSizeErrors = getBooleanValue(servletContext
186 .getInitParameter(UPLOAD_CACHE_FILE_SIZE_ERRORS), false);
187
188 context.getApplicationMap().put(MULTIPART_REQUEST_WRAPPER_CONFIG,
189 config);
190 }
191 else
192 {
193 Object portletContext = context.getContext();
194
195 String param = PortletUtils.getContextInitParameter(
196 portletContext, UPLOAD_MAX_FILE_SIZE);
197
198 config._uploadMaxFileSize = resolveSize(param,
199 config._uploadMaxFileSize);
200
201 param = PortletUtils.getContextInitParameter(
202 portletContext, UPLOAD_MAX_SIZE);
203
204 if (param != null)
205 {
206 config._uploadMaxSize = resolveSize(param,
207 config._uploadMaxSize);
208 }
209 else
210 {
211
212 config._uploadMaxSize = resolveSize(param,
213 config._uploadMaxFileSize);
214 }
215
216 param = PortletUtils.getContextInitParameter(
217 portletContext, UPLOAD_THRESHOLD_SIZE);
218
219 config._uploadThresholdSize = resolveSize(param,
220 config._uploadThresholdSize);
221
222 config._uploadRepositoryPath = PortletUtils.getContextInitParameter(
223 portletContext,UPLOAD_MAX_REPOSITORY_PATH);
224
225 config._cacheFileSizeErrors = getBooleanValue(PortletUtils
226 .getContextInitParameter(portletContext,
227 UPLOAD_CACHE_FILE_SIZE_ERRORS), false);
228
229 context.getApplicationMap().put(MULTIPART_REQUEST_WRAPPER_CONFIG,
230 config);
231 }
232 }
233
234 return config;
235 }
236 }