-
30. httprouter issues with GET/POST WADL Services and Web Routing
luke.samad Apr 22, 2011 7:00 PM (in response to jackalista)I found this post extremely useful and I managed this by writing a custom action. I figured it might help others. Thank you guys for the great start.
<service category="Case" description="CRUD on docket" invmScope="GLOBAL" invmTransacted="true" name="Docket">
<listeners>
<http-gateway name="httpDocket" payloadAs="STRING" urlPattern="client/docket/*">
<property name="synchronousTimeout" value="150000" />
</http-gateway>
</listeners>
<actions mep="RequestResponse">
<action class="gov.usc.commons.soa.esb.actions.HttpRestProxyAction" name="DocketAction">
<property name="endpointUrl" value="${client.docket.war.url}" />
<property name="logDebug" value="${log.case}" />
</action>
</actions>
</service>
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.actions.BeanConfiguredAction;
import org.jboss.soa.esb.http.HttpHeader;
import org.jboss.soa.esb.http.HttpRequest;
import org.jboss.soa.esb.message.Message;
public class HttpRestProxyAction extends AbstractActionLifecycle implements BeanConfiguredAction {
private static final Logger log = Logger.getLogger(HttpRestProxyAction.class);
private String endpointUrl;
private String logDebug;
private Boolean debug = Boolean.FALSE;
public Message process(Message message) throws ActionProcessingException {
debug = (logDebug != null && logDebug.equalsIgnoreCase("true")) ? Boolean.TRUE : Boolean.FALSE;
HttpRequest request = HttpRequest.getRequest(message);
String outputXml = null;
String inputXml = null;
Object defaultLocation = message.getBody().get();
if (defaultLocation != null && !defaultLocation.getClass().getSimpleName().equals("byte[]")) {
log.info("defaultLocation class type " + defaultLocation.getClass().getSimpleName());
inputXml = (String) defaultLocation;
}
if(debug && inputXml != null){
log.info("#############################");
log.info("Input XML >>>> \n"+inputXml);
log.info("#############################");
}
HashMap<String, String> headers = populateHeaders(request.getHeaders());
HashMap<String, String> params = populateParameters(request.getQueryParams());
if (request.getMethod().equals("GET")) {
if (!headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executeGet(endpointUrl, params, headers);
message.getBody().add(outputXml);
} else if (headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executeGet(endpointUrl, params);
message.getBody().add(outputXml);
} else {
outputXml = HttpClientUtil.executeGet(endpointUrl);
message.getBody().add(outputXml);
}
} else if (request.getMethod().equals("POST")) {
if (!headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executePost(inputXml, endpointUrl, params, headers);
message.getBody().add(outputXml);
} else if (headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executePost(inputXml, endpointUrl, params);
message.getBody().add(outputXml);
} else {
outputXml = HttpClientUtil.executePost(inputXml, endpointUrl);
message.getBody().add(outputXml);
}
} else if (request.getMethod().equals("PUT")) {
if (!headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executePut(inputXml, endpointUrl, params, headers);
message.getBody().add(outputXml);
} else if (headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executePut(inputXml, endpointUrl, params);
message.getBody().add(outputXml);
} else {
outputXml = HttpClientUtil.executePut(inputXml, endpointUrl);
message.getBody().add(outputXml);
}
} else if (request.getMethod().equals("DELETE")) {
if (!headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executeDelete(endpointUrl, params, headers);
message.getBody().add(outputXml);
} else if (headers.isEmpty() && !params.isEmpty()) {
outputXml = HttpClientUtil.executeDelete(endpointUrl, params);
message.getBody().add(outputXml);
} else {
outputXml = HttpClientUtil.executeDelete(endpointUrl);
message.getBody().add(outputXml);
}
}
if(debug && outputXml != null){
log.info("#############################");
log.info("Output XML \n"+outputXml);
log.info("#############################");
}
return message;
}
private HashMap<String, String> populateHeaders(List<HttpHeader> requestHeaders) {
HashMap<String, String> headers = new HashMap<String, String>();
if (requestHeaders != null && !requestHeaders.isEmpty()) {
if (debug) {
log.debug("****************** Request Headers ******************");
}
for (HttpHeader header : requestHeaders) {
if (debug) {
log.info("key: " + header.getName() + " " + "value: " + header.getValue());
}
headers.put(header.getName(), header.getValue());
}
}
return headers;
}
private HashMap<String, String> populateParameters(Map<String, String[]> requestParameters) {
HashMap<String, String> params = new HashMap<String, String>();
if (requestParameters != null && !requestParameters.isEmpty()) {
if (debug) {
log.info("******************Request parameters ******************");
}
for (String s : requestParameters.keySet()) {
String[] value = requestParameters.get(s);
if (debug) {
log.info("key " + s + " " + "value " + value[0]);
}
params.put(s, value[0]);
}
}
return params;
}
public void setEndpointUrl(String endpointUrl) {
this.endpointUrl = endpointUrl;
}
public void setLogDebug(String logDebug) {
this.logDebug = logDebug;
}
}
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
public class HttpClientUtil {
public static final Logger log = Logger.getLogger(HttpClientUtil.class);
public static String executeGet(String url){
log.info("entering HTTP GET with no parameters and no headers");
log.info("URL >> "+url);
String xml = null;
GetMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new GetMethod(url);
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
public static String executeGet(String url, HashMap<String, String> parameters){
log.info("entering HTTP GET with parameters and no headers");
log.info("URL >> "+url);
String xml = null;
GetMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new GetMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
method.setQueryString(params);
}
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
public static String executeGet(String url, HashMap<String, String> parameters, HashMap<String, String> headers ){
log.info("entering HTTP GET with parameters and headers");
log.info("URL >> "+url);
String xml = null;
GetMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new GetMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
method.setQueryString(params);
}
if(headers!=null && headers.size() > 0){
setRequestHeaders(method, headers);
}
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
public static String executePut(String xml, String url){
log.info("entering HTTP PUT with no parameters and no headers");
log.info("URL >> "+url);
String xmlReturn = null;
PutMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PutMethod(url);
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream updateResponse = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(updateResponse ), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executePut(String xml, String url, HashMap<String, String> parameters){
log.info("entering HTTP PUT with parameters and no headers");
log.info("URL >> "+url);
String xmlReturn = null;
PutMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PutMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
logHTTPStatusCode(httpStatusCode);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
InputStream updateResponse = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(updateResponse ), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executePut(String xml, String url, HashMap<String, String> parameters, HashMap<String, String> headers ){
log.info("entering HTTP PUT with parameters and headers");
log.info("URL >> "+url);
String xmlReturn = null;
PutMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PutMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
if(headers!=null && headers.size() > 0){
setRequestHeaders(method, headers);
}
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
logHTTPStatusCode(httpStatusCode);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
InputStream updateResponse = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(updateResponse ), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executePost(String xml, String url){
log.info("entering HTTP POST with no parameters and no headers");
log.info("URL >> "+url);
String xmlReturn = null;
PostMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PostMethod(url);
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executePost(String xml, String url, HashMap<String, String> parameters){
log.info("entering HTTP POST with parameters and no headers");
log.info("URL >> "+url);
String xmlReturn = null;
PostMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PostMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executePost(String xml, String url, HashMap<String, String> parameters, HashMap<String, String> headers ){
log.info("entering HTTP POST with parameters and headers");
log.info("URL >> "+url);
String xmlReturn = null;
PostMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new PostMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
if(headers!=null && headers.size() > 0){
setRequestHeaders(method, headers);
}
method.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF8"));
int httpStatusCode = httpClient.executeMethod(method);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
logHTTPStatusCode(httpStatusCode);
InputStream response = method.getResponseBodyAsStream();
xmlReturn = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xmlReturn;
}
public static String executeDelete(String url){
log.info("entering HTTP DELETE with no parameters and no headers");
log.info("URL >> "+url);
DeleteMethod method = null;
String xml = null;
try{
HttpClient httpClient = new HttpClient();
method = new DeleteMethod(url);
int httpStatusCode = httpClient.executeMethod(method);
logHTTPStatusCode(httpStatusCode);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
public static String executeDelete(String url, HashMap<String, String> parameters){
log.info("entering HTTP DELETE with parameters and no headers");
log.info("URL >> "+url);
String xml = null;
DeleteMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new DeleteMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
int httpStatusCode = httpClient.executeMethod(method);
logHTTPStatusCode(httpStatusCode);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
public static String executeDelete(String url, HashMap<String, String> parameters, HashMap<String, String> headers ){
log.info("entering HTTP DELETE with parameters and headers");
log.info("URL >> "+url);
String xml = null;
DeleteMethod method = null;
try{
HttpClient httpClient = new HttpClient();
method = new DeleteMethod(url);
if(parameters!=null && parameters.size() > 0){
NameValuePair[] params = buildQueryParameters(parameters);
if(params!= null)
method.setQueryString(params);
}
if(headers!=null && headers.size() > 0){
setRequestHeaders(method, headers);
}
int httpStatusCode = httpClient.executeMethod(method);
logHTTPStatusCode(httpStatusCode);
assert (httpStatusCode == 200 || httpStatusCode == 204) ;
InputStream response = method.getResponseBodyAsStream();
xml = new String(IOUtils.toByteArray(response), method.getResponseCharSet());
}catch (Exception e) {
String exception = getExceptionStackTraceAsString(e);
log.error(exception);
}finally{
method.releaseConnection();
}
return xml;
}
private static void setRequestHeaders(HttpMethod method , HashMap<String, String> headers ){
log.info(" ************** headers ************** ");
if(headers != null && headers.size() > 0){
for(String s : headers.keySet()) {
log.info("header="+s+" valkue="+headers.get(s));
method.setRequestHeader(s, headers.get(s));
}
}
}
private static NameValuePair[] buildQueryParameters(HashMap<String, String> parameters ) throws Exception{
log.info(" ************** parameters ************** ");
NameValuePair[] params = null;
if (parameters != null) {
params = new NameValuePair[parameters.size()];
int i = 0;
for (String s : parameters.keySet()) {
log.info("parameter="+s+" value="+parameters.get(s));
params[i] = new NameValuePair(s, URIUtil.encodeQuery(parameters.get(s)));
i++;
}
}
return params;
}
private static String getExceptionStackTraceAsString(Exception exception) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
private static void logHTTPStatusCode(int code ){
log.info("HTTP Status code is >> "+code);
}
}
-
31. Re: httprouter issues with GET/POST WADL Services and Web Routing
manelfera Jun 30, 2011 10:46 AM (in response to luke.samad)Your solution is pretty nice and workfull
I have a question on how to send the rest params for GET, DELETE, and PUT actions.
My RestService has methods with the following specification:
@GET @Path("{id}") @Produces("application/xml") public StreamingOutput getCustomer(@PathParam("id") int id) { .... }
How am I supposed to send the id through the custom http proxy.
The rest service expects the id in the url http://localhost:8080/rest-service/customer/1
If I send the id at the end of the custom url it doesn't work.
http://localhost:8080/esbRestProxy/http/esb-proxy/simple/customers/1
Hope somebody can help!
Cheers!
-
32. Re: httprouter issues with GET/POST WADL Services and Web Routing
luke.samad Jul 7, 2011 3:25 PM (in response to manelfera)Because of the URL pattern (urlPattern="client/docket/*"), this is hard to accomplish. You have to use a different style of URL/Resource styling.
Something like:
http://localhost:8080/esbRestProxy/http/esb-proxy/simple/customers?id=1
@GET
@Produces("application/xml")
public StreamingOutput getCustomer(@QueryParam("id") int id) {
....
}It kinda sucks, it is a limitation of SOA-P. As far as I know, you can not accomplish this with the current version 5.0.2.