How to receive HTTP POST message via HTTP gateway?
bensonfungjava Mar 15, 2011 5:34 AMHi,
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>
-
jboss-esb.xml 907 bytes
-
TestClient.java.zip 874 bytes
-
MyHttpAction.java.zip 553 bytes