From YiPs Wiki (i powered)

XMLService: XMLSERVICE Java

(click to open)

Quick Page Table of Contents

Scanning…

Java to XMLSERVICE …

Goto Main Page
Goto Documents

Who is this page for?

Instructions designed for IBM i developer learning Java to XMLSERVICE starting from ground zero …

IBM i (verified using J9, not classic Java):

call qp2term
$ javac javaXMLserviceDemoJDBC.java 
$ java javaXMLserviceDemoJDBC

$ javac javaXMLserviceDemoREST.java 
$ java javaXMLserviceDemoREST

Java calling by JDBC to XMLSERVICE (fast interface)

Example program (javaXMLserviceDemoJDBC.java) …

import java.io.*;
import java.util.*;
import java.sql.*;
import java.math.*;

public class javaXMLserviceDemoJDBC {
public static void main(String[] args)   {
  Connection conn = null;
  Statement stmt=null; 
  CallableStatement cstmt = null ;
  PreparedStatement pstmt = null;
  String url = "jdbc:db2://localhost"; // Set URL for data source
  String user = "MYUID";
  String password = "MYPWD";
  try
  { // Load the DB2(R) Universal JDBC Driver with DriverManager
    Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
    conn = DriverManager.getConnection(url, user, password); 
    String inputClob = 
        "<?xml version='1.0'?>"
      + " <script>"
      + " <pgm name='ZZCALL' lib='XMLSERVICE'>"
      + " <parm><data type='1A'>a</data></parm>"
      + " <parm><data type='1A'>b</data></parm>"
      + " <parm><data type='7p4'>11.1111</data></parm>"
      + " <parm><data type='12p2'>222.22</data></parm>"
      + " <parm>"
      + " <ds>"
      + " <data type='1A'>x</data>"
      + " <data type='1A'>y</data>"
      + " <data type='7p4'>66.6666</data>"
      + " <data type='12p2'>77777.77</data>"
      + " </ds>"
      + " </parm>"
      + " <return><data type='10i0'>0</data></return>"
      + " </pgm>"
      + " </script>";
    String sql="CALL XMLSERVICE.iPLUG512K(?,?,?,?)";
    cstmt = conn.prepareCall(sql);
    System.out.println("Calling with valid name"); 
    cstmt.setString(1,"/tmp/packers01");
    cstmt.setString(2,"*sbmjob");
    cstmt.setString(3,inputClob);
    cstmt.registerOutParameter(4, Types.CLOB);
    cstmt.execute();
    String doc = cstmt.getString(4);
    System.out.println("****** Documento XML: **********");
    System.out.println(doc);
  } 
  catch (Exception e) 
  { System.out.println("******* Eccezione !!! *********");
    e.printStackTrace();
  } 
}
}

Java calling by REST to XMLSERVICE (flexible interface)

Example program (javaXMLserviceDemo.java) …

import java.net.*;
import java.io.*;
public class javaXMLserviceDemoREST {
  public static void main(String[] args) 
  { try 
    { // The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. 
      // It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, 
      // and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, 
      // it does not recognise equivalence between the encoded or decoded form of the same URL
      String inputURL = 
          "http://174.79.32.155/cgi-bin/xmlcgi.pgm?" 
        + java.net.URLEncoder.encode(
          "db2=*LOCAL"
        + "&uid=*NONE"
        + "&pwd=*NONE"
        + "&ipc=/tmp/rangerhtmlonly"
        + "&ctl=*sbmjob"
        + "&xmlin="
        + "<?xml version='1.0'?>"
        + " <script>"
        + " <pgm name='ZZCALL' lib='XMLSERVICE'>"
        + " <parm><data type='1A'>a</data></parm>"
        + " <parm><data type='1A'>b</data></parm>"
        + " <parm><data type='7p4'>11.1111</data></parm>"
        + " <parm><data type='12p2'>222.22</data></parm>"
        + " <parm>"
        + " <ds>"
        + " <data type='1A'>x</data>"
        + " <data type='1A'>y</data>"
        + " <data type='7p4'>66.6666</data>"
        + " <data type='12p2'>77777.77</data>"
        + " </ds>"
        + " </parm>"
        + " <return><data type='10i0'>0</data></return>"
        + " </pgm>"
        + " </script>"
        + "&xmlout=32768", 
          "ISO-8859-1");
      // make REST call to XMLSERVICE
      URL restUrl = new URL(inputURL);
      URLConnection conn = restUrl.openConnection();
      // output processing
      System.out.println("Content type: " + conn.getContentType());
      System.out.println("Content length: " + conn.getContentLength());
      BufferedReader strm = new BufferedReader(new InputStreamReader(conn.getInputStream()));		
      String inputLine;
      String doc = "";
      while ((inputLine = strm.readLine()) != null) {
       doc = doc + inputLine;
      }
      System.out.println("****** Documento XML: **********");
      System.out.println(doc);
    } 
    catch (Exception e) 
    { System.out.println("******* Eccezione !!! *********");
      e.printStackTrace();
    }
  }
}

