Skip to content

Commit 8eca250

Browse files
committed
simplify: big CXF refactor
1 parent b791b05 commit 8eca250

File tree

4 files changed

+58
-86
lines changed

4 files changed

+58
-86
lines changed

pom.xml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,10 @@
485485
<artifactId>httpclient5</artifactId>
486486
</dependency>
487487

488-
<!-- CXF -->
488+
<!-- CXF https://cxf.apache.org/docs/springboot.html -->
489489
<dependency>
490490
<groupId>org.apache.cxf</groupId>
491-
<artifactId>cxf-rt-frontend-jaxws</artifactId>
492-
<version>${cxf.version}</version>
493-
</dependency>
494-
<dependency>
495-
<groupId>org.apache.cxf</groupId>
496-
<artifactId>cxf-rt-transports-http</artifactId>
497-
<version>${cxf.version}</version>
498-
</dependency>
499-
<dependency>
500-
<groupId>org.apache.cxf</groupId>
501-
<artifactId>cxf-rt-transports-http-hc</artifactId>
491+
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
502492
<version>${cxf.version}</version>
503493
</dependency>
504494
<dependency>
@@ -547,6 +537,12 @@
547537
<artifactId>jetty-websocket-jetty-client</artifactId>
548538
<scope>test</scope>
549539
</dependency>
540+
<dependency>
541+
<groupId>org.apache.cxf</groupId>
542+
<artifactId>cxf-rt-transports-http-hc</artifactId>
543+
<version>${cxf.version}</version>
544+
<scope>test</scope>
545+
</dependency>
550546

551547
<!-- https://github.com/zafarkhaja/jsemver -->
552548
<dependency>

src/main/java/de/rwth/idsg/steve/SteveApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import de.rwth.idsg.steve.config.SteveProperties;
2222
import lombok.extern.slf4j.Slf4j;
23+
import org.apache.cxf.common.logging.LogUtils;
24+
import org.apache.cxf.common.logging.Slf4jLogger;
2325
import org.joda.time.DateTime;
2426
import org.joda.time.DateTimeZone;
2527
import org.springframework.boot.SpringApplication;
@@ -38,6 +40,9 @@
3840
public class SteveApplication {
3941

4042
static {
43+
// Apache CXF
44+
LogUtils.setLoggerClass(Slf4jLogger.class);
45+
4146
// For Hibernate validator
4247
System.setProperty("org.jboss.logging.provider", "slf4j");
4348

src/main/java/de/rwth/idsg/steve/config/OcppConfiguration.java

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,16 @@
2424
import de.rwth.idsg.steve.ocpp.soap.MessageIdInterceptor;
2525
import lombok.RequiredArgsConstructor;
2626
import org.apache.cxf.Bus;
27-
import org.apache.cxf.bus.spring.SpringBus;
28-
import org.apache.cxf.common.logging.LogUtils;
29-
import org.apache.cxf.common.logging.Slf4jLogger;
3027
import org.apache.cxf.feature.Feature;
3128
import org.apache.cxf.interceptor.Interceptor;
32-
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
29+
import org.apache.cxf.jaxws.EndpointImpl;
3330
import org.apache.cxf.message.Message;
34-
import org.apache.cxf.transport.servlet.CXFServlet;
35-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
3631
import org.springframework.context.annotation.Bean;
3732
import org.springframework.context.annotation.Configuration;
3833

39-
import java.util.Collection;
4034
import java.util.Collections;
4135
import java.util.List;
4236

43-
import static java.util.Arrays.asList;
44-
import static java.util.Collections.singletonList;
45-
4637
/**
4738
* Configuration and beans related to OCPP.
4839
*
@@ -53,53 +44,55 @@
5344
@Configuration
5445
public class OcppConfiguration {
5546

56-
static {
57-
LogUtils.setLoggerClass(Slf4jLogger.class);
58-
}
59-
47+
private final Bus bus;
6048
private final ocpp.cs._2010._08.CentralSystemService ocpp12Server;
6149
private final ocpp.cs._2012._06.CentralSystemService ocpp15Server;
6250
private final ocpp.cs._2015._10.CentralSystemService ocpp16Server;
6351
private final MessageHeaderInterceptor messageHeaderInterceptor;
6452

53+
private final MessageIdInterceptor messageIdInterceptor = new MessageIdInterceptor();
54+
6555
@Bean
66-
public ServletRegistrationBean<CXFServlet> cxfServletServletRegistrationBean() {
67-
var bean = new ServletRegistrationBean<>(new CXFServlet(), SteveProperties.CXF_MAPPING + "/*");
68-
bean.setLoadOnStartup(1);
69-
return bean;
56+
public EndpointImpl ocpp12Endpoint() {
57+
return createDefaultEndpoint(ocpp12Server, "/CentralSystemServiceOCPP12");
7058
}
7159

72-
@Bean(name = Bus.DEFAULT_BUS_ID, destroyMethod = "shutdown")
73-
public SpringBus cxf() {
74-
SpringBus bus = new SpringBus();
75-
configure(bus);
76-
return bus;
60+
@Bean
61+
public EndpointImpl ocpp15Endpoint() {
62+
return createDefaultEndpoint(ocpp15Server, "/CentralSystemServiceOCPP15");
7763
}
7864

79-
private void configure(Bus bus) {
80-
List<Interceptor<? extends Message>> interceptors = asList(new MessageIdInterceptor(), messageHeaderInterceptor);
81-
List<Feature> logging = singletonList(LoggingFeatureProxy.INSTANCE.get());
65+
@Bean
66+
public EndpointImpl ocpp16Endpoint() {
67+
return createDefaultEndpoint(ocpp16Server, "/CentralSystemServiceOCPP16");
68+
}
8269

83-
createOcppService(bus, ocpp12Server, "/CentralSystemServiceOCPP12", interceptors, logging);
84-
createOcppService(bus, ocpp15Server, "/CentralSystemServiceOCPP15", interceptors, logging);
85-
createOcppService(bus, ocpp16Server, "/CentralSystemServiceOCPP16", interceptors, logging);
70+
/**
71+
* Just a dummy service to route incoming messages to the appropriate service version.
72+
*/
73+
@Bean
74+
public EndpointImpl routerEndpoint(EndpointImpl ocpp12Endpoint,
75+
EndpointImpl ocpp15Endpoint,
76+
EndpointImpl ocpp16Endpoint) {
77+
var mediator = new MediatorInInterceptor(List.of(ocpp12Endpoint, ocpp15Endpoint, ocpp16Endpoint));
78+
return createEndpoint(
79+
ocpp12Server, SteveProperties.ROUTER_ENDPOINT_PATH, List.of(mediator), Collections.emptyList()
80+
);
81+
}
8682

87-
// Just a dummy service to route incoming messages to the appropriate service version. This should be the last
88-
// one to be created, since in MediatorInInterceptor we go over created/registered services and build a map.
89-
//
90-
List<Interceptor<? extends Message>> mediator = singletonList(new MediatorInInterceptor(bus));
91-
createOcppService(bus, ocpp12Server, SteveProperties.ROUTER_ENDPOINT_PATH, mediator, Collections.emptyList());
83+
private EndpointImpl createDefaultEndpoint(Object serviceBean, String address) {
84+
return createEndpoint(
85+
serviceBean, address, List.of(messageIdInterceptor, messageHeaderInterceptor), List.of(LoggingFeatureProxy.INSTANCE.get())
86+
);
9287
}
9388

94-
private void createOcppService(Bus bus, Object serviceBean, String address,
95-
List<Interceptor<? extends Message>> interceptors,
96-
Collection<? extends Feature> features) {
97-
JaxWsServerFactoryBean f = new JaxWsServerFactoryBean();
98-
f.setBus(bus);
99-
f.setServiceBean(serviceBean);
100-
f.setAddress(address);
101-
f.getFeatures().addAll(features);
102-
f.getInInterceptors().addAll(interceptors);
103-
f.create();
89+
private EndpointImpl createEndpoint(Object serviceBean, String address,
90+
List<Interceptor<? extends Message>> interceptors,
91+
List<? extends Feature> features) {
92+
EndpointImpl endpoint = new EndpointImpl(bus, serviceBean);
93+
endpoint.getInInterceptors().addAll(interceptors);
94+
endpoint.getFeatures().addAll(features);
95+
endpoint.publish(address);
96+
return endpoint;
10497
}
10598
}

src/main/java/de/rwth/idsg/steve/ocpp/soap/MediatorInInterceptor.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@
1818
*/
1919
package de.rwth.idsg.steve.ocpp.soap;
2020

21-
import de.rwth.idsg.steve.config.SteveProperties;
2221
import lombok.extern.slf4j.Slf4j;
23-
import org.apache.cxf.Bus;
2422
import org.apache.cxf.binding.soap.SoapMessage;
2523
import org.apache.cxf.binding.soap.SoapVersion;
2624
import org.apache.cxf.endpoint.Server;
27-
import org.apache.cxf.endpoint.ServerRegistry;
2825
import org.apache.cxf.interceptor.StaxInInterceptor;
26+
import org.apache.cxf.jaxws.EndpointImpl;
2927
import org.apache.cxf.message.Message;
3028
import org.apache.cxf.phase.AbstractPhaseInterceptor;
3129
import org.apache.cxf.phase.Phase;
32-
import org.apache.cxf.service.model.EndpointInfo;
3330
import org.apache.cxf.staxutils.DepthXMLStreamReader;
3431
import org.apache.cxf.staxutils.StaxUtils;
3532

@@ -51,10 +48,10 @@ public class MediatorInInterceptor extends AbstractPhaseInterceptor<SoapMessage>
5148

5249
private final Map<String, Server> actualServers;
5350

54-
public MediatorInInterceptor(Bus bus) {
51+
public MediatorInInterceptor(List<EndpointImpl> endpoints) {
5552
super(Phase.POST_STREAM);
5653
super.addBefore(StaxInInterceptor.class.getName());
57-
actualServers = initServerLookupMap(bus);
54+
actualServers = initServerLookupMap(endpoints);
5855
}
5956

6057
public final void handleMessage(SoapMessage message) {
@@ -104,30 +101,11 @@ public final void handleMessage(SoapMessage message) {
104101
* redirect to the version-specific implementation according to the namespace
105102
* of the incoming message.
106103
*/
107-
private static Map<String, Server> initServerLookupMap(Bus bus) {
108-
String exceptionMsg = "The services are not created and/or registered to the bus yet.";
109-
110-
ServerRegistry serverRegistry = bus.getExtension(ServerRegistry.class);
111-
if (serverRegistry == null) {
112-
throw new RuntimeException(exceptionMsg);
113-
}
114-
115-
List<Server> temp = serverRegistry.getServers();
116-
if (temp.isEmpty()) {
117-
throw new RuntimeException(exceptionMsg);
118-
}
119-
120-
Map<String, Server> actualServers = new HashMap<>(temp.size() - 1);
121-
for (Server server : temp) {
122-
EndpointInfo info = server.getEndpoint().getEndpointInfo();
123-
String address = info.getAddress();
124-
125-
// exclude the 'dummy' routing server
126-
if (SteveProperties.ROUTER_ENDPOINT_PATH.equals(address)) {
127-
continue;
128-
}
129-
130-
String serverNamespace = info.getName().getNamespaceURI();
104+
private static Map<String, Server> initServerLookupMap(List<EndpointImpl> endpoints) {
105+
Map<String, Server> actualServers = new HashMap<>();
106+
for (EndpointImpl endpoint : endpoints) {
107+
Server server = endpoint.getServer();
108+
String serverNamespace = server.getEndpoint().getEndpointInfo().getName().getNamespaceURI();
131109
actualServers.put(serverNamespace, server);
132110
}
133111
return actualServers;

0 commit comments

Comments
 (0)