18
18
19
19
import java .net .URI ;
20
20
import java .net .URISyntaxException ;
21
+ import java .nio .charset .StandardCharsets ;
21
22
22
23
import org .springframework .http .HttpHeaders ;
23
24
import org .springframework .http .HttpMethod ;
@@ -79,18 +80,51 @@ public ServerHttpRequest.Builder header(String key, String value) {
79
80
80
81
@ Override
81
82
public ServerHttpRequest build () {
82
- URI uri = null ;
83
- if (this .path != null ) {
84
- uri = this .delegate .getURI ();
85
- try {
86
- uri = new URI (uri .getScheme (), uri .getUserInfo (), uri .getHost (), uri .getPort (),
87
- this .path , uri .getQuery (), uri .getFragment ());
88
- }
89
- catch (URISyntaxException ex ) {
90
- throw new IllegalStateException ("Invalid URI path: \" " + this .path + "\" " );
91
- }
83
+ URI uriToUse = getUriToUse ();
84
+ RequestPath path = getRequestPathToUse (uriToUse );
85
+ HttpHeaders headers = getHeadersToUse ();
86
+ return new MutativeDecorator (this .delegate , this .httpMethod , uriToUse , path , headers );
87
+ }
88
+
89
+ @ Nullable
90
+ private URI getUriToUse () {
91
+ if (this .path == null ) {
92
+ return null ;
93
+ }
94
+ URI uri = this .delegate .getURI ();
95
+ try {
96
+ return new URI (uri .getScheme (), uri .getUserInfo (), uri .getHost (), uri .getPort (),
97
+ this .path , uri .getQuery (), uri .getFragment ());
98
+ }
99
+ catch (URISyntaxException ex ) {
100
+ throw new IllegalStateException ("Invalid URI path: \" " + this .path + "\" " );
101
+ }
102
+ }
103
+
104
+ @ Nullable
105
+ private RequestPath getRequestPathToUse (@ Nullable URI uriToUse ) {
106
+ if (uriToUse == null && this .contextPath == null ) {
107
+ return null ;
108
+ }
109
+ else if (uriToUse == null ) {
110
+ return new DefaultRequestPath (this .delegate .getPath (), this .contextPath , StandardCharsets .UTF_8 );
111
+ }
112
+ else {
113
+ return new DefaultRequestPath (uriToUse , this .contextPath , StandardCharsets .UTF_8 );
114
+ }
115
+ }
116
+
117
+ @ Nullable
118
+ private HttpHeaders getHeadersToUse () {
119
+ if (this .httpHeaders != null ) {
120
+ HttpHeaders headers = new HttpHeaders ();
121
+ headers .putAll (this .delegate .getHeaders ());
122
+ headers .putAll (this .httpHeaders );
123
+ return headers ;
124
+ }
125
+ else {
126
+ return null ;
92
127
}
93
- return new MutativeDecorator (this .delegate , this .httpMethod , uri , this .contextPath , this .httpHeaders );
94
128
}
95
129
96
130
@@ -104,25 +138,20 @@ private static class MutativeDecorator extends ServerHttpRequestDecorator {
104
138
105
139
private final URI uri ;
106
140
107
- private final String contextPath ;
141
+ private final RequestPath requestPath ;
108
142
109
143
private final HttpHeaders httpHeaders ;
110
144
111
- public MutativeDecorator (ServerHttpRequest delegate , HttpMethod httpMethod ,
112
- @ Nullable URI uri , String contextPath , @ Nullable HttpHeaders httpHeaders ) {
145
+
146
+ public MutativeDecorator (ServerHttpRequest delegate , HttpMethod method ,
147
+ @ Nullable URI uri , @ Nullable RequestPath requestPath ,
148
+ @ Nullable HttpHeaders httpHeaders ) {
113
149
114
150
super (delegate );
115
- this .httpMethod = httpMethod ;
151
+ this .httpMethod = method ;
116
152
this .uri = uri ;
117
- this .contextPath = contextPath ;
118
- if (httpHeaders != null ) {
119
- this .httpHeaders = new HttpHeaders ();
120
- this .httpHeaders .putAll (super .getHeaders ());
121
- this .httpHeaders .putAll (httpHeaders );
122
- }
123
- else {
124
- this .httpHeaders = null ;
125
- }
153
+ this .requestPath = requestPath ;
154
+ this .httpHeaders = httpHeaders ;
126
155
}
127
156
128
157
@ Override
@@ -136,8 +165,8 @@ public URI getURI() {
136
165
}
137
166
138
167
@ Override
139
- public String getContextPath () {
140
- return (this .contextPath != null ? this .contextPath : super .getContextPath ());
168
+ public RequestPath getPath () {
169
+ return (this .requestPath != null ? this .requestPath : super .getPath ());
141
170
}
142
171
143
172
@ Override
0 commit comments