Java REST GET to XMLSERVICE

import java.io.*;
import java.net.*;

// javac java_util_hello.java
// java java_util_hello 

public class java_util_hello
{
public String go () throws UnsupportedEncodingException, UnsupportedEncodingException, IOException
{ 
  String getDataBytes = new String("http://lp0364d/cgi-bin/xmlcgi.pgm"
	+"?"
        + java.net.URLEncoder.encode(
          "db2=" + "*LOCAL"
        + "&uid=" + "*NONE"
        + "&pwd=" + "*NONE"
        + "&ipc=" + "*na"
        + "&ctl=" + "*here"
        + "&xmlin=" + "<?xml version='1.0'?><xmlservice><sh>ps -ef</sh></xmlservice>"
        + "&xmlout=" + "512000"
        , "ISO-8859-1"));
  URL restUrl = new URL(getDataBytes);
  URLConnection conn = restUrl.openConnection();
  BufferedReader strm = new BufferedReader(new InputStreamReader(conn.getInputStream()));		
  String inputLine;
  String doc = "";
  while ((inputLine = strm.readLine()) != null) {
   doc = doc + inputLine;
  }
  return new String(doc);
}

public static void main (String args[])  throws UnsupportedEncodingException, UnsupportedEncodingException, IOException
{
  java_util_hello tryme = new java_util_hello();
  System.out.println(tryme.go());
}

} // end class java_util_hello

Java REST POST to XMLSERVICE

import java.io.*;
import java.net.*;

// javac java_util_hello2.java
// java java_util_hello2 

public class java_util_hello2
{
public String go () throws UnsupportedEncodingException, UnsupportedEncodingException, IOException
{ 
  URL url = new URL("http://lp0364d/cgi-bin/xmlcgi.pgm");
  String query = new String(
        java.net.URLEncoder.encode(
          "db2=" + "*LOCAL"
        + "&uid=" + "*NONE"
        + "&pwd=" + "*NONE"
        + "&ipc=" + "*na"
        + "&ctl=" + "*here"
        + "&xmlin=" + "<?xml version='1.0'?><xmlservice><sh>ps -ef</sh></xmlservice>"
        + "&xmlout=" + "512000"
        , "ISO-8859-1"));
  byte[] queryBytes = query.getBytes("UTF-8");
  String length = String.valueOf((url + query).getBytes("UTF-8").length);

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Length", length);
  conn.setDoOutput(true);
  OutputStream os = conn.getOutputStream();
  os.write(queryBytes);
  os.flush();
  conn.connect();
  BufferedReader strm = new BufferedReader(new InputStreamReader(conn.getInputStream()));		
  String inputLine;
  String doc = "";
  while ((inputLine = strm.readLine()) != null) {
   doc = doc + inputLine;
  }
  return new String(doc);
}

public static void main (String args[])  throws UnsupportedEncodingException, UnsupportedEncodingException, IOException
{
  java_util_hello2 tryme = new java_util_hello2();
  System.out.println(tryme.go());
}

} // end class java_util_hello2


Author(s)

Tony “Ranger” Cairns - IBM i PHP / PASE

Retrieved from http://youngiprofessionals.com/wiki/index.php/XMLService/XMLSERVICEJava
Page last modified on September 29, 2014, at 11:22 AM EST