diff --git a/README.adoc b/README.adoc index f12b9c56..a5f80a2b 100644 --- a/README.adoc +++ b/README.adoc @@ -27,7 +27,7 @@ readme's instructions. === Examples // examples: START -Number of Examples: 65 (0 deprecated) +Number of Examples: 66 (0 deprecated) [width="100%",cols="4,2,4",options="header"] |=== @@ -153,6 +153,8 @@ Number of Examples: 65 (0 deprecated) | link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An example showing Camel REST DSL with platform HTTP +| link:platform-http-proxy/README.adoc[Platform Http Proxy] (platform-http-proxy) | EIP | An example with Camel Platform HTTP act as reverse proxy + | link:rest-cxf/README.adoc[Rest Cxf] (rest-cxf) | Rest | An example showing Camel REST using CXF with Spring Boot | link:rest-openapi/README.adoc[Rest Openapi] (rest-openapi) | Rest | An example showing Camel REST DSL and OpenApi with Spring Boot diff --git a/platform-http-proxy/README.adoc b/platform-http-proxy/README.adoc new file mode 100644 index 00000000..7ab369be --- /dev/null +++ b/platform-http-proxy/README.adoc @@ -0,0 +1,43 @@ +== Spring Boot Example with Camel Platform HTTP act as reverse proxy + +=== Introduction + +This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel]. It implements a reverse proxy using https://camel.apache.org/components/latest/platform-http-component.html[platform-http]. + +The project uses `camel-platform-http-starter` component as the implementation for platform-http-engine and `camel-http-starter` to implement the producer calling the http endpoint. + +=== Run + +You can run this example using: + +[source,bash] +---- +mvn spring-boot:run +---- + +After the Spring Boot application is started, you can execute the following HTTP requests: + +[source,bash] +---- +curl --proxy http://localhost:8080 -L http://httpbin.org/get?arg1=val1 -H 'accept: application/json' +---- + +The command will call a test endpoint using the application as reverse proxy, you should see the Camel headers from both request and response in the log. Something similar to: + +---- +INFO 70370 --- [ad #1 - WireTap] header-request : {accept=application/json, arg1=val1, CamelHttpCharacterEncoding=UTF-8, CamelHttpMethod=GET, CamelHttpPath=/get, CamelHttpQuery=arg1=val1, CamelHttpServletRequest=org.apache.catalina.connector.RequestFacade@4f31b074, CamelHttpServletResponse=org.springframework.web.context.request.async.StandardServletAsyncWebRequest$LifecycleHttpServletResponse@158137c0, CamelHttpUri=/get, CamelHttpUrl=http://httpbin.org/get, CamelPlatformHttpContextPath=/, host=httpbin.org, proxy-connection=Keep-Alive, user-agent=curl/8.9.1} +INFO 70370 --- [ad #2 - WireTap] header-response : {accept=application/json, Access-Control-Allow-Credentials=true, Access-Control-Allow-Origin=*, arg1=val1, CamelHttpCharacterEncoding=UTF-8, CamelHttpMethod=GET, CamelHttpQuery=arg1=val1, CamelHttpResponseCode=200, CamelHttpResponseText=OK, CamelHttpServletRequest=org.apache.catalina.connector.RequestFacade@4f31b074, CamelHttpServletResponse=org.springframework.web.context.request.async.StandardServletAsyncWebRequest$LifecycleHttpServletResponse@158137c0, CamelHttpUri=/get, CamelHttpUrl=http://httpbin.org/get, CamelPlatformHttpContextPath=/, Connection=keep-alive, Content-Length=387, Content-Type=application/json, Date=Thu, 04 Sep 2025 11:59:30 GMT, proxy-connection=Keep-Alive, Server=gunicorn/19.9.0} + +---- + +The Spring Boot application can be stopped pressing `[CTRL] + [C]` in the shell. + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/community/support/[let us know]. + +We also love contributors, so +https://camel.apache.org/community/contributing/[get involved] :-) + +The Camel riders! diff --git a/platform-http-proxy/pom.xml b/platform-http-proxy/pom.xml new file mode 100644 index 00000000..e247eb7a --- /dev/null +++ b/platform-http-proxy/pom.xml @@ -0,0 +1,108 @@ + + + 4.0.0 + + + org.apache.camel.springboot.example + examples + 4.15.0-SNAPSHOT + + + camel-example-spring-boot-platform-http-proxy + Camel SB Examples :: Platform HTTP as reverse proxy + An example showing Camel reverse proxy with platform HTTP + + + EIP + + + + + + + org.apache.camel.springboot + camel-spring-boot-bom + ${project.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot-version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.apache.camel.springboot + camel-spring-boot-starter + + + org.apache.camel.springboot + camel-platform-http-starter + + + org.apache.camel.springboot + camel-http-starter + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.apache.camel + camel-test-junit5 + ${camel-version} + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot-version} + + + + repackage + + + + + + + + diff --git a/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/Application.java b/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/Application.java new file mode 100644 index 00000000..4c0bd710 --- /dev/null +++ b/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/Application.java @@ -0,0 +1,34 @@ +/* + * 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.example.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +// CHECKSTYLE:OFF +@SpringBootApplication +public class Application { + + /** + * Main method to start the application. + */ + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} +// CHECKSTYLE:ON diff --git a/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/CamelRouter.java b/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/CamelRouter.java new file mode 100644 index 00000000..400838da --- /dev/null +++ b/platform-http-proxy/src/main/java/org/apache/camel/example/springboot/CamelRouter.java @@ -0,0 +1,50 @@ +/* + * 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.example.springboot; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.springframework.stereotype.Component; + +/** + * Reverse proxy route with routes that logs heders around the backend call + */ +@Component +public class CamelRouter extends RouteBuilder { + + @Override + public void configure() throws Exception { + + // @formatter:off + from("platform-http:proxy/*?matchOnUriPrefix=true") + .routeId("reverse-proxy") + .wireTap("direct:request") + .removeHeader(Exchange.HTTP_PATH) + .log("calling ${headers." + Exchange.HTTP_URL + "}") + .toD("${headers." + Exchange.HTTP_URL + "}" + + "?throwExceptionOnFailure=false&bridgeEndpoint=true") + .wireTap("direct:response"); + + from("direct:request") + .routeId("header-request") + .log("${headers}"); + from("direct:response") + .routeId("header-response") + .log("${headers}"); + // @formatter:on + } +} diff --git a/platform-http-proxy/src/main/resources/application.properties b/platform-http-proxy/src/main/resources/application.properties new file mode 100644 index 00000000..a4ed5c28 --- /dev/null +++ b/platform-http-proxy/src/main/resources/application.properties @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +# the name of Camel +camel.main.name = MyCamelReverseProxy diff --git a/pom.xml b/pom.xml index 9f010242..8efdd1a2 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,7 @@ opentelemetry paho-mqtt5-shared-subscriptions platform-http + platform-http-proxy pojo rabbitmq reactive-streams