Monday, November 10, 2014

A Simple HTTP client to retrieve response status code for Wso2 ESB

 int responseStatus = 0;
       
        String strSoapAction = "getQuote";
        // Get file to be posted
        String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator +
                                "ESB" + File.separator + "mediatorconfig/property/MyRequest.xml";

        File input = new File(strXMLFilename);

        PostMethod post = new PostMethod(getProxyServiceURLHttp("Axis2ProxyService"));
        // Request content will be retrieved directly
        // from the input stream
        RequestEntity entity = new FileRequestEntity(input, "text/xml");
        post.setRequestEntity(entity);
         post.setRequestHeader("SOAPAction", strSoapAction);
        HttpClient httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(post);

        } finally {
            post.releaseConnection();
        }

----
MyRequest.xml file

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getQuote>
         <!--Optional:-->
         <ser:request>
            <!--Optional:-->
            <xsd:symbol>WSO2</xsd:symbol>
         </ser:request>
      </ser:getQuote>
   </soapenv:Body>
</soapenv:Envelope>


A Simple HTTPClient that you can retrieve both status code and the response payload (Lets imagine your api url is "http://yourip:8280/Transform")

   SimpleHttpClient httpClient = new SimpleHttpClient();
      
    HttpResponse httpResponse = httpClient.doPost("http://yourip:8280/Transform", null, xmlPayload, "application/xml");

    String responsePayload = httpClient.getResponsePayload(httpResponse);

A Simple HTTP-Client to retrieve JSON Array responses correctly 

Notes: Lets imagine you have created an API named "Transform" . In order to retrieve the following JSON array. 

URL url = new URL(getApiInvocationURL("Transform"));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/xml");

        String input = " ----- Your XML Array goes array";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

      assertTrue(conn.getResponseCode() == HttpURLConnection.HTTP_OK,
                   "Response Code Mismatch. Expected 200 : Recived " + conn.getResponseCode());

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));
String response = br.readLine();

        assertTrue(response.contains("{ \"StockQuotes\": { \"Stock\":"), "Response is not in JSON");
        assertTrue(response.contains("IBM"), "Response does not contain Second JSON array element");
        assertTrue(response.contains("WSO2"), "Response does not contain first JSON array element");

No comments:

Post a Comment