Skip to content

Commit

Permalink
Return UserFriendlyException
Browse files Browse the repository at this point in the history
Fixes #27
  • Loading branch information
slominskir committed Nov 13, 2024
1 parent 2414ece commit 324961d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.jlab.demo.presentation.params;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jlab.demo.business.params.ReportOneParams;
import org.jlab.smoothness.business.exception.UserFriendlyException;
import org.jlab.smoothness.business.util.IOUtil;
import org.jlab.smoothness.business.util.TimeUtil;
import org.jlab.smoothness.presentation.util.ParamBuilder;
Expand Down Expand Up @@ -37,7 +37,7 @@ public ReportOneParams convert() {
try {
start = ParamConverter.convertFriendlyDateTime(request, "start");
end = ParamConverter.convertFriendlyDateTime(request, "end");
} catch (ParseException e) {
} catch (UserFriendlyException e) {
throw new RuntimeException("Unable to parse date", e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.jlab.smoothness.business.exception.UserFriendlyException;
import org.jlab.smoothness.business.util.TimeUtil;

/**
* Utility methods for converting parameters. Built-in unchecked exceptions such as
* NumberFormatException and IllegalArgumentException are thrown if conversion fails. In some cases
* the built-in checked ParseException is thrown, as declared.
* NumberFormatException and IllegalArgumentException are wrapped in UserFriendlyException if
* conversion fails. In some cases DateTimeParseException or ParseException is also wrapped in a
* UserFriendlyException.
*
* <p>The UserFriendlyException.getUserMessage() can be used to provide a user-friendly message to
* the user without tripping the CodeQL Scanner.
*
* @author ryans
*/
Expand All @@ -31,16 +37,16 @@ private ParamConverter() {
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Character or null
* @throws IllegalArgumentException If the parameter contains more than one character
* @throws UserFriendlyException If the parameter contains more than one character
*/
public static Character convertCharacter(HttpServletRequest request, String name)
throws IllegalArgumentException {
throws UserFriendlyException {
String valueStr = request.getParameter(name);
Character value = null;

if (valueStr != null && !valueStr.isEmpty()) {
if (valueStr.length() > 1) {
throw new IllegalArgumentException("String contains more than one Character");
throw new UserFriendlyException("String contains more than one Character");
} else {
value = valueStr.charAt(0);
}
Expand Down Expand Up @@ -91,10 +97,10 @@ public static Integer convertInteger(HttpServletRequest request, String name) {
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Boolean or null
* @throws IllegalArgumentException If parameter does not contain one of "Y" or "N"
* @throws UserFriendlyException If parameter does not contain one of "Y" or "N"
*/
public static Boolean convertYNBoolean(HttpServletRequest request, String name)
throws IllegalArgumentException {
throws UserFriendlyException {
String valueStr = request.getParameter(name);
Boolean value = null;

Expand All @@ -104,7 +110,7 @@ public static Boolean convertYNBoolean(HttpServletRequest request, String name)
} else if ("Y".equals(valueStr)) {
value = true;
} else {
throw new IllegalArgumentException("Value must be one of 'Y' or 'N'");
throw new UserFriendlyException("Value must be one of 'Y' or 'N'");
}
}

Expand All @@ -117,20 +123,24 @@ public static Boolean convertYNBoolean(HttpServletRequest request, String name)
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Date or null
* @throws ParseException If the parameter format does not match expected format
* @throws UserFriendlyException If the parameter format does not match expected format
*/
public static Date convertISO8601Date(HttpServletRequest request, String name)
throws ParseException {
throws UserFriendlyException {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Date value = null;

String valueStr = request.getParameter(name);

if (valueStr != null && !valueStr.isEmpty()) {
final LocalDate ld = formatter.parse(valueStr, LocalDate::from);
final ZonedDateTime zdt = ld.atStartOfDay(ZoneId.of("America/New_York"));
value = Date.from(zdt.toInstant());
try {
final LocalDate ld = formatter.parse(valueStr, LocalDate::from);
final ZonedDateTime zdt = ld.atStartOfDay(ZoneId.of("America/New_York"));
value = Date.from(zdt.toInstant());
} catch (DateTimeParseException e) {
throw new UserFriendlyException("Date format error", e);
}
}

return value;
Expand All @@ -143,20 +153,24 @@ public static Date convertISO8601Date(HttpServletRequest request, String name)
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Date or null
* @throws ParseException If the parameter format does not match expected format
* @throws UserFriendlyException If the parameter format does not match expected format
*/
public static Date convertISO8601DateTime(HttpServletRequest request, String name)
throws ParseException {
throws UserFriendlyException {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

Date value = null;

String valueStr = request.getParameter(name);

if (valueStr != null && !valueStr.isEmpty()) {
final LocalDateTime ldt = formatter.parse(valueStr, LocalDateTime::from);
final ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York"));
value = Date.from(zdt.toInstant());
try {
final LocalDateTime ldt = formatter.parse(valueStr, LocalDateTime::from);
final ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York"));
value = Date.from(zdt.toInstant());
} catch (DateTimeParseException e) {
throw new UserFriendlyException("Date format error", e);
}
}

return value;
Expand Down Expand Up @@ -303,18 +317,22 @@ public static BigInteger[] convertBigIntegerArray(HttpServletRequest request, St
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Date or null
* @throws ParseException If the parameter format does not match expected format
* @throws UserFriendlyException If the parameter format does not match expected format
*/
public static Date convertFriendlyDateTime(HttpServletRequest request, String name)
throws ParseException {
throws UserFriendlyException {
SimpleDateFormat format = new SimpleDateFormat(TimeUtil.getFriendlyDateTimePattern());

Date value = null;

String valueStr = request.getParameter(name);

if (valueStr != null && !valueStr.isEmpty()) {
value = format.parse(valueStr);
try {
value = format.parse(valueStr);
} catch (ParseException e) {
throw new UserFriendlyException("format error", e);
}
}

return value;
Expand All @@ -326,18 +344,22 @@ public static Date convertFriendlyDateTime(HttpServletRequest request, String na
* @param request The HttpServletRequest
* @param name The parameter name
* @return A Date or null
* @throws ParseException If the parameter format does not match expected format
* @throws UserFriendlyException If the parameter format does not match expected format
*/
public static Date convertFriendlyDate(HttpServletRequest request, String name)
throws ParseException {
throws UserFriendlyException {
SimpleDateFormat format = new SimpleDateFormat(TimeUtil.getFriendlyDatePattern());

Date value = null;

String valueStr = request.getParameter(name);

if (valueStr != null && !valueStr.isEmpty()) {
value = format.parse(valueStr);
try {
value = format.parse(valueStr);
} catch (ParseException e) {
throw new UserFriendlyException("format error", e);
}
}

return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jlab.smoothness.presentation.util;

import javax.servlet.http.HttpServletRequest;
import org.jlab.smoothness.business.exception.UserFriendlyException;

/**
* Utility methods for combined conversion and validation, with optional default values.
Expand Down Expand Up @@ -72,8 +73,10 @@ public static Integer convertAndValidateNonNegativeInteger(
* @param request The request
* @param name The parameter name
* @return The boolean
* @throws UserFriendlyException If the input string cannot be parsed
*/
public static boolean convertAndValidateYNBoolean(HttpServletRequest request, String name) {
public static boolean convertAndValidateYNBoolean(HttpServletRequest request, String name)
throws UserFriendlyException {
Boolean value = ParamConverter.convertYNBoolean(request, name);

ParamValidator.validateNonNull(name, value);
Expand All @@ -88,9 +91,10 @@ public static boolean convertAndValidateYNBoolean(HttpServletRequest request, St
* @param name The parameter name
* @param defaultValue The default to use if the parameter is null
* @return The boolean
* @throws UserFriendlyException If the input string cannot be parsed
*/
public static boolean convertAndValidateYNBoolean(
HttpServletRequest request, String name, boolean defaultValue) {
HttpServletRequest request, String name, boolean defaultValue) throws UserFriendlyException {
Boolean value = ParamConverter.convertYNBoolean(request, name);

if (value == null) {
Expand Down

0 comments on commit 324961d

Please sign in to comment.