XML over HTTP : Inbound XML/HTTP call to websphere commerce server.

WebSphere commerce server receives inbound XML over HTTP by using the program adapter .
This is the overall flow: taken from info center : 
  1. An external system sends an XML message to WebSphere Commerce over HTTP via a POST request. For example, http://host_name/webapp/wcs/stores/servlet/.
  2. The request is mapped to the Program Adapter.
  3. The Program Adapter passes the XML request to the appropriate message mapper.
  4. The message mapper converts the XML request into a CommandProperty object and passes it back to the Program Adapter.
  5. The Program Adapter prepares the command for execution and passes it to the WebController for execution.
  6. The Program Adapter generates the proper XML response and returns the XML response to the external system that made the request.
lets look into the configuration part for inbound message to work.
So,
Step1)   Configure Program Adapter : Enable program adapter for inbound message , go to wc-server.xml and find this tag ' HttpAdapter deviceFormatTypeId="-10000" and make enable="true", check 'deviceFormatType =XmlHttp' The configuration should similar to the below :

Step2)  configure message mapper :  go to the wc-server.xml and check for MessageMapper tag, this should look similar to the below :
if we are doing customization except then wcs default values, then we need to mention that in the configuration tag in the above mapping.actually we can configure the
ECInboundMessageDtdFiles - the dtd file which we need to validate the inbound xml
   by default this is empty, whatever we define in the xml (XML DOCTYPE 'abc_xyzs ' SYSTEM 'abc_xyzs.dtd ' )
   the inbound message mapper will look for the dtd in the default location of wcs i.e; inside messaging folder.(in wcs 7 - path is [toolkit location]\wc\xml\messaging\)
ECInboundMessageDtdPath - path of the inbound xml message dtd location, if we are defining different    path then default wcs, default is messaging.
ECSystemTemplateFile -  system template file, i.e; sys_template.xml. this is the default template file of wcs, actually this is the file where we are defining the variable which we need from the xml and the command which we are going to call when the xml arrives to wcs. we should leave this blank to take the default values, if we have to do customization to call our command for some specific xml we should then define the same template as sys_template.xml in user_template.xml, which is the ECUserTempateFile.
ECUserTempateFile - by default wcs is giving user_template file to configure the customization. we should leave this blank to take wcs the default template file i.e; user_template file.
isValidating -  if true will validate the xml with the dtd, else will not do validation.

Step 3 )  configure user_template.xml file based on the result.xml


      
Step 4 ) Now you have defined the command, so create the command an get the xpath values as request properties in the command and do you work , and return the xml to the jsp or create the xml in the jsp. to check how the jsp should be configured, please have a look into Device Format Algorithm to put the correct deviceId to hte jsp.
 NOTE -- make sure the jsp has the first line as <?xml version="1.0" encoding="utf-8"?>, so that it  should be a valid xml.

Step 5) Obviously follow the steps to create the controller command and the view from wcs info center and don't forget struts change,CMDREG entry and ACPolicy.so after the command execution the control should go the jsp

Now final you need to do unit test the implementation in local, so for this we can use HttpURLConnection or do ajax xhr post the request and check request and response in developer tools in IE or Chrome or firebug in firefox.

below is simple template :

 public static String excutePost(String targetURL, String urlParameters)
  {
    URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
     //------//
      set connection properties here 
     //------//
      //Send request
      DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      //Get Response	
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      return response.toString();
    } catch (Exception e) {
       e.printStackTrace();
      return null;
    } finally {
       if(connection != null) {
        connection.disconnect(); 
      }
    }
  }   

3 comments:

  1. Another way to test it:

    String pathXML = "C:\\myXML.xml";
    String miUrl = "http://localhost/webapp/wcs/stores/servlet/";

    String xml = readEntireFile(pathXML);

    RequestEntity req = new StringRequestEntity(xml, "text/xml", "UTF-8");
    PostMethod postMethod = new PostMethod(miUrl);
    postMethod.setRequestEntity(req);


    // execute the POST
    HttpClient client = new HttpClient();

    int status = client.executeMethod(postMethod);
    String response = postMethod.getResponseBodyAsString();
    System.out.println(response);

    ReplyDelete
  2. dosen't the input xml needs to pass the login credentials?

    ReplyDelete
  3. End point URL - http://localhost/webapp/wcs/stores/servlet/ is not working for us. It is throwing 404 error.

    Is there anything specific we need to do ?

    Thanks

    ReplyDelete