|
| 1 | +/** |
| 2 | + * Copyright 2012-2018 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | + |
| 18 | +/** |
| 19 | + * Various ways to encode collections in URL parameters. |
| 20 | + * |
| 21 | + * <p>These specific cases are inspired by the |
| 22 | + * <a href="http://swagger.io/specification/">OpenAPI specification</a>.</p> |
| 23 | + */ |
| 24 | +public enum CollectionFormat { |
| 25 | + /** Comma separated values, eg foo=bar,baz */ |
| 26 | + CSV(","), |
| 27 | + /** Space separated values, eg foo=bar baz */ |
| 28 | + SSV(" "), |
| 29 | + /** Tab separated values, eg foo=bar[tab]baz */ |
| 30 | + TSV("\t"), |
| 31 | + /** Values separated with the pipe (|) character, eg foo=bar|baz */ |
| 32 | + PIPES("|"), |
| 33 | + /** Parameter name repeated for each value, eg foo=bar&foo=baz */ |
| 34 | + // Using null as a special case since there is no single separator character |
| 35 | + EXPLODED(null); |
| 36 | + |
| 37 | + private final String separator; |
| 38 | + |
| 39 | + CollectionFormat(String separator) { |
| 40 | + this.separator = separator; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Joins the field and possibly multiple values with the given separator. |
| 45 | + * |
| 46 | + * <p>Calling EXPLODED.join("foo", ["bar"]) will return "foo=bar".</p> |
| 47 | + * |
| 48 | + * <p>Calling CSV.join("foo", ["bar", "baz"]) will return "foo=bar,baz". </p> |
| 49 | + * |
| 50 | + * <p>Null values are treated somewhat specially. With EXPLODED, the field |
| 51 | + * is repeated without any "=" for backwards compatibility. With all other |
| 52 | + * formats, null values are not included in the joined value list.</p> |
| 53 | + * |
| 54 | + * @param field The field name corresponding to these values. |
| 55 | + * @param values A collection of value strings for the given field. |
| 56 | + * @return The formatted char sequence of the field and joined values. If the |
| 57 | + * value collection is empty, an empty char sequence will be returned. |
| 58 | + */ |
| 59 | + CharSequence join(String field, Collection<String> values) { |
| 60 | + StringBuilder builder = new StringBuilder(); |
| 61 | + int valueCount = 0; |
| 62 | + for (String value : values) { |
| 63 | + if (separator == null) { |
| 64 | + // exploded |
| 65 | + builder.append(valueCount++ == 0 ? "" : "&"); |
| 66 | + builder.append(field); |
| 67 | + if (value != null) { |
| 68 | + builder.append('='); |
| 69 | + builder.append(value); |
| 70 | + } |
| 71 | + } else { |
| 72 | + // delimited with a separator character |
| 73 | + if (builder.length() == 0) { |
| 74 | + builder.append(field); |
| 75 | + } |
| 76 | + if (value == null) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + builder.append(valueCount++ == 0 ? "=" : separator); |
| 80 | + builder.append(value); |
| 81 | + } |
| 82 | + } |
| 83 | + return builder; |
| 84 | + } |
| 85 | +} |
0 commit comments