Skip to content

Commit f45188b

Browse files
authored
#284: support for synchronous rest client using http client (#410)
1 parent 9c8fbc0 commit f45188b

File tree

52 files changed

+1800
-685
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1800
-685
lines changed

boms/minimal/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@
261261
<artifactId>devon4j-starter-http-client-rest-async</artifactId>
262262
<version>${project.version}</version>
263263
</dependency>
264+
<dependency>
265+
<groupId>com.devonfw.java.starters</groupId>
266+
<artifactId>devon4j-starter-http-client-rest-sync</artifactId>
267+
<version>${project.version}</version>
268+
</dependency>
264269
</dependencies>
265270
</dependencyManagement>
266271

documentation/guide-service-client.asciidoc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ You need to add (at least one of) these dependencies to your application:
1818
<groupId>com.devonfw.java.starters</groupId>
1919
<artifactId>devon4j-starter-http-client-rest-async</artifactId>
2020
</dependency>
21-
<!-- Starter for synchronous consuming REST services via Apache CXF (Java8+) -->
21+
<!-- Starter for synchronous consuming REST services via Jaca HTTP Client (Java11+) -->
22+
<dependency>
23+
<groupId>com.devonfw.java.starters</groupId>
24+
<artifactId>devon4j-starter-http-client-rest-sync</artifactId>
25+
</dependency>
26+
<!-- Starter for synchronous consuming REST services via Apache CXF (Java8+)
27+
NOTE: This is an alternative to devon4j-starter-http-client-rest-sync
28+
-->
29+
<!--
2230
<dependency>
2331
<groupId>com.devonfw.java.starters</groupId>
2432
<artifactId>devon4j-starter-cxf-client-rest</artifactId>
2533
</dependency>
34+
-->
2635
<!-- Starter for synchronous consuming SOAP services via Apache CXF (Java8+) -->
2736
<dependency>
2837
<groupId>com.devonfw.java.starters</groupId>

modules/cxf-client-rest/src/main/java/com/devonfw/module/cxf/common/impl/client/rest/SyncServiceClientFactoryCxfRest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.List;
66

77
import javax.inject.Inject;
8-
import javax.ws.rs.Path;
98

109
import org.apache.cxf.jaxrs.client.Client;
1110
import org.apache.cxf.jaxrs.client.ClientConfiguration;
@@ -15,7 +14,8 @@
1514
import com.devonfw.module.cxf.common.impl.client.SyncServiceClientFactoryCxf;
1615
import com.devonfw.module.service.common.api.client.context.ServiceContext;
1716
import com.devonfw.module.service.common.api.client.sync.SyncServiceClientFactory;
18-
import com.devonfw.module.service.common.api.constants.ServiceConstants;
17+
import com.devonfw.module.service.common.base.client.ServiceClientTypeHandler;
18+
import com.devonfw.module.service.common.base.client.ServiceClientTypeHandlerRest;
1919
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
2020

2121
/**
@@ -35,6 +35,12 @@ public SyncServiceClientFactoryCxfRest() {
3535
super();
3636
}
3737

38+
@Override
39+
protected ServiceClientTypeHandler getTypeHandler() {
40+
41+
return ServiceClientTypeHandlerRest.get();
42+
}
43+
3844
/**
3945
* @param jsonProvider the {@link JacksonJsonProvider} to {@link Inject}.
4046
*/
@@ -57,13 +63,9 @@ protected <S> void applyAspects(ServiceContext<S> context, S serviceClient) {
5763
protected <S> S createService(ServiceContext<S> context, String url) {
5864

5965
List<Object> providers = createProviderList(context);
60-
return JAXRSClientFactory.create(url, context.getApi(), providers);
61-
}
62-
63-
@Override
64-
protected String getServiceTypeFolderName() {
65-
66-
return ServiceConstants.URL_FOLDER_REST;
66+
S service = JAXRSClientFactory.create(url, context.getApi(), providers);
67+
applyAspects(context, service);
68+
return service;
6769
}
6870

6971
@Override
@@ -78,12 +80,6 @@ protected void applyHeaders(ServiceContext<?> context, Object serviceClient) {
7880
}
7981
}
8082

