2 Replies Latest reply on Mar 24, 2011 11:40 AM by Benson Fung

    How to receive HTTP POST message via HTTP gateway?

    Benson Fung Newbie

      Hi,

       

      I developed a service of HTTP gateway to receive HTTP POST message.  However, it looks like it only can receive HTTP GET message, but no for HTTP POST message.  The following are the source code of service and the testing client and also the xml file.

      MyHTTPACtion.java is the service class of Java.  And the TestClient.java is the client standalone program to generate HTTP POST request to the ESB.  JBOSS-ESB.xml is the descriptor xml for MyHTTPAction.java. 

       

      From the code of MyHTTPAction.java, it can display the HTTP request method successfully, but the query String is null.

       

       

      Please help

       

      package org.esb.test.client;

       

      import java.io.BufferedReader;

      import java.io.InputStreamReader;

      import java.io.OutputStream;

      import java.io.OutputStreamWriter;

      import java.net.HttpURLConnection;

      import java.net.URL;

       

       

      import org.antlr.stringtemplate.test.TestStringTemplate.Connector;

       

      public class TestClient {

       

          /**

           * @param args

           */

          public static void main(String[] args) {

              // TODO Auto-generated method stub

              String data = "\n\nid=<fields>" +

                              "<field>" +

                                  "<name>XXX1</name>" +

                                  "<value>XXX1_value</value>" +

                              "</field>" +

                              "</fields>";

      //        String data = "id=Mary&age=13";

       

              try {

                  URL url = new URL("http://localhost:8080/Test_ESB/http/httptest");

       

                  HttpURLConnection conn = (HttpURLConnection)url.openConnection();

                  conn.setRequestMethod("POST");

                  conn.setUseCaches(false);

                  conn.setDoInput(true);

                  conn.setDoOutput(true);

       

                conn.setRequestProperty("Content-Length", "" +

                         Integer.toString(data.getBytes().length));

                System.out.println("Content-Lengt=" + data.getBytes().length);

                conn.setRequestProperty("Content-Language", "en-US"); 

                OutputStream wr = conn.getOutputStream();

                  System.out.println("Send...");

                  wr.write(data.getBytes());

                  wr.flush();

                  wr.close();

       

                  // Get the response

                  BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                  String line;

                  while ((line = rd.readLine()) != null) {

                      // Process line...

                  }

      //            wr.close();

                  rd.close();

       

       

       

       

              } catch(Exception e) {

                  e.printStackTrace();

              }

       

       

          }

       

      }

       

       

      package org.mytest.esb;

       

      import java.util.Map;

       

      import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;

      import org.jboss.soa.esb.helpers.ConfigTree;

      import org.jboss.soa.esb.http.HttpRequest;

      import org.jboss.soa.esb.actions.ActionProcessingException;

      import org.jboss.soa.esb.message.Message;

       

      public class MyHttpAction extends AbstractActionPipelineProcessor {

          protected ConfigTree _config;

       

          public MyHttpAction(ConfigTree config) {

              _config = config;

          }

       

          public Message process(Message message) throws ActionProcessingException {

              //ADD CUSTOM ACTION CODE HERE

              HttpRequest httpRequestInfo = HttpRequest.getRequest(message);

              String qStr = httpRequestInfo.getQueryString();

              int iLength = httpRequestInfo.getContentLength();

              String method = httpRequestInfo.getMethod();

              System.out.println("QueryString(" + method + ")=" + qStr);

              System.out.println("Received content length=" + iLength);

       

              return message;

          }

      }

       

       

      <?xml version="1.0"?>

      <jbossesb parameterReloadSecs="5"

      xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd">

      <providers>

        <http-provider name="HTTP">

         <http-bus busid="HTTP-1"/>

        </http-provider>

      </providers>

      <services>

        <service category="HttpTest" description="HttpTest" invmScope="GLOBAL" name="HttpTest">

         <listeners>

          <http-gateway name="HTTP Listener" payloadAs="STRING" urlPattern="httptest/*"/>

         </listeners>

         <actions mep="RequestResponse">

          <action class="org.mytest.esb.MyHttpAction" name="MyHttpAction"/>

         </actions>

        </service>

      </services>

      </jbossesb>

        • 1. How to receive HTTP POST message via HTTP gateway?
          Chua Khoon Yong Novice

          hi,

           

          The HTTP post parameter (name, value pair) are in the message body, try this and print out the params to see:

           

                      String params = new String((byte[]) message.getBody().get());

           

           

          Then you can call this routine to decode each http post parameter into a map:

           

                      Map<String, String> paraMap;

                      try {

                          paraMap = getUrlParameters(params);

          ....

          }

           

          :

          :

           

           

              public Map<String, String> getUrlParameters(String url)

                      throws UnsupportedEncodingException {

                  Map<String, String> params = new HashMap<String, String>();

                  if (url.length() > 1) {

                      String query = url;

                      for (String param : query.split("&")) {

                          String pair[] = param.split("=");

                          if (pair.length > 1) {

                              if (pair[1] != null) {

                                  String key = URLDecoder.decode(pair[0], "UTF-8");

                                  String value = URLDecoder.decode(pair[1], "UTF-8");

                                  params.put(key, value);

                              }

                          }

                      }

                  }

                  return params;

              }

           

          Hope this help, i got this idea from somewhere also.

          Best regards.

          • 2. How to receive HTTP POST message via HTTP gateway?
            Benson Fung Newbie

            Thank you very much.  It totally works.