Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,119 +23,146 @@
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.serialization.JsonParserHelper;

public final class ApiParameterHelper {

private static final String GENERIC_RESULT_SET = "genericResultSet";

// Optimized for performance and Error Prone compliance
private static final Pattern COMMA_SEPARATOR = Pattern.compile("\\s*,\\s*");

private ApiParameterHelper() {

}

// ---------------- COMMAND ID ----------------

public static Long commandId(final MultivaluedMap<String, String> queryParams) {
Long id = null;
if (queryParams.getFirst("commandId") != null) {
if (queryParams != null && queryParams.getFirst("commandId") != null) {
final String value = queryParams.getFirst("commandId");
if (StringUtils.isNotBlank(value)) {
id = Long.valueOf(value);
try {
return Long.valueOf(value);
} catch (NumberFormatException e) {
return null;
}
}
}
return id;
return null;
}

// ---------------- FIELDS ----------------

public static Set<String> extractFieldsForResponseIfProvided(final MultivaluedMap<String, String> queryParams) {
Set<String> fields = new HashSet<>();
String commaSeparatedParameters = "";
if (queryParams.getFirst("fields") != null) {
commaSeparatedParameters = queryParams.getFirst("fields");
if (StringUtils.isNotBlank(commaSeparatedParameters)) {
fields = new HashSet<>(Arrays.asList(commaSeparatedParameters.split("\\s*,\\s*"))); // NOSONAR
if (queryParams != null && queryParams.getFirst("fields") != null) {
String value = queryParams.getFirst("fields");
if (StringUtils.isNotBlank(value)) {
return new HashSet<>(Arrays.asList(COMMA_SEPARATOR.split(value)));
}
}
return fields;
return new HashSet<>();
}

// ---------------- ASSOCIATIONS ----------------

public static Set<String> extractAssociationsForResponseIfProvided(final MultivaluedMap<String, String> queryParams) {
Set<String> fields = new HashSet<>();
String commaSeparatedParameters = "";
if (queryParams.getFirst("associations") != null) {
commaSeparatedParameters = queryParams.getFirst("associations");
if (StringUtils.isNotBlank(commaSeparatedParameters)) {
fields = new HashSet<>(Arrays.asList(commaSeparatedParameters.split("\\s*,\\s*"))); // NOSONAR
if (queryParams != null && queryParams.getFirst("associations") != null) {
String value = queryParams.getFirst("associations");
if (StringUtils.isNotBlank(value)) {
return new HashSet<>(Arrays.asList(COMMA_SEPARATOR.split(value)));
}
}
return fields;
return new HashSet<>();
}

public static void excludeAssociationsForResponseIfProvided(final String commaSeparatedParameters, Set<String> fields) {
if (StringUtils.isNotBlank(commaSeparatedParameters)) {
fields.removeAll(new HashSet<>(Arrays.asList(commaSeparatedParameters.split("\\s*,\\s*")))); // NOSONAR
if (StringUtils.isNotBlank(commaSeparatedParameters) && fields != null) {
fields.removeAll(new HashSet<>(Arrays.asList(COMMA_SEPARATOR.split(commaSeparatedParameters))));
}
}

public static void excludeAssociationsForResponseIfProvided(final MultivaluedMap<String, String> queryParams, Set<String> fields) {
if (queryParams.getFirst("exclude") != null) {
public static void excludeAssociationsForResponseIfProvided(final MultivaluedMap<String, String> queryParams,
Set<String> fields) {
if (queryParams != null && queryParams.getFirst("exclude") != null) {
excludeAssociationsForResponseIfProvided(queryParams.getFirst("exclude"), fields);
}
}

// ---------------- LOCALE ----------------

public static Locale extractLocale(final MultivaluedMap<String, String> queryParams) {
Locale locale = null;
if (queryParams.getFirst("locale") != null) {
if (queryParams != null && queryParams.getFirst("locale") != null) {
final String localeAsString = queryParams.getFirst("locale");
locale = JsonParserHelper.localeFromString(localeAsString);
if (StringUtils.isNotBlank(localeAsString)) {
try {
return JsonParserHelper.localeFromString(localeAsString);
} catch (Exception e) {
return null;
}
}
}
return locale;
return null;
}

public static boolean parameterType(final MultivaluedMap<String, String> queryParams) {
boolean parameterType = false;
if (queryParams.getFirst("parameterType") != null) {
final String parameterTypeValue = queryParams.getFirst("parameterType");
parameterType = "true".equalsIgnoreCase(parameterTypeValue);
// ---------------- PAGINATION ----------------

public static Integer offset(final MultivaluedMap<String, String> params) {
if (params != null && params.getFirst("offset") != null) {
try {
return Math.max(0, Integer.parseInt(params.getFirst("offset")));
} catch (NumberFormatException e) {
return 0;
}
}
return parameterType;
return 0;
}

public static boolean template(final MultivaluedMap<String, String> queryParams) {
boolean template = false;
if (queryParams.getFirst("template") != null) {
final String prettyPrintValue = queryParams.getFirst("template");
template = "true".equalsIgnoreCase(prettyPrintValue);
public static Integer limit(final MultivaluedMap<String, String> params) {
if (params != null && params.getFirst("limit") != null) {
try {
int limit = Integer.parseInt(params.getFirst("limit"));
return limit > 0 ? limit : 200; // Standard Fineract default
} catch (NumberFormatException e) {
return 200;
}
}
return template;
return 200;
}

// ---------------- BOOLEAN FLAGS ----------------

public static boolean parameterType(final MultivaluedMap<String, String> queryParams) {
return isTrue(queryParams, "parameterType");
}

public static boolean template(final MultivaluedMap<String, String> queryParams) {
return isTrue(queryParams, "template");
}

public static boolean makerCheckerable(final MultivaluedMap<String, String> queryParams) {
boolean makerCheckerable = false;
if (queryParams.getFirst("makerCheckerable") != null) {
final String prettyPrintValue = queryParams.getFirst("makerCheckerable");
makerCheckerable = "true".equalsIgnoreCase(prettyPrintValue);
}
return makerCheckerable;
return isTrue(queryParams, "makerCheckerable");
}

public static boolean includeJson(final MultivaluedMap<String, String> queryParams) {
boolean includeJson = false;
if (queryParams.getFirst("includeJson") != null) {
final String includeJsonValue = queryParams.getFirst("includeJson");
includeJson = "true".equalsIgnoreCase(includeJsonValue);
}
return includeJson;
return isTrue(queryParams, "includeJson");
}

public static boolean genericResultSet(final MultivaluedMap<String, String> queryParams) {
boolean genericResultSet = false;
if (queryParams.getFirst(GENERIC_RESULT_SET) != null) {
final String genericResultSetValue = queryParams.getFirst(GENERIC_RESULT_SET);
genericResultSet = "true".equalsIgnoreCase(genericResultSetValue);
}
return genericResultSet;
return isTrue(queryParams, GENERIC_RESULT_SET);
}

public static boolean genericResultSetPassed(final MultivaluedMap<String, String> queryParams) {
return queryParams.getFirst(GENERIC_RESULT_SET) != null;
return queryParams != null && queryParams.getFirst(GENERIC_RESULT_SET) != null;
}

// ---------------- HELPER ----------------

private static boolean isTrue(final MultivaluedMap<String, String> queryParams, String key) {
if (queryParams != null && queryParams.getFirst(key) != null) {
return "true".equalsIgnoreCase(queryParams.getFirst(key));
}
return false;
}
}
}
Loading