/
HierarchicalUriComponents.java
1071 lines (922 loc) · 30.8 KB
/
HierarchicalUriComponents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.util;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Extension of {@link UriComponents} for hierarchical URIs.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @author Phillip Webb
* @since 3.1.3
* @see <a href="https://tools.ietf.org/html/rfc3986#section-1.2.3">Hierarchical URIs</a>
*/
@SuppressWarnings("serial")
final class HierarchicalUriComponents extends UriComponents {
private static final char PATH_DELIMITER = '/';
private static final String PATH_DELIMITER_STRING = String.valueOf(PATH_DELIMITER);
private static final MultiValueMap<String, String> EMPTY_QUERY_PARAMS =
CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>());
/**
* Represents an empty path.
*/
static final PathComponent NULL_PATH_COMPONENT = new PathComponent() {
@Override
public String getPath() {
return "";
}
@Override
public List<String> getPathSegments() {
return Collections.emptyList();
}
@Override
public PathComponent encode(BiFunction<String, Type, String> encoder) {
return this;
}
@Override
public void verify() {
}
@Override
public PathComponent expand(UriTemplateVariables uriVariables, @Nullable UnaryOperator<String> encoder) {
return this;
}
@Override
public void copyToUriComponentsBuilder(UriComponentsBuilder builder) {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other);
}
@Override
public int hashCode() {
return getClass().hashCode();
}
};
@Nullable
private final String userInfo;
@Nullable
private final String host;
@Nullable
private final String port;
private final PathComponent path;
private final MultiValueMap<String, String> queryParams;
private final EncodeState encodeState;
@Nullable
private UnaryOperator<String> variableEncoder;
/**
* Package-private constructor. All arguments are optional, and can be {@code null}.
* @param scheme the scheme
* @param userInfo the user info
* @param host the host
* @param port the port
* @param path the path
* @param query the query parameters
* @param fragment the fragment
* @param encoded whether the components are already encoded
*/
HierarchicalUriComponents(@Nullable String scheme, @Nullable String fragment, @Nullable String userInfo,
@Nullable String host, @Nullable String port, @Nullable PathComponent path,
@Nullable MultiValueMap<String, String> query, boolean encoded) {
super(scheme, fragment);
this.userInfo = userInfo;
this.host = host;
this.port = port;
this.path = path != null ? path : NULL_PATH_COMPONENT;
this.queryParams = query != null ? CollectionUtils.unmodifiableMultiValueMap(query) : EMPTY_QUERY_PARAMS;
this.encodeState = encoded ? EncodeState.FULLY_ENCODED : EncodeState.RAW;
// Check for illegal characters..
if (encoded) {
verify();
}
}
private HierarchicalUriComponents(@Nullable String scheme, @Nullable String fragment,
@Nullable String userInfo, @Nullable String host, @Nullable String port,
PathComponent path, MultiValueMap<String, String> queryParams,
EncodeState encodeState, @Nullable UnaryOperator<String> variableEncoder) {
super(scheme, fragment);
this.userInfo = userInfo;
this.host = host;
this.port = port;
this.path = path;
this.queryParams = queryParams;
this.encodeState = encodeState;
this.variableEncoder = variableEncoder;
}
// Component getters
@Override
@Nullable
public String getSchemeSpecificPart() {
return null;
}
@Override
@Nullable
public String getUserInfo() {
return this.userInfo;
}
@Override
@Nullable
public String getHost() {
return this.host;
}
@Override
public int getPort() {
if (this.port == null) {
return -1;
}
else if (this.port.contains("{")) {
throw new IllegalStateException(
"The port contains a URI variable but has not been expanded yet: " + this.port);
}
return Integer.parseInt(this.port);
}
@Override
@NonNull
public String getPath() {
return this.path.getPath();
}
@Override
public List<String> getPathSegments() {
return this.path.getPathSegments();
}
@Override
@Nullable
public String getQuery() {
if (!this.queryParams.isEmpty()) {
StringBuilder queryBuilder = new StringBuilder();
this.queryParams.forEach((name, values) -> {
if (CollectionUtils.isEmpty(values)) {
if (queryBuilder.length() != 0) {
queryBuilder.append('&');
}
queryBuilder.append(name);
}
else {
for (Object value : values) {
if (queryBuilder.length() != 0) {
queryBuilder.append('&');
}
queryBuilder.append(name);
if (value != null) {
queryBuilder.append('=').append(value.toString());
}
}
}
});
return queryBuilder.toString();
}
else {
return null;
}
}
/**
* Return the map of query parameters. Empty if no query has been set.
*/
@Override
public MultiValueMap<String, String> getQueryParams() {
return this.queryParams;
}
// Encoding
/**
* Identical to {@link #encode()} but skipping over URI variable placeholders.
* Also {@link #variableEncoder} is initialized with the given charset for
* use later when URI variables are expanded.
*/
HierarchicalUriComponents encodeTemplate(Charset charset) {
if (this.encodeState.isEncoded()) {
return this;
}
// Remember the charset to encode URI variables later..
this.variableEncoder = value -> encodeUriComponent(value, charset, Type.URI);
UriTemplateEncoder encoder = new UriTemplateEncoder(charset);
String schemeTo = (getScheme() != null ? encoder.apply(getScheme(), Type.SCHEME) : null);
String fragmentTo = (getFragment() != null ? encoder.apply(getFragment(), Type.FRAGMENT) : null);
String userInfoTo = (getUserInfo() != null ? encoder.apply(getUserInfo(), Type.USER_INFO) : null);
String hostTo = (getHost() != null ? encoder.apply(getHost(), getHostType()) : null);
PathComponent pathTo = this.path.encode(encoder);
MultiValueMap<String, String> queryParamsTo = encodeQueryParams(encoder);
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo,
hostTo, this.port, pathTo, queryParamsTo, EncodeState.TEMPLATE_ENCODED, this.variableEncoder);
}
@Override
public HierarchicalUriComponents encode(Charset charset) {
if (this.encodeState.isEncoded()) {
return this;
}
String scheme = getScheme();
String fragment = getFragment();
String schemeTo = (scheme != null ? encodeUriComponent(scheme, charset, Type.SCHEME) : null);
String fragmentTo = (fragment != null ? encodeUriComponent(fragment, charset, Type.FRAGMENT) : null);
String userInfoTo = (this.userInfo != null ? encodeUriComponent(this.userInfo, charset, Type.USER_INFO) : null);
String hostTo = (this.host != null ? encodeUriComponent(this.host, charset, getHostType()) : null);
BiFunction<String, Type, String> encoder = (s, type) -> encodeUriComponent(s, charset, type);
PathComponent pathTo = this.path.encode(encoder);
MultiValueMap<String, String> queryParamsTo = encodeQueryParams(encoder);
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo,
hostTo, this.port, pathTo, queryParamsTo, EncodeState.FULLY_ENCODED, null);
}
private MultiValueMap<String, String> encodeQueryParams(BiFunction<String, Type, String> encoder) {
int size = this.queryParams.size();
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(size);
this.queryParams.forEach((key, values) -> {
String name = encoder.apply(key, Type.QUERY_PARAM);
List<String> encodedValues = new ArrayList<>(values.size());
for (String value : values) {
encodedValues.add(value != null ? encoder.apply(value, Type.QUERY_PARAM) : null);
}
result.put(name, encodedValues);
});
return CollectionUtils.unmodifiableMultiValueMap(result);
}
/**
* Encode the given source into an encoded String using the rules specified
* by the given component and with the given options.
* @param source the source String
* @param encoding the encoding of the source String
* @param type the URI component for the source
* @return the encoded URI
* @throws IllegalArgumentException when the given value is not a valid URI component
*/
static String encodeUriComponent(String source, String encoding, Type type) {
return encodeUriComponent(source, Charset.forName(encoding), type);
}
/**
* Encode the given source into an encoded String using the rules specified
* by the given component and with the given options.
* @param source the source String
* @param charset the encoding of the source String
* @param type the URI component for the source
* @return the encoded URI
* @throws IllegalArgumentException when the given value is not a valid URI component
*/
static String encodeUriComponent(String source, Charset charset, Type type) {
if (!StringUtils.hasLength(source)) {
return source;
}
Assert.notNull(charset, "Charset must not be null");
Assert.notNull(type, "Type must not be null");
byte[] bytes = source.getBytes(charset);
boolean original = true;
for (byte b : bytes) {
if (b < 0) {
b += 256;
}
if (!type.isAllowed(b)) {
original = false;
break;
}
}
if (original) {
return source;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
for (byte b : bytes) {
if (b < 0) {
b += 256;
}
if (type.isAllowed(b)) {
bos.write(b);
}
else {
bos.write('%');
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
bos.write(hex1);
bos.write(hex2);
}
}
return new String(bos.toByteArray(), charset);
}
private Type getHostType() {
return (this.host != null && this.host.startsWith("[") ? Type.HOST_IPV6 : Type.HOST_IPV4);
}
// Verifying
/**
* Check if any of the URI components contain any illegal characters.
* @throws IllegalArgumentException if any component has illegal characters
*/
private void verify() {
verifyUriComponent(getScheme(), Type.SCHEME);
verifyUriComponent(this.userInfo, Type.USER_INFO);
verifyUriComponent(this.host, getHostType());
this.path.verify();
this.queryParams.forEach((key, values) -> {
verifyUriComponent(key, Type.QUERY_PARAM);
for (String value : values) {
verifyUriComponent(value, Type.QUERY_PARAM);
}
});
verifyUriComponent(getFragment(), Type.FRAGMENT);
}
private static void verifyUriComponent(@Nullable String source, Type type) {
if (source == null) {
return;
}
int length = source.length();
for (int i = 0; i < length; i++) {
char ch = source.charAt(i);
if (ch == '%') {
if ((i + 2) < length) {
char hex1 = source.charAt(i + 1);
char hex2 = source.charAt(i + 2);
int u = Character.digit(hex1, 16);
int l = Character.digit(hex2, 16);
if (u == -1 || l == -1) {
throw new IllegalArgumentException("Invalid encoded sequence \"" +
source.substring(i) + "\"");
}
i += 2;
}
else {
throw new IllegalArgumentException("Invalid encoded sequence \"" +
source.substring(i) + "\"");
}
}
else if (!type.isAllowed(ch)) {
throw new IllegalArgumentException("Invalid character '" + ch + "' for " +
type.name() + " in \"" + source + "\"");
}
}
}
// Expanding
@Override
protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVariables) {
Assert.state(!this.encodeState.equals(EncodeState.FULLY_ENCODED),
"URI components already encoded, and could not possibly contain '{' or '}'.");
// Array-based vars rely on the order below...
String schemeTo = expandUriComponent(getScheme(), uriVariables, this.variableEncoder);
String userInfoTo = expandUriComponent(this.userInfo, uriVariables, this.variableEncoder);
String hostTo = expandUriComponent(this.host, uriVariables, this.variableEncoder);
String portTo = expandUriComponent(this.port, uriVariables, this.variableEncoder);
PathComponent pathTo = this.path.expand(uriVariables, this.variableEncoder);
MultiValueMap<String, String> queryParamsTo = expandQueryParams(uriVariables);
String fragmentTo = expandUriComponent(getFragment(), uriVariables, this.variableEncoder);
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo,
hostTo, portTo, pathTo, queryParamsTo, this.encodeState, this.variableEncoder);
}
private MultiValueMap<String, String> expandQueryParams(UriTemplateVariables variables) {
int size = this.queryParams.size();
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(size);
UriTemplateVariables queryVariables = new QueryUriTemplateVariables(variables);
this.queryParams.forEach((key, values) -> {
String name = expandUriComponent(key, queryVariables, this.variableEncoder);
List<String> expandedValues = new ArrayList<>(values.size());
for (String value : values) {
expandedValues.add(expandUriComponent(value, queryVariables, this.variableEncoder));
}
result.put(name, expandedValues);
});
return CollectionUtils.unmodifiableMultiValueMap(result);
}
@Override
public UriComponents normalize() {
String normalizedPath = StringUtils.cleanPath(getPath());
FullPathComponent path = new FullPathComponent(normalizedPath);
return new HierarchicalUriComponents(getScheme(), getFragment(), this.userInfo, this.host, this.port,
path, this.queryParams, this.encodeState, this.variableEncoder);
}
// Other functionality
@Override
public String toUriString() {
StringBuilder uriBuilder = new StringBuilder();
if (getScheme() != null) {
uriBuilder.append(getScheme()).append(':');
}
if (this.userInfo != null || this.host != null) {
uriBuilder.append("//");
if (this.userInfo != null) {
uriBuilder.append(this.userInfo).append('@');
}
if (this.host != null) {
uriBuilder.append(this.host);
}
if (getPort() != -1) {
uriBuilder.append(':').append(this.port);
}
}
String path = getPath();
if (StringUtils.hasLength(path)) {
if (uriBuilder.length() != 0 && path.charAt(0) != PATH_DELIMITER) {
uriBuilder.append(PATH_DELIMITER);
}
uriBuilder.append(path);
}
String query = getQuery();
if (query != null) {
uriBuilder.append('?').append(query);
}
if (getFragment() != null) {
uriBuilder.append('#').append(getFragment());
}
return uriBuilder.toString();
}
@Override
public URI toUri() {
try {
if (this.encodeState.isEncoded()) {
return new URI(toUriString());
}
else {
String path = getPath();
if (StringUtils.hasLength(path) && path.charAt(0) != PATH_DELIMITER) {
// Only prefix the path delimiter if something exists before it
if (getScheme() != null || getUserInfo() != null || getHost() != null || getPort() != -1) {
path = PATH_DELIMITER + path;
}
}
return new URI(getScheme(), getUserInfo(), getHost(), getPort(), path, getQuery(), getFragment());
}
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Could not create URI object: " + ex.getMessage(), ex);
}
}
@Override
protected void copyToUriComponentsBuilder(UriComponentsBuilder builder) {
if (getScheme() != null) {
builder.scheme(getScheme());
}
if (getUserInfo() != null) {
builder.userInfo(getUserInfo());
}
if (getHost() != null) {
builder.host(getHost());
}
// Avoid parsing the port, may have URI variable..
if (this.port != null) {
builder.port(this.port);
}
this.path.copyToUriComponentsBuilder(builder);
if (!getQueryParams().isEmpty()) {
builder.queryParams(getQueryParams());
}
if (getFragment() != null) {
builder.fragment(getFragment());
}
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof HierarchicalUriComponents)) {
return false;
}
HierarchicalUriComponents otherComp = (HierarchicalUriComponents) other;
return (ObjectUtils.nullSafeEquals(getScheme(), otherComp.getScheme()) &&
ObjectUtils.nullSafeEquals(getUserInfo(), otherComp.getUserInfo()) &&
ObjectUtils.nullSafeEquals(getHost(), otherComp.getHost()) &&
getPort() == otherComp.getPort() &&
this.path.equals(otherComp.path) &&
this.queryParams.equals(otherComp.queryParams) &&
ObjectUtils.nullSafeEquals(getFragment(), otherComp.getFragment()));
}
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(getScheme());
result = 31 * result + ObjectUtils.nullSafeHashCode(this.userInfo);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.host);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.port);
result = 31 * result + this.path.hashCode();
result = 31 * result + this.queryParams.hashCode();
result = 31 * result + ObjectUtils.nullSafeHashCode(getFragment());
return result;
}
// Nested types
/**
* Enumeration used to identify the allowed characters per URI component.
* <p>Contains methods to indicate whether a given character is valid in a specific URI component.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
*/
enum Type {
SCHEME {
@Override
public boolean isAllowed(int c) {
return isAlpha(c) || isDigit(c) || '+' == c || '-' == c || '.' == c;
}
},
AUTHORITY {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c;
}
},
USER_INFO {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || ':' == c;
}
},
HOST_IPV4 {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c);
}
},
HOST_IPV6 {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c) || isSubDelimiter(c) || '[' == c || ']' == c || ':' == c;
}
},
PORT {
@Override
public boolean isAllowed(int c) {
return isDigit(c);
}
},
PATH {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c;
}
},
PATH_SEGMENT {
@Override
public boolean isAllowed(int c) {
return isPchar(c);
}
},
QUERY {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c || '?' == c;
}
},
QUERY_PARAM {
@Override
public boolean isAllowed(int c) {
if ('=' == c || '&' == c) {
return false;
}
else {
return isPchar(c) || '/' == c || '?' == c;
}
}
},
FRAGMENT {
@Override
public boolean isAllowed(int c) {
return isPchar(c) || '/' == c || '?' == c;
}
},
URI {
@Override
public boolean isAllowed(int c) {
return isUnreserved(c);
}
};
/**
* Indicates whether the given character is allowed in this URI component.
* @return {@code true} if the character is allowed; {@code false} otherwise
*/
public abstract boolean isAllowed(int c);
/**
* Indicates whether the given character is in the {@code ALPHA} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isAlpha(int c) {
return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
}
/**
* Indicates whether the given character is in the {@code DIGIT} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isDigit(int c) {
return (c >= '0' && c <= '9');
}
/**
* Indicates whether the given character is in the {@code gen-delims} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isGenericDelimiter(int c) {
return (':' == c || '/' == c || '?' == c || '#' == c || '[' == c || ']' == c || '@' == c);
}
/**
* Indicates whether the given character is in the {@code sub-delims} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isSubDelimiter(int c) {
return ('!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || '*' == c || '+' == c ||
',' == c || ';' == c || '=' == c);
}
/**
* Indicates whether the given character is in the {@code reserved} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isReserved(int c) {
return (isGenericDelimiter(c) || isSubDelimiter(c));
}
/**
* Indicates whether the given character is in the {@code unreserved} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isUnreserved(int c) {
return (isAlpha(c) || isDigit(c) || '-' == c || '.' == c || '_' == c || '~' == c);
}
/**
* Indicates whether the given character is in the {@code pchar} set.
* @see <a href="https://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isPchar(int c) {
return (isUnreserved(c) || isSubDelimiter(c) || ':' == c || '@' == c);
}
}
private enum EncodeState {
/**
* Not encoded.
*/
RAW,
/**
* URI vars expanded first and then each URI component encoded by
* quoting only illegal characters within that URI component.
*/
FULLY_ENCODED,
/**
* URI template encoded first by quoting illegal characters only, and
* then URI vars encoded more strictly when expanded, by quoting both
* illegal chars and chars with reserved meaning.
*/
TEMPLATE_ENCODED;
public boolean isEncoded() {
return this.equals(FULLY_ENCODED) || this.equals(TEMPLATE_ENCODED);
}
}
private static class UriTemplateEncoder implements BiFunction<String, Type, String> {
private final Charset charset;
private final StringBuilder currentLiteral = new StringBuilder();
private final StringBuilder currentVariable = new StringBuilder();
private final StringBuilder output = new StringBuilder();
public UriTemplateEncoder(Charset charset) {
this.charset = charset;
}
@Override
public String apply(String source, Type type) {
// Only URI variable (nothing to encode)..
if (source.length() > 1 && source.charAt(0) == '{' && source.charAt(source.length() -1) == '}') {
return source;
}
// Only literal (encode full source)..
if (source.indexOf('{') == -1) {
return encodeUriComponent(source, this.charset, type);
}
// Mixed literal parts and URI variables, maybe (encode literal parts only)..
int level = 0;
clear(this.currentLiteral);
clear(this.currentVariable);
clear(this.output);
for (char c : source.toCharArray()) {
if (c == '{') {
level++;
if (level == 1) {
encodeAndAppendCurrentLiteral(type);
}
}
if (c == '}' && level > 0) {
level--;
this.currentVariable.append('}');
if (level == 0) {
this.output.append(this.currentVariable);
clear(this.currentVariable);
}
}
else if (level > 0) {
this.currentVariable.append(c);
}
else {
this.currentLiteral.append(c);
}
}
if (level > 0) {
this.currentLiteral.append(this.currentVariable);
}
encodeAndAppendCurrentLiteral(type);
return this.output.toString();
}
private void encodeAndAppendCurrentLiteral(Type type) {
this.output.append(encodeUriComponent(this.currentLiteral.toString(), this.charset, type));
clear(this.currentLiteral);
}
private void clear(StringBuilder sb) {
sb.delete(0, sb.length());
}
}
/**
* Defines the contract for path (segments).
*/
interface PathComponent extends Serializable {
String getPath();
List<String> getPathSegments();
PathComponent encode(BiFunction<String, Type, String> encoder);
void verify();
PathComponent expand(UriTemplateVariables uriVariables, @Nullable UnaryOperator<String> encoder);
void copyToUriComponentsBuilder(UriComponentsBuilder builder);
}
/**
* Represents a path backed by a String.
*/
static final class FullPathComponent implements PathComponent {
private final String path;
public FullPathComponent(@Nullable String path) {
this.path = (path != null ? path : "");
}
@Override
public String getPath() {
return this.path;
}
@Override
public List<String> getPathSegments() {
String[] segments = StringUtils.tokenizeToStringArray(getPath(), PATH_DELIMITER_STRING);
return Collections.unmodifiableList(Arrays.asList(segments));
}
@Override
public PathComponent encode(BiFunction<String, Type, String> encoder) {
String encodedPath = encoder.apply(getPath(), Type.PATH);
return new FullPathComponent(encodedPath);
}
@Override
public void verify() {
verifyUriComponent(getPath(), Type.PATH);
}
@Override
public PathComponent expand(UriTemplateVariables uriVariables, @Nullable UnaryOperator<String> encoder) {
String expandedPath = expandUriComponent(getPath(), uriVariables, encoder);
return new FullPathComponent(expandedPath);
}
@Override
public void copyToUriComponentsBuilder(UriComponentsBuilder builder) {
builder.path(getPath());
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof FullPathComponent &&
getPath().equals(((FullPathComponent) other).getPath())));
}
@Override
public int hashCode() {
return getPath().hashCode();
}
}
/**
* Represents a path backed by a String list (i.e. path segments).
*/
static final class PathSegmentComponent implements PathComponent {
private final List<String> pathSegments;
public PathSegmentComponent(List<String> pathSegments) {
Assert.notNull(pathSegments, "List must not be null");
this.pathSegments = Collections.unmodifiableList(new ArrayList<>(pathSegments));
}
@Override
public String getPath() {
String delimiter = PATH_DELIMITER_STRING;
StringJoiner pathBuilder = new StringJoiner(delimiter, delimiter, "");
for (String pathSegment : this.pathSegments) {
pathBuilder.add(pathSegment);
}
return pathBuilder.toString();
}
@Override
public List<String> getPathSegments() {
return this.pathSegments;
}
@Override
public PathComponent encode(BiFunction<String, Type, String> encoder) {
List<String> pathSegments = getPathSegments();
List<String> encodedPathSegments = new ArrayList<>(pathSegments.size());
for (String pathSegment : pathSegments) {
String encodedPathSegment = encoder.apply(pathSegment, Type.PATH_SEGMENT);
encodedPathSegments.add(encodedPathSegment);
}
return new PathSegmentComponent(encodedPathSegments);
}
@Override
public void verify() {
for (String pathSegment : getPathSegments()) {
verifyUriComponent(pathSegment, Type.PATH_SEGMENT);
}
}
@Override
public PathComponent expand(UriTemplateVariables uriVariables, @Nullable UnaryOperator<String> encoder) {
List<String> pathSegments = getPathSegments();
List<String> expandedPathSegments = new ArrayList<>(pathSegments.size());
for (String pathSegment : pathSegments) {
String expandedPathSegment = expandUriComponent(pathSegment, uriVariables, encoder);
expandedPathSegments.add(expandedPathSegment);
}
return new PathSegmentComponent(expandedPathSegments);
}
@Override
public void copyToUriComponentsBuilder(UriComponentsBuilder builder) {
builder.pathSegment(StringUtils.toStringArray(getPathSegments()));
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof PathSegmentComponent &&
getPathSegments().equals(((PathSegmentComponent) other).getPathSegments())));
}
@Override
public int hashCode() {
return getPathSegments().hashCode();
}
}
/**
* Represents a collection of PathComponents.
*/
static final class PathComponentComposite implements PathComponent {
private final List<PathComponent> pathComponents;
public PathComponentComposite(List<PathComponent> pathComponents) {
Assert.notNull(pathComponents, "PathComponent List must not be null");
this.pathComponents = pathComponents;
}