Skip to content

Commit d950f03

Browse files
committed
Refactor to throw servlet exception
1 parent af01542 commit d950f03

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.bastillion</groupId>
55
<artifactId>lmvc</artifactId>
6-
<version>1.10.3</version>
6+
<version>1.10.4</version>
77
<packaging>jar</packaging>
88
<name>Loophole MVC</name>
99
<description>Loophole - Model-View-Controller Framework</description>

src/main/java/loophole/mvc/base/BaseKontroller.java

+40-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
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
55
*/
66
package loophole.mvc.base;
77

@@ -13,12 +13,25 @@
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
1515

16+
import javax.servlet.ServletException;
1617
import javax.servlet.http.HttpServletRequest;
1718
import javax.servlet.http.HttpServletResponse;
1819
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;
2027
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;
2235

2336
/**
2437
* Base controller class that initializes all controllers and is called through
@@ -46,7 +59,7 @@ public class BaseKontroller {
4659
for (File directory : dirs) {
4760
loadKontrollers(directory, packageNm);
4861
}
49-
} catch (Exception ex) {
62+
} catch (ClassNotFoundException | IOException ex) {
5063
log.error(ex.toString(), ex);
5164
}
5265
}
@@ -96,7 +109,7 @@ private static List<Field> getAllFields(Class<?> type) {
96109
*
97110
* @return page to forward / redirect
98111
*/
99-
public String execute() {
112+
public String execute() throws ServletException {
100113
String forward = null;
101114

102115
for (Class<?> clazz : ktrlList) {
@@ -161,10 +174,9 @@ public String execute() {
161174
request.setAttribute(v.name(), null);
162175
try {
163176
request.setAttribute(v.name(), field.getType().getDeclaredConstructor().newInstance());
164-
} catch (NoSuchMethodException ex){
165-
//ignore exception
177+
} catch (NoSuchMethodException ex) {
178+
//ignore exception
166179
}
167-
168180
}
169181
}
170182
}
@@ -173,8 +185,9 @@ public String execute() {
173185
request.setAttribute("errors", ctrl.getErrors());
174186
request.setAttribute("fieldErrors", ctrl.getFieldErrors());
175187

176-
} catch (Exception ex) {
188+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | ClassNotFoundException ex) {
177189
log.error(ex.toString(), ex);
190+
throw new ServletException(ex.toString(), ex);
178191
}
179192
}
180193
}
@@ -186,7 +199,7 @@ public String execute() {
186199
}
187200

188201
private void setFieldFromParams(Object ctrl, String param, HttpServletRequest request)
189-
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
202+
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
190203

191204
if (param != null) {
192205

@@ -212,18 +225,15 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
212225
for (String k : requestMap.keySet()) {
213226
Object keyOb = null;
214227
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+
227237
map.put(keyOb, valOb);
228238
}
229239
field.set(ctrl, map);
@@ -237,14 +247,11 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
237247
List list = List.class.cast(field.get(ctrl));
238248
for (String p : parameterMap.get(param)) {
239249
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+
248255
list.add(valOb);
249256
}
250257
field.set(ctrl, list);
@@ -290,7 +297,7 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
290297
field.set(ctrl, null);
291298
try {
292299
field.set(ctrl, field.getType().getDeclaredConstructor().newInstance());
293-
} catch (NoSuchMethodException ex){
300+
} catch (NoSuchMethodException ex) {
294301
//ignore exception
295302
}
296303
}

src/main/java/loophole/mvc/filter/CSRFFilter.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

13-
import javax.servlet.*;
13+
import javax.servlet.Filter;
14+
import javax.servlet.FilterChain;
15+
import javax.servlet.FilterConfig;
16+
import javax.servlet.ServletException;
17+
import javax.servlet.ServletRequest;
18+
import javax.servlet.ServletResponse;
1419
import javax.servlet.annotation.WebFilter;
1520
import javax.servlet.http.HttpServletRequest;
1621
import javax.servlet.http.HttpServletResponse;

src/main/java/loophole/mvc/filter/SecurityFilter.java

-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
*/
66
package loophole.mvc.filter;
77

8-
import loophole.mvc.base.DispatcherServlet;
9-
import loophole.mvc.base.TemplateServlet;
108
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
1210

1311
import java.io.IOException;
14-
import java.math.BigInteger;
1512
import java.security.SecureRandom;
1613

1714
import javax.servlet.*;
1815
import javax.servlet.annotation.WebFilter;
19-
import javax.servlet.http.HttpServletRequest;
2016
import javax.servlet.http.HttpServletResponse;
2117

2218
/**
@@ -25,8 +21,6 @@
2521
@WebFilter(urlPatterns = {"/*"})
2622
public class SecurityFilter implements Filter {
2723

28-
private static Logger log = LoggerFactory.getLogger(SecurityFilter.class);
29-
3024
// csrf parameter and session name
3125
public static final String _CSRF = "_csrf";
3226
private static final SecureRandom random = new SecureRandom();

0 commit comments

Comments
 (0)