Skip to content

Commit

Permalink
Refactor to throw servlet exception
Browse files Browse the repository at this point in the history
  • Loading branch information
skavanagh committed Feb 12, 2022
1 parent af01542 commit d950f03
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.bastillion</groupId>
<artifactId>lmvc</artifactId>
<version>1.10.3</version>
<version>1.10.4</version>
<packaging>jar</packaging>
<name>Loophole MVC</name>
<description>Loophole - Model-View-Controller Framework</description>
Expand Down
73 changes: 40 additions & 33 deletions src/main/java/loophole/mvc/base/BaseKontroller.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Copyright (C) 2018 Loophole, LLC
*
* Licensed under The Prosperity Public License 3.0.0
* Copyright (C) 2018 Loophole, LLC
* <p>
* Licensed under The Prosperity Public License 3.0.0
*/
package loophole.mvc.base;

Expand All @@ -13,12 +13,25 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.reflect.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Base controller class that initializes all controllers and is called through
Expand Down Expand Up @@ -46,7 +59,7 @@ public class BaseKontroller {
for (File directory : dirs) {
loadKontrollers(directory, packageNm);
}
} catch (Exception ex) {
} catch (ClassNotFoundException | IOException ex) {
log.error(ex.toString(), ex);
}
}
Expand Down Expand Up @@ -96,7 +109,7 @@ private static List<Field> getAllFields(Class<?> type) {
*
* @return page to forward / redirect
*/
public String execute() {
public String execute() throws ServletException {
String forward = null;

for (Class<?> clazz : ktrlList) {
Expand Down Expand Up @@ -161,10 +174,9 @@ public String execute() {
request.setAttribute(v.name(), null);
try {
request.setAttribute(v.name(), field.getType().getDeclaredConstructor().newInstance());
} catch (NoSuchMethodException ex){
//ignore exception
} catch (NoSuchMethodException ex) {
//ignore exception
}

}
}
}
Expand All @@ -173,8 +185,9 @@ public String execute() {
request.setAttribute("errors", ctrl.getErrors());
request.setAttribute("fieldErrors", ctrl.getFieldErrors());

} catch (Exception ex) {
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | ClassNotFoundException ex) {
log.error(ex.toString(), ex);
throw new ServletException(ex.toString(), ex);
}
}
}
Expand All @@ -186,7 +199,7 @@ public String execute() {
}

private void setFieldFromParams(Object ctrl, String param, HttpServletRequest request)
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {

if (param != null) {

Expand All @@ -212,18 +225,15 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
for (String k : requestMap.keySet()) {
Object keyOb = null;
Object valOb = null;
try {
Class<?> theClass = Class.forName(keyType.getTypeName());
Constructor<?> cons = theClass.getConstructor(String.class);
keyOb = cons.newInstance(k);

theClass = Class.forName(valueType.getTypeName());
cons = theClass.getConstructor(String.class);
valOb = cons.newInstance(requestMap.get(k));
log.debug("Setting " + param + " : " + keyOb + " - " + valOb);
} catch (ClassNotFoundException ex) {
log.error(ex.toString(), ex);
}
Class<?> theClass = Class.forName(keyType.getTypeName());
Constructor<?> cons = theClass.getConstructor(String.class);
keyOb = cons.newInstance(k);

theClass = Class.forName(valueType.getTypeName());
cons = theClass.getConstructor(String.class);
valOb = cons.newInstance(requestMap.get(k));
log.debug("Setting " + param + " : " + keyOb + " - " + valOb);

map.put(keyOb, valOb);
}
field.set(ctrl, map);
Expand All @@ -237,14 +247,11 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
List list = List.class.cast(field.get(ctrl));
for (String p : parameterMap.get(param)) {
Object valOb = null;
try {
Class<?> theClass = Class.forName(valueType.getTypeName());
Constructor<?> cons = theClass.getConstructor(String.class);
valOb = cons.newInstance(p);
log.debug("Setting " + param + " : " + valOb);
} catch (ClassNotFoundException ex) {
log.error(ex.toString(), ex);
}
Class<?> theClass = Class.forName(valueType.getTypeName());
Constructor<?> cons = theClass.getConstructor(String.class);
valOb = cons.newInstance(p);
log.debug("Setting " + param + " : " + valOb);

list.add(valOb);
}
field.set(ctrl, list);
Expand Down Expand Up @@ -290,7 +297,7 @@ private void setFieldFromParams(Object ctrl, String param, HttpServletRequest re
field.set(ctrl, null);
try {
field.set(ctrl, field.getType().getDeclaredConstructor().newInstance());
} catch (NoSuchMethodException ex){
} catch (NoSuchMethodException ex) {
//ignore exception
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/loophole/mvc/filter/CSRFFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/loophole/mvc/filter/SecurityFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
*/
package loophole.mvc.filter;

import loophole.mvc.base.DispatcherServlet;
import loophole.mvc.base.TemplateServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
Expand All @@ -25,8 +21,6 @@
@WebFilter(urlPatterns = {"/*"})
public class SecurityFilter implements Filter {

private static Logger log = LoggerFactory.getLogger(SecurityFilter.class);

// csrf parameter and session name
public static final String _CSRF = "_csrf";
private static final SecureRandom random = new SecureRandom();
Expand Down

0 comments on commit d950f03

Please sign in to comment.