diff --git a/src/main/java/it/geosolutions/geoserver/rest/Util.java b/src/main/java/it/geosolutions/geoserver/rest/Util.java index 20a5175..56419bf 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/Util.java +++ b/src/main/java/it/geosolutions/geoserver/rest/Util.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2013 GeoSolutions S.A.S. + * Copyright (C) 2007,2015 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -27,15 +27,18 @@ package it.geosolutions.geoserver.rest; import it.geosolutions.geoserver.rest.decoder.RESTStyle; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; /** * * @author ETj (etj at geo-solutions.it) */ public class Util { - - public static final String QUIET_ON_NOT_FOUND_PARAM = "quietOnNotFound="; + +public static final String QUIET_ON_NOT_FOUND_PARAM = "quietOnNotFound="; public static final boolean DEFAULT_QUIET_ON_NOT_FOUND = true; @@ -70,4 +73,44 @@ public class Util { String composed = url + (contains ? "&":"?") + QUIET_ON_NOT_FOUND_PARAM + quietOnNotFound; return composed; } + + public static List safeList(List list) { + return list == null ? Collections.EMPTY_LIST : list; + } + + public static Collection safeCollection(Collection collection) { + return collection == null ? Collections.EMPTY_SET : collection; + } + + public static Map safeMap(Map map) { + return map == null ? Collections.EMPTY_MAP : map; + } + + public static char getParameterSeparator(String url) { + char parameterSeparator = '?'; + if (url.contains("?")) { + parameterSeparator = '&'; + } + return parameterSeparator; + } + + public static char getParameterSeparator(StringBuilder url) { + char parameterSeparator = '?'; + if (url.indexOf("?") != -1) { + parameterSeparator = '&'; + } + return parameterSeparator; + } + + public static boolean appendParameter(StringBuilder url, String parameterName, + String parameterValue) { + boolean result = false; + if (parameterName != null && !parameterName.isEmpty() + && parameterValue != null && !parameterValue.isEmpty()) { + char parameterSeparator = getParameterSeparator(url); + url.append(parameterSeparator).append(parameterName.trim()) + .append('=').append(parameterValue.trim()); + } + return result; + } }