1
+ /*
2
+ * Copyright 2024 NAVER Corp.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .pinpoint .test .plugin ;
18
+
19
+ import com .pinpoint .test .common .view .ApiLinkPage ;
20
+ import com .pinpoint .test .common .view .HrefTag ;
21
+ import org .apache .logging .log4j .LogManager ;
22
+ import org .apache .logging .log4j .Logger ;
23
+ import org .springframework .beans .factory .annotation .Autowired ;
24
+ import org .springframework .web .bind .annotation .GetMapping ;
25
+ import org .springframework .web .bind .annotation .RestController ;
26
+ import org .springframework .web .method .HandlerMethod ;
27
+ import org .springframework .web .reactive .result .method .RequestMappingInfo ;
28
+ import org .springframework .web .reactive .result .method .annotation .RequestMappingHandlerMapping ;
29
+ import org .springframework .web .server .ServerWebExchange ;
30
+ import reactor .core .publisher .Mono ;
31
+
32
+ import java .io .IOException ;
33
+ import java .net .URI ;
34
+ import java .net .URISyntaxException ;
35
+ import java .net .http .HttpClient ;
36
+ import java .net .http .HttpRequest ;
37
+ import java .net .http .HttpResponse ;
38
+ import java .util .ArrayList ;
39
+ import java .util .Arrays ;
40
+ import java .util .Comparator ;
41
+ import java .util .List ;
42
+ import java .util .Map ;
43
+ import java .util .concurrent .CompletableFuture ;
44
+ import java .util .stream .Collectors ;
45
+
46
+ @ RestController
47
+ public class JdkHttpClientPluginController {
48
+ private final Logger logger = LogManager .getLogger (this .getClass ());
49
+
50
+ private final RequestMappingHandlerMapping handlerMapping ;
51
+
52
+ @ Autowired
53
+ public JdkHttpClientPluginController (RequestMappingHandlerMapping handlerMapping ) {
54
+ this .handlerMapping = handlerMapping ;
55
+ }
56
+
57
+ @ GetMapping ("/" )
58
+ String welcome () {
59
+ Map <RequestMappingInfo , HandlerMethod > handlerMethods = this .handlerMapping .getHandlerMethods ();
60
+ List <HrefTag > list = new ArrayList <>();
61
+ for (RequestMappingInfo info : handlerMethods .keySet ()) {
62
+ for (String path : info .getDirectPaths ()) {
63
+ list .add (HrefTag .of (path ));
64
+ }
65
+ }
66
+ list .sort (Comparator .comparing (HrefTag ::getPath ));
67
+ return new ApiLinkPage ("spring-webflux-plugin-testweb" )
68
+ .addHrefTag (list )
69
+ .build ();
70
+ }
71
+
72
+ @ GetMapping ("/client/get" )
73
+ public Mono <String > clientGet (ServerWebExchange exchange ) throws IOException , InterruptedException {
74
+ HttpClient client = HttpClient .newHttpClient ();
75
+ HttpRequest request = HttpRequest .newBuilder ()
76
+ .version (HttpClient .Version .HTTP_2 )
77
+ .uri (URI .create ("http://httpbin.org" ))
78
+ .headers ("Accept-Enconding" , "gzip, deflate" )
79
+ .headers ("Cookie" , "foo-bar" )
80
+ .build ();
81
+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
82
+
83
+ String responseBody = response .body ();
84
+ int responseStatusCode = response .statusCode ();
85
+
86
+ return Mono .just (responseStatusCode + " " + responseBody );
87
+ }
88
+
89
+ @ GetMapping ("/client/retry" )
90
+ public Mono <String > clientRetry (ServerWebExchange exchange ) throws IOException , InterruptedException {
91
+ HttpClient client = HttpClient .newHttpClient ();
92
+ HttpRequest request = HttpRequest .newBuilder ()
93
+ .version (HttpClient .Version .HTTP_2 )
94
+ .uri (URI .create ("http://naver.com" ))
95
+ .headers ("Accept-Enconding" , "gzip, deflate" )
96
+ .build ();
97
+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
98
+
99
+ String responseBody = response .body ();
100
+ int responseStatusCode = response .statusCode ();
101
+
102
+ return Mono .just (responseStatusCode + " " + responseBody );
103
+ }
104
+
105
+ @ GetMapping ("/client/unknown" )
106
+ public Mono <String > clientUnknown (ServerWebExchange exchange ) throws IOException , InterruptedException {
107
+ HttpClient client = HttpClient .newHttpClient ();
108
+ HttpRequest request = HttpRequest .newBuilder ()
109
+ .version (HttpClient .Version .HTTP_2 )
110
+ .uri (URI .create ("http://fjaklfjkladjfklajlf" ))
111
+ .headers ("Accept-Enconding" , "gzip, deflate" )
112
+ .build ();
113
+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
114
+
115
+ String responseBody = response .body ();
116
+ int responseStatusCode = response .statusCode ();
117
+
118
+ return Mono .just (responseStatusCode + " " + responseBody );
119
+ }
120
+
121
+ @ GetMapping ("/client/post" )
122
+ public Mono <String > clientPost (ServerWebExchange exchange ) throws IOException , InterruptedException , URISyntaxException {
123
+ HttpClient client = HttpClient .newBuilder ()
124
+ .version (HttpClient .Version .HTTP_2 )
125
+ .build ();
126
+ HttpRequest request = HttpRequest .newBuilder (new URI ("http://httpbin.org" ))
127
+ .version (HttpClient .Version .HTTP_2 )
128
+ .POST (HttpRequest .BodyPublishers .ofString ("Sample Post Request" ))
129
+ .build ();
130
+ HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
131
+ String responseBody = response .body ();
132
+
133
+ return Mono .just (responseBody );
134
+ }
135
+
136
+ @ GetMapping ("/client/async" )
137
+ public Mono <String > clientAsync (ServerWebExchange exchange ) throws IOException , InterruptedException , URISyntaxException {
138
+ HttpClient client = HttpClient .newHttpClient ();
139
+ URI httpURI = new URI ("http://httpbin.org" );
140
+ HttpRequest request = HttpRequest .newBuilder (httpURI )
141
+ .version (HttpClient .Version .HTTP_2 )
142
+ .build ();
143
+ CompletableFuture <Void > futureResponse = client .sendAsync (request , HttpResponse .BodyHandlers .ofString ())
144
+ .thenAccept (resp -> {
145
+ System .out .println ("Got pushed response " + resp .uri ());
146
+ System .out .println ("Response statuscode: " + resp .statusCode ());
147
+ System .out .println ("Response body: " + resp .body ());
148
+ });
149
+ System .out .println ("futureResponse" + futureResponse );
150
+ return Mono .just (futureResponse .toString ());
151
+ }
152
+
153
+ @ GetMapping ("/client/multi" )
154
+ public Mono <String > clientMulti (ServerWebExchange exchange ) throws IOException , InterruptedException , URISyntaxException {
155
+ HttpClient client = HttpClient .newHttpClient ();
156
+ List <URI > uris = Arrays .asList (new URI ("http://httpbin.org" ), new URI ("http://google.com" ));
157
+ List <HttpRequest > requests = uris .stream ()
158
+ .map (HttpRequest ::newBuilder )
159
+ .map (reqBuilder -> reqBuilder .build ())
160
+ .collect (Collectors .toList ());
161
+ System .out .println ("Got pushed response1 " + requests );
162
+ CompletableFuture .allOf (requests .stream ()
163
+ .map (request -> client .sendAsync (request , HttpResponse .BodyHandlers .ofString ()))
164
+ .toArray (CompletableFuture <?>[]::new ))
165
+ .thenAccept (System .out ::println )
166
+ .join ();
167
+ return Mono .just ("OK" );
168
+ }
169
+ }
0 commit comments