1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.repository

File UriBuilder.java

 

Coverage histogram

../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

76
121
11
1
301
222
57
0.47
11
11
5.18

Classes

Class Line # Actions
UriBuilder 34 121 0% 57 38
0.817307781.7%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.repository;
21   
22    import java.io.UnsupportedEncodingException;
23    import java.net.URI;
24    import java.net.URISyntaxException;
25   
26    /**
27    * Generate a URI to use to request a REST server.
28    * <p>
29    * Support javax.ws.rs.Path style URIs.
30    *
31    * @version $Id: 5824050bcf9880a53cd10150292316671f51f5cd $
32    * @since 4.2M1
33    */
 
34    public class UriBuilder implements Cloneable
35    {
36    private String scheme;
37   
38    private String host;
39   
40    private Integer port;
41   
42    private String userInfo;
43   
44    private CharSequence path;
45   
46    private CharSequence query;
47   
48    private String fragment;
49   
 
50  15 toggle public UriBuilder(URI base, String path)
51    {
52  15 uri(base);
53  15 path(path);
54    }
55   
 
56  8 toggle public UriBuilder(String base, String path)
57    {
58  8 try {
59  8 uri(new URI(base));
60    } catch (URISyntaxException e) {
61  0 throw new IllegalArgumentException("Invalid base URI [" + base + "]", e);
62    }
63   
64  8 path(path);
65    }
66   
 
67  23 toggle private void uri(URI uri) throws IllegalArgumentException
68    {
69  23 if (uri == null) {
70  0 throw new IllegalArgumentException("The URI must not be null");
71    }
72   
73  23 if (uri.getScheme() != null) {
74  23 this.scheme = uri.getScheme();
75    }
76  23 if (uri.getHost() != null) {
77  21 this.host = uri.getHost();
78    }
79  23 if (uri.getPort() > 0) {
80  6 this.port = uri.getPort();
81    }
82  23 if (uri.getRawUserInfo() != null) {
83  0 this.userInfo = uri.getRawUserInfo();
84    }
85  23 if (uri.getRawPath() != null) {
86  23 this.path = uri.getRawPath();
87    }
88  23 if (uri.getRawQuery() != null) {
89  1 this.query = uri.getRawQuery();
90    }
91  23 if (uri.getRawFragment() != null) {
92  1 this.fragment = uri.getRawFragment();
93    }
94    }
95   
 
96  23 toggle private void path(String path)
97    {
98  23 if (path != null && !path.isEmpty()) {
99  22 StringBuilder stringBuilder;
100  22 if (this.path != null) {
101  22 if (this.path instanceof String) {
102  22 stringBuilder = new StringBuilder(this.path);
103  22 this.path = stringBuilder;
104    } else {
105  0 stringBuilder = (StringBuilder) this.path;
106    }
107    } else {
108  0 stringBuilder = new StringBuilder();
109  0 this.path = stringBuilder;
110    }
111   
112  22 if (this.path.length() == 0 || this.path.charAt(this.path.length() - 1) != '/') {
113  22 if (path.charAt(0) != '/') {
114  5 stringBuilder.append('/');
115    }
116   
117  22 stringBuilder.append(path);
118    } else {
119  0 int i = 0;
120  0 for (; i < path.length() && path.charAt(i) == '/'; ++i) {
121    // Nothing to do, the for loop already has all the required code
122    }
123   
124  0 if (i > 0) {
125  0 stringBuilder.append(path.substring(i));
126    } else {
127  0 stringBuilder.append(path);
128    }
129    }
130    }
131    }
132   
 
133  194 toggle public static String encode(String toEncode)
134    {
135  194 String result = null;
136   
137  194 if (toEncode != null) {
138  194 try {
139  194 result = java.net.URLEncoder.encode(toEncode, "UTF-8");
140    } catch (UnsupportedEncodingException e) {
141    // Should never happen
142    }
143    }
144   
145  194 return result;
146    }
147   
 
148  14 toggle public UriBuilder queryParam(String name, Object... values) throws IllegalArgumentException
149    {
150  14 if (values == null) {
151  0 throw new IllegalArgumentException("The values must not be null");
152    }
153   
154  14 String encodedName = encode(name);
155   
156  14 StringBuilder queryBuilder;
157  14 if (this.query == null) {
158  7 queryBuilder = new StringBuilder();
159  7 this.query = queryBuilder;
160  7 } else if (this.query instanceof StringBuilder) {
161  6 queryBuilder = (StringBuilder) this.query;
162    } else {
163  1 queryBuilder = new StringBuilder(this.query);
164  1 this.query = queryBuilder;
165    }
166   
167  14 for (Object value : values) {
168  14 if (queryBuilder.length() > 0) {
169  7 queryBuilder.append('&');
170    }
171  14 queryBuilder.append(encodedName);
172  14 queryBuilder.append('=');
173  14 queryBuilder.append(encode(value.toString()));
174    }
175   
176  14 return this;
177    }
178   
 
179  117 toggle public URI build(Object... values)
180    {
181  117 final StringBuilder stb = new StringBuilder();
182  117 if (this.scheme != null) {
183  117 stb.append(this.scheme);
184  117 stb.append("://");
185    }
186  117 if (this.userInfo != null) {
187  0 stb.append(this.userInfo);
188  0 stb.append('@');
189    }
190  117 if (this.host != null) {
191  115 stb.append(this.host);
192    }
193  117 if (this.port != null) {
194  108 stb.append(':');
195  108 stb.append(this.port);
196    }
197   
198  117 String resolvePath = formatPath(values);
199  117 if (resolvePath != null) {
200  117 if (stb.length() > 0) {
201  117 if (resolvePath.length() == 0 || resolvePath.charAt(0) != '/') {
202  1 stb.append('/');
203    }
204    }
205  117 stb.append(resolvePath);
206    }
207   
208  117 String query = this.query != null ? this.query.toString() : null;
209  117 if (query != null) {
210  8 stb.append('?');
211  8 stb.append(query);
212    }
213  117 if (this.fragment != null) {
214  1 stb.append('#');
215  1 stb.append(this.fragment);
216    }
217   
218  117 try {
219  117 return new URI(stb.toString());
220    } catch (URISyntaxException e) {
221  0 throw new IllegalArgumentException("Failed to build the URI", e);
222    }
223    }
224   
 
225  2369 toggle public static boolean isUnreserved(char character)
226    {
227  2369 return Character.isLetter(character) || Character.isDigit(character) || (character == '-')
228    || (character == '.') || (character == '_') || (character == '~');
229    }
230   
 
231  117 toggle private String formatPath(Object[] values)
232    {
233  117 final StringBuilder result = new StringBuilder();
234   
235  117 StringBuilder varBuffer = null;
236  117 char c;
237  117 boolean inVariable = false;
238  117 final int patternLength = this.path.length();
239  117 int valueId = 0;
240  7219 for (int i = 0; i < patternLength; i++) {
241  7102 c = this.path.charAt(i);
242   
243  7102 if (inVariable) {
244  2369 if (isUnreserved(c)) {
245    // Append to the variable name
246  2203 varBuffer.append(c);
247  166 } else if (c == '}') {
248    // End of variable detected
249  166 if (varBuffer.length() == 0) {
250    // TODO: log ?
251    } else {
252  166 Object varValue = values[valueId++];
253   
254  166 String varValueString = (varValue == null) ? null : varValue.toString();
255   
256  166 result.append(encode(varValueString));
257   
258    // Reset the variable name buffer
259  166 varBuffer = new StringBuilder();
260    }
261  166 inVariable = false;
262    } else {
263    // TODO: log ?
264    }
265    } else {
266  4733 if (c == '{') {
267  166 inVariable = true;
268  166 varBuffer = new StringBuilder();
269  4567 } else if (c == '}') {
270    // TODO: log ?
271    } else {
272  4567 result.append(c);
273    }
274    }
275    }
276   
277  117 return result.toString();
278    }
279   
280    // Object
281   
 
282  0 toggle @Override
283    public String toString()
284    {
285  0 return build().toString();
286    }
287   
 
288  32 toggle @Override
289    public UriBuilder clone()
290    {
291  32 UriBuilder clone = null;
292   
293  32 try {
294  32 clone = (UriBuilder) super.clone();
295    } catch (CloneNotSupportedException e) {
296    // Should never happen
297    }
298   
299  32 return clone;
300    }
301    }