This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch soap-examples in repository https://gitbox.apache.org/repos/asf/camel-jbang-examples.git
commit d70046142c7e2974c823d7aff93f52f001019c6c Author: Claus Ibsen <[email protected]> AuthorDate: Sun Nov 29 10:04:10 2026 +0100 contracts: SOAP facade and SOAP client, with the soap-source and soap-action Kamelets Two WSDL-first examples with the shop's orders, next to openapi-server and openapi-client: - soap-facade: soap-source serves the shop's order service from orders.wsdl, so old SOAP clients keep calling getOrder, and the route answers from the JSON order API with bodyFormat json; an unknown order gets the declared fault orderNotFound. - soap-client: soap-action calls getOrder for each order with bodyFormat json, and faultAsBody passes the fault of an unknown order to the route; the old order service is a soap-source route in the same example. Each has a Citrus YAML test, run with camel test run, and both are in the CI matrix. The Kamelets are new in camel-kamelets 4.24.0, so the CI needs Camel 4.24.0. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .github/workflows/build.yml | 4 + README.md | 4 +- camel-jbang-example-catalog.json | 108 ++++++++++++++++++- contracts/README.md | 4 +- contracts/keycloak-security-rest/metadata.json | 2 +- contracts/soap-client/README.md | 95 +++++++++++++++++ contracts/soap-client/application.properties | 18 ++++ contracts/soap-client/metadata.json | 42 ++++++++ contracts/soap-client/order-service.camel.yaml | 57 ++++++++++ contracts/soap-client/order-status.camel.yaml | 56 ++++++++++ contracts/soap-client/orders.json | 5 + contracts/soap-client/orders.wsdl | 92 ++++++++++++++++ .../soap-client/test/soap-client.citrus.it.yaml | 30 ++++++ contracts/soap-facade/README.md | 118 +++++++++++++++++++++ contracts/soap-facade/application.properties | 18 ++++ contracts/soap-facade/metadata.json | 42 ++++++++ contracts/soap-facade/orders-api.camel.yaml | 48 +++++++++ contracts/soap-facade/orders.json | 5 + contracts/soap-facade/orders.wsdl | 92 ++++++++++++++++ contracts/soap-facade/soap-facade.camel.yaml | 61 +++++++++++ .../soap-facade/test/soap-facade.citrus.it.yaml | 69 ++++++++++++ generate-catalog.sh | 2 +- 22 files changed, 967 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e257d4c..9e16dab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -107,6 +107,10 @@ jobs: path: contracts/openapi-server - name: OpenAPI client path: contracts/openapi-client + - name: SOAP facade + path: contracts/soap-facade + - name: SOAP client + path: contracts/soap-client - name: AWS SQS path: cloud/aws-sqs - name: EDI X12 AS2 diff --git a/README.md b/README.md index 42e4f57..6c75866 100644 --- a/README.md +++ b/README.md @@ -115,13 +115,15 @@ SQL, JMS, MQTT, Kafka and FTP against a service the Camel CLI starts for you wit ### [Contracts and security](contracts/) -An OpenAPI contract served and called, and an API protected by Keycloak. +An OpenAPI contract served and called, a SOAP service served and called from its WSDL, and an API protected by Keycloak. | Example | What you will see | Needs | |---|---|---| | [REST code first](contracts/rest-code-first/) | The shop's order API written with the REST DSL: GET /orders and GET /orders/{id} answer the orders as JSON (404 for an unknown id), POST /orders binds the JSON of a new order to a Java record and answers 201 (400 without lines), and the API description generated from the operations is served at /openapi. | nothing | | [OpenAPI server](contracts/openapi-server/) | The stock API contract first: stock-api.json is the OpenAPI contract, the REST DSL serves its three operations on port 8080 with request validation, GET /stock/{sku} answers from a file or 404, POST /stock/{sku}/reserve answers 200, 409 when the stock is short or 400 for a bad reservation, and /openapi serves the contract. | nothing | | [OpenAPI client](contracts/openapi-client/) | The picking desk reserves stock for every order line by calling the stock API by contract: rest-openapi turns the operationId reserveStock into the HTTP call from stock-api.json; the log shows each reservation and one 409 for the cap that is out of stock. Needs the openapi-server example running. | `contracts/openapi-server` running first | +| [SOAP facade](contracts/soap-facade/) | The shop's old SOAP clients keep calling getOrder: soap-source serves the order service from orders.wsdl at /services/orders on port 8080, and the route answers each call from the shop's JSON order API with bodyFormat json, so no XML is mapped by hand; an unknown order gets the fault orderNotFound that the WSDL declares. | nothing | +| [SOAP client](contracts/soap-client/) | The shop checks its orders with an old SOAP order service: soap-action calls getOrder from orders.wsdl once for each order with bodyFormat json, and the log shows the status of each order; the order the service does not have gets its fault, which faultAsBody passes to the route instead of failing the message. The order service is a soap-source route in the same example. | nothing | | [Keycloak security](contracts/keycloak-security-rest/) | Two HTTP endpoints on port 8081: the public one answers everyone, the protected one requires a bearer token from a Keycloak started with camel infra whose user has the admin role, otherwise 403; the realm, client and users are created in the Keycloak console as the README describes. | `camel infra run keycloak` | ### [AI](ai/) diff --git a/camel-jbang-example-catalog.json b/camel-jbang-example-catalog.json index 33c2074..ac93b00 100644 --- a/camel-jbang-example-catalog.json +++ b/camel-jbang-example-catalog.json @@ -826,7 +826,7 @@ "infraServices": [ "keycloak" ], - "order": 4 + "order": 6 }, { "name": "contracts/openapi-client", @@ -972,6 +972,112 @@ ], "order": 1 }, + { + "name": "contracts/soap-client", + "title": "SOAP client", + "description": "The shop checks its orders with an old SOAP order service: soap-action calls getOrder from orders.wsdl once for each order with bodyFormat json, and the log shows the status of each order; the order the service does not have gets its fault, which faultAsBody passes to the route instead of failing the message. The order service is a soap-source route in the same example.", + "level": "contracts", + "teaches": { + "components": [ + "kamelet", + "timer", + "log" + ], + "eips": [ + "split", + "choice", + "setVariable", + "setHeader", + "marshal", + "unmarshal" + ], + "languages": [ + "simple", + "jq", + "groovy", + "tokenize", + "constant" + ], + "dataformats": [ + "json" + ] + }, + "tags": [ + "shop", + "soap", + "wsdl", + "client", + "contract", + "legacy", + "soap-action", + "soap-source" + ], + "bundled": true, + "requiresDocker": false, + "hasCitrusTests": true, + "files": [ + "README.md", + "application.properties", + "order-service.camel.yaml", + "order-status.camel.yaml", + "orders.json", + "orders.wsdl" + ], + "order": 5 + }, + { + "name": "contracts/soap-facade", + "title": "SOAP facade", + "description": "The shop's old SOAP clients keep calling getOrder: soap-source serves the order service from orders.wsdl at /services/orders on port 8080, and the route answers each call from the shop's JSON order API with bodyFormat json, so no XML is mapped by hand; an unknown order gets the fault orderNotFound that the WSDL declares.", + "level": "contracts", + "teaches": { + "components": [ + "kamelet", + "rest", + "http", + "direct", + "log" + ], + "eips": [ + "choice", + "setVariable", + "setHeader", + "removeHeaders", + "toD", + "marshal", + "unmarshal" + ], + "languages": [ + "simple", + "jq", + "groovy", + "constant" + ], + "dataformats": [ + "json" + ] + }, + "tags": [ + "shop", + "soap", + "wsdl", + "contract", + "legacy", + "soap-source" + ], + "bundled": true, + "requiresDocker": false, + "hasCitrusTests": true, + "files": [ + "README.md", + "application.properties", + "orders-api.camel.yaml", + "orders.json", + "orders.wsdl", + "soap-facade.camel.yaml" + ], + "order": 4 + }, { "name": "fail-well/chaos", "title": "Chaos", diff --git a/contracts/README.md b/contracts/README.md index e3803e4..c5f7965 100644 --- a/contracts/README.md +++ b/contracts/README.md @@ -1,13 +1,15 @@ # Contracts and security <!-- group:start --> -An OpenAPI contract served and called, and an API protected by Keycloak. +An OpenAPI contract served and called, a SOAP service served and called from its WSDL, and an API protected by Keycloak. | Example | What you will see | Needs | |---|---|---| | [REST code first](rest-code-first/) | The shop's order API written with the REST DSL: GET /orders and GET /orders/{id} answer the orders as JSON (404 for an unknown id), POST /orders binds the JSON of a new order to a Java record and answers 201 (400 without lines), and the API description generated from the operations is served at /openapi. | nothing | | [OpenAPI server](openapi-server/) | The stock API contract first: stock-api.json is the OpenAPI contract, the REST DSL serves its three operations on port 8080 with request validation, GET /stock/{sku} answers from a file or 404, POST /stock/{sku}/reserve answers 200, 409 when the stock is short or 400 for a bad reservation, and /openapi serves the contract. | nothing | | [OpenAPI client](openapi-client/) | The picking desk reserves stock for every order line by calling the stock API by contract: rest-openapi turns the operationId reserveStock into the HTTP call from stock-api.json; the log shows each reservation and one 409 for the cap that is out of stock. Needs the openapi-server example running. | `contracts/openapi-server` running first | +| [SOAP facade](soap-facade/) | The shop's old SOAP clients keep calling getOrder: soap-source serves the order service from orders.wsdl at /services/orders on port 8080, and the route answers each call from the shop's JSON order API with bodyFormat json, so no XML is mapped by hand; an unknown order gets the fault orderNotFound that the WSDL declares. | nothing | +| [SOAP client](soap-client/) | The shop checks its orders with an old SOAP order service: soap-action calls getOrder from orders.wsdl once for each order with bodyFormat json, and the log shows the status of each order; the order the service does not have gets its fault, which faultAsBody passes to the route instead of failing the message. The order service is a soap-source route in the same example. | nothing | | [Keycloak security](keycloak-security-rest/) | Two HTTP endpoints on port 8081: the public one answers everyone, the protected one requires a bearer token from a Keycloak started with camel infra whose user has the admin role, otherwise 403; the realm, client and users are created in the Keycloak console as the README describes. | `camel infra run keycloak` | Start with [REST code first](rest-code-first/); the examples read best in the order above, each one building on what the one before it set up. diff --git a/contracts/keycloak-security-rest/metadata.json b/contracts/keycloak-security-rest/metadata.json index d0b2474..9a3ca4e 100644 --- a/contracts/keycloak-security-rest/metadata.json +++ b/contracts/keycloak-security-rest/metadata.json @@ -2,7 +2,7 @@ "title": "Keycloak security", "description": "Two HTTP endpoints on port 8081: the public one answers everyone, the protected one requires a bearer token from a Keycloak started with camel infra whose user has the admin role, otherwise 403; the realm, client and users are created in the Keycloak console as the README describes.", "level": "contracts", - "order": 4, + "order": 6, "teaches": { "components": [ "platform-http", diff --git a/contracts/soap-client/README.md b/contracts/soap-client/README.md new file mode 100644 index 0000000..e4eba6b --- /dev/null +++ b/contracts/soap-client/README.md @@ -0,0 +1,95 @@ +# SOAP client + +The shop still has to ask an old SOAP order service about its orders. This example calls it from a route: the +`soap-action` Kamelet calls the operation `getOrder` of `orders.wsdl` once for each order, and the route logs what +the service answered. With `bodyFormat: json` the route sends and gets JSON, and with `faultAsBody: true` a fault +of the service does not fail the message: the route gets the fault and goes on with the next order. + +The old order service is part of the example, so it needs no SOAP server of its own: `order-service.camel.yaml` +serves `getOrder` with the `soap-source` Kamelet, as the `soap-facade` example does. + +## What you will see + +```text +INFO ... order-status.camel.yaml:55 : Order ORD-1001 for customer C-482 is paid, with 2 line(s) +INFO ... order-status.camel.yaml:55 : Order ORD-1002 for customer C-207 is paid, with 1 line(s) +INFO ... order-status.camel.yaml:55 : Order ORD-1003 for customer C-134 is pending, with 3 line(s) +INFO ... order-status.camel.yaml:45 : Order ORD-1004: the order service answered with the fault: Order ORD-1004 not found +``` + +## Install Camel CLI + +Install [JBang](https://www.jbang.dev/download/) and the Camel CLI as described in the +[root README](../../README.md#install-the-camel-cli); `camel --version` confirms the install. + +## Run it + +```shell +camel run * +``` + +The orders are checked once, 3 seconds after the start, so that the order service is up. Stop the example with +`ctrl` + `c`. + +## How it works + +- `order-status.camel.yaml` splits the order ids and sends `{"orderId": "ORD-1001"}` and so on to + `kamelet:soap-action`. `wsdlURL: orders.wsdl` and `operation: getOrder` say what to call, and `address` where: + without it, the address in the WSDL is used. +- With `bodyFormat: json` the Kamelet writes the XML request of `getOrder` from the JSON, as the schema of the WSDL + describes it, and turns the XML answer back into JSON: `{"order": {"orderId": "ORD-1001", ..., "line": [...]}}`. + `qty` is a number and `price` a decimal there, and `line` is a JSON array, as it can repeat. +- `jq` takes the order out of the answer into a variable as a `java.util.Map`, so the log can read + `${variable.order[customer]}` and `${variable.order[line].size()}`. +- With `faultAsBody: true`, a fault of the service sets the headers `SoapFault` (its text) and `SoapFaultCode` (its + code), and the body is the detail of the fault: here the XML of `orderNotFound`. The `choice` logs the fault and + the split goes on. Without `faultAsBody` the fault fails the message, as any error would. +- `order-service.camel.yaml` is the old service: `soap-source` with `bodyFormat: json` reads the order id, looks the + order up in `orders.json`, and answers it, or the fault `orderNotFound` that the WSDL declares. + +## Build it step by step + +1. Start with the service: `order-service.camel.yaml`, as in the `soap-facade` example. + `camel generate wsdl orders.wsdl` lists what it offers. +2. A timer route that sends one order id as JSON to `kamelet:soap-action` with `operation: getOrder` and + `bodyFormat: json`, and logs the body. +3. Split a list of order ids and log the customer and the status of each order. +4. Add an order id that the service does not have: the fault fails the message. Add `faultAsBody: true` and log + the fault instead. + +## Try changing + +- Leave out `bodyFormat: json` on `soap-action`: the route then sends and gets the XML of the operation's element, + such as `<getOrder xmlns="http://camel.apache.org/examples/shop"><orderId>ORD-1001</orderId></getOrder>`. +- Require a login on the service and send it from the client: set `wsSecurityUsername: shop` and + `wsSecurityPassword: secret` on both `soap-source` and `soap-action`, and the calls work as before. Give the client + a wrong password, and every call gets the fault + `A security error was encountered when verifying the message`, which the route logs and goes on. +- Point `address` at a port where nothing runs, and see the connection error fail the message. + +## Good to know + +- A route that catches the fault of `soap-action` by its class name, `org.apache.cxf.binding.soap.SoapFault`, + needs the dependency `camel:cxf-soap`. With `faultAsBody: true`, as here, it does not. +- The `soap-source` and `soap-action` Kamelets are new in Camel 4.24, and Preview. + +## Integration testing + +The example comes with a test in the [Citrus](https://citrusframework.org/) YAML DSL, +`test/soap-client.citrus.it.yaml`, which the Camel CLI runs: + +```shell +camel test run test/soap-client.citrus.it.yaml +``` + +The test starts the example and checks the log lines of the orders and of the fault. + +## Help and contributions + +If you hit any problem using Camel or have some feedback, then please +[let us know](https://camel.apache.org/community/support/). + +We also love contributors, so +[get involved](https://camel.apache.org/community/contributing/) :-) + +The Camel riders! diff --git a/contracts/soap-client/application.properties b/contracts/soap-client/application.properties new file mode 100644 index 0000000..5d54c44 --- /dev/null +++ b/contracts/soap-client/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. + +camel.server.port=8080 diff --git a/contracts/soap-client/metadata.json b/contracts/soap-client/metadata.json new file mode 100644 index 0000000..1ea1050 --- /dev/null +++ b/contracts/soap-client/metadata.json @@ -0,0 +1,42 @@ +{ + "title": "SOAP client", + "description": "The shop checks its orders with an old SOAP order service: soap-action calls getOrder from orders.wsdl once for each order with bodyFormat json, and the log shows the status of each order; the order the service does not have gets its fault, which faultAsBody passes to the route instead of failing the message. The order service is a soap-source route in the same example.", + "level": "contracts", + "order": 5, + "teaches": { + "components": [ + "kamelet", + "timer", + "log" + ], + "eips": [ + "split", + "choice", + "setVariable", + "setHeader", + "marshal", + "unmarshal" + ], + "languages": [ + "simple", + "jq", + "groovy", + "tokenize", + "constant" + ], + "dataformats": [ + "json" + ] + }, + "tags": [ + "shop", + "soap", + "wsdl", + "client", + "contract", + "legacy", + "soap-action", + "soap-source" + ], + "bundled": true +} diff --git a/contracts/soap-client/order-service.camel.yaml b/contracts/soap-client/order-service.camel.yaml new file mode 100644 index 0000000..0b6be53 --- /dev/null +++ b/contracts/soap-client/order-service.camel.yaml @@ -0,0 +1,57 @@ +# Stands in for the shop's old order service, so the example needs no SOAP server of its own: soap-source serves +# getOrder from the WSDL, and answers from orders.json, or with the fault orderNotFound for an order it does not have. +- route: + id: order-service + from: + uri: kamelet:soap-source + parameters: + path: /orders + wsdlURL: orders.wsdl + bodyFormat: json + steps: + - setVariable: + name: orderId + expression: + jq: + expression: ".orderId" + resultType: java.lang.String + - setBody: + expression: + constant: + expression: "resource:file:orders.json" + - unmarshal: + json: + library: Jackson + - setBody: + expression: + groovy: + expression: "body.find { it.orderId == variables.orderId }" + - choice: + when: + - expression: + simple: + expression: "${body} == null" + steps: + - setHeader: + name: SoapFault + expression: + simple: + expression: "Order ${variable.orderId} not found" + - setHeader: + name: SoapFaultCode + expression: + constant: + expression: Client + - setBody: + expression: + simple: + expression: '<orderNotFound xmlns="http://camel.apache.org/examples/shop"><orderId>${variable.orderId}</orderId></orderNotFound>' + otherwise: + steps: + - marshal: + json: + library: Jackson + - setBody: + expression: + jq: + expression: "{order: {orderId, customer, country, status, line: .lines}}" diff --git a/contracts/soap-client/order-status.camel.yaml b/contracts/soap-client/order-status.camel.yaml new file mode 100644 index 0000000..4813afe --- /dev/null +++ b/contracts/soap-client/order-status.camel.yaml @@ -0,0 +1,56 @@ +# The shop checks the status of its orders with the old order service. soap-action calls getOrder once for each order: +# with bodyFormat json the route sends {"orderId": "..."} and gets the order back as JSON, and with faultAsBody true a +# fault of the service (an order it does not have) does not fail the message: the route gets the fault and goes on. +- route: + id: order-status + from: + uri: timer + parameters: + timerName: order-status + repeatCount: 1 + delay: 3000 + steps: + - setBody: + expression: + constant: + expression: "ORD-1001,ORD-1002,ORD-1003,ORD-1004" + - split: + expression: + tokenize: + token: "," + steps: + - setVariable: + name: orderId + expression: + simple: + expression: "${body}" + - setBody: + expression: + simple: + expression: '{"orderId": "${variable.orderId}"}' + - to: + uri: kamelet:soap-action + parameters: + address: "http://localhost:{{camel.server.port}}/services/orders" + wsdlURL: orders.wsdl + operation: getOrder + bodyFormat: json + faultAsBody: true + - choice: + when: + - expression: + simple: + expression: "${header.SoapFault} != null" + steps: + - log: + message: "Order ${variable.orderId}: the order service answered with the fault: ${header.SoapFault}" + otherwise: + steps: + - setVariable: + name: order + expression: + jq: + expression: ".order" + resultType: java.util.Map + - log: + message: "Order ${variable.orderId} for customer ${variable.order[customer]} is ${variable.order[status]}, with ${variable.order[line].size()} line(s)" diff --git a/contracts/soap-client/orders.json b/contracts/soap-client/orders.json new file mode 100644 index 0000000..ceea517 --- /dev/null +++ b/contracts/soap-client/orders.json @@ -0,0 +1,5 @@ +[ + {"orderId": "ORD-1001", "customer": "C-482", "country": "DK", "status": "paid", "lines": [{"sku": "CAMEL-TSHIRT", "qty": 2, "price": 19.95}, {"sku": "CAMEL-MUG", "qty": 1, "price": 9.5}]}, + {"orderId": "ORD-1002", "customer": "C-207", "country": "DE", "status": "paid", "lines": [{"sku": "CAMEL-MUG", "qty": 3, "price": 9.5}]}, + {"orderId": "ORD-1003", "customer": "C-134", "country": "US", "status": "pending", "lines": [{"sku": "CAMEL-TSHIRT", "qty": 1, "price": 19.95}, {"sku": "CAMEL-CAP", "qty": 1, "price": 14.0}, {"sku": "CAMEL-MUG", "qty": 2, "price": 9.5}]} +] diff --git a/contracts/soap-client/orders.wsdl b/contracts/soap-client/orders.wsdl new file mode 100644 index 0000000..d6a0928 --- /dev/null +++ b/contracts/soap-client/orders.wsdl @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- The shop's order service, an old SOAP service the shop still has to call: one operation, getOrder, which answers the order with + its lines, or the fault orderNotFound for an order the shop does not have. --> +<wsdl:definitions name="OrderService" + targetNamespace="http://camel.apache.org/examples/shop" + xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" + xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:tns="http://camel.apache.org/examples/shop"> + + <wsdl:types> + <xs:schema targetNamespace="http://camel.apache.org/examples/shop" elementFormDefault="qualified"> + <xs:element name="getOrder"> + <xs:complexType> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="getOrderResponse"> + <xs:complexType> + <xs:sequence> + <xs:element name="order" type="tns:Order"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:complexType name="Order"> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + <xs:element name="customer" type="xs:string"/> + <xs:element name="country" type="xs:string"/> + <xs:element name="status" type="xs:string"/> + <xs:element name="line" type="tns:Line" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + <xs:complexType name="Line"> + <xs:sequence> + <xs:element name="sku" type="xs:string"/> + <xs:element name="qty" type="xs:int"/> + <xs:element name="price" type="xs:decimal"/> + </xs:sequence> + </xs:complexType> + <xs:element name="orderNotFound"> + <xs:complexType> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + </xs:sequence> + </xs:complexType> + </xs:element> + </xs:schema> + </wsdl:types> + + <wsdl:message name="getOrderRequest"> + <wsdl:part name="parameters" element="tns:getOrder"/> + </wsdl:message> + <wsdl:message name="getOrderResponse"> + <wsdl:part name="parameters" element="tns:getOrderResponse"/> + </wsdl:message> + <wsdl:message name="orderNotFound"> + <wsdl:part name="fault" element="tns:orderNotFound"/> + </wsdl:message> + + <wsdl:portType name="OrderPortType"> + <wsdl:operation name="getOrder"> + <wsdl:input message="tns:getOrderRequest"/> + <wsdl:output message="tns:getOrderResponse"/> + <wsdl:fault name="orderNotFound" message="tns:orderNotFound"/> + </wsdl:operation> + </wsdl:portType> + + <wsdl:binding name="OrderBinding" type="tns:OrderPortType"> + <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> + <wsdl:operation name="getOrder"> + <soap:operation soapAction="http://camel.apache.org/examples/shop/getOrder"/> + <wsdl:input> + <soap:body use="literal"/> + </wsdl:input> + <wsdl:output> + <soap:body use="literal"/> + </wsdl:output> + <wsdl:fault name="orderNotFound"> + <soap:fault name="orderNotFound" use="literal"/> + </wsdl:fault> + </wsdl:operation> + </wsdl:binding> + + <wsdl:service name="OrderService"> + <wsdl:port name="OrderPort" binding="tns:OrderBinding"> + <soap:address location="http://localhost:8080/services/orders"/> + </wsdl:port> + </wsdl:service> +</wsdl:definitions> diff --git a/contracts/soap-client/test/soap-client.citrus.it.yaml b/contracts/soap-client/test/soap-client.citrus.it.yaml new file mode 100644 index 0000000..2aa6bc0 --- /dev/null +++ b/contracts/soap-client/test/soap-client.citrus.it.yaml @@ -0,0 +1,30 @@ +name: soap-client-test +description: The order status job calls the old order service with SOAP for each order, and goes on after the fault for an unknown order +actions: + - camel: + cli: + run: + integration: + name: "soap-client" + file: "../order-status.camel.yaml" + systemProperties: + file: "../application.properties" + resources: + - "order-service.camel.yaml" + - "orders.wsdl" + - "orders.json" + - camel: + cli: + verify: + integration: "soap-client" + logMessage: "Order ORD-1001 for customer C-482 is paid, with 2 line(s)" + - camel: + cli: + verify: + integration: "soap-client" + logMessage: "Order ORD-1003 for customer C-134 is pending, with 3 line(s)" + - camel: + cli: + verify: + integration: "soap-client" + logMessage: "Order ORD-1004: the order service answered with the fault: Order ORD-1004 not found" diff --git a/contracts/soap-facade/README.md b/contracts/soap-facade/README.md new file mode 100644 index 0000000..bfe5c34 --- /dev/null +++ b/contracts/soap-facade/README.md @@ -0,0 +1,118 @@ +# SOAP facade + +The shop's order service used to be a SOAP web service, and some old clients still call it. The orders now live +behind a JSON API. This example keeps the old clients working: the `soap-source` Kamelet serves the order service +from its WSDL, `orders.wsdl`, and the route answers each SOAP call from the JSON API. With `bodyFormat: json` the +route gets the request as JSON and answers with JSON, so no XML is mapped by hand. + +## What you will see + +```text +INFO ... soap-facade.camel.yaml:13 : SOAP getOrder: {"orderId":"ORD-1002"} +INFO ... soap-facade.camel.yaml:60 : Order ORD-1002 answered from the order API +INFO ... soap-facade.camel.yaml:13 : SOAP getOrder: {"orderId":"ORD-9999"} +INFO ... soap-facade.camel.yaml:37 : Order ORD-9999 not found: the client gets the fault orderNotFound +``` + +and the answers the old clients get: + +```shell +$ curl -H 'Content-Type: text/xml' -d '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><s:getOrder xmlns:s="http://camel.apache.org/examples/shop"><s:orderId>ORD-1002</s:orderId></s:getOrder></soap:Body></soap:Envelope>' http://localhost:8080/services/orders +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><tns:getOrderResponse xmlns:tns="http://camel.apache.org/examples/shop"><tns:order><tns:orderId>ORD-1002</tns:orderId><tns:customer>C-207</tns:customer><tns:country>DE</tns:country><tns:status>paid</tns:status><tns:line><tns:sku>CAMEL-MUG</tns:sku><tns:qty>3</tns:qty><tns:price>9.5</tns:price></tns:line></tns:order></tns:getOrderResponse></soap:Body></soap:Envelope> + +$ curl -H 'Content-Type: text/xml' -d '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><s:getOrder xmlns:s="http://camel.apache.org/examples/shop"><s:orderId>ORD-9999</s:orderId></s:getOrder></soap:Body></soap:Envelope>' http://localhost:8080/services/orders +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>Order ORD-9999 not found</faultstring><detail><orderNotFound xmlns="http://camel.apache.org/examples/shop"><orderId>ORD-9999</orderId></orderNotFound></detail></soap:Fault></soap:Body></soap:Envelope> +``` + +The fault comes with HTTP status 500, as SOAP 1.1 says. + +## Install Camel CLI + +Install [JBang](https://www.jbang.dev/download/) and the Camel CLI as described in the +[root README](../../README.md#install-the-camel-cli); `camel --version` confirms the install. + +## Run it + +```shell +camel run * +``` + +The service is at `http://localhost:8080/services/orders`, and its WSDL at +`http://localhost:8080/services/orders?wsdl`. The JSON API it calls is in the same example: +`curl http://localhost:8080/api/orders/ORD-1002` answers the same order as JSON. +Stop the example with `ctrl` + `c`. + +## How it works + +- `orders-api.camel.yaml` is the order API of today: `GET /api/orders/{id}` answers an order of `orders.json` as + JSON, or 404 with `{"error": "unknown order <id>"}`. In a real shop it runs somewhere else. +- `soap-facade.camel.yaml` starts with `kamelet:soap-source`. `path: /orders` serves the service under `/services` + on the HTTP server of the integration (`camel.server.port` in `application.properties`), and + `wsdlURL: orders.wsdl` is the contract: the service, its one operation `getOrder`, the XML of the request and the + answer, and the fault `orderNotFound`. No Java classes are generated from the WSDL. +- With `bodyFormat: json` the request arrives as `{"orderId":"ORD-1002"}`: JSON typed by the XML schema of the WSDL. + The route answers with JSON of the same kind, `{"order": {...}}`, and `soap-source` writes the XML of + `getOrderResponse` from it, with the namespace and the element order of the schema. `qty` is a number and + `price` a decimal there, as the schema says, and `line` can repeat, so it is a JSON array. +- `jq` reads the order id from the request into a variable, and maps the answer of the order API to the shape the + WSDL wants: the API calls the lines `lines`, the WSDL calls each one `line`. +- The headers of the SOAP call are removed before the call to the order API, and `httpMethod: GET` makes it a GET: + the body still holds the request, which would make it a POST. +- For a 404 the route answers the fault that the WSDL declares: the header `SoapFault` is its text, `SoapFaultCode` + its code (`Client`: the client asked for something the service does not have), and the body is its detail, the + XML of `orderNotFound`. This also works with `bodyFormat: json`, as the detail is XML text. + +## Build it step by step + +1. Look at the contract: `camel generate wsdl orders.wsdl` lists the service, its port and address, and the + operation `getOrder` with its fault. `camel generate wsdl orders.wsdl --samples` prints a request envelope and + the `curl` command that sends it. +2. A route from `kamelet:soap-source` with `path: /orders` and `wsdlURL: orders.wsdl` that logs the body and answers + with a fixed order as XML. Send the sample request: the answer is the SOAP response. +3. Add `bodyFormat: json`: the log shows the request as JSON, and the route answers with a fixed order as JSON. +4. Add `orders-api.camel.yaml` and call it from the facade with `toD` for the order id of the request, mapping its + answer with `jq`. +5. Answer the fault `orderNotFound` when the order API answers 404. + +## Try changing + +- Require a login: set `wsSecurityUsername` and `wsSecurityPassword` on `soap-source`, and the service accepts only + calls with a WS-Security UsernameToken with that user and password. The `soap-client` example shows how a client + sends one. +- Send a request that the schema does not allow, such as `<s:orderNo>` in place of `<s:orderId>`: the service + answers with the fault `tag name "orderNo" is not allowed. Possible tag names are: <orderId>` before the route + sees it, as `schemaValidation` is on by default. +- Make the route fail, for example by pointing the call at a port where no API runs: the client gets a fault with + the text `Exchange processing failed`, which does not say why, and the log of the integration has the cause. +- Serve SOAP 1.2: give the WSDL a SOAP 1.2 binding (`xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"`) and + `soap-source` serves what the WSDL says. + +## Good to know + +- There is no login unless `wsSecurityUsername` and `wsSecurityPassword` are set. Protect the address with your + network too. +- A request with a DOCTYPE is refused. While `schemaValidation` is on (the default), so is a request or an answer that + does not match the XML schema of the WSDL. +- The `soap-source` and `soap-action` Kamelets are new in Camel 4.24, and Preview. + +## Integration testing + +The example comes with a test in the [Citrus](https://citrusframework.org/) YAML DSL, +`test/soap-facade.citrus.it.yaml`, which the Camel CLI runs: + +```shell +camel test run test/soap-facade.citrus.it.yaml +``` + +The test starts the example, calls `getOrder` for ORD-1002 and for ORD-9999 as an old SOAP client would, and checks +the answer and the fault. + +## Help and contributions + +If you hit any problem using Camel or have some feedback, then please +[let us know](https://camel.apache.org/community/support/). + +We also love contributors, so +[get involved](https://camel.apache.org/community/contributing/) :-) + +The Camel riders! diff --git a/contracts/soap-facade/application.properties b/contracts/soap-facade/application.properties new file mode 100644 index 0000000..5d54c44 --- /dev/null +++ b/contracts/soap-facade/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. + +camel.server.port=8080 diff --git a/contracts/soap-facade/metadata.json b/contracts/soap-facade/metadata.json new file mode 100644 index 0000000..d1fc8ef --- /dev/null +++ b/contracts/soap-facade/metadata.json @@ -0,0 +1,42 @@ +{ + "title": "SOAP facade", + "description": "The shop's old SOAP clients keep calling getOrder: soap-source serves the order service from orders.wsdl at /services/orders on port 8080, and the route answers each call from the shop's JSON order API with bodyFormat json, so no XML is mapped by hand; an unknown order gets the fault orderNotFound that the WSDL declares.", + "level": "contracts", + "order": 4, + "teaches": { + "components": [ + "kamelet", + "rest", + "http", + "direct", + "log" + ], + "eips": [ + "choice", + "setVariable", + "setHeader", + "removeHeaders", + "toD", + "marshal", + "unmarshal" + ], + "languages": [ + "simple", + "jq", + "groovy", + "constant" + ], + "dataformats": [ + "json" + ] + }, + "tags": [ + "shop", + "soap", + "wsdl", + "contract", + "legacy", + "soap-source" + ], + "bundled": true +} diff --git a/contracts/soap-facade/orders-api.camel.yaml b/contracts/soap-facade/orders-api.camel.yaml new file mode 100644 index 0000000..1e4f145 --- /dev/null +++ b/contracts/soap-facade/orders-api.camel.yaml @@ -0,0 +1,48 @@ +# The shop's order API of today: JSON over HTTP. GET /api/orders/{id} answers the order from orders.json, or 404. +- rest: + path: /api/orders + get: + - id: getOrder + path: "/{id}" + description: One order, by its id + to: + uri: direct:get-order + +- route: + id: get-order + from: + uri: direct + parameters: + name: get-order + steps: + - setBody: + expression: + constant: + expression: "resource:file:orders.json" + - unmarshal: + json: + library: Jackson + - setBody: + expression: + groovy: + expression: "body.find { it.orderId == headers.id }" + - choice: + when: + - expression: + simple: + expression: "${body} == null" + steps: + - setHeader: + name: CamelHttpResponseCode + expression: + constant: + expression: "404" + - setBody: + expression: + simple: + expression: '{"error": "unknown order ${header.id}"}' + otherwise: + steps: + - marshal: + json: + library: Jackson diff --git a/contracts/soap-facade/orders.json b/contracts/soap-facade/orders.json new file mode 100644 index 0000000..ceea517 --- /dev/null +++ b/contracts/soap-facade/orders.json @@ -0,0 +1,5 @@ +[ + {"orderId": "ORD-1001", "customer": "C-482", "country": "DK", "status": "paid", "lines": [{"sku": "CAMEL-TSHIRT", "qty": 2, "price": 19.95}, {"sku": "CAMEL-MUG", "qty": 1, "price": 9.5}]}, + {"orderId": "ORD-1002", "customer": "C-207", "country": "DE", "status": "paid", "lines": [{"sku": "CAMEL-MUG", "qty": 3, "price": 9.5}]}, + {"orderId": "ORD-1003", "customer": "C-134", "country": "US", "status": "pending", "lines": [{"sku": "CAMEL-TSHIRT", "qty": 1, "price": 19.95}, {"sku": "CAMEL-CAP", "qty": 1, "price": 14.0}, {"sku": "CAMEL-MUG", "qty": 2, "price": 9.5}]} +] diff --git a/contracts/soap-facade/orders.wsdl b/contracts/soap-facade/orders.wsdl new file mode 100644 index 0000000..b8fbac0 --- /dev/null +++ b/contracts/soap-facade/orders.wsdl @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- The shop's order service, as its old SOAP clients know it: one operation, getOrder, which answers the order with + its lines, or the fault orderNotFound for an order the shop does not have. --> +<wsdl:definitions name="OrderService" + targetNamespace="http://camel.apache.org/examples/shop" + xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" + xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:tns="http://camel.apache.org/examples/shop"> + + <wsdl:types> + <xs:schema targetNamespace="http://camel.apache.org/examples/shop" elementFormDefault="qualified"> + <xs:element name="getOrder"> + <xs:complexType> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:element name="getOrderResponse"> + <xs:complexType> + <xs:sequence> + <xs:element name="order" type="tns:Order"/> + </xs:sequence> + </xs:complexType> + </xs:element> + <xs:complexType name="Order"> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + <xs:element name="customer" type="xs:string"/> + <xs:element name="country" type="xs:string"/> + <xs:element name="status" type="xs:string"/> + <xs:element name="line" type="tns:Line" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + <xs:complexType name="Line"> + <xs:sequence> + <xs:element name="sku" type="xs:string"/> + <xs:element name="qty" type="xs:int"/> + <xs:element name="price" type="xs:decimal"/> + </xs:sequence> + </xs:complexType> + <xs:element name="orderNotFound"> + <xs:complexType> + <xs:sequence> + <xs:element name="orderId" type="xs:string"/> + </xs:sequence> + </xs:complexType> + </xs:element> + </xs:schema> + </wsdl:types> + + <wsdl:message name="getOrderRequest"> + <wsdl:part name="parameters" element="tns:getOrder"/> + </wsdl:message> + <wsdl:message name="getOrderResponse"> + <wsdl:part name="parameters" element="tns:getOrderResponse"/> + </wsdl:message> + <wsdl:message name="orderNotFound"> + <wsdl:part name="fault" element="tns:orderNotFound"/> + </wsdl:message> + + <wsdl:portType name="OrderPortType"> + <wsdl:operation name="getOrder"> + <wsdl:input message="tns:getOrderRequest"/> + <wsdl:output message="tns:getOrderResponse"/> + <wsdl:fault name="orderNotFound" message="tns:orderNotFound"/> + </wsdl:operation> + </wsdl:portType> + + <wsdl:binding name="OrderBinding" type="tns:OrderPortType"> + <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> + <wsdl:operation name="getOrder"> + <soap:operation soapAction="http://camel.apache.org/examples/shop/getOrder"/> + <wsdl:input> + <soap:body use="literal"/> + </wsdl:input> + <wsdl:output> + <soap:body use="literal"/> + </wsdl:output> + <wsdl:fault name="orderNotFound"> + <soap:fault name="orderNotFound" use="literal"/> + </wsdl:fault> + </wsdl:operation> + </wsdl:binding> + + <wsdl:service name="OrderService"> + <wsdl:port name="OrderPort" binding="tns:OrderBinding"> + <soap:address location="http://localhost:8080/services/orders"/> + </wsdl:port> + </wsdl:service> +</wsdl:definitions> diff --git a/contracts/soap-facade/soap-facade.camel.yaml b/contracts/soap-facade/soap-facade.camel.yaml new file mode 100644 index 0000000..0d26cc3 --- /dev/null +++ b/contracts/soap-facade/soap-facade.camel.yaml @@ -0,0 +1,61 @@ +# The shop's old SOAP clients call getOrder as they always did. soap-source serves the order service from its WSDL, +# and the route answers each call from the order API, which speaks JSON. With bodyFormat json the route gets the +# request as JSON and answers with JSON: soap-source turns both into the XML that the WSDL describes. +- route: + id: soap-facade + from: + uri: kamelet:soap-source + parameters: + path: /orders + wsdlURL: orders.wsdl + bodyFormat: json + steps: + - log: + message: "SOAP getOrder: ${body}" + - setVariable: + name: orderId + expression: + jq: + expression: ".orderId" + resultType: java.lang.String + # the headers of the SOAP call do not belong on the call to the order API + - removeHeaders: + pattern: "*" + - toD: + uri: "http://localhost:{{camel.server.port}}/api/orders/${variable.orderId}" + parameters: + # the body still holds the request, which would make the call a POST + httpMethod: GET + throwExceptionOnFailure: false + - choice: + when: + - expression: + simple: + expression: "${header.CamelHttpResponseCode} == 404" + # the fault that the WSDL declares for getOrder: its text and code as headers, its detail as body + steps: + - log: + message: "Order ${variable.orderId} not found: the client gets the fault orderNotFound" + - setHeader: + name: SoapFault + expression: + simple: + expression: "Order ${variable.orderId} not found" + - setHeader: + name: SoapFaultCode + expression: + constant: + expression: Client + - setBody: + expression: + simple: + expression: '<orderNotFound xmlns="http://camel.apache.org/examples/shop"><orderId>${variable.orderId}</orderId></orderNotFound>' + otherwise: + # the answer of getOrder is an order, whose lines are called line in the WSDL + steps: + - setBody: + expression: + jq: + expression: "{order: {orderId, customer, country, status, line: .lines}}" + - log: + message: "Order ${variable.orderId} answered from the order API" diff --git a/contracts/soap-facade/test/soap-facade.citrus.it.yaml b/contracts/soap-facade/test/soap-facade.citrus.it.yaml new file mode 100644 index 0000000..6079356 --- /dev/null +++ b/contracts/soap-facade/test/soap-facade.citrus.it.yaml @@ -0,0 +1,69 @@ +name: soap-facade-test +description: The old SOAP clients get the order from the order API behind the facade, and the declared fault for an unknown order +actions: + - camel: + cli: + run: + integration: + name: "soap-facade" + file: "../soap-facade.camel.yaml" + systemProperties: + file: "../application.properties" + resources: + - "orders-api.camel.yaml" + - "orders.wsdl" + - "orders.json" + - camel: + cli: + verify: + integration: "soap-facade" + logMessage: "(soap-facade) started" + # getOrder as an old SOAP client sends it: the answer is the order of the order API, as the WSDL describes it + - http: + client: "http://localhost:8080" + sendRequest: + POST: + path: "/services/orders" + contentType: "text/xml" + body: + data: '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><s:getOrder xmlns:s="http://camel.apache.org/examples/shop"><s:orderId>ORD-1002</s:orderId></s:getOrder></soap:Body></soap:Envelope>' + - http: + client: "http://localhost:8080" + receiveResponse: + response: + status: 200 + type: "plaintext" + body: + data: '@contains(''<tns:order><tns:orderId>ORD-1002</tns:orderId><tns:customer>C-207</tns:customer><tns:country>DE</tns:country><tns:status>paid</tns:status><tns:line><tns:sku>CAMEL-MUG</tns:sku><tns:qty>3</tns:qty><tns:price>9.5</tns:price></tns:line></tns:order>'')@' + - camel: + cli: + verify: + integration: "soap-facade" + logMessage: 'SOAP getOrder: {"orderId":"ORD-1002"}' + - camel: + cli: + verify: + integration: "soap-facade" + logMessage: "Order ORD-1002 answered from the order API" + # an order the shop does not have: the fault orderNotFound of the WSDL, with its detail + - http: + client: "http://localhost:8080" + sendRequest: + POST: + path: "/services/orders" + contentType: "text/xml" + body: + data: '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><s:getOrder xmlns:s="http://camel.apache.org/examples/shop"><s:orderId>ORD-9999</s:orderId></s:getOrder></soap:Body></soap:Envelope>' + - http: + client: "http://localhost:8080" + receiveResponse: + response: + status: 500 + type: "plaintext" + body: + data: '@contains(''<faultcode>soap:Client</faultcode><faultstring>Order ORD-9999 not found</faultstring><detail><orderNotFound xmlns="http://camel.apache.org/examples/shop"><orderId>ORD-9999</orderId></orderNotFound></detail>'')@' + - camel: + cli: + verify: + integration: "soap-facade" + logMessage: "Order ORD-9999 not found: the client gets the fault orderNotFound" diff --git a/generate-catalog.sh b/generate-catalog.sh index eb8bd24..ca25d3a 100755 --- a/generate-catalog.sh +++ b/generate-catalog.sh @@ -157,7 +157,7 @@ GROUPS = [ ("fail-well", "Fail well", "Retries, a dead letter channel, and a circuit breaker in front of a flaky service, then chaos rules that break the routes on purpose to prove it."), ("connect", "Connect without a service", "Files, an HTTP client and a REST server; everything runs inside the example."), ("connect-service", "Connect to one service", "SQL, JMS, MQTT, Kafka and FTP against a service the Camel CLI starts for you with `camel infra run`."), - ("contracts", "Contracts and security", "An OpenAPI contract served and called, and an API protected by Keycloak."), + ("contracts", "Contracts and security", "An OpenAPI contract served and called, a SOAP service served and called from its WSDL, and an API protected by Keycloak."), ("ai", "AI", "A local model writing text, routes exposed as MCP tools, an agent calling route tools, RAG over documents, PII redaction."), ("cloud", "Cloud", "A cloud service, run locally through LocalStack and switched to the real thing by properties."), ("showcase", "Showcase", "Tooling demos outside the ladder: the TUI, a memory leak, message sizes, log analysis."),