81-
@Override
82-
protected boolean isResponsibleForService(ServiceContext<?> context) {
83-
84-
return context.getApi().isAnnotationPresent(Path.class);
85-
}
86-
8783
/**
8884
* @param context the {@link ServiceContext}.
8985
* @return the {@link List} of {@link javax.ws.rs.ext.Provider}s.

modules/cxf-client-ws/src/main/java/com/devonfw/module/cxf/common/impl/client/ws/SyncServiceClientFactoryCxfWs.java

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import com.devonfw.module.service.common.api.client.context.ServiceContext;
2424
import com.devonfw.module.service.common.api.client.sync.SyncServiceClientFactory;
2525
import com.devonfw.module.service.common.api.config.ServiceConfig;
26-
import com.devonfw.module.service.common.api.constants.ServiceConstants;
26+
import com.devonfw.module.service.common.base.client.ServiceClientTypeHandler;
27+
import com.devonfw.module.service.common.base.client.ServiceClientTypeHandlerWs;
2728

2829
/**
2930
* Implementation of {@link SyncServiceClientFactory} for JAX-WS SOAP service clients using Apache CXF.
@@ -32,7 +33,11 @@
3233
*/
3334
public class SyncServiceClientFactoryCxfWs extends SyncServiceClientFactoryCxf {
3435

35-
private static final String WSDL_SUFFIX = "?wsdl";
36+
@Override
37+
protected ServiceClientTypeHandler getTypeHandler() {
38+
39+
return ServiceClientTypeHandlerWs.get();
40+
}
3641

3742
@Override
3843
protected <S> S createService(ServiceContext<S> context, String url) {
@@ -55,6 +60,7 @@ protected <S> S createService(ServiceContext<S> context, String url) {
5560
BindingProvider bindingProvider = (BindingProvider) serviceClient;
5661
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
5762
}
63+
applyAspects(context, serviceClient);
5864
return serviceClient;
5965
}
6066

@@ -71,29 +77,6 @@ protected <S> void applyAspects(ServiceContext<S> context, S serviceClient) {
7177
applyHeaders(context, cxfClient);
7278
}
7379

74-
@Override
75-
protected String getServiceTypeFolderName() {
76-
77-
return ServiceConstants.URL_FOLDER_WEB_SERVICE;
78-
}
79-
80-
@Override
81-
protected String getUrl(ServiceContext<?> context) {
82-
83-
String url = super.getUrl(context);
84-
if (!url.endsWith(WSDL_SUFFIX)) {
85-
String serviceName = context.getApi().getSimpleName();
86-
if (!url.endsWith(serviceName)) {
87-
if (!url.endsWith("/")) {
88-
url = url + "/";
89-
}
90-
url = url + serviceName;
91-
}
92-
url = url + WSDL_SUFFIX;
93-
}
94-
return url;
95-
}
96-
9780
private String getLocalName(Class<?> api, WebService webService) {
9881

9982
String portName = webService.portName();
@@ -125,15 +108,4 @@ protected void applyHeaders(ServiceContext<?> context, Object client) {
125108
}
126109
}
127110

128-
/**
129-
* @param context the {@link ServiceContext}.
130-
* @return {@code true} if this implementation is responsibe for creating a service client corresponding to the given
131-
* {@link ServiceContext}, {@code false} otherwise.
132-
*/
133-
@Override
134-
protected boolean isResponsibleForService(ServiceContext<?> context) {
135-
136-
return context.getApi().isAnnotationPresent(WebService.class);
137-
}
138-
139111
}

modules/cxf-client/src/main/java/com/devonfw/module/cxf/common/impl/client/PartialServiceClientFactoryCxf.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

modules/cxf-client/src/main/java/com/devonfw/module/cxf/common/impl/client/SyncServiceClientFactoryCxf.java

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,97 @@
11
package com.devonfw.module.cxf.common.impl.client;
22

3+
import javax.inject.Inject;
4+
35
import org.apache.cxf.interceptor.InterceptorProvider;
46
import org.apache.cxf.transport.http.HTTPConduit;
7+
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
58

9+
import com.devonfw.module.basic.common.api.config.ConfigProperties;
10+
import com.devonfw.module.cxf.common.impl.client.interceptor.PerformanceStartInterceptor;
11+
import com.devonfw.module.cxf.common.impl.client.interceptor.PerformanceStopInterceptor;
12+
import com.devonfw.module.cxf.common.impl.client.interceptor.TechnicalExceptionInterceptor;
13+
import com.devonfw.module.service.common.api.client.ServiceClientErrorFactory;
614
import com.devonfw.module.service.common.api.client.context.ServiceContext;
7-
import com.devonfw.module.service.common.api.client.sync.SyncServiceClientFactory;
15+
import com.devonfw.module.service.common.api.config.ServiceConfig;
16+
import com.devonfw.module.service.common.base.client.AbstractSyncServiceClientFactory;
817

