/** * Verifies that the passed parameter is greater than 0. * * @param parameter The parameter to be validated. * @param parameterName The name of the parameter to appear in the error message * * @throws BadPaginationException if 'parameter' is not greater than 0. */ private static void validate(int parameter, String parameterName) throws BadPaginationException { if (parameter < MINIMAL_VALUE) { ErrorMessageFormat errorMessage = ErrorMessageFormat.PAGINATION_PARAMETER_INVALID; LOG.debug(errorMessage.logFormat(parameterName, parameter)); throw new BadPaginationException(errorMessage.format(parameterName, parameter)); } }
/** * Builds the paginationParameters object, if the request provides both a perPage and page field. * * @param perPage The number of rows per page. * @param page The page to display. * * @return An Optional wrapping a PaginationParameters if both 'perPage' and 'page' exist. * @throws BadApiRequestException if 'perPage' or 'page' is not a positive integer, or if either one is empty * string but not both. */ protected Optional<PaginationParameters> generatePaginationParameters(String perPage, String page) throws BadApiRequestException { try { return "".equals(perPage) && "".equals(page) ? Optional.empty() : Optional.of(new PaginationParameters(perPage, page)); } catch (BadPaginationException invalidParameters) { throw new BadApiRequestException(invalidParameters.getMessage()); } }
ErrorMessageFormat errorMessage = ErrorMessageFormat.PAGINATION_PARAMETER_MISSING; LOG.debug(errorMessage.logFormat(parameterName)); throw new BadPaginationException(errorMessage.format(parameterName)); ErrorMessageFormat errorMessage = ErrorMessageFormat.PAGINATION_PARAMETER_INVALID; LOG.debug(errorMessage.logFormat(parameterName, parameter)); throw new BadPaginationException(errorMessage.format(parameterName, parameter));