Skip to content

Commit

Permalink
refactoring JAX-RS module to follow our common factory approach
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Dec 9, 2023
1 parent decbd10 commit 5f5a40d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,24 @@

package io.bootique.cxf;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
* Default service.
*
* @author Ruslan Ibragimov
*/
// TODO: why do we need a "default" resource? Remove?
public class CxfDefaultService {
private final CxfJaxrsModuleConfig config;

@Inject
public CxfDefaultService(CxfJaxrsModuleConfig config) {
this.config = config;
private final String welcomeText;

public CxfDefaultService(String welcomeText) {
this.welcomeText = welcomeText;
}

@GET()
@Path("/")
@Produces(MediaType.TEXT_HTML)
public String get() {
return config.getWelcomeText();
return welcomeText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.bootique.config.ConfigurationFactory;
import io.bootique.cxf.annotations.CxfFeature;
import io.bootique.cxf.annotations.CxfResource;
import io.bootique.cxf.annotations.CxfServlet;
import io.bootique.di.Binder;
import io.bootique.di.Key;
import io.bootique.di.Provides;
Expand All @@ -37,9 +36,7 @@
import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet;

import javax.inject.Singleton;
import javax.servlet.Servlet;
import javax.ws.rs.core.Application;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -68,41 +65,45 @@ public static CxfJaxrsModuleExtender extend(Binder binder) {
public ModuleCrate crate() {
return ModuleCrate.of(this)
.description("Integrates Apache CXF JAX-RS engine")
.config(CONFIG_PREFIX, CxfJaxrsModuleConfig.class)
.config(CONFIG_PREFIX, CxfJaxrsServletFactory.class)
.build();
}

@Override
public void configure(Binder binder) {
CxfJaxrsModule.extend(binder).initAllExtensions();

final TypeLiteral<MappedServlet<Servlet>> servletTypeLiteral = new TypeLiteral<MappedServlet<Servlet>>() {};
final Key<MappedServlet<Servlet>> servletKey = Key.get(servletTypeLiteral, CxfServlet.class);
JettyModule.extend(binder).addMappedServlet(servletKey);
TypeLiteral<MappedServlet<CXFNonSpringJaxrsServlet>> servletTypeLiteral = new TypeLiteral<>() {
};
JettyModule.extend(binder).addMappedServlet(Key.get(servletTypeLiteral));

// TODO: we probably should not have a default endpoint in the container.
CxfJaxrsModule.extend(binder).addResource(CxfDefaultService.class);
}

@CxfServlet
@Singleton
@Provides
private MappedServlet<Servlet> createCxfServlet(CxfJaxrsModuleConfig config, Application application, Bus bus) {

CXFNonSpringJaxrsServlet servlet = new CXFNonSpringJaxrsServlet(application);
servlet.setBus(bus);

return new MappedServlet<>(servlet, Collections.singleton(config.getUrlPattern()), CxfServlet.class.getName());
CxfDefaultService provideDefaultService(ConfigurationFactory configFactory) {
return configFactory.config(CxfJaxrsServletFactory.class, CONFIG_PREFIX).createDefaultService();
}

@Singleton
@Provides
private CxfJaxrsModuleConfig createCxfFactory(ConfigurationFactory configFactory) {
return configFactory.config(CxfJaxrsModuleConfig.class, CONFIG_PREFIX);
MappedServlet<CXFNonSpringJaxrsServlet> createCxfServlet(
ConfigurationFactory configFactory,
Application application,
Bus bus) {

return configFactory.config(CxfJaxrsServletFactory.class, CONFIG_PREFIX).createServlet(application, bus);
}

@Singleton
@Provides
private Application createApplication(@CxfResource Set<Object> resources, @CxfFeature Set<Feature> features) {
final Map<String, String> props = new HashMap<>();
Application createApplication(
@CxfResource Set<Object> resources,
@CxfFeature Set<Feature> features) {

Map<String, String> props = new HashMap<>();

// TODO deliver interceptors in module extender
props.put("jaxrs.inInterceptors", LoggingInInterceptor.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,18 @@
import io.bootique.ModuleExtender;
import io.bootique.cxf.annotations.CxfFeature;
import io.bootique.cxf.annotations.CxfResource;
import io.bootique.cxf.annotations.CxfServlet;
import io.bootique.di.Binder;
import io.bootique.di.Key;
import io.bootique.di.SetBuilder;
import io.bootique.di.TypeLiteral;
import io.bootique.jetty.MappedServlet;
import org.apache.cxf.feature.Feature;

/**
* Init all {@link SetBuilder}s on module loading.
*
* @author Ruslan Ibragimov
*/
public class CxfJaxrsModuleExtender extends ModuleExtender<CxfJaxrsModuleExtender> {

private SetBuilder<Feature> cxfFeatures;
private SetBuilder<Object> resources;
private SetBuilder<MappedServlet<?>> servlets;

CxfJaxrsModuleExtender(Binder binder) {
super(binder);
Expand All @@ -49,8 +43,6 @@ public class CxfJaxrsModuleExtender extends ModuleExtender<CxfJaxrsModuleExtende
public CxfJaxrsModuleExtender initAllExtensions() {
contributeCxfFeatures();
contributeResources();
contributeMappedServlet();

return this;
}

Expand Down Expand Up @@ -89,12 +81,4 @@ protected SetBuilder<Feature> contributeCxfFeatures() {

return cxfFeatures;
}

protected SetBuilder<MappedServlet<?>> contributeMappedServlet() {
if (servlets == null) {
servlets = newSet(Key.get(new TypeLiteral<MappedServlet<?>>(){}, CxfServlet.class));
}

return servlets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,40 @@

import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;
import io.bootique.jetty.MappedServlet;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet;

import javax.ws.rs.core.Application;
import java.util.Set;

/**
* CXF configuration.
*
* @author Ruslan Ibragimov
*/
@BQConfig("Configures the servlet that is an entry point to CXF REST API engine.")
public class CxfJaxrsModuleConfig {
private String urlPattern;
public class CxfJaxrsServletFactory {

private String urlPattern;
private String welcomeText;

public CxfJaxrsModuleConfig() {
this.urlPattern = "/*";
this.welcomeText = "CXF REST API Module";
public MappedServlet<CXFNonSpringJaxrsServlet> createServlet(Application application, Bus bus) {
CXFNonSpringJaxrsServlet servlet = new CXFNonSpringJaxrsServlet(application);
servlet.setBus(bus);

String urlPattern = this.urlPattern != null ? this.urlPattern : "/*";
return new MappedServlet<>(servlet, Set.of(urlPattern));
}

public String getUrlPattern() {
return urlPattern;
public CxfDefaultService createDefaultService() {
String welcomeText = this.welcomeText != null ? this.welcomeText : "CXF REST API Module";
return new CxfDefaultService(welcomeText);
}

@BQConfigProperty
public void setUrlPattern(String urlPattern) {
this.urlPattern = urlPattern;
}

public String getWelcomeText() {
return welcomeText;
}

@BQConfigProperty
public void setWelcomeText(String welcomeText) {
this.welcomeText = welcomeText;
Expand Down

This file was deleted.

0 comments on commit 5f5a40d

Please sign in to comment.