Time for action – using the HTTP gateway

We will now run the Chapter6 sample application that demonstrates the use of the HTTP gateway:

  1. In JBoss Developer Studio, open the jboss-esb.xml file in Source mode.
  2. Append the following listener definition to the <listeners> tag.
    <listeners>
      <jms-listener busidref="chapter6ESBChannel"
                    name="Chapter6ESBListener"/>
      <jms-listener busidref="chapter6GwChannel" is-gateway="true"
                    name="Chapter6GwListener"/>
      <http-gateway busidref="chapter6HttpGateway"
                    name="Chapter6HttpGwListener"/>
    </listeners>
  3. Remove the following action:
    <action name="notificationAction"
            class="org.jboss.soa.esb.actions.Notifier">
      <property name="okMethod" value="notifyOK"/>
      <property name="notification-details"> 
        <NotificationList type="ok">
          <target class="NotifyFiles">
            <file append="false" URI="${java.io.tmpdir}/results.log"/>
          </target>
        </NotificationList>
      </property>
    </action>
  4. Click the Save button and the modified application should now be deployed in the server.
  5. Select the src folder and expand it till the SendHttpMessage.java file is displayed in the tree. Now click Run | Run As | Java Application.

    The console will display the following:

    Http Gateway says Hello!
    

What just happened?

We sent a message via the HTTP gateway listener and received a synchronous response, on the same connection which transferred the request.

Here is the listing of the client code for reference:

public static void main(String[] args) throws Exception {
    String serverURL = "http://localhost:8080/Chapter6/http/Chapter6Sample/Chapter6Service";
    HttpURLConnection connection = (HttpURLConnection)new URL(serverURL).openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.connect();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
    out.println("Hello Http Gateway!");
    out.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
    }
    in.close();
}

Have a go hero – using asynchronous behavior

Go ahead and modify the listener to add asynchronous behavior. Are you able to send a GET request now? What status code does it return by default?

The HTTP bus and HTTP provider

Aspects of the HTTP configuration can be shared between multiple gateways, specified through the <http-provider> or <http-bus> elements.

The <http-provider> element exposes the shared configuration of the HTTP exception mappings. Using the <exception> element specifies the translations from exceptions into the appropriate HTTP response.

The <http-bus> element exposes the shared configuration of the HTTP methods that are supported using the <protected-methods> element, the security roles required to access the endpoint using the <allowed-roles> element, and any requirement to secure the HTTP transport using the transportGuarantee attribute.

A typical HTTP provider configuration would look like this:

<http-provider name="http">
  <http-bus busid="chapter6HttpGateway"
            transportGuarantee="CONFIDENTIAL">
    <allowed-roles>
      <role name="friend">
    </allowed-roles>
    <protected-methods>
      <method name="GET">
      <method name="PUT">
      <method name="POST">
      <method name="DELETE">
    </protected-methods>
  </http-bus>
  <exception mappingsFile="/http-exception-mappings.properties"/>
</http-provider>

Note that the configuration stipulates a requirement to use a CONFIDENTIAL transport, in other words SSL, and requires the user to authenticate using credentials that include the friend role.

The exception element can also be configured directly as shown in the following:

<exception>
  <mapping class="com.acme.AcmeException" status="503"/>
</exception>

Note

Details from the original request are stored within the ESB message, represented by the HttpRequest class, and can be retrieved using the convenience method org.jboss.soa.esb.http.HttpRequest.getRequest(). This class provides access to the properties which describe the incoming HTTP request.

Have a go hero – configuring the HTTP provider

Go ahead and secure the http-provider in our sample. Try using exception mapping with HTTP status codes both globally in the http-provider and also in http-gateway.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset