The past few days I've been searching how to do the below without having any knowledge in Tomcat or Camel and I'm surprised I haven't found any good resources on this:
Make a .war file that will be deployed into Tomcat (e.g. from Manager App) which will accept a request on a given URI /test
and will forward the request to an internal service in PHP which runs on localhost:8222/index.php?q=test
.
I have managed to have a working example of forwarding a request to a different url by building on top of the camel-archetype-java
and the router looks like this:
package camelinaction;
import org.apache.camel.builder.RouteBuilder;
public class MyRouteBuilder extends RouteBuilder {
public void configure() {
String targetUrl = "https://another.service.com/api/test";
from("jetty:http://127.0.0.1:25566/test?matchOnUriPrefix=true")
.to("jetty:" + targetUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
}
}
and I have managed also to create a .war file from the camel-example-servlet-tomcat
example that in Camel and deploy it successfully in tomcat. That example doesn't have any Java code though in its project folder and is consisted basically from just .xml files and a .html page which is served by the Camel servlet when requests the related servlet path which is served by tomcat.
The basic xml of that project looks like below:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="helloRoute">
<!-- incoming requests from the servlet is routed -->
<from uri="servlet:hello"/>
<choice>
<when>
<!-- is there a header with the key name? -->
<header>name</header>
<!-- yes so return back a message to the user -->
<transform>
<simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
</transform>
</when>
<otherwise>
<!-- if no name parameter then output a syntax to the user -->
<transform>
<constant>Add a name parameter to uri, eg ?name=foo</constant>
</transform>
</otherwise>
</choice>
</route>
</camelContext>
</beans>
How would someone would combine the two examples/functionalities to achieve the end goal of having a request that comes from tomcat to be forwarded with camel to a different endpoint?