8 Replies Latest reply on Jan 27, 2012 4:38 AM by madhucm

    Access REST via ESB

    madhucm

      Hi,

      I am trying to access REST webserive URL via ESB. is there any example on how to access REST based.

      Can it be done using org.jboss.soa.esb.actions.routing.http.HttpRouter ?


      Please provide me example that will really help.

       

      Thanks,

      Madhu CM

        • 1. Access REST via ESB
          scottdawson

          Madhu,

            Yes, you can use org.jboss.soa.esb.actions.routing.http.HttpRouter.

           

            See this thread: http://community.jboss.org/thread/159784 for an example and some additional information.

           

          Regards,

          Scott

          • 2. Access REST via ESB
            madhucm

            Scott,

            I went thought the link which you provided . but the example shows on how to send the string parameter . what if i want to send the XML to the endpoint ?

            I tried with custom action  :

             

            body = msgString.getBytes("UTF8");

            configTree.setAttribute("method", "POST");

            configTree.setAttribute("contentType", "text/xml");

            configTree.setAttribute("endpointUrl",  "http://xxxxx:8080/BHS_webservice_WAR/servlet/rpcrouter?ele="+body);

            myhttp = new HttpRouter(configTree);

            myhttp.process(message);

             

            Is this the corrrect way to send xml to endpoint ? please clarify.

             

            Thanks,

            Madhu CM

            • 3. Access REST via ESB
              scottdawson

              Madhu,

              If the XML is provided by an incoming message and you can send the XML on to the REST service in the body of the POST message, then I don't see why you need a custom action. You can just configure the HttpRouter like this:

              <action name="httprouter" class="org.jboss.soa.esb.actions.routing.http.HttpRouter">

                 <property name="endpointUrl" value="http://www.example.com/myservice"/>

                 <property name="method" value="POST"/>

                 <property name="headers">

                    <header name="Content-Type" value="text/xml"/>

                 </property>

              </action>

               

              Regards,

              Scott

              • 4. Re: Access REST via ESB
                madhucm

                thanks scott ,

                 

                I followed the above method earlier and i was getting below error :

                 

                WARN  [HttpRouter] Received status code '400' on HTTP org.apache.commons.httpclient.methods.PostMethod@1732af9 request to 'http://nxxxxx:9080/abc_webservice_WAR/servlet/rpcrouter'.

                INFO  [STDOUT] [Error 400: Error unmarshalling envelope: Root element of a SOAP message must be: &#39;http://schemas.xmlsoap.org/soap/envelope/:Envelope'.

                 

                 

                 

                Config file :

                 

                 

                <?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">

                <services>

                  <service category="MyCat" description="MyDes" invmScope="GLOBAL" name="MyRest">

                   <listeners>

                    <http-gateway name="MyHttp"/>

                   </listeners>

                 

                   <actions inXsd="schema/A203/203_input.xsd"

                    outXsd="schema/A203/203_output.xsd" validate="false" webservice="true">

                 

                    <action class="org.jboss.soa.esb.actions.SystemPrintln" name="Start">

                     <property name="message" value="This is incoming message"/>

                    </action>

                 

                    <action class="org.jboss.soa.esb.actions.routing.http.HttpRouter" name="httprouter">

                     <property name="method" value="POST"/>

                     <property name="endpointUrl" value="http://xxxx.local:9080/nba_webservice_WAR/servlet/rpcrouter"/>

                     <property name="headers">

                      <header name="Content-Type" value="text/xml"/>

                     </property>

                    </action>

                 

                 

                    <action class="org.jboss.soa.esb.actions.SystemPrintln" name="End">

                     <property name="message" value="Response"/>

                    </action>

                 

                   </actions> 

                  </service>

                </services>

                </jbossesb>

                 

                I am sending soap message for the published URL for above config file.

                 

                I am able to send same soap message/request using SOAPUI tool and getting response.

                 

                Do i need to do any other configurations. please let me know.

                 

                 

                Thanks,

                Madhu CM

                • 5. Re: Access REST via ESB
                  madhucm

                  scott, Got solved above error after adding SOAP header to my incoming request:

                   

                  <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://xml.apache.org/xml-soap/literalxml" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd1="http://webservice.ejb.nba.fsg.csc.com/">

                     <SOAP-ENV:Body>

                  ......

                  .....

                  .....

                  </SOAP-ENV:Body>

                  </SOAP-ENV:Envelope>

                   

                  Thanks,

                  Madhu CM

                  • 6. Re: Access REST via ESB
                    luke.samad

                    I managed this by writing a custom action. I figured it might help others. Thank you guys for the greate starting point.

                     

                            <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);

                        }

                    }

                    • 7. Re: Access REST via ESB
                      nelioini

                      I am new into Jboss ESB and I couldn't find enough documentation on how to access a REST service using ESB. Could you please give me some directions or if you have some simple project to start with?

                       

                      Thanks

                      • 8. Re: Access REST via ESB
                        madhucm

                        You will get solution if you read from top .