Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit185ff61

Browse files
committed
Proxy registry integration for GraphQL clients
1 parent1ee41b9 commit185ff61

File tree

5 files changed

+215
-4
lines changed

5 files changed

+215
-4
lines changed

‎extensions/smallrye-graphql-client/deployment/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
<groupId>io.quarkus</groupId>
4646
<artifactId>quarkus-tls-registry-deployment</artifactId>
4747
</dependency>
48+
<dependency>
49+
<groupId>io.quarkus</groupId>
50+
<artifactId>quarkus-proxy-registry-deployment</artifactId>
51+
</dependency>
4852
<dependency>
4953
<groupId>io.smallrye</groupId>
5054
<artifactId>smallrye-graphql-client-model</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
packageio.quarkus.smallrye.graphql.client.deployment.proxy;
2+
3+
importstaticorg.assertj.core.api.Assertions.assertThat;
4+
5+
importjava.net.URI;
6+
importjava.util.concurrent.ExecutionException;
7+
importjava.util.concurrent.atomic.AtomicBoolean;
8+
9+
importjakarta.inject.Inject;
10+
11+
importorg.eclipse.microprofile.graphql.GraphQLApi;
12+
importorg.eclipse.microprofile.graphql.Query;
13+
importorg.jboss.shrinkwrap.api.asset.EmptyAsset;
14+
importorg.jboss.shrinkwrap.api.asset.StringAsset;
15+
importorg.junit.jupiter.api.AfterAll;
16+
importorg.junit.jupiter.api.BeforeAll;
17+
importorg.junit.jupiter.api.Test;
18+
importorg.junit.jupiter.api.extension.RegisterExtension;
19+
20+
importio.quarkus.logging.Log;
21+
importio.quarkus.test.QuarkusUnitTest;
22+
importio.smallrye.graphql.client.typesafe.api.GraphQLClientApi;
23+
importio.vertx.core.Vertx;
24+
importio.vertx.core.http.HttpClient;
25+
importio.vertx.core.http.HttpServer;
26+
27+
publicclassGraphQLClientProxyRegistryTest {
28+
29+
staticStringtargetUrl ="http://" +System.getProperty("quarkus.http.host","localhost") +":" +
30+
System.getProperty("quarkus.http.test-port","8081") +"/graphql";
31+
32+
@RegisterExtension
33+
staticQuarkusUnitTesttest =newQuarkusUnitTest()
34+
.withApplicationRoot((jar) ->jar
35+
.addClasses(DummyApi.class)
36+
.addAsResource(
37+
newStringAsset("""
38+
quarkus.smallrye-graphql-client.dummy.url=%s
39+
quarkus.smallrye-graphql-client.dummy.proxy-configuration-name=MYPROXY
40+
quarkus.proxy.MYPROXY.host=localhost
41+
quarkus.proxy.MYPROXY.port=3456
42+
""".formatted(targetUrl)),
43+
"application.properties")
44+
.addAsManifestResource(EmptyAsset.INSTANCE,"beans.xml"));
45+
46+
privatestaticVertxvertx;
47+
privatestaticHttpServerproxyServer;
48+
privatestaticHttpClientproxyClient;
49+
privatestaticfinalAtomicBooleanwasProxyCalled =newAtomicBoolean(false);
50+
51+
/**
52+
* Starts a simple HTTP proxy server that forwards requests to the final destination.
53+
*/
54+
@BeforeAll
55+
publicstaticvoidstartProxyServer()throwsExecutionException,InterruptedException {
56+
vertx =Vertx.vertx();
57+
proxyClient =vertx.createHttpClient();
58+
proxyServer =vertx.createHttpServer().requestHandler(originalRequest -> {
59+
try {
60+
originalRequest.bodyHandler(originalRequestBody -> {
61+
wasProxyCalled.set(true);
62+
URIuri =URI.create(originalRequest.absoluteURI());
63+
proxyClient.request(originalRequest.method(),uri.getPort(),uri.getHost(),originalRequest.uri())
64+
.andThen(proxyRequest -> {
65+
try {
66+
Log.info("Proxying request to " +originalRequest.absoluteURI());
67+
if (!proxyRequest.succeeded()) {
68+
proxyRequest.cause().printStackTrace();
69+
originalRequest.response().setStatusCode(500).end();
70+
return;
71+
}
72+
proxyRequest.result().send(originalRequestBody,proxyRequestResponse -> {
73+
if (proxyRequestResponse.succeeded()) {
74+
originalRequest.response()
75+
.setStatusCode(proxyRequestResponse.result().statusCode());
76+
originalRequest.response().headers()
77+
.setAll(proxyRequestResponse.result().headers());
78+
proxyRequestResponse.result().bodyHandler(body -> {
79+
originalRequest.response().end(body);
80+
});
81+
}else {
82+
originalRequest.response().setStatusCode(500).end();
83+
}
84+
});
85+
}catch (Exceptione) {
86+
e.printStackTrace();
87+
originalRequest.response().setStatusCode(500).end();
88+
89+
}
90+
});
91+
});
92+
}catch (Exceptione) {
93+
e.printStackTrace();
94+
originalRequest.response().setStatusCode(500).end();
95+
}
96+
}).listen(3456).toCompletionStage().toCompletableFuture().get();
97+
}
98+
99+
@AfterAll
100+
publicstaticvoidstopProxyServer() {
101+
if (proxyServer !=null) {
102+
proxyServer.close(x -> {
103+
});
104+
}
105+
if (proxyClient !=null) {
106+
proxyClient.close(x -> {
107+
});
108+
}
109+
if (vertx !=null) {
110+
vertx.close(x -> {
111+
});
112+
}
113+
}
114+
115+
@Inject
116+
DummyApiClientclient;
117+
118+
@Test
119+
publicvoidperformQueryViaProxy() {
120+
assertThat(client.dummyQuery()).isEqualTo("dummy");
121+
assertThat(wasProxyCalled.get()).isTrue();
122+
}
123+
124+
@GraphQLClientApi(configKey ="dummy")
125+
publicinterfaceDummyApiClient {
126+
@Query
127+
StringdummyQuery();
128+
}
129+
130+
@GraphQLApi
131+
publicstaticclassDummyApi {
132+
@Query
133+
publicStringdummyQuery() {
134+
return"dummy";
135+
}
136+
}
137+
}

‎extensions/smallrye-graphql-client/runtime/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<groupId>io.quarkus</groupId>
4343
<artifactId>quarkus-tls-registry</artifactId>
4444
</dependency>
45+
<dependency>
46+
<groupId>io.quarkus</groupId>
47+
<artifactId>quarkus-proxy-registry</artifactId>
48+
</dependency>
4549
<dependency>
4650
<groupId>io.smallrye</groupId>
4751
<artifactId>smallrye-graphql-client-implementation-vertx</artifactId>

‎extensions/smallrye-graphql-client/runtime/src/main/java/io/quarkus/smallrye/graphql/client/runtime/GraphQLClientConfig.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,47 @@ public interface GraphQLClientConfig {
4848
*/
4949
OptionalIntwebsocketInitializationTimeout();
5050

51+
/**
52+
* The name of the proxy configuration to use.
53+
* This setting is ignored if {@code proxy-host} is set.
54+
*
55+
* If not set and the default proxy configuration is configured ({@code quarkus.proxy.*}) then that will be used.
56+
* If the proxy configuration name is set, the configuration from {@code quarkus.proxy.<name>.*} will be used.
57+
* If the proxy configuration name is set, but no proxy configuration is found with that name, then an error will be thrown
58+
* at runtime.
59+
*/
60+
Optional<String>proxyConfigurationName();
61+
5162
/**
5263
* Hostname of the proxy to use.
64+
*
65+
* @deprecated Use {@code proxy-configuration-name} instead.
5366
*/
67+
@Deprecated(since ="3.31.0",forRemoval =true)
5468
Optional<String>proxyHost();
5569

5670
/**
5771
* Port number of the proxy to use.
72+
*
73+
* @deprecated Use {@code proxy-configuration-name} instead.
5874
*/
75+
@Deprecated(since ="3.31.0",forRemoval =true)
5976
OptionalIntproxyPort();
6077

6178
/**
6279
* Username for the proxy to use.
80+
*
81+
* @deprecated Use {@code proxy-configuration-name} instead.
6382
*/
83+
@Deprecated(since ="3.31.0",forRemoval =true)
6484
Optional<String>proxyUsername();
6585

6686
/**
6787
* Password for the proxy to use.
88+
*
89+
* @deprecated Use {@code proxy-configuration-name} instead.
6890
*/
91+
@Deprecated(since ="3.31.0",forRemoval =true)
6992
Optional<String>proxyPassword();
7093

7194
/**

‎extensions/smallrye-graphql-client/runtime/src/main/java/io/quarkus/smallrye/graphql/client/runtime/SmallRyeGraphQLClientRecorder.java‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
importio.quarkus.arc.Arc;
1717
importio.quarkus.arc.SyntheticCreationalContext;
18+
importio.quarkus.proxy.ProxyConfiguration;
19+
importio.quarkus.proxy.ProxyConfigurationRegistry;
1820
importio.quarkus.runtime.LaunchMode;
1921
importio.quarkus.runtime.RuntimeValue;
2022
importio.quarkus.runtime.annotations.Recorder;
@@ -139,10 +141,23 @@ private GraphQLClientConfiguration toSmallRyeNativeConfiguration(GraphQLClientCo
139141
transformed.setUsesSni(Boolean.valueOf(tlsConfiguration.usesSni()));
140142
});
141143

142-
quarkusConfig.proxyHost().ifPresent(transformed::setProxyHost);
143-
quarkusConfig.proxyPort().ifPresent(transformed::setProxyPort);
144-
quarkusConfig.proxyUsername().ifPresent(transformed::setProxyUsername);
145-
quarkusConfig.proxyPassword().ifPresent(transformed::setProxyPassword);
144+
if (quarkusConfig.proxyHost().isPresent()) {
145+
// use the deprecated proxy settings if they are set
146+
quarkusConfig.proxyHost().ifPresent(transformed::setProxyHost);
147+
quarkusConfig.proxyPort().ifPresent(transformed::setProxyPort);
148+
quarkusConfig.proxyUsername().ifPresent(transformed::setProxyUsername);
149+
quarkusConfig.proxyPassword().ifPresent(transformed::setProxyPassword);
150+
}else {
151+
// use the proxy configuration registry
152+
resolveProxyConfiguration(quarkusConfig)
153+
.ifPresent(proxyConfiguration -> {
154+
transformed.setProxyHost(proxyConfiguration.host());
155+
transformed.setProxyPort(proxyConfiguration.port());
156+
proxyConfiguration.username().ifPresent(transformed::setProxyUsername);
157+
proxyConfiguration.password().ifPresent(transformed::setProxyPassword);
158+
});
159+
}
160+
146161
quarkusConfig.maxRedirects().ifPresent(transformed::setMaxRedirects);
147162
quarkusConfig.executeSingleResultOperationsOverWebsocket()
148163
.ifPresent(transformed::setExecuteSingleOperationsOverWebsocket);
@@ -189,4 +204,32 @@ private Optional<TlsConfiguration> resolveTlsConfigurationForRegistry(GraphQLCli
189204
}
190205
returnOptional.empty();
191206
}
207+
208+
privateOptional<ProxyConfiguration>resolveProxyConfiguration(GraphQLClientConfigclientConfig) {
209+
if (Arc.container() !=null) {
210+
ProxyConfigurationRegistryproxyConfigurationRegistry =Arc.container().select(ProxyConfigurationRegistry.class)
211+
.orNull();
212+
if (proxyConfigurationRegistry !=null) {
213+
if (clientConfig.proxyConfigurationName().isPresent()) {
214+
// explicit proxy config
215+
Optional<ProxyConfiguration>namedConfig =proxyConfigurationRegistry
216+
.get(clientConfig.proxyConfigurationName());
217+
if (namedConfig.isEmpty()) {
218+
thrownewConfigurationException("Proxy configuration '" +clientConfig.proxyConfigurationName().get()
219+
+"' was specified, but it does not exist.");
220+
}
221+
returnnamedConfig;
222+
}else {
223+
// no explicit proxy config -> get the default proxy configuration if it exists
224+
returnproxyConfigurationRegistry.get(Optional.empty());
225+
}
226+
}else {
227+
if (clientConfig.proxyConfigurationName().isPresent()) {
228+
thrownewConfigurationException("Proxy configuration '" +clientConfig.proxyConfigurationName().get()
229+
+"' was specified, but no Proxy configuration registry could be found.");
230+
}
231+
}
232+
}
233+
returnOptional.empty();
234+
}
192235
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp