1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rundeck.api.util;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URLEncoder;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23 import java.util.Map.Entry;
24 import org.apache.commons.lang.StringUtils;
25
26
27
28
29
30
31 public class ParametersUtil {
32
33
34
35
36
37
38
39 public static String urlEncode(String input) {
40 if (StringUtils.isBlank(input)) {
41 return input;
42 }
43 try {
44 return URLEncoder.encode(input, "UTF-8");
45 } catch (UnsupportedEncodingException e) {
46 throw new RuntimeException(e);
47 }
48 }
49
50
51
52
53
54
55
56
57 public static String generateArgString(Properties options) {
58 if (options == null) {
59 return null;
60 }
61
62 StringBuilder argString = new StringBuilder();
63 for (Entry<Object, Object> option : options.entrySet()) {
64 String key = String.valueOf(option.getKey());
65 String value = String.valueOf(option.getValue());
66
67 if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
68 if (argString.length() > 0) {
69 argString.append(" ");
70 }
71 argString.append("-").append(key);
72 argString.append(" ");
73 if (value.indexOf(" ") >= 0
74 && !(0 == value.indexOf("'") && (value.length() - 1) == value.lastIndexOf("'"))) {
75 argString.append("'").append(value).append("'");
76 } else {
77 argString.append(value);
78 }
79 }
80 }
81 return argString.toString();
82 }
83
84
85
86
87
88
89
90
91 public static String generateNodeFiltersString(Properties nodeFilters) {
92 if (nodeFilters == null) {
93 return null;
94 }
95
96 List<String> filters = new ArrayList<String>();
97 for (Entry<Object, Object> filter : nodeFilters.entrySet()) {
98 String key = String.valueOf(filter.getKey());
99 String value = String.valueOf(filter.getValue());
100
101 if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
102 try {
103 filters.add(URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"));
104 } catch (UnsupportedEncodingException e) {
105 throw new RuntimeException(e);
106 }
107 }
108 }
109 return StringUtils.join(filters, "&");
110 }
111
112 }