This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new 2870d4c Added a servletregistration customizer and improved examples. 2870d4c is described below commit 2870d4c1c5aca589c8a3807ea1f478113458acf3 Author: Andrea Tarocchi <andrea.taroc...@gmail.com> AuthorDate: Wed May 22 23:08:52 2019 +0200 Added a servletregistration customizer and improved examples. --- .../camel-k-runtime-example-health/README.adoc | 14 ++++++ .../src/main/resources/application.properties | 1 + .../camel-k-runtime-example-servlet/README.adoc | 14 ++++++ .../apache/camel/k/example/WebhookCustomizer.java | 33 -------------- .../services/org/apache/camel/k/customizer/webhook | 18 -------- .../src/main/resources/application.properties | 3 +- .../ServletRegistrationContextCustomizer.java | 53 ++++++++++++++++++++++ .../apache/camel/k/customizer/servletregistration | 2 +- .../camel/k/servlet/ServletCustomizerTest.java | 8 ++-- ...java => ServletRegistrationCustomizerTest.java} | 12 ++--- 10 files changed, 93 insertions(+), 65 deletions(-) diff --git a/camel-k-runtime-examples/camel-k-runtime-example-health/README.adoc b/camel-k-runtime-examples/camel-k-runtime-example-health/README.adoc new file mode 100644 index 0000000..dbecf56 --- /dev/null +++ b/camel-k-runtime-examples/camel-k-runtime-example-health/README.adoc @@ -0,0 +1,14 @@ +Health Apache Camel K Runtime example +====================================== + +This repository contains an Apache Camel-K Runtime application that expose `/health` endpoint to gather Camel context status. + +In order to run it: +```bash +mvn clean exec:java +``` +while it is running (from another terminal) you can access the rest camel route exposed through the servlet with: +```bash +curl http://localhost:8080/health +``` +if the camel context has started properly that should get `OK`. diff --git a/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/application.properties b/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/application.properties index b58849b..52e42dc 100644 --- a/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/application.properties +++ b/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/application.properties @@ -7,4 +7,5 @@ camel.context.streamCaching = true # Camel K # customizer.servlet.enabled = true +customizer.servletregistration.enabled = true customizer.health.enabled = true \ No newline at end of file diff --git a/camel-k-runtime-examples/camel-k-runtime-example-servlet/README.adoc b/camel-k-runtime-examples/camel-k-runtime-example-servlet/README.adoc new file mode 100644 index 0000000..601c3be --- /dev/null +++ b/camel-k-runtime-examples/camel-k-runtime-example-servlet/README.adoc @@ -0,0 +1,14 @@ +Servlet Apache Camel K Runtime example +====================================== + +This repository contains an Apache Camel-K Runtime application that use camel servlet. + +In order to run it: +```bash +mvn clean exec:java +``` +while it is running (from another terminal) you can access the rest camel route exposed through the servlet with: +```bash +curl http://localhost:8080/mypath/test +``` +that should log and empty exchange in the running application console logs. diff --git a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/java/org/apache/camel/k/example/WebhookCustomizer.java b/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/java/org/apache/camel/k/example/WebhookCustomizer.java deleted file mode 100644 index b474194..0000000 --- a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/java/org/apache/camel/k/example/WebhookCustomizer.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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.camel.k.example; - -import org.apache.camel.CamelContext; -import org.apache.camel.component.servlet.CamelHttpTransportServlet; -import org.apache.camel.k.ContextCustomizer; -import org.apache.camel.k.Runtime; -import org.apache.camel.k.servlet.ServletRegistration; - -public class WebhookCustomizer implements ContextCustomizer { - @Override - public void apply(CamelContext camelContext, Runtime.Registry registry) { - registry.bind( - "webhook-servlet", - new ServletRegistration("CamelServlet", new CamelHttpTransportServlet(), "/webhook/*") - ); - } -} diff --git a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook b/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook deleted file mode 100644 index 6952fcf..0000000 --- a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook +++ /dev/null @@ -1,18 +0,0 @@ -# -# 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. -# - -class=org.apache.camel.k.example.WebhookCustomizer diff --git a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/application.properties b/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/application.properties index 10c4282..aa5a559 100644 --- a/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/application.properties +++ b/camel-k-runtime-examples/camel-k-runtime-example-servlet/src/main/resources/application.properties @@ -6,5 +6,6 @@ camel.context.streamCaching = true # # Camel K # -customizer.webhook.enabled = true +customizer.servletregistration.enabled = true +customizer.servletregistration.path = /mypath/* customizer.servlet.enabled = true \ No newline at end of file diff --git a/camel-k-runtime-servlet/src/main/java/org/apache/camel/k/servlet/ServletRegistrationContextCustomizer.java b/camel-k-runtime-servlet/src/main/java/org/apache/camel/k/servlet/ServletRegistrationContextCustomizer.java new file mode 100644 index 0000000..232643d --- /dev/null +++ b/camel-k-runtime-servlet/src/main/java/org/apache/camel/k/servlet/ServletRegistrationContextCustomizer.java @@ -0,0 +1,53 @@ +package org.apache.camel.k.servlet; + +import org.apache.camel.CamelContext; +import org.apache.camel.Ordered; +import org.apache.camel.component.servlet.CamelHttpTransportServlet; +import org.apache.camel.k.ContextCustomizer; +import org.apache.camel.k.Runtime; + +public class ServletRegistrationContextCustomizer implements ContextCustomizer { + public static final String DEFAULT_PATH = "/camel/*"; + public static final String DEFAULT_CAMEL_SERVLET_NAME = "CamelServlet"; + + private String path; + private String camelServletName; + + public ServletRegistrationContextCustomizer(){ + this(DEFAULT_PATH, DEFAULT_CAMEL_SERVLET_NAME); + } + + public ServletRegistrationContextCustomizer(String path, String camelServletName){ + this.path = path; + this.camelServletName = camelServletName; + } + + @Override + public void apply(CamelContext camelContext, Runtime.Registry registry) { + registry.bind( + camelServletName, + new ServletRegistration(camelServletName, new CamelHttpTransportServlet(), path) + ); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getCamelServletName() { + return camelServletName; + } + + public void setCamelServletName(String camelServletName) { + this.camelServletName = camelServletName; + } +} \ No newline at end of file diff --git a/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook b/camel-k-runtime-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/servletregistration similarity index 91% rename from camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook rename to camel-k-runtime-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/servletregistration index 6952fcf..018dda8 100644 --- a/camel-k-runtime-examples/camel-k-runtime-example-health/src/main/resources/META-INF/services/org/apache/camel/k/customizer/webhook +++ b/camel-k-runtime-servlet/src/main/resources/META-INF/services/org/apache/camel/k/customizer/servletregistration @@ -15,4 +15,4 @@ # limitations under the License. # -class=org.apache.camel.k.example.WebhookCustomizer +class=org.apache.camel.k.servlet.ServletRegistrationContextCustomizer diff --git a/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java b/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java index e44bb51..627970b 100644 --- a/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java +++ b/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java @@ -36,10 +36,8 @@ public class ServletCustomizerTest { Runtime.Registry registry = new InMemoryRegistry(); Runtime runtime = Runtime.of(new DefaultCamelContext(registry), registry); - runtime.getRegistry().bind( - "camel-servlet", - new ServletRegistration("CamelServlet", new CamelHttpTransportServlet(), "/webhook/*") - ); + ServletRegistrationContextCustomizer servletRegistrationCustomizer = new ServletRegistrationContextCustomizer("/webhook/*", "webhook-servlet"); + servletRegistrationCustomizer.apply(runtime.getContext(), runtime.getRegistry()); ServletContextCustomizer servletCustomizer = new ServletContextCustomizer(); servletCustomizer.setBindPort(AvailablePortFinder.getNextAvailable()); @@ -47,7 +45,7 @@ public class ServletCustomizerTest { DeploymentManager manager = Servlets.defaultContainer().getDeploymentByPath("/"); ManagedServlets managedServlets = manager.getDeployment().getServlets(); - ManagedServlet servlet = managedServlets.getManagedServlet("CamelServlet"); + ManagedServlet servlet = managedServlets.getManagedServlet("webhook-servlet"); assertThat(servlet).isNotNull(); assertThat(servlet.getServletInfo().getMappings()).contains("/webhook/*"); diff --git a/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java b/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletRegistrationCustomizerTest.java similarity index 86% copy from camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java copy to camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletRegistrationCustomizerTest.java index e44bb51..d39d3e4 100644 --- a/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletCustomizerTest.java +++ b/camel-k-runtime-servlet/src/test/java/org/apache/camel/k/servlet/ServletRegistrationCustomizerTest.java @@ -29,17 +29,15 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class ServletCustomizerTest { +public class ServletRegistrationCustomizerTest { @Test - public void testServletConfigurer() { + public void testServletRegistrationConfigurer() { Runtime.Registry registry = new InMemoryRegistry(); Runtime runtime = Runtime.of(new DefaultCamelContext(registry), registry); - runtime.getRegistry().bind( - "camel-servlet", - new ServletRegistration("CamelServlet", new CamelHttpTransportServlet(), "/webhook/*") - ); + ServletRegistrationContextCustomizer servletRegistrationCustomizer = new ServletRegistrationContextCustomizer(); + servletRegistrationCustomizer.apply(runtime.getContext(), runtime.getRegistry()); ServletContextCustomizer servletCustomizer = new ServletContextCustomizer(); servletCustomizer.setBindPort(AvailablePortFinder.getNextAvailable()); @@ -50,6 +48,6 @@ public class ServletCustomizerTest { ManagedServlet servlet = managedServlets.getManagedServlet("CamelServlet"); assertThat(servlet).isNotNull(); - assertThat(servlet.getServletInfo().getMappings()).contains("/webhook/*"); + assertThat(servlet.getServletInfo().getMappings()).contains("/camel/*"); } }