1 /*
2 * Copyright 2011 Vincent Behar
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Utility class for API parameters that should be passed in URLs.
28 *
29 * @author Vincent Behar
30 */
31 public class ParametersUtil {
32
33 /**
34 * URL-encode the given string
35 *
36 * @param input string to be encoded
37 * @return an url-encoded string
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 * Generates a Rundeck "argString" representing the given options. Format of the argString is
52 * <code>"-key1 value1 -key2 'value 2 with spaces'"</code>. You might want to url-encode this string...
53 *
54 * @param options to be converted
55 * @return a string. null if options is null, empty if there are no valid options.
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 * Generates an url-encoded string representing the given nodeFilters. Format of the string is
86 * <code>"filter1=value1&filter2=value2"</code>.
87 *
88 * @param nodeFilters to be converted
89 * @return an url-encoded string. null if nodeFilters is null, empty if there are no valid filters.
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 }