3.2. Create a Router Project


Overview

This section describes how to generate a router project, which acts as a proxy for the WS server described in Section 3.1, “Create a Web Services Project”. The starting point for this project is the karaf-camel-cbr-archetype Maven archetype.

Prerequisites

This project depends on the cxf-basic project and requires that you have already generated and built the cxf-basic project, as described in Section 3.1, “Create a Web Services Project”.

Create project from the command line

Open a command prompt and change directory to the get-started directory. You can now use the archetype:generate goal to invoke the karaf-camel-cbr-archetype archetype, which generates a simple Apache Camel demonstration, as follows:
mvn archetype:generate
-DarchetypeGroupId=io.fabric8.archetypes
-DarchetypeArtifactId=karaf-camel-cbr-archetype
-DarchetypeVersion=1.2.0.redhat-133
-DgroupId=org.fusesource.example
-DartifactId=camel-basic
-Dversion=1.0-SNAPSHOT
-Dfabric8-profile=camel-basic-profile
Note
The arguments of the preceding command are shown on separate lines for readability, but when you are actually entering the command, the entire command must be entered on a single line.
You will be prompted to confirm the project settings, with a message similar to this one:
[INFO] Using property: groupId = org.fusesource.example
[INFO] Using property: artifactId = camel-basic
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = org.fusesource.example
[INFO] Using property: fabric8-profile = camel-basic-profile
Confirm properties configuration:
groupId: org.fusesource.example
artifactId: camel-basic
version: 1.0-SNAPSHOT
package: org.fusesource.example
fabric8-profile: camel-basic-profile
 Y: :
Type Return to accept the settings and generate the project. When the command finishes, you should find a new Maven project in the get-started/camel-basic directory.

Add the required Maven dependency

Because the route uses the Apache Camel Jetty component, you must add a Maven dependency on the camel-jetty artifact, so that the requisite JAR files are added to the classpath. To add the dependency, edit the camel-basic/pom.xml file and add the following highlighted dependency as a child of the dependencies element:
<project ...>
    ...
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-blueprint</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
        </dependency>
        ...
    </dependencies>
    ...
</project>

Modify the route

You are going to modify the default route generated by the archetype and change it into a route that implements a HTTP bridge. This bridge will be interposed between the WS client and Web service, enabling us to apply some routing logic to the WSDL messages that pass through the route.
Using your favourite text editor, open camel-basic/src/main/resources/OSGI-INF/blueprint/cbr.xml. Remove the existing camelContext element and replace it with the camelContext element highlighted in the following example:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/blueprint"
       xsi:schemaLocation="
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

  <camelContext id="blueprintContext"
                trace="false"
                xmlns="http://camel.apache.org/schema/blueprint">
    <route id="httpBridge">
      <from uri="jetty:http://0.0.0.0:8282/cxf/HelloWorld?matchOnUriPrefix=true"/>
      <delay><constant>5000</constant></delay>
      <to uri="jetty:http://localhost:8181/cxf/HelloWorld?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
    </route>
  </camelContext>

</blueprint>
The from element defines a new HTTP server port, which listens on IP port 8282. The to element defines a HTTP client endpoint that attempts to connect to the real Web service, which is listening on IP port 8181. To make the route a little more interesting, we add a delay element, which imposes a five second (5000 millisecond) delay on all requests passing through the route.
For a detailed discussion and explanation of the HTTP bridge, see Proxying with HTTP.

Change the port in the Web client test

By default, the Web client connects directly to the Web server on port 8181. In order to test the HTTP bridge, however, we want the Web client to connect to the Jetty port exposed by the HTTP bridge, which listens on port 8282. Hence, we need to edit the following file in the cxf-basic project:
cxf-basic/src/test/java/org/fusesource/example/SoapTest.java
Open the SoapTest.java file in your favourite text editor, and search for the following line:
URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorld").openConnection();
Change the port number in this line from 8181 to 8282, as highlighted in the following extract:
URLConnection connection = new URL("http://localhost:8282/cxf/HelloWorld").openConnection();

Build the router project

Build the router project and install the generated JAR file into your local Maven repository. From a command prompt, enter the following commands:
cd camel-basic
mvn install

Deploy and start the route

If you have not already started the Red Hat JBoss Fuse container and deployed the Web services bundle, you should do so now—see the section called “Deploy and start the WS server”.
To install and start up the camel-basic route as an OSGi bundle, enter the following console command:
JBossFuse:karaf@root> install -s mvn:org.fusesource.example/camel-basic/1.0-SNAPSHOT
If the bundle is successfully resolved and installed, the container responds by giving you the ID of the newly created bundle—for example:
Bundle ID: 230

Test the route with the WS client

The cxf-basic project includes a simple WS client, which you can use to test the deployed route and Web service. In a command prompt, navigate to the cxf-basic directory and run the simple WS client as follows:
cd ../cxf-basic
mvn -Ptest
If the client runs successfully, you should see output like the following:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.fusesource.example.SoapTest
After a five second delay, you will see the following response:
the response is ====> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://example.fusesource.org/"><return>Hello John Doe</return></ns2:sayHiResponse></soap:Body></soap:Envelope>
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.153 sec - in org.fusesource.example.SoapTest
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.