docs for http

INT-590
This commit is contained in:
Jonas Partner
2009-03-24 14:27:22 +00:00
parent 157124882b
commit dd78f1de0a
2 changed files with 115 additions and 2 deletions

View File

@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<chapter id="http">
<title>Http Support</title>
<section id="http-intro">
<title>Introduction</title>
<para>
The Http support allows for the making of Http requests and the processing of inbound Http requests. Because HTTP is always
request repsonse even if all that is returned is a 200 status code the Http support consists of two gateway implementations
<classname>HttpInboundEndpoint</classname> and <classname>HttpOutboundEndpoint</classname>.
</para>
</section>
<section id="http-inbound">
<title>Http Inbound Gateway</title>
<para>
To receive messages over http you need to use an <classname>HttpInboundGateway</classname>. In common with the HttpInvoker
support the Http Inbound Gateway needs to be deployed within a servlet container. The easiest way to do this is to provide a servlet
definition in <emphasis>web.xml</emphasis>, see
<xref linkend="httpinvoker-inbound"/> for further details. Below is an example bean defintion for a simple <classname>HttpInboundEndpoint</classname>
<programlisting language="xml"><![CDATA[<bean id="httpInbound" class="org.springframework.integration.http.HttpInboundEndpoint">
<property name="requestChannel" ref="httpRequestChannel" />
<property name="replyChannel" ref="httpReplyChannel" />
</bean>]]></programlisting>
The <classname>HttpInboundGateway</classname> accepts an instance of <interfacename>InboundRequestMapper</interfacename> which allows
customisation of the mapping from <interfacename>HttpServletRequest</interfacename> to <interfacename>Message</interfacename>. If none is
provided the an instance of <classname>DefaultInboundRequestMapper</classname> will be used. This encapsualtes a simple strategy, which for
example will create a String message for a <emphasis>POST</emphasis> request where the content type starts with "text", see the Javadoc for
full details.
</para>
<para>
In sending a response to the client there are a number of ways to customise the behaviour of the gateway. By default the gateway will
simply acknowledge that the request was received by sending a 200 status code back. It is possible to customise this response by providing an
implementation of the Spring MVC <interfacename>View</interfacename> which will be invoked with the created <interfacename>Message</interfacename>.
In the case that the gateway should expect a reply to the <interfacename>Message</interfacename> then setting the expectReply flag will cause
the gateway to wait for a reponse <interfacename>Message</interfacename> before creating a Http response. Below is an example of a gateway
configured to use a custom view and to wait for a response. It also shows how to customise the Http methods accepted by the gateway, which
are <emphasis>POST</emphasis> and <emphasis>GET</emphasis> by default.
<programlisting language="xml"><![CDATA[<bean id="httpInbound" class="org.springframework.integration.http.HttpInboundEndpoint">
<property name="requestChannel" ref="httpRequestChannel" />
<property name="replyChannel" ref="httpReplyChannel" />
<property name="view" ref="jsonView" />
<property name="supportedMethods" >
<list>
<value>GET</value>
<value>DELETE</value>
</list>
</property>
<property name="expectReply" value="true" />
<property name="requestMapper" ref="customRequestMapper" />
</bean>]]></programlisting>
</para>
</section>
<section id="http-outbound">
<title>Http Outbound Gateway</title>
<para>
To configure the <classname>HttpOutboundGateway</classname> write a bean definition like this:
<programlisting language="xml"><![CDATA[<bean id="httpOutbound" class="org.springframework.integration.http.HttpOutboundEndpoint" >
<property name="outputChannel" ref="responseChannel" />
</bean>]]></programlisting>
This bean definition will execute Http requests by first converting the message to the Http request using an instance of
<classname>DefaultOutboundRequestMapper</classname>. This will expect to find the request URL in the message header under
the key <emphasis>HttpHeaders.REQUEST_URL</emphasis>. It is also possilbe to set a default target URL as a constructor argument
along with other options as shown below.
<programlisting language="xml"><![CDATA[<bean id="httpOutbound" class="org.springframework.integration.http.HttpOutboundEndpoint" >
<constructor-arg value="http://localhost:8080/example" />
<property name="outputChannel" ref="responseChannel" />
<property name="sendTimeout" value="5000" />
<property name="requestMapper" ref="customRequestMapper" />
</bean>]]></programlisting>
By default the Http request will be made using an instance of <classname>SimpleHttpRequestExecutor</classname> which uses the JDK
<classname>HttpURLConnection</classname>. Use of the Apache Commons Http Client is also supported through the provided
<classname>CommonsHttpRequestExecutor</classname> which can be injected into the outbound gateway.
</para>
</section>
<section id="http-namespace">
<title>Http Namespace Support</title>
<para>
Spring Integration provides an "http" namespace and schema definition. To include it in your
configuration, simply provide the following URI within a namespace declaration:
'http://www.springframework.org/schema/integration/http'. The schema location should then map to
'http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd'.
</para>
<para>
To configure an inbound http channel adapter which is an instance of <classname>HttpInboundEndpoint</classname> configured
not to expect a response.
<programlisting language="xml"><![CDATA[<http:inbound-channel-adapter id="httpChannelAdapter " channel="requests" supported-methods="PUT, DELETE"/>]]></programlisting>
</para>
<para>
To configure an inbound http gateway which expects a response.
<programlisting language="xml"><![CDATA[<http:inbound-gateway id="inboundGateway" request-channel="requests"
reply-channel="responses" />]]></programlisting>
</para>
<para>
To configure the outbound gateway you can use the namespace support as well. The following code snippet shows the different configuration options for an outbound Http gateway.
<programlisting language="xml"><![CDATA[<http:outbound-gateway id="fullConfigWithoutMapper"
request-channel="requests"
default-url="http://localhost/test"
extract-request-payload="false"
charset="UTF-8"
request-executor="executor"
request-timeout="1234"
reply-channel="replies"/>]]></programlisting>
</para>
</section>
</chapter>