View Javadoc

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;
17  
18  import java.util.Properties;
19  import org.apache.commons.lang.StringUtils;
20  import org.rundeck.api.util.ParametersUtil;
21  
22  /**
23   * Builder for API paths
24   * 
25   * @author Vincent Behar
26   */
27  class ApiPathBuilder {
28  
29      /** Internally, we store everything in a {@link StringBuilder} */
30      private final StringBuilder apiPath;
31  
32      /** Maker for using the right separator between parameters ("?" or "&") */
33      private boolean firstParamDone = false;
34  
35      /**
36       * Build a new instance, for the given "path" (the "path" is the part before the parameters. The path and the
37       * parameters are separated by a "?")
38       * 
39       * @param paths elements of the path
40       */
41      public ApiPathBuilder(String... paths) {
42          apiPath = new StringBuilder();
43          if (paths != null) {
44              for (String path : paths) {
45                  if (StringUtils.isNotBlank(path)) {
46                      append(path);
47                  }
48              }
49          }
50      }
51  
52      /**
53       * Append the given parameter (key and value). This will only append the parameter if it is not blank (null, empty
54       * or whitespace), and make sure to add the right separator ("?" or "&") before. The key and value will be separated
55       * by the "=" character. Also, the value will be url-encoded.
56       * 
57       * @param key of the parameter. Must not be null or empty
58       * @param value of the parameter. May be null/empty/blank. Will be url-encoded.
59       * @return this, for method chaining
60       */
61      public ApiPathBuilder param(String key, String value) {
62          if (StringUtils.isNotBlank(value)) {
63              appendSeparator();
64              append(key);
65              append("=");
66              append(ParametersUtil.urlEncode(value));
67          }
68          return this;
69      }
70  
71      /**
72       * Append the given parameter (key and value). This will only append the parameter if it is not null, and make sure
73       * to add the right separator ("?" or "&") before. The key and value will be separated by the "=" character.
74       * 
75       * @param key of the parameter. Must not be null or empty
76       * @param value of the parameter. May be null
77       * @return this, for method chaining
78       */
79      public ApiPathBuilder param(String key, Long value) {
80          if (value != null) {
81              param(key, value.toString());
82          }
83          return this;
84      }
85  
86      /**
87       * Append the given node filters, only if it is not null/empty
88       * 
89       * @param nodeFilters may be null/empty
90       * @return this, for method chaining
91       * @see ParametersUtil#generateNodeFiltersString(Properties)
92       */
93      public ApiPathBuilder nodeFilters(Properties nodeFilters) {
94          String filters = ParametersUtil.generateNodeFiltersString(nodeFilters);
95          if (StringUtils.isNotBlank(filters)) {
96              appendSeparator();
97              append(filters);
98          }
99          return this;
100     }
101 
102     @Override
103     public String toString() {
104         return apiPath.toString();
105     }
106 
107     /**
108      * Append the given string
109      * 
110      * @param str to append
111      */
112     private void append(String str) {
113         apiPath.append(str);
114     }
115 
116     /**
117      * Append the right separator "?" or "&" between 2 parameters
118      */
119     private void appendSeparator() {
120         if (firstParamDone) {
121             append("&");
122         } else {
123             append("?");
124             firstParamDone = true;
125         }
126     }
127 
128 }