Wednesday, July 6, 2011

Calling and Posting a Request to SOAP Web services

In earlier article, we have “learnt” to convert the wsdl to java. That article will not only help you to convert the wsdl to java, but also it will create the jar file, out of java file, which can be used in the code which actually does the calling to the web services.
The definition of WSDL which I got from internet while learning this area is:
WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. The operations and messages are described abstractly, and then bound to a concrete network protocol and message format to define an endpoint. Related concrete endpoints are combined into abstract endpoints (services). WSDL is extensible to allow description of endpoints and their messages regardless of what message formats or network protocols are used to communicate, however, the only bindings described in this document describe how to use WSDL in conjunction with SOAP 1.1, HTTP GET/POST, and MIME.
In my area of work, I have web service (and wsdl) hosted in remote location. The below code will call the web services and extract the details.
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.btretail.nibs.webservice.GetNetworkSessionByIP;
import net.btretail.nibs.webservice.GetNetworkSessionByIPDocument;
import net.btretail.nibs.webservice.GetNetworkSessionByIPResponse;
import net.btretail.nibs.webservice.GetNetworkSessionByIPResponseDocument;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.log4j.Category;

import com.project.types.SouthboundStub;
import com.project.types.SouthboundStub.Account;
import com. project.types.SouthboundStub.AccountRequestDocument;
import com.project.types.SouthboundStub.AccountRequestDocument.AccountRequest;
import com.project.types.SouthboundStub.AccountResponseDocument;
import com.project.types.SouthboundStub.RequestHeader;
import com.project.types.SouthboundStub.ResponseHeader;


public class WebServiceUtil {

     
      public static void main(String args[]) {
            WebServiceUtil webServiceUtil = new WebServiceUtil();
            try {
                  webServiceUtil.doAccount();
            } catch (RemoteException e) {
                  e.printStackTrace();
            }
           
      }
     
     
      public void doAccount() throws RemoteException {
            String eIdType = "ID";
            String eId = "1234";
           
           
            String outputid = null;
            String outputResponseCode = null;
            SouthboundStub stub = new SouthboundStub();
            int statusCount = 0;

            /*http authentication; for calling the interfaces which implements basic authentication*/
            HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
            auth.setUsername("username");
            auth.setPassword("password");

            EndpointReference endpointRef = new EndpointReference("http://ipaddress/sample.jws?WSDL");
           
            Options options = new Options();
            options.setTo(endpointRef);
           
            options.setProperty(
                        org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,
                        auth);
            options.setProperty(
                        org.apache.axis2.transport.http.HTTPConstants.HTTP_PROTOCOL_VERSION,
                        org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10);
            options.setProperty(
                        org.apache.axis2.transport.http.HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED,
                        false);
           
            // Set connection timeout in the Client
        options.setProperty(org.apache.axis2.transport.http.HTTPConstants.CONNECTION_TIMEOUT,10000);

            // Set read timeout in the Client - start
            options.setTimeOutInMilliSeconds(10000);
            // Set read timeout in the Client - end

            // Proxy Setting - Start
            System.getProperties().setProperty("http.proxySet","true");
            System.getProperties().setProperty("http.proxyHost","147.151.237.69");
            System.getProperties().setProperty("http.proxyPort","8080");

            // Proxy Setting - Start
           
            //Setting all the option property into stub instance
            stub._getServiceClient().setOptions(options);
           
           
            //Constructing the request - start
           
            AccountRequestDocument accountRequestDocument = AccountRequestDocument.Factory.newInstance();
            AccountRequest accountRequest = AccountRequest.Factory
                        .newInstance();
           
            //Constructing the request - end
           
            RequestHeader requestHeader = requestHeader.Factory.newInstance();
            requestHeader.setCorrelationId(Long.toString(Calendar.getInstance().getTimeInMillis()));
            requestHeader.setAttribute("RID");
           
            accountRequest.setRequestHeader(requestHeader);
            accountRequest.setExternalIdType(eIdType);
            accountRequest.setExternalId(eId);
           
           
            accountRequestDocument
                        .setAccountRequest(accountRequest);
           
            //Constructing the request - end
           
            System.out.println("account Request"+accountRequestDocument);
           
            AccountResponseDocument accountResponseDocument = stub
                        .account(accountRequestDocument);
            AccountResponseDocument.AccountResponse accountResponse = accountResponseDocument.getAccountResponse();
            com.project.sample.AccountResponse accountResponseDetail = accountResponse.getAccountResult();

            System.out.println("Account response "+accountResponseDocument);
            ResponseHeader responseHeader = accountResponseDetail
                        .getResponseHeader();
            System.out.println("Correlation Id :" + responseHeader.getCorrelationId());
            outputResponseCode = responseHeader.getGenReturnCode();
            System.out.println("GenReturnCode :" + outputResponseCode);
            System.out.println("GenReturnText :" + responseHeader.getGenReturnText());
            System.out.println("SourceSystemId :" + responseHeader.getSourceSystemId());
      }

API details can be found in http://axis.apache.org/axis2/java/core/api/index.html

This code will give you a hint how to implement and extract the details from the SOAP web service interfaces. However, the above code is specific to my application which is accessing the interface situated in remote location. You will have to keep your heads around to get it.

1 comment:

Anonymous said...

bro...can i get your project file for upload and download file from ftp server...i have try all the code given but still have an error...if can email me at papan_setereka@yahoo.com...tq

Total Pageviews