Skip to content

Commit

Permalink
Ensure CXF WebClient to use async conduit (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-patricelli committed Mar 28, 2024
1 parent f0c6d04 commit d7618ab
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.BookmarkablePageLinkBuilder;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.client.console.SyncopeWebApplication;
Expand All @@ -49,6 +48,7 @@
import org.apache.syncope.client.console.rest.WAConfigRestClient;
import org.apache.syncope.client.console.rest.WASessionRestClient;
import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.keymaster.client.api.ServiceOps;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
import org.apache.syncope.common.lib.types.AMEntitlement;
Expand Down Expand Up @@ -128,12 +128,10 @@ public void onClick(final AjaxRequestTarget target) {
if (!instances.isEmpty()) {
String actuatorEndpoint = StringUtils.appendIfMissing(instances.get(0).getAddress(), "/") + "actuator/env";
try {
Response response = WebClient.create(
actuatorEndpoint,
Response response = WebClientBuilder.build(actuatorEndpoint,
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
accept(MediaType.APPLICATION_JSON_TYPE).get();
List.of()).accept(MediaType.APPLICATION_JSON_TYPE).get();
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
JsonNode env = MAPPER.readTree((InputStream) response.getEntity());
if (env.has("propertySources")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeWebApplication;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.client.ui.commons.rest.RestClient;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
import org.apache.syncope.common.lib.AMSession;
Expand Down Expand Up @@ -56,13 +56,13 @@ public void delete(final String key) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Unknown);

try {
Response response = WebClient.create(
getActuatorEndpoint(),
Response response = WebClientBuilder.build(getActuatorEndpoint(),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
path(key).
accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_JSON_TYPE).delete();
List.of()).
accept(MediaType.APPLICATION_JSON_TYPE).
type(MediaType.APPLICATION_JSON_TYPE).
path(key).delete();
if (response.getStatus() != Response.Status.OK.getStatusCode()
&& response.getStatus() != Response.Status.NO_CONTENT.getStatusCode()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.List;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeWebApplication;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
import org.apache.syncope.common.lib.AMSession;

Expand All @@ -43,15 +43,10 @@ protected String getActuatorEndpoint() {
@Override
public List<AMSession> list() {
try {
WebClient client = WebClient.create(
getActuatorEndpoint(),
JAX_RS_PROVIDERS,
Response response = WebClientBuilder.build(getActuatorEndpoint(),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
accept(MediaType.APPLICATION_JSON_TYPE);

Response response = client.get();
JAX_RS_PROVIDERS).accept(MediaType.APPLICATION_JSON_TYPE).get();
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
return response.readEntity(new GenericType<>() {
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeWebApplication;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,13 +44,10 @@ protected String getActuatorEndpoint(final List<NetworkService> instances) {

public SRAStatistics get(final List<NetworkService> instances, final List<Pair<String, String>> selected) {
try {
WebClient client = WebClient.create(
getActuatorEndpoint(instances),
JAX_RS_PROVIDERS,
WebClient client = WebClientBuilder.build(getActuatorEndpoint(instances),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
accept(MediaType.APPLICATION_JSON_TYPE);
JAX_RS_PROVIDERS).accept(MediaType.APPLICATION_JSON_TYPE);

if (!selected.isEmpty()) {
client.query("tag", selected.stream().map(s -> s.getKey() + ":" + s.getValue()).toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import jakarta.ws.rs.core.Response;
import java.io.InputStream;
import java.util.List;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeWebApplication;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
import org.apache.syncope.common.lib.AMSession;

Expand All @@ -50,12 +50,10 @@ protected String getActuatorEndpoint() {
@Override
public List<AMSession> list() {
try {
Response response = WebClient.create(
getActuatorEndpoint(),
Response response = WebClientBuilder.build(getActuatorEndpoint(),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
accept(MediaType.APPLICATION_JSON_TYPE).get();
List.of()).accept(MediaType.APPLICATION_JSON_TYPE).get();
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
JsonNode node = MAPPER.readTree((InputStream) response.getEntity());
if (node.has("activeSsoSessions")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeConsoleSession;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.lib.SyncopeClientException;
import org.apache.syncope.common.lib.to.ConnIdBundle;
import org.apache.syncope.common.lib.to.ConnIdObjectClass;
Expand Down Expand Up @@ -173,7 +174,8 @@ protected List<ConnConfProperty> filterProperties(final Collection<ConnConfPrope
public boolean check(final String coreAddress, final String domain, final String jwt, final String key)
throws IOException {

WebClient client = WebClient.create(coreAddress).path("connectors").
WebClient client = WebClientBuilder.build(coreAddress).
path("connectors").
accept(MediaType.APPLICATION_JSON_TYPE).
type(MediaType.APPLICATION_JSON_TYPE).
header(RESTHeaders.DOMAIN, domain).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.common.lib.to.ConnObject;
import org.apache.syncope.common.lib.to.PagedConnObjectResult;
import org.apache.syncope.common.lib.to.ResourceTO;
Expand All @@ -47,7 +48,8 @@ public class ResourceRestClient extends BaseRestClient {
public boolean check(final String coreAddress, final String domain, final String jwt, final String key)
throws IOException {

WebClient client = WebClient.create(coreAddress).path("resources").
WebClient client = WebClientBuilder.build(coreAddress).
path("resources").
accept(MediaType.APPLICATION_JSON_TYPE).
type(MediaType.APPLICATION_JSON_TYPE).
header(RESTHeaders.DOMAIN, domain).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.syncope.client.console.SyncopeWebApplication;
import org.apache.syncope.client.lib.WebClientBuilder;
import org.apache.syncope.client.ui.commons.rest.RestClient;
import org.apache.syncope.common.keymaster.client.api.model.Domain;
import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
Expand Down Expand Up @@ -67,22 +67,15 @@ protected String getActuatorEndpoint(final NetworkService instance) {
return address + "actuator/loggers";
}

protected WebClient webClient(final NetworkService instance) {
return WebClient.create(
getActuatorEndpoint(instance),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
null).
accept(MediaType.APPLICATION_JSON_TYPE).
type(MediaType.APPLICATION_JSON_TYPE);
}

@Override
public List<LoggerConf> list() {
List<LoggerConf> loggerConfs = new ArrayList<>();

try {
Response response = webClient(instances.get(0)).get();
Response response = WebClientBuilder.build(getActuatorEndpoint(instances.get(0)),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
List.of()).accept(MediaType.APPLICATION_JSON_TYPE).get();
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
JsonNode node = MAPPER.readTree((InputStream) response.getEntity());
if (node.has("loggers")) {
Expand Down Expand Up @@ -114,6 +107,13 @@ public List<LoggerConf> list() {

@Override
public void setLevel(final String key, final LogLevel level) {
instances.forEach(i -> webClient(i).path(key).post("{\"configuredLevel\": \"" + level.name() + "\"}"));
instances.forEach(i -> WebClientBuilder.build(getActuatorEndpoint(i),
SyncopeWebApplication.get().getAnonymousUser(),
SyncopeWebApplication.get().getAnonymousKey(),
List.of())
.accept(MediaType.APPLICATION_JSON_TYPE)
.type(MediaType.APPLICATION_JSON_TYPE)
.path(key)
.post("{\"configuredLevel\": \"" + level.name() + "\"}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SyncopeAnonymousClient(
}

public JsonNode info() throws IOException {
WebClient webClient = WebClient.create(
WebClient webClient = WebClientBuilder.build(
StringUtils.removeEnd(restClientFactory.getAddress().replace("/rest", "/actuator/info"), "/")).
accept(MediaType.APPLICATION_JSON_TYPE).
header(RESTHeaders.DOMAIN, getDomain()).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.client.lib;

import java.net.URI;
import java.util.List;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit;

public final class WebClientBuilder {

private WebClientBuilder() {
}

public static WebClient build(final String address,
final String username,
final String password,
final List<?> providers) {
return setAsync(WebClient.create(address, providers, username, password, null));
}

public static WebClient build(final String address) {
return setAsync(WebClient.create(address));
}

public static WebClient build(final URI uri) {
return setAsync(WebClient.create(uri));
}

protected static WebClient setAsync(final WebClient webClient) {
ClientConfiguration config = WebClient.getConfig(webClient);
config.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);

return webClient;
}
}
4 changes: 4 additions & 0 deletions common/keymaster/self/client-self/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ under the License.
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-hc</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit;
import org.apache.cxf.transports.http.configuration.ConnectionType;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

Expand Down Expand Up @@ -66,6 +67,7 @@ public <T> T client(final Class<T> serviceClass, final Map<String, String> heade
client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);

ClientConfiguration config = WebClient.getConfig(client);
config.getRequestContext().put(AsyncHTTPConduit.USE_ASYNC, Boolean.TRUE);
config.getInInterceptors().add(new GZIPInInterceptor());
config.getOutInterceptors().add(new GZIPOutInterceptor());

Expand Down

0 comments on commit d7618ab

Please sign in to comment.