|
21 | 21 | import java.lang.reflect.Field; |
22 | 22 | import java.lang.reflect.Modifier; |
23 | 23 | import java.net.HttpURLConnection; |
| 24 | +import java.net.URL; |
| 25 | +import java.net.URLConnection; |
| 26 | +import java.net.URLEncoder; |
24 | 27 | import java.nio.ByteBuffer; |
25 | 28 | import java.text.SimpleDateFormat; |
26 | 29 | import java.util.ArrayList; |
27 | 30 | import java.util.Arrays; |
28 | 31 | import java.util.Calendar; |
29 | 32 | import java.util.Date; |
30 | 33 | import java.util.HashMap; |
| 34 | +import java.util.Iterator; |
31 | 35 | import java.util.List; |
32 | 36 | import java.util.Map; |
| 37 | +import java.util.Objects; |
33 | 38 | import java.util.UUID; |
34 | 39 |
|
35 | 40 | import static net.servicestack.func.Func.last; |
@@ -394,6 +399,21 @@ else if (parts.length == 2) |
394 | 399 | } |
395 | 400 |
|
396 | 401 | /*String Utils*/ |
| 402 | + public static boolean isEmpty(final String string) |
| 403 | + { |
| 404 | + return string == null || string.length() == 0; |
| 405 | + } |
| 406 | + |
| 407 | + public static boolean isNullOrEmpty(final String string) |
| 408 | + { |
| 409 | + return string == null || string.length() == 0; |
| 410 | + } |
| 411 | + |
| 412 | + public static boolean isNullOrWhiteSpace(final String string) |
| 413 | + { |
| 414 | + return string == null || string.length() == 0 || string.trim().length() == 0; |
| 415 | + } |
| 416 | + |
397 | 417 | public static String[] splitOnFirst(String strVal, char needle) { |
398 | 418 | return splitOnFirst(strVal, needle, 0); |
399 | 419 | } |
@@ -475,6 +495,16 @@ public static byte[] toUtf8Bytes(String string) { |
475 | 495 | } |
476 | 496 | } |
477 | 497 |
|
| 498 | + public static String readToEnd(String url) { |
| 499 | + try { |
| 500 | + URL heartbeatUrl = new URL(url); |
| 501 | + HttpURLConnection conn = (HttpURLConnection)heartbeatUrl.openConnection(); |
| 502 | + return readToEnd(conn); |
| 503 | + } catch (Exception e) { |
| 504 | + throw new RuntimeException(e); |
| 505 | + } |
| 506 | + } |
| 507 | + |
478 | 508 | public static String readToEnd(HttpURLConnection response){ |
479 | 509 | try { |
480 | 510 | return readToEnd(response.getInputStream(), "UTF-8"); |
@@ -786,4 +816,71 @@ private static byte[] encode3to4( |
786 | 816 | } |
787 | 817 | } |
788 | 818 |
|
| 819 | + public static String addQueryParam(String url, String key, String value) { |
| 820 | + return addQueryParam(url, key, value, true); |
| 821 | + } |
| 822 | + |
| 823 | + public static String addQueryParam(String url, String key, String val, boolean encode) { |
| 824 | + if (url == null || url.length() == 0) |
| 825 | + return null; |
| 826 | + String prefix = ""; |
| 827 | + if (!url.endsWith("?") && !url.endsWith("&")) { |
| 828 | + prefix = url.indexOf('?') == -1 ? "?" : "&"; |
| 829 | + } |
| 830 | + try { |
| 831 | + return url + prefix + key + "=" + (encode && val != null ? URLEncoder.encode(val, "UTF-8") : val); |
| 832 | + } catch (UnsupportedEncodingException e) { |
| 833 | + throw new RuntimeException(e); |
| 834 | + } |
| 835 | + } |
| 836 | + |
| 837 | + public static final String EMPTY = ""; |
| 838 | + |
| 839 | + public static String toString(Object o, String nullDefault) { |
| 840 | + return (o != null) ? o.toString() : nullDefault; |
| 841 | + } |
| 842 | + |
| 843 | + public static String join(final String[] array, final String separator) { |
| 844 | + if (array == null) |
| 845 | + return null; |
| 846 | + |
| 847 | + return join(Arrays.asList(array), separator); |
| 848 | + } |
| 849 | + |
| 850 | + public static String join(final Iterable<?> iterable, final String separator) { |
| 851 | + if (iterable == null) { |
| 852 | + return null; |
| 853 | + } |
| 854 | + return join(iterable.iterator(), separator); |
| 855 | + } |
| 856 | + |
| 857 | + public static String join(final Iterator<?> iterator, final String separator) { |
| 858 | + if (iterator == null) { |
| 859 | + return null; |
| 860 | + } |
| 861 | + if (!iterator.hasNext()) { |
| 862 | + return EMPTY; |
| 863 | + } |
| 864 | + final Object first = iterator.next(); |
| 865 | + if (!iterator.hasNext()) { |
| 866 | + final String result = toString(first, ""); |
| 867 | + return result; |
| 868 | + } |
| 869 | + |
| 870 | + final StringBuilder buf = new StringBuilder(256); |
| 871 | + if (first != null) { |
| 872 | + buf.append(first); |
| 873 | + } |
| 874 | + |
| 875 | + while (iterator.hasNext()) { |
| 876 | + if (separator != null) { |
| 877 | + buf.append(separator); |
| 878 | + } |
| 879 | + final Object obj = iterator.next(); |
| 880 | + if (obj != null) { |
| 881 | + buf.append(obj); |
| 882 | + } |
| 883 | + } |
| 884 | + return buf.toString(); |
| 885 | + } |
789 | 886 | } |
0 commit comments