0 Replies Latest reply: Jan 23, 2013 7:53 AM by Ajay Sharma RSS

    Calling Web service from Java Concurrent Program

    Ajay Sharma
      Hi,

      I created a Java concurrent program and created executable. Here is my code.
          public void runProgram(CpContext ctx) {
              
              String value = "Java Concurrent Program Testing";
              Hello hell = new Hello();
              String returnValue = hell.testURL(value); 
              if(returnValue.equalsIgnoreCase("TRUE")){
                  
                  ctx.getLogFile().writeln("-- Java Concurrent Program Testing --", 0);
                  ctx.getOutFile().writeln("-- Java Concurrent Program Testing --");
                  
                  ctx.getReqCompletion().setCompletion(ReqCompletion.NORMAL, "");
              
              }
              else{
              
                  ctx.getLogFile().writeln("-- Hello World! --", 0);
                  ctx.getOutFile().writeln("-- Hello World! --");
                  
                  ctx.getReqCompletion().setCompletion(ReqCompletion.NORMAL, "");
              
              }
                  
          }
      testURL() call the web service and get the response. but when I am selecting View Output option I am always geting out put as
      -- Hello World! --
      I tested the logic of calling web service. It giving me out put "true". Here is my Web service calling code
          public String testURL(String Value){
              
              HttpURLConnection httpConn = null;
              ByteArrayOutputStream bout = null;
              String setWebServiceURLResponse = "";
              String responseString="";
              String outputString = "";
              
              try{
              
                  httpConn = getHttpConnection();
                  bout = new ByteArrayOutputStream();
                  String xmlInput = soapBodyStart +
                  "<ns1:getTestURL>\n" + 
                  "            <ns1:URL>"+Value+"</ns1:URL>\n" + 
                  "        </ns1:getTestURL>"+
                  soapBodyEnd;
                  
                  byte[] buffer = new byte[xmlInput.length()];
                  buffer = xmlInput.getBytes();
                  bout.write(buffer);
                  byte[] b = bout.toByteArray();
                  httpConn = setHttpConnectionRequest(b,httpConn);
                  //Read the response.
                  InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
                  BufferedReader in = new BufferedReader(isr);
                  
                  //Write the SOAP message response to a String.
                  while ((responseString = in.readLine()) != null) {
                      outputString = outputString + responseString;
                  }
                  //Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
                  Document document = parseXmlFile(outputString);
                  String formattedSOAPResponse = formatXML(outputString);
                  System.out.println("Formatted response = \n" +formattedSOAPResponse);
                  
                  //NodeList nodes = document.getElementsByTagName("setWebServiceURLResponse");
                  //NodeList nodes = document.getElementsByTagName("getTestURLResponse");
                   NodeList nodes = document.getElementsByTagName("ns0:getTestURLResponse");
                  int len = nodes.getLength();
                  
                  System.out.println("Inside testURL Node Lenght  = "+ len);
                  
                  for(int s=0; s<nodes.getLength() ; s++){
      
      
                      Node authenticateResultNode = nodes.item(s);
                      if(authenticateResultNode.getNodeType() == Node.ELEMENT_NODE){
      
      
                          Element authenticateResultElement = (Element)authenticateResultNode;
      
                          //-------
                          //NodeList authenticateResultValueNode = authenticateResultElement.getElementsByTagName("ns0:return");
                          //NodeList authenticateResultValueNode = authenticateResultElement.getElementsByTagName("return");
                           NodeList authenticateResultValueNode = authenticateResultElement.getElementsByTagName("ns0:return");
                          Element authenticateResultValue = (Element)authenticateResultValueNode.item(0);
      
                          NodeList textFNList = authenticateResultValue.getChildNodes();
                          //System.out.println("Authenticate Result : " + ((Node)textFNList.item(0)).getNodeValue().trim());
                           setWebServiceURLResponse = ((Node)textFNList.item(0)).getNodeValue();
                           //System.out.println("Authenticate Response in getAuthenticate method : " + authenticateresponse);
      
                      }//end of if clause
      
      
                  }//end of for loop with s var
                  
                  System.out.println("Inside setWebServcieURLToFile response = " + setWebServiceURLResponse);
                  
                  
              
              }
              catch(Exception e){
              
                  e.printStackTrace();
              }
              
              return setWebServiceURLResponse;
              
          }
      Where I am going wrong ?

      Regards,
      Ajay Sharma