918
/**
10-
* Abstract base implementation of {@link SyncServiceClientFactory} for service clients using Apache CXF.
19+
* Abstract base implementation of {@link AbstractSyncServiceClientFactory} for service clients using Apache CXF.
1120
*
1221
* @since 3.0.0
1322
*/
14-
public abstract class SyncServiceClientFactoryCxf extends PartialServiceClientFactoryCxf
15-
implements SyncServiceClientFactory {
23+
public abstract class SyncServiceClientFactoryCxf extends AbstractSyncServiceClientFactory {
24+
25+
private ServiceClientErrorFactory errorFactory;
26+
27+
/**
28+
* @return the {@link ServiceClientErrorFactory}.
29+
*/
30+
public ServiceClientErrorFactory getErrorFactory() {
31+
32+
return this.errorFactory;
33+
}
1634

17-
@Override
18-
public <S> S create(ServiceContext<S> context) {
35+
/**
36+
* @param errorFactory new value of {@link #getErrorFactory() errorFactory} to {@link Inject}.
37+
*/
38+
@Inject
39+
public void setErrorFactory(ServiceClientErrorFactory errorFactory) {
40+
41+
this.errorFactory = errorFactory;
42+
}
43+
44+
/**
45+
* @param context the {@link ServiceContext}.
46+
* @return the {@link HTTPClientPolicy} for the {@link ServiceContext#getConfig() configuration} or {@code null} to
47+
* use defaults.
48+
*/
49+
protected HTTPClientPolicy createClientPolicy(ServiceContext<?> context) {
1950

20-
boolean responsible = isResponsibleForService(context);
21-
if (!responsible) {
22-
return null;
51+
ConfigProperties timeoutConfig = context.getConfig().getChild(ServiceConfig.KEY_SEGMENT_TIMEOUT);
52+
if (!timeoutConfig.isEmpty()) {
53+
HTTPClientPolicy policy = new HTTPClientPolicy();
54+
Long connectionTimeout = timeoutConfig.getChild(ServiceConfig.KEY_SEGMENT_TIMEOUT_CONNECTION)
55+
.getValue(Long.class);
56+
if (connectionTimeout != null) {
57+
policy.setConnectionTimeout(connectionTimeout.longValue());
58+
}
59+
Long responseTimeout = timeoutConfig.getChild(ServiceConfig.KEY_SEGMENT_TIMEOUT_RESPONSE).getValue(Long.class);
60+
if (responseTimeout != null) {
61+
policy.setReceiveTimeout(responseTimeout.longValue());
62+
}
63+
return policy;
2364
}
24-
String url = getUrl(context);
25-
S serviceClient = createService(context, url);
65+
return null;
66+
}
2667

27-
applyAspects(context, serviceClient);
28-
return serviceClient;
68+
/**
69+
* Applies CXF interceptors to the given {@code serviceClient}.
70+
*
71+
* @param context the {@link ServiceContext}.
72+
* @param client the {@link InterceptorProvider}.
73+
*/
74+
protected void applyInterceptors(ServiceContext<?> context, InterceptorProvider client) {
75+
76+
client.getOutInterceptors().add(new PerformanceStartInterceptor());
77+
client.getInInterceptors().add(new PerformanceStopInterceptor());
78+
client.getInFaultInterceptors().add(new TechnicalExceptionInterceptor(getErrorFactory(), context));
2979
}
3080

3181
/**
32-
* @param <S> the generic type of the {@link ServiceContext#getApi() service API}.
3382
* @param context the {@link ServiceContext}.
34-
* @param url the resolved end-point URL of the service to invoke.
35-
* @return a new client stub for the service. See {@link #create(ServiceContext)} for further details.
83+
* @param conduit the {@link HTTPConduit} where to apply the {@link HTTPClientPolicy} to.
3684
*/
37-
protected abstract <S> S createService(ServiceContext<S> context, String url);
85+
protected void applyClientPolicy(ServiceContext<?> context, HTTPConduit conduit) {
86+
87+
if (conduit == null) {
88+
return;
89+
}
90+
HTTPClientPolicy clientPolicy = createClientPolicy(context);
91+
if (clientPolicy != null) {
92+
conduit.setClient(clientPolicy);
93+
}
94+
}
3895

3996
/**
4097
* Implementations should call the following methods:

0 commit comments

Comments
 (0)