10
10
import hudson .ProxyConfiguration ;
11
11
import java .io .File ;
12
12
import java .io .IOException ;
13
+ import java .nio .charset .StandardCharsets ;
13
14
import java .nio .file .Path ;
14
15
import java .util .Base64 ;
15
16
import java .util .Date ;
16
17
import java .util .Optional ;
17
18
import java .util .concurrent .TimeUnit ;
18
- import javax .servlet .ServletException ;
19
- import javax .servlet .http .HttpServletRequest ;
20
- import javax .servlet .http .HttpServletResponse ;
21
19
import jenkins .model .Jenkins ;
22
20
import org .apache .commons .io .IOUtils ;
23
21
import org .apache .commons .lang3 .StringUtils ;
24
22
import org .eclipse .jetty .http .HttpHeader ;
25
23
import org .eclipse .jetty .http .HttpStatus ;
24
+ import org .eclipse .jetty .io .Content ;
26
25
import org .eclipse .jetty .server .ConnectionFactory ;
27
26
import org .eclipse .jetty .server .Handler ;
28
27
import org .eclipse .jetty .server .HttpConnectionFactory ;
29
28
import org .eclipse .jetty .server .Server ;
30
29
import org .eclipse .jetty .server .ServerConnector ;
31
- import org .eclipse .jetty .server . handler . AbstractHandler ;
30
+ import org .eclipse .jetty .util . Callback ;
32
31
import org .junit .After ;
33
32
import org .junit .Assert ;
34
33
import org .junit .Rule ;
@@ -151,7 +150,7 @@ public void simple_post_with_proxy() throws Exception {
151
150
Assert .assertEquals ("FOO" , testHandler .postReceived );
152
151
}
153
152
154
- public class ProxyTestHandler extends AbstractHandler {
153
+ public static class ProxyTestHandler extends Handler . Abstract {
155
154
156
155
String postReceived ;
157
156
@@ -164,55 +163,50 @@ public class ProxyTestHandler extends AbstractHandler {
164
163
final String realm = "test_realm" ;
165
164
166
165
@ Override
167
- public void handle (
168
- String target ,
169
- org .eclipse .jetty .server .Request jettyRequest ,
170
- HttpServletRequest request ,
171
- HttpServletResponse response )
172
- throws IOException , ServletException {
166
+ public boolean handle (
167
+ org .eclipse .jetty .server .Request request , org .eclipse .jetty .server .Response response , Callback callback )
168
+ throws IOException {
173
169
174
170
final String credentials = Base64 .getEncoder ().encodeToString ((user + ":" + password ).getBytes ("UTF-8" ));
175
171
176
- jettyRequest .setHandled (true );
177
-
178
- String authorization = request .getHeader (HttpHeader .PROXY_AUTHORIZATION .asString ());
172
+ String authorization = request .getHeaders ().get (HttpHeader .PROXY_AUTHORIZATION .asString ());
179
173
if (authorization == null ) {
180
174
response .setStatus (HttpStatus .PROXY_AUTHENTICATION_REQUIRED_407 );
181
- response .setHeader (HttpHeader .PROXY_AUTHENTICATE .asString (), "Basic realm=\" " + realm + "\" " );
182
- return ;
175
+ response .getHeaders ().add (HttpHeader .PROXY_AUTHENTICATE .asString (), "Basic realm=\" " + realm + "\" " );
176
+ callback .succeeded ();
177
+ return true ;
183
178
} else {
184
179
String prefix = "Basic " ;
185
180
if (authorization .startsWith (prefix )) {
186
181
String attempt = authorization .substring (prefix .length ());
187
182
if (!credentials .equals (attempt )) {
188
- return ;
183
+ callback .succeeded ();
184
+ return true ;
189
185
}
190
186
}
191
187
}
192
188
193
189
if (StringUtils .equalsIgnoreCase ("post" , request .getMethod ())) {
194
- postReceived = IOUtils . toString (request . getReader () );
190
+ postReceived = Content . Source . asString (request , StandardCharsets . UTF_8 );
195
191
}
196
- response .getWriter ().write (CONTENT_RESPONSE );
192
+ Content .Sink .write (response , true , CONTENT_RESPONSE , callback );
193
+ return true ;
197
194
}
198
195
}
199
196
200
- public class TestHandler extends AbstractHandler {
197
+ public static class TestHandler extends Handler . Abstract {
201
198
202
199
String postReceived ;
203
200
204
201
@ Override
205
- public void handle (
206
- String target ,
207
- org .eclipse .jetty .server .Request jettyRequest ,
208
- HttpServletRequest request ,
209
- HttpServletResponse response )
210
- throws IOException , ServletException {
211
- jettyRequest .setHandled (true );
202
+ public boolean handle (
203
+ org .eclipse .jetty .server .Request request , org .eclipse .jetty .server .Response response , Callback callback )
204
+ throws IOException {
212
205
if (StringUtils .equalsIgnoreCase ("post" , request .getMethod ())) {
213
- postReceived = IOUtils . toString (request . getReader () );
206
+ postReceived = Content . Source . asString (request , StandardCharsets . UTF_8 );
214
207
}
215
- response .getWriter ().write (CONTENT_RESPONSE );
208
+ Content .Sink .write (response , true , CONTENT_RESPONSE , callback );
209
+ return true ;
216
210
}
217
211
}
218
212
0 commit comments