@@ -284,7 +284,7 @@ public UriComponentsBuilder encode(Charset charset) {
284
284
* @return the URI components
285
285
*/
286
286
public UriComponents build () {
287
- return build (false );
287
+ return build (false , true );
288
288
}
289
289
290
290
/**
@@ -297,19 +297,39 @@ public UriComponents build() {
297
297
* characters that should have been encoded.
298
298
*/
299
299
public UriComponents build (boolean encoded ) {
300
- return buildInternal (encoded ? EncodingHint .FULLY_ENCODED :
301
- (this .encodeTemplate ? EncodingHint .ENCODE_TEMPLATE : EncodingHint .NONE ));
300
+ return build (encoded , true );
301
+ }
302
+
303
+ /**
304
+ * Variant of {@link #build()} to create a {@link UriComponents} instance
305
+ * when components are already fully encoded. In addition, this method allows
306
+ * to control whether the double slashes in the path should be replaced with
307
+ * a single slash.
308
+ * @param encoded whether the components in this builder are already encoded
309
+ * @param sanitizePath whether the double slashes should be replaced with single slash
310
+ * @return the URI components
311
+ * @throws IllegalArgumentException if any of the components contain illegal
312
+ * characters that should have been encoded.
313
+ */
314
+ public UriComponents build (boolean encoded , boolean sanitizePath ) {
315
+ EncodingHint hint = encoded ? EncodingHint .FULLY_ENCODED :
316
+ (this .encodeTemplate ? EncodingHint .ENCODE_TEMPLATE : EncodingHint .NONE );
317
+ return buildInternal (hint , sanitizePath );
302
318
}
303
319
304
320
private UriComponents buildInternal (EncodingHint hint ) {
321
+ return buildInternal (hint , true );
322
+ }
323
+
324
+ private UriComponents buildInternal (EncodingHint hint , boolean sanitizePath ) {
305
325
UriComponents result ;
306
326
if (this .ssp != null ) {
307
327
result = new OpaqueUriComponents (this .scheme , this .ssp , this .fragment );
308
328
}
309
329
else {
310
330
MultiValueMap <String , String > queryParams = new LinkedMultiValueMap <>(this .queryParams );
311
331
HierarchicalUriComponents uric = new HierarchicalUriComponents (this .scheme , this .fragment ,
312
- this .userInfo , this .host , this .port , this .pathBuilder .build (), queryParams ,
332
+ this .userInfo , this .host , this .port , this .pathBuilder .build (sanitizePath ), queryParams ,
313
333
hint == EncodingHint .FULLY_ENCODED );
314
334
result = (hint == EncodingHint .ENCODE_TEMPLATE ? uric .encodeTemplate (this .charset ) : uric );
315
335
}
@@ -771,7 +791,7 @@ public enum ParserType {
771
791
772
792
private interface PathComponentBuilder {
773
793
774
- @ Nullable PathComponent build ();
794
+ @ Nullable PathComponent build (boolean sanitizePath );
775
795
776
796
PathComponentBuilder cloneBuilder ();
777
797
}
@@ -823,11 +843,11 @@ public void addPath(String path) {
823
843
}
824
844
825
845
@ Override
826
- public PathComponent build () {
846
+ public PathComponent build (boolean sanitizePath ) {
827
847
int size = this .builders .size ();
828
848
List <PathComponent > components = new ArrayList <>(size );
829
849
for (PathComponentBuilder componentBuilder : this .builders ) {
830
- PathComponent pathComponent = componentBuilder .build ();
850
+ PathComponent pathComponent = componentBuilder .build (sanitizePath );
831
851
if (pathComponent != null ) {
832
852
components .add (pathComponent );
833
853
}
@@ -861,12 +881,12 @@ public void append(String path) {
861
881
}
862
882
863
883
@ Override
864
- public @ Nullable PathComponent build () {
884
+ public @ Nullable PathComponent build (boolean sanitizePath ) {
865
885
if (this .path .isEmpty ()) {
866
886
return null ;
867
887
}
868
- String sanitized = getSanitizedPath (this .path );
869
- return new HierarchicalUriComponents .FullPathComponent (sanitized );
888
+ String path = sanitizePath ? getSanitizedPath (this .path ) : this . path . toString ( );
889
+ return new HierarchicalUriComponents .FullPathComponent (path );
870
890
}
871
891
872
892
private static String getSanitizedPath (final StringBuilder path ) {
@@ -911,7 +931,7 @@ public void append(String... pathSegments) {
911
931
}
912
932
913
933
@ Override
914
- public @ Nullable PathComponent build () {
934
+ public @ Nullable PathComponent build (boolean sanitizePath ) {
915
935
return (this .pathSegments .isEmpty () ? null :
916
936
new HierarchicalUriComponents .PathSegmentComponent (this .pathSegments ));
917
937
}
0 commit comments