Added utility methods to add query string params to an existing URL

This commit is contained in:
andypower 2013-11-12 18:24:33 +01:00
parent 3e810bb869
commit 1533c7bec4

View File

@ -1,7 +1,7 @@
/* /*
* GeoServer-Manager - Simple Manager Library for GeoServer * 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 * http://www.geo-solutions.it
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -27,7 +27,10 @@ package it.geosolutions.geoserver.rest;
import it.geosolutions.geoserver.rest.decoder.RESTStyle; import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* *
@ -70,4 +73,44 @@ public class Util {
String composed = url + (contains ? "&":"?") + QUIET_ON_NOT_FOUND_PARAM + quietOnNotFound; String composed = url + (contains ? "&":"?") + QUIET_ON_NOT_FOUND_PARAM + quietOnNotFound;
return composed; return composed;
} }
public static <T> List<T> safeList(List<T> list) {
return list == null ? Collections.EMPTY_LIST : list;
}
public static <T> Collection<T> safeCollection(Collection<T> collection) {
return collection == null ? Collections.EMPTY_SET : collection;
}
public static <TK, TV> Map<TK, TV> safeMap(Map<TK, TV> 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;
}
} }