1
1
/**
2
- * Copyright (C) 2018 Loophole, LLC
3
- *
4
- * Licensed under The Prosperity Public License 3.0.0
2
+ * Copyright (C) 2018 Loophole, LLC
3
+ * <p>
4
+ * Licensed under The Prosperity Public License 3.0.0
5
5
*/
6
6
package loophole .mvc .base ;
7
7
13
13
import org .slf4j .Logger ;
14
14
import org .slf4j .LoggerFactory ;
15
15
16
+ import javax .servlet .ServletException ;
16
17
import javax .servlet .http .HttpServletRequest ;
17
18
import javax .servlet .http .HttpServletResponse ;
18
19
import java .io .File ;
19
- import java .lang .reflect .*;
20
+ import java .io .IOException ;
21
+ import java .lang .reflect .Constructor ;
22
+ import java .lang .reflect .Field ;
23
+ import java .lang .reflect .InvocationTargetException ;
24
+ import java .lang .reflect .Method ;
25
+ import java .lang .reflect .ParameterizedType ;
26
+ import java .lang .reflect .Type ;
20
27
import java .net .URL ;
21
- import java .util .*;
28
+ import java .util .ArrayList ;
29
+ import java .util .Arrays ;
30
+ import java .util .Enumeration ;
31
+ import java .util .HashMap ;
32
+ import java .util .LinkedHashMap ;
33
+ import java .util .List ;
34
+ import java .util .Map ;
22
35
23
36
/**
24
37
* Base controller class that initializes all controllers and is called through
@@ -46,7 +59,7 @@ public class BaseKontroller {
46
59
for (File directory : dirs ) {
47
60
loadKontrollers (directory , packageNm );
48
61
}
49
- } catch (Exception ex ) {
62
+ } catch (ClassNotFoundException | IOException ex ) {
50
63
log .error (ex .toString (), ex );
51
64
}
52
65
}
@@ -96,7 +109,7 @@ private static List<Field> getAllFields(Class<?> type) {
96
109
*
97
110
* @return page to forward / redirect
98
111
*/
99
- public String execute () {
112
+ public String execute () throws ServletException {
100
113
String forward = null ;
101
114
102
115
for (Class <?> clazz : ktrlList ) {
@@ -161,10 +174,9 @@ public String execute() {
161
174
request .setAttribute (v .name (), null );
162
175
try {
163
176
request .setAttribute (v .name (), field .getType ().getDeclaredConstructor ().newInstance ());
164
- } catch (NoSuchMethodException ex ){
165
- //ignore exception
177
+ } catch (NoSuchMethodException ex ) {
178
+ //ignore exception
166
179
}
167
-
168
180
}
169
181
}
170
182
}
@@ -173,8 +185,9 @@ public String execute() {
173
185
request .setAttribute ("errors" , ctrl .getErrors ());
174
186
request .setAttribute ("fieldErrors" , ctrl .getFieldErrors ());
175
187
176
- } catch (Exception ex ) {
188
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | ClassNotFoundException ex ) {
177
189
log .error (ex .toString (), ex );
190
+ throw new ServletException (ex .toString (), ex );
178
191
}
179
192
}
180
193
}
@@ -186,7 +199,7 @@ public String execute() {
186
199
}
187
200
188
201
private void setFieldFromParams (Object ctrl , String param , HttpServletRequest request )
189
- throws IllegalAccessException , InstantiationException , NoSuchMethodException , InvocationTargetException {
202
+ throws IllegalAccessException , InstantiationException , NoSuchMethodException , InvocationTargetException , ClassNotFoundException {
190
203
191
204
if (param != null ) {
192
205
@@ -212,18 +225,15 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
212
225
for (String k : requestMap .keySet ()) {
213
226
Object keyOb = null ;
214
227
Object valOb = null ;
215
- try {
216
- Class <?> theClass = Class .forName (keyType .getTypeName ());
217
- Constructor <?> cons = theClass .getConstructor (String .class );
218
- keyOb = cons .newInstance (k );
219
-
220
- theClass = Class .forName (valueType .getTypeName ());
221
- cons = theClass .getConstructor (String .class );
222
- valOb = cons .newInstance (requestMap .get (k ));
223
- log .debug ("Setting " + param + " : " + keyOb + " - " + valOb );
224
- } catch (ClassNotFoundException ex ) {
225
- log .error (ex .toString (), ex );
226
- }
228
+ Class <?> theClass = Class .forName (keyType .getTypeName ());
229
+ Constructor <?> cons = theClass .getConstructor (String .class );
230
+ keyOb = cons .newInstance (k );
231
+
232
+ theClass = Class .forName (valueType .getTypeName ());
233
+ cons = theClass .getConstructor (String .class );
234
+ valOb = cons .newInstance (requestMap .get (k ));
235
+ log .debug ("Setting " + param + " : " + keyOb + " - " + valOb );
236
+
227
237
map .put (keyOb , valOb );
228
238
}
229
239
field .set (ctrl , map );
@@ -237,14 +247,11 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
237
247
List list = List .class .cast (field .get (ctrl ));
238
248
for (String p : parameterMap .get (param )) {
239
249
Object valOb = null ;
240
- try {
241
- Class <?> theClass = Class .forName (valueType .getTypeName ());
242
- Constructor <?> cons = theClass .getConstructor (String .class );
243
- valOb = cons .newInstance (p );
244
- log .debug ("Setting " + param + " : " + valOb );
245
- } catch (ClassNotFoundException ex ) {
246
- log .error (ex .toString (), ex );
247
- }
250
+ Class <?> theClass = Class .forName (valueType .getTypeName ());
251
+ Constructor <?> cons = theClass .getConstructor (String .class );
252
+ valOb = cons .newInstance (p );
253
+ log .debug ("Setting " + param + " : " + valOb );
254
+
248
255
list .add (valOb );
249
256
}
250
257
field .set (ctrl , list );
@@ -290,7 +297,7 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
290
297
field .set (ctrl , null );
291
298
try {
292
299
field .set (ctrl , field .getType ().getDeclaredConstructor ().newInstance ());
293
- } catch (NoSuchMethodException ex ){
300
+ } catch (NoSuchMethodException ex ) {
294
301
//ignore exception
295
302
}
296
303
}
0 commit comments