diff --git a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfDefaultService.java b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfDefaultService.java index b18fb69..9286c54 100644 --- a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfDefaultService.java +++ b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfDefaultService.java @@ -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; } } diff --git a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModule.java b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModule.java index bd09a14..bca68d7 100644 --- a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModule.java +++ b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModule.java @@ -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; @@ -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; @@ -68,7 +65,7 @@ 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(); } @@ -76,33 +73,37 @@ public ModuleCrate crate() { public void configure(Binder binder) { CxfJaxrsModule.extend(binder).initAllExtensions(); - final TypeLiteral> servletTypeLiteral = new TypeLiteral>() {}; - final Key> servletKey = Key.get(servletTypeLiteral, CxfServlet.class); - JettyModule.extend(binder).addMappedServlet(servletKey); + TypeLiteral> 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 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 createCxfServlet( + ConfigurationFactory configFactory, + Application application, + Bus bus) { + + return configFactory.config(CxfJaxrsServletFactory.class, CONFIG_PREFIX).createServlet(application, bus); } @Singleton @Provides - private Application createApplication(@CxfResource Set resources, @CxfFeature Set features) { - final Map props = new HashMap<>(); + Application createApplication( + @CxfResource Set resources, + @CxfFeature Set features) { + + Map props = new HashMap<>(); // TODO deliver interceptors in module extender props.put("jaxrs.inInterceptors", LoggingInInterceptor.class.getName()); diff --git a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleExtender.java b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleExtender.java index 088babe..85f628e 100644 --- a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleExtender.java +++ b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleExtender.java @@ -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 { private SetBuilder cxfFeatures; private SetBuilder resources; - private SetBuilder> servlets; CxfJaxrsModuleExtender(Binder binder) { super(binder); @@ -49,8 +43,6 @@ public class CxfJaxrsModuleExtender extends ModuleExtender contributeCxfFeatures() { return cxfFeatures; } - - protected SetBuilder> contributeMappedServlet() { - if (servlets == null) { - servlets = newSet(Key.get(new TypeLiteral>(){}, CxfServlet.class)); - } - - return servlets; - } } diff --git a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleConfig.java b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsServletFactory.java similarity index 63% rename from bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleConfig.java rename to bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsServletFactory.java index edc7163..b3a368a 100644 --- a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsModuleConfig.java +++ b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/CxfJaxrsServletFactory.java @@ -21,25 +21,33 @@ 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 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 @@ -47,10 +55,6 @@ public void setUrlPattern(String urlPattern) { this.urlPattern = urlPattern; } - public String getWelcomeText() { - return welcomeText; - } - @BQConfigProperty public void setWelcomeText(String welcomeText) { this.welcomeText = welcomeText; diff --git a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/annotations/CxfServlet.java b/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/annotations/CxfServlet.java deleted file mode 100644 index 65d65c1..0000000 --- a/bootique-cxf-jaxrs/src/main/java/io/bootique/cxf/annotations/CxfServlet.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to ObjectStyle LLC under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ObjectStyle LLC 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 io.bootique.cxf.annotations; - -import javax.inject.Qualifier; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Binding annotation for CXF servlets. - * - * @author Ruslan Ibragimov - */ -@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -@Qualifier -public @interface CxfServlet { -}