mirror of
https://github.com/sismics/docs.git
synced 2025-12-31 02:22:27 +00:00
Closes #29: Upgrade to Jersey 2
This commit is contained in:
@@ -1,40 +1,38 @@
|
||||
package com.sismics.rest.util;
|
||||
|
||||
import org.codehaus.jettison.json.JSONArray;
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonValue;
|
||||
|
||||
/**
|
||||
* JSON utilities.
|
||||
*
|
||||
* @author jtremeaux
|
||||
* @author bgamard
|
||||
*/
|
||||
public class JsonUtil {
|
||||
|
||||
/**
|
||||
* Returns a JsonValue from a String.
|
||||
*
|
||||
* @param value Value
|
||||
* @return JsonValue
|
||||
*/
|
||||
public static JsonValue nullable(String value) {
|
||||
if (value == null) {
|
||||
return JsonValue.NULL;
|
||||
}
|
||||
return Json.createObjectBuilder().add("_", value).build().get("_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix of {@see JsonObject.append()}, which seems to create nested arrays.
|
||||
* Returns a JsonValue from an Integer.
|
||||
*
|
||||
* @param o JSON Object
|
||||
* @param key Key containing the array of null
|
||||
* @param value Value to append
|
||||
* @return Updated object
|
||||
* @throws JSONException
|
||||
* @param value Value
|
||||
* @return JsonValue
|
||||
*/
|
||||
public static JSONObject append(JSONObject o, String key, JSONObject value) throws JSONException {
|
||||
Object prevValue = o.opt(key);
|
||||
if (prevValue == null) {
|
||||
o.put(key, new JSONArray().put(value));
|
||||
} else if (!(prevValue instanceof JSONArray)){
|
||||
throw new JSONException("JSONObject[" + key + "] is not a JSONArray.");
|
||||
} else {
|
||||
JSONArray newArray = new JSONArray();
|
||||
JSONArray oldArray = ((JSONArray) prevValue);
|
||||
for (int i = 0; i < oldArray.length(); i++) {
|
||||
newArray.put(oldArray.get(i));
|
||||
}
|
||||
newArray.put(value);
|
||||
o.put(key, newArray);
|
||||
public static JsonValue nullable(Integer value) {
|
||||
if (value == null) {
|
||||
return JsonValue.NULL;
|
||||
}
|
||||
return o;
|
||||
return Json.createObjectBuilder().add("_", value).build().get("_");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,22 @@
|
||||
package com.sismics.rest.util;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.sismics.docs.core.dao.file.theme.ThemeDao;
|
||||
import com.sismics.docs.core.dao.jpa.LocaleDao;
|
||||
import com.sismics.docs.core.model.jpa.Locale;
|
||||
import com.sismics.rest.exception.ClientException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.sismics.rest.exception.ClientException;
|
||||
|
||||
/**
|
||||
* Utility class to validate parameters.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class ValidationUtil {
|
||||
private static Pattern EMAIL_PATTERN = Pattern.compile(".+@.+\\..+");
|
||||
private static Pattern EMAIL_PATTERN = Pattern.compile(".+@.+");
|
||||
|
||||
private static Pattern HTTP_URL_PATTERN = Pattern.compile("https?://.+");
|
||||
|
||||
@@ -31,9 +27,9 @@ public class ValidationUtil {
|
||||
*
|
||||
* @param s Object tu validate
|
||||
* @param name Name of the parameter
|
||||
* @throws JSONException
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static void validateRequired(Object s, String name) throws JSONException {
|
||||
public static void validateRequired(Object s, String name) throws ClientException {
|
||||
if (s == null) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be set", name));
|
||||
}
|
||||
@@ -50,7 +46,7 @@ public class ValidationUtil {
|
||||
* @return String without white spaces
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateLength(String s, String name, Integer lengthMin, Integer lengthMax, boolean nullable) throws JSONException {
|
||||
public static String validateLength(String s, String name, Integer lengthMin, Integer lengthMax, boolean nullable) throws ClientException {
|
||||
s = StringUtils.strip(s);
|
||||
if (nullable && StringUtils.isEmpty(s)) {
|
||||
return s;
|
||||
@@ -62,7 +58,7 @@ public class ValidationUtil {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be more than {1} characters", name, lengthMin));
|
||||
}
|
||||
if (lengthMax != null && s.length() > lengthMax) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be less than {1} characters", name, lengthMax));
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be more than {1} characters", name, lengthMax));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
@@ -77,7 +73,7 @@ public class ValidationUtil {
|
||||
* @return String without white spaces
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateLength(String s, String name, Integer lengthMin, Integer lengthMax) throws JSONException {
|
||||
public static String validateLength(String s, String name, Integer lengthMin, Integer lengthMax) throws ClientException {
|
||||
return validateLength(s, name, lengthMin, lengthMax, false);
|
||||
}
|
||||
|
||||
@@ -87,12 +83,25 @@ public class ValidationUtil {
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @return String without white spaces
|
||||
* @throws JSONException
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateStringNotBlank(String s, String name) throws JSONException {
|
||||
public static String validateStringNotBlank(String s, String name) throws ClientException {
|
||||
return validateLength(s, name, 1, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is an email.
|
||||
*
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static void validateEmail(String s, String name) throws ClientException {
|
||||
if (!EMAIL_PATTERN.matcher(s).matches()) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be an email", name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is a hexadecimal color.
|
||||
*
|
||||
@@ -101,32 +110,19 @@ public class ValidationUtil {
|
||||
* @param nullable True if the string can be empty or null
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static void validateHexColor(String s, String name, boolean nullable) throws JSONException {
|
||||
public static void validateHexColor(String s, String name, boolean nullable) throws ClientException {
|
||||
ValidationUtil.validateLength(s, "name", 7, 7, nullable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is an email.
|
||||
*
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static void validateEmail(String s, String name) throws JSONException {
|
||||
if (!EMAIL_PATTERN.matcher(s).matches()) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be an email", name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the provided string matches an URL with HTTP or HTTPS scheme.
|
||||
*
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @return Stripped URL
|
||||
* @throws JSONException
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateHttpUrl(String s, String name) throws JSONException {
|
||||
public static String validateHttpUrl(String s, String name) throws ClientException {
|
||||
s = StringUtils.strip(s);
|
||||
if (!HTTP_URL_PATTERN.matcher(s).matches()) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be an HTTP(s) URL", name));
|
||||
@@ -139,14 +135,30 @@ public class ValidationUtil {
|
||||
*
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @throws JSONException
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static void validateAlphanumeric(String s, String name) throws JSONException {
|
||||
public static void validateAlphanumeric(String s, String name) throws ClientException {
|
||||
if (!ALPHANUMERIC_PATTERN.matcher(s).matches()) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must have only alphanumeric or underscore characters", name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is a number.
|
||||
*
|
||||
* @param s String to validate
|
||||
* @param name Name of the parameter
|
||||
* @return Parsed number
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static Integer validateInteger(String s, String name) throws ClientException {
|
||||
try {
|
||||
return Integer.valueOf(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ClientException("Validation Error", MessageFormat.format("{0} is not a number", name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and parses a date.
|
||||
*
|
||||
@@ -154,9 +166,9 @@ public class ValidationUtil {
|
||||
* @param name Name of the parameter
|
||||
* @param nullable True if the string can be empty or null
|
||||
* @return Parsed date
|
||||
* @throws JSONException
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static Date validateDate(String s, String name, boolean nullable) throws JSONException {
|
||||
public static Date validateDate(String s, String name, boolean nullable) throws ClientException {
|
||||
if (Strings.isNullOrEmpty(s)) {
|
||||
if (!nullable) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be set", name));
|
||||
@@ -170,56 +182,4 @@ public class ValidationUtil {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} must be a date", name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a locale.
|
||||
*
|
||||
* @param localeId String to validate
|
||||
* @param name Name of the parameter
|
||||
* @return String without white spaces
|
||||
* @param nullable True if the string can be empty or null
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateLocale(String localeId, String name, boolean nullable) throws JSONException {
|
||||
localeId = StringUtils.strip(localeId);
|
||||
if (StringUtils.isEmpty(localeId)) {
|
||||
if (!nullable) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} is required", name));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
LocaleDao localeDao = new LocaleDao();
|
||||
Locale locale = localeDao.getById(localeId);
|
||||
if (locale == null) {
|
||||
throw new ClientException("ValidationError", "Locale not found: " + localeId);
|
||||
}
|
||||
return localeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a theme.
|
||||
*
|
||||
* @param themeId ID of the theme to validate
|
||||
* @param name Name of the parameter
|
||||
* @return String without white spaces
|
||||
* @param nullable True if the string can be empty or null
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static String validateTheme(String themeId, String name, boolean nullable) throws JSONException {
|
||||
themeId = StringUtils.strip(themeId);
|
||||
if (StringUtils.isEmpty(themeId)) {
|
||||
if (!nullable) {
|
||||
throw new ClientException("ValidationError", MessageFormat.format("{0} is required", name));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
ThemeDao themeDao = new ThemeDao();
|
||||
List<String> themeList = themeDao.findAll();
|
||||
if (!themeList.contains(themeId)) {
|
||||
throw new ClientException("ValidationError", "Theme not found: " + themeId);
|
||||
}
|
||||
return themeId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user