@@ -284,32 +284,42 @@ 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
+ }
289
+
290
+ public UriComponents build (boolean encoded ) {
291
+ return build (encoded , true );
288
292
}
289
293
290
294
/**
291
295
* Variant of {@link #build()} to create a {@link UriComponents} instance
292
296
* when components are already fully encoded. This is useful for example if
293
297
* the builder was created via {@link UriComponentsBuilder#fromUri(URI)}.
294
298
* @param encoded whether the components in this builder are already encoded
299
+ * @param sanitizePath whether the double slashes should be replaced with single slash
295
300
* @return the URI components
296
301
* @throws IllegalArgumentException if any of the components contain illegal
297
302
* characters that should have been encoded.
298
303
*/
299
- public UriComponents build (boolean encoded ) {
300
- return buildInternal (encoded ? EncodingHint .FULLY_ENCODED :
301
- (this .encodeTemplate ? EncodingHint .ENCODE_TEMPLATE : EncodingHint .NONE ));
304
+ public UriComponents build (boolean encoded , boolean sanitizePath ) {
305
+ EncodingHint hint = encoded ? EncodingHint .FULLY_ENCODED :
306
+ (this .encodeTemplate ? EncodingHint .ENCODE_TEMPLATE : EncodingHint .NONE );
307
+ return buildInternal (hint , sanitizePath );
302
308
}
303
309
304
310
private UriComponents buildInternal (EncodingHint hint ) {
311
+ return buildInternal (hint , true );
312
+ }
313
+
314
+ private UriComponents buildInternal (EncodingHint hint , boolean sanitizePath ) {
305
315
UriComponents result ;
306
316
if (this .ssp != null ) {
307
317
result = new OpaqueUriComponents (this .scheme , this .ssp , this .fragment );
308
318
}
309
319
else {
310
320
MultiValueMap <String , String > queryParams = new LinkedMultiValueMap <>(this .queryParams );
311
321
HierarchicalUriComponents uric = new HierarchicalUriComponents (this .scheme , this .fragment ,
312
- this .userInfo , this .host , this .port , this .pathBuilder .build (), queryParams ,
322
+ this .userInfo , this .host , this .port , this .pathBuilder .build (sanitizePath ), queryParams ,
313
323
hint == EncodingHint .FULLY_ENCODED );
314
324
result = (hint == EncodingHint .ENCODE_TEMPLATE ? uric .encodeTemplate (this .charset ) : uric );
315
325
}
@@ -771,7 +781,7 @@ public enum ParserType {
771
781
772
782
private interface PathComponentBuilder {
773
783
774
- @ Nullable PathComponent build ();
784
+ @ Nullable PathComponent build (boolean sanitizePath );
775
785
776
786
PathComponentBuilder cloneBuilder ();
777
787
}
@@ -823,11 +833,11 @@ public void addPath(String path) {
823
833
}
824
834
825
835
@ Override
826
- public PathComponent build () {
836
+ public PathComponent build (boolean sanitizePath ) {
827
837
int size = this .builders .size ();
828
838
List <PathComponent > components = new ArrayList <>(size );
829
839
for (PathComponentBuilder componentBuilder : this .builders ) {
830
- PathComponent pathComponent = componentBuilder .build ();
840
+ PathComponent pathComponent = componentBuilder .build (sanitizePath );
831
841
if (pathComponent != null ) {
832
842
components .add (pathComponent );
833
843
}
@@ -861,12 +871,12 @@ public void append(String path) {
861
871
}
862
872
863
873
@ Override
864
- public @ Nullable PathComponent build () {
874
+ public @ Nullable PathComponent build (boolean sanitizePath ) {
865
875
if (this .path .isEmpty ()) {
866
876
return null ;
867
877
}
868
- String sanitized = getSanitizedPath (this .path );
869
- return new HierarchicalUriComponents .FullPathComponent (sanitized );
878
+ String path = sanitizePath ? getSanitizedPath (this .path ) : this . path . toString ( );
879
+ return new HierarchicalUriComponents .FullPathComponent (path );
870
880
}
871
881
872
882
private static String getSanitizedPath (final StringBuilder path ) {
@@ -911,7 +921,7 @@ public void append(String... pathSegments) {
911
921
}
912
922
913
923
@ Override
914
- public @ Nullable PathComponent build () {
924
+ public @ Nullable PathComponent build (boolean sanitizePath ) {
915
925
return (this .pathSegments .isEmpty () ? null :
916
926
new HierarchicalUriComponents .PathSegmentComponent (this .pathSegments ));
917
927
}
0 commit comments