Skip to content

Commit

Permalink
refactoring ConverterException
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmugra committed Dec 29, 2020
1 parent f852912 commit 906a081
Show file tree
Hide file tree
Showing 59 changed files with 190 additions and 270 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>net.cactusthorn.routing</groupId>
<artifactId>root</artifactId>
<version>0.23</version>
<version>0.24</version>
</parent>

<artifactId>core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void process(HttpServletRequest req, HttpServletResponse resp, List<Entr
if (LOG.isTraceEnabled()) {
LOG.trace("", ce);
} else if (LOG.isDebugEnabled()) {
LOG.debug("ConverterException: {}", ce.getMessage());
LOG.debug("{} : converterException: {}", req.getPathInfo(), ce.getMessage());
}
resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Not Found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Boolean convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Byte convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Character convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ public final class ConsumerConverter implements Converter {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String value) throws ConverterException {
try {
return consumer.consume(type, contentType, requestData);
} catch (Exception e) {
throw new ConverterException("Consumer converting failed", e);
}
public Object convert(RequestData requestData, Class<?> type, String value) {
return consumer.consume(type, contentType, requestData);
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
throw new UnsupportedOperationException("Array support is senseless for consumer converting");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

public interface Converter {

default Object convert(Class<?> type, String value) throws ConverterException {
default Object convert(Class<?> type, String value) throws Exception {
return convert(null, type, value);
}

default Object convert(RequestData requestData, Class<?> type) throws ConverterException {
default Object convert(RequestData requestData, Class<?> type) throws Exception {
return convert(requestData, type, (String) null);
}

default Object convert(Class<?> type, String[] value) throws ConverterException {
default Object convert(Class<?> type, String[] value) throws Exception {
return convert(null, type, value);
}

Object convert(RequestData requestData, Class<?> type, String value) throws ConverterException;
Object convert(RequestData requestData, Class<?> type, String value) throws Exception;

default Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
default Object convert(RequestData requestData, Class<?> type, String[] value) throws Exception {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ public class ConverterException extends Exception {

private static final long serialVersionUID = 0L;

public ConverterException(String message, Throwable cause) {
super(message, cause);
}

public ConverterException(String message, Throwable cause, Object param) {
super(String.format(message, param), cause);
}
private static final String MESSAGE = "Parameter position: %s; Parameter type: %s; %s";

public ConverterException(Throwable cause) {
super(cause);
public ConverterException(Throwable cause, int position, String type) {
super(String.format(MESSAGE, position, type, cause), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Double convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Float convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Integer convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Long convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public final class NullConverter implements Converter {
private PrimitiveConverter primitiveConverter = new PrimitiveConverter();

@Override //
public Object convert(RequestData requestData, Class<?> type, String value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String value) throws Exception {
if (type.isPrimitive()) {
return primitiveConverter.convert(type, (String) null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Short convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ public StaticStringMethodConverter(String methodName) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String value) throws ConverterException {
try {
if (value == null) {
return null;
}
Method method = methods.get(type);
return method.invoke(null, value);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new ConverterException("The problem with method invocation", e);
public Object convert(RequestData requestData, Class<?> type, String value)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (value == null) {
return null;
}
Method method = methods.get(type);
return method.invoke(null, value);
}

boolean register(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ public class StringConstructorConverter implements Converter {
private final Map<Class<?>, Constructor<?>> constructors = new HashMap<>();

@Override //
public Object convert(RequestData requestData, Class<?> type, String value) throws ConverterException {
try {
if (value == null) {
return null;
}
return constructors.get(type).newInstance(value);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new ConverterException("The problem with method invocation", e);
public Object convert(RequestData requestData, Class<?> type, String value)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (value == null) {
return null;
}
return constructors.get(type).newInstance(value);
}

boolean register(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public String convert(RequestData requestData, Class<?> type, String value) {
}

@Override //
public Object convert(RequestData requestData, Class<?> type, String[] value) throws ConverterException {
public Object convert(RequestData requestData, Class<?> type, String[] value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import javax.servlet.http.HttpServletResponse;

import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.RoutingInitializationException;
import net.cactusthorn.routing.convert.ConsumerConverter;
import net.cactusthorn.routing.convert.ConverterException;
import net.cactusthorn.routing.convert.ConvertersHolder;

public final class BodyParameter extends MethodParameter {
Expand All @@ -21,15 +21,14 @@ public BodyParameter(Method method, Parameter parameter, ConvertersHolder conver
super(parameter);
Optional<ConsumerConverter> optional = convertersHolder.findConsumerConverter(contentType);
if (!optional.isPresent()) {
throw new IllegalArgumentException(
"@Context: consumer for contentType " + contentType + " unknown; Method: " + method.toGenericString());
throw new RoutingInitializationException(
"@Context: consumer for contentType %s unknown; Method: %s", contentType, method);
}
converter = optional.get();
}

@Override //
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData) throws Exception {
return converter.convert(requestData, classType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.RoutingInitializationException;
import net.cactusthorn.routing.annotation.CookieParam;
import net.cactusthorn.routing.convert.ConverterException;

public class CookieParamParameter extends MethodParameter {

Expand All @@ -28,21 +27,16 @@ public CookieParamParameter(Method method, Parameter parameter) {
}

@Override //
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
try {
Cookie[] cookies = req.getCookies();
if (cookies == null) {
return null;
}
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData) throws Exception {
Cookie[] cookies = req.getCookies();
if (cookies == null) {
return null;
} catch (Exception e) {
throw new ConverterException("Type Converting problem", e);
}
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.RoutingInitializationException;
import net.cactusthorn.routing.annotation.FormPart;
import net.cactusthorn.routing.convert.ConverterException;

public class FormPartParameter extends MethodParameter {

Expand All @@ -29,21 +28,16 @@ public FormPartParameter(Method method, Parameter parameter) {
}

@Override //
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
try {
Collection<Part> parts = req.getParts();
if (parts == null) {
return null;
}
for (Part part : req.getParts()) {
if (name.equals(part.getName())) {
return part;
}
}
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData) throws Exception {
Collection<Part> parts = req.getParts();
if (parts == null) {
return null;
} catch (Exception e) {
throw new ConverterException("Type Converting problem", e);
}
for (Part part : req.getParts()) {
if (name.equals(part.getName())) {
return part;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.annotation.HeaderParam;
import net.cactusthorn.routing.convert.ConverterException;
import net.cactusthorn.routing.convert.ConvertersHolder;

public class HeaderParamParameter extends PathParamParameter {
Expand All @@ -28,19 +27,12 @@ protected String initName(Parameter parameter) {
}

@Override //
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
try {
String value = req.getHeader(name());
if (defaultValue() != null && value == null) {
value = defaultValue();
}
return converter().convert(classType(), value);
} catch (ConverterException ce) {
throw ce;
} catch (Exception e) {
throw new ConverterException("Type Converting failed: header parameter \"%s\"", e, name());
Object findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData) throws Exception {
String value = req.getHeader(name());
if (defaultValue() != null && value == null) {
value = defaultValue();
}
return converter().convert(classType(), value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.servlet.http.HttpServletResponse;

import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.convert.ConverterException;

public final class HttpServletRequestParameter extends MethodParameter {

Expand All @@ -17,7 +16,7 @@ public HttpServletRequestParameter(Parameter parameter) {

@Override //
HttpServletRequest findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
throws Exception {
return req;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.servlet.http.HttpServletResponse;

import net.cactusthorn.routing.RequestData;
import net.cactusthorn.routing.convert.ConverterException;

public final class HttpServletResponseParameter extends MethodParameter {

Expand All @@ -17,7 +16,7 @@ public HttpServletResponseParameter(Parameter parameter) {

@Override //
HttpServletResponse findValue(HttpServletRequest req, HttpServletResponse res, ServletContext con, RequestData requestData)
throws ConverterException {
throws Exception {
return res;
}
}
Loading

0 comments on commit 906a081

Please sign in to comment.