## Endpoint adapter

Endpoint adapter help to customize the behavior of a Citrus server such as HTTP or SOAP web servers. As the servers get started with the Citrus context they are ready to receive incoming client requests. Now there are different ways to process these incoming requests and to provide a proper response message. By default the server will forward the incoming request to a in memory message channel where a test can receive the message and provide a synchronous response. This message channel handling is done automatically behind the scenes so the tester does not care about these things. The tester just uses the server directly as endpoint reference in the test case. This is the default behaviour. In addition to that you can define custom endpoint adapters on the Citrus server in order to change this default behavior.

You set the custom endpoint adapter directly on the server configuration as follows:

    <citrus-http:server id="helloHttpServer"
        port="8080"
        auto-start="true"
        endpoint-adapter="emptyResponseEndpointAdapter"
        resource-base="src/it/resources"/>

        <citrus:empty-response-adapter id="emptyResponseEndpointAdapter"/>

Now let us have a closer look at the provided endpoint adapter implementations.

### Empty response endpoint adapter

This is the simplest endpoint adapter you can think of. It simply provides an empty success response using the HTTP response code 200 . The adapter does not need any configurations or properties as it simply responds with an empty HTTP response.

    <citrus:empty-response-adapter id="emptyResponseEndpointAdapter"/>

### Static response endpoint adapter

The next more complex endpoint adapter will always return a static response message.

    <citrus:static-response-adapter id="endpointAdapter">
        <citrus:payload>
            <![CDATA[
              <HelloResponse
                xmlns="http://www.consol.de/schemas/samples/sayHello.xsd">
                  <MessageId>123456789</MessageId>
                  <CorrelationId>Cx1x123456789</CorrelationId>
                  <Text>Hello User</Text>
              </HelloResponse>
            ]]>
        </citrus:payload>
        <citrus:header>
            <citrus:element name="{http://www.consol.de/schemas/samples}h1:Operation"
                      value="sayHello"/>
            <citrus:element name="{http://www.consol.de/schemas/samples}h1:MessageId"
                      value="123456789"/>
        </citrus:header>
     </citrus:static-response-adapter>

The endpoint adapter is configured with a static message payload and static response header values. The response to the client is therefore always the same. You can add dynamic values by using Citrus functions such as randomString or randomNumber. Also we are able to use values of the actual request message that has triggered the response adapter. The request is available via the local message store. In combination with Xpath or JsonPath functions we can map values from the actual request.

    <citrus:static-response-adapter id="endpointAdapter">
        <citrus:payload>
            <![CDATA[
              <HelloResponse
                xmlns="http://www.consol.de/schemas/samples/sayHello.xsd">
                  <MessageId>citrus:randomNumber(10)</MessageId>
                  <CorrelationId>citrus:xpath(citrus:message(request.payload()), '/hello:HelloRequest/hello:CorrelationId')</CorrelationId>
                  <Text>Hello User</Text>
              </HelloResponse>
            ]]>
        </citrus:payload>
        <citrus:header>
            <citrus:element name="{http://www.consol.de/schemas/samples}h1:Operation"
                      value="sayHello"/>
            <citrus:element name="{http://www.consol.de/schemas/samples}h1:MessageId"
                      value="citrus:randomNumber(10)"/>
        </citrus:header>
     </citrus:static-response-adapter>

The example above maps the CorrelationId of the HelloRequest message to the response with Xpath function. The local message store automatically has the message named request stored so we can access the payload with this message name.

Note XML is namespace specific so we need to use the namespace prefix hello in the Xpath expression. The namespace prefix should evaluate to a global namespace entry in the global Citrus xpath-namespace.

### Request dispatching endpoint adapter

The idea behind the request dispatching endpoint adapter is that the incoming requests are dispatched to several other endpoint adapters. The decision which endpoint adapter should handle the actual request is done depending on some adapter mapping. The mapping is done based on the payload or header data of the incoming request. A mapping strategy evaluates a mapping key using the incoming request. You can think of an XPath expression that evaluates to the mapping key for instance. The endpoint adapter that maps to the mapping key is then called to handle the request.

So the request dispatching endpoint adapter is able to dynamically call several other endpoint adapters based on the incoming request message at runtime. This is very powerful. The next example uses the request dispatching endpoint adapter with a XPath mapping key extractor.

    <citrus:dispatching-endpoint-adapter id="dispatchingEndpointAdapter"
             mapping-key-extractor="mappingKeyExtractor"
             mapping-strategy="mappingStrategy"/>

    <bean id="mappingStrategy"
      class="com.consol.citrus.endpoint.adapter.mapping.SimpleMappingStrategy">
        <property name="adapterMappings">
          <map>
              <entry key="sayHello" ref="helloEndpointAdapter"/>
          </map>
        </property>
    </bean>

    <bean id="mappingKeyExtractor"
      class="com.consol.citrus.endpoint.adapter.mapping.XPathPayloadMappingKeyExtractor">
        <property name="xpathExpression" value="//TestMessage/Operation/*"/>
    </bean>

    <citrus:static-response-adapter id="helloEndpointAdapter">
        <citrus:payload>
            <![CDATA[
                <HelloResponse
                    xmlns="http://www.consol.de/schemas/samples/sayHello.xsd">
                    <MessageId>123456789</MessageId>
                    <Text>Hello User</Text>
                </HelloResponse>
            ]]>
        </citrus:payload>
    </citrus:static-response-adapter>

The XPath mapping key extractor expression decides for each request which mapping key to use in order to find a proper endpoint adapter through the mapping strategy. The endpoint adapters available in the application context are mapped via their bean id. For instance an incoming request with a matching element //TestMessage/Operation/sayHello would be handled by the endpoint adapter bean that is registered in the mapping strategy as "sayHello" key. The available endpoint adapters are configured in the same Spring application context.

Citrus provides several default mapping key extractor implementations.

- HeaderMappingKeyExtractor : Reads a special header entry and uses its value as mapping key

- SoapActionMappingKeyExtractor : Uses the soap action header entry as mapping key

- XPathPayloadMappingKeyExtractor : Evaluates a XPath expression on the request payload and uses the result as mapping key

In addition to that we need a mapping strategy. Citrus provides following default implementations.

- SimpleMappingStrategy : Simple key value map with endpoint adapter references

- BeanNameMappingStrategy : Loads the endpoint adapter Spring bean with the given id matching the mapping key

- ContextLoadingMappingStrategy : Same as BeanNameMappingStrategy but loads a separate application context defined by external file resource

### Channel endpoint adapter

The channel connecting endpoint adapter is the default adapter used in all Citrus server components. Indeed this adapter also provides the most flexibility. This adapter forwards incoming requests to a channel destination. The adapter is waiting for a proper response on a reply destination synchronously. With the channel endpoint components you can read the requests on the channel and provide a proper response on the reply destination.

    <citrus:channel-endpoint-adapter id="channelEndpointAdapter"
                  channel-name="inbound.channel"
                  timeout="2500"/>

### JMS endpoint adapter

Another powerful endpoint adapter is the JMS connecting adapter implementation. This adapter forwards incoming requests to a JMS destination and waits for a proper response on a reply destination. A JMS endpoint can access the requests internally and provide a proper response on the reply destination. So this adapter is very flexible to provide proper response messages.

This special adapter comes with the citrus-jms module. So you have to add the module and the special XML namespace for this module to your configuration files. The Maven module for citrus-jms goes to the Maven POM file as normal project dependency. The citrus-jms namespace goes to the Spring bean XML configuration file as follows:

Note Citrus provides a "citrus-jms" configuration namespace and schema definition for JMS related components and features. Include this namespace into your Spring configuration in order to use the Citrus JMS configuration elements. The namespace URI and schema location are added to the Spring configuration XML file as follows.

    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:citrus-jms="http://www.citrusframework.org/schema/jms/config"
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.citrusframework.org/schema/jms/config
            http://www.citrusframework.org/schema/jms/config/citrus-jms-config.xsd">

        [...]

    </beans>

After that you are able to use the adapter implementation in the Spring bean configuration.

    <citrus-jms:endpoint-adapter id="jmsEndpointAdapter"
                  destination-name="JMS.Queue.Requests.In"
                  reply-destination-name="JMS.Queue.Response.Out"
                  connection-factory="jmsConnectionFactory"
                  timeout="2500"/>

    <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
          <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>