if_rig, appendix G annoying Java snippet ServletAdapterTest

//Not pretty, but it works.
import com.indus.apifw.util.*;
import com.indus.common.utils.ICDataUtils;
import com.indus.common.utils.ICEncodingUtils;
import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class ifbo {

public static void main(String[] args) {

String url;
String environment;
String filename;

url = "http://server:port/apifw/APIFWAdapterServlet";

environment = "";

/* Read file */

String fileName = "c:/temp/ifbo/WORR.xml";

// Get the size of the file
File input = new File(fileName);
long filelength = input.length();

// Create the byte array to hold the data.
// The array size matches the file size.
byte fileByteArray[] = new byte[(int)filelength];

try {

FileInputStream fis = new FileInputStream(input);
filelength = fis.read(fileByteArray);
fis.close();

} catch (UnsupportedEncodingException ee) {
System.out.println("IOException error" + ee.getMessage());
} catch (IOException e) {
System.out.println("IOException error" + e.getMessage());
}

System.out.println("filelength = " + filelength);

/* end Read file */

try {

java.io.OutputStream bos = new FileOutputStream(input);

OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF8");

osw.write(new String(fileByteArray, "UTF8"));

osw.close();

PostMethod fwPost = new PostMethod(url);

Part parts[] = {

new StringPart("envName", ICEncodingUtils.urlEncode(environment, "UTF8")), new FilePart(input.getName(), input, "text/xml", "UTF8")

};

fwPost.setRequestEntity(new MultipartRequestEntity(parts, fwPost.getParams()));

HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams();

connectionParams.setConnectionTimeout(5000);

HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();

connectionManager.setParams(connectionParams);

HttpClient httpClient = new HttpClient(connectionManager);

int result = httpClient.executeMethod(fwPost);

int httpCode = fwPost.getStatusCode();

String httpResponseText = fwPost.getStatusText();

Header headr = fwPost.getResponseHeader("ApifwRC");

headr = fwPost.getResponseHeader("InternalReqID");

String xmlResult = new String();

BufferedReader bufRdr = null;

java.io.InputStream inStrm = fwPost.getResponseBodyAsStream();

InputStreamReader ISR = new InputStreamReader(inStrm);
bufRdr = new BufferedReader(ISR, 3);

String sTmp;

while((sTmp = bufRdr.readLine()) != null)

xmlResult = xmlResult + sTmp;

bufRdr.close();

fwPost.releaseConnection();

System.out.println("xmlResult:" + xmlResult);

//input.delete();

return;

} catch (Exception e) {
System.out.println("IOException error" + e.getMessage());
return;
}

}

}

Appendix G pared to the minimum

//Note that MultipartPostMethod is not used. This program uses Apache Jakarta Http Client 3.0.1 (commons-httpclient-3.0.1.jar) in which MultipartPostMethod is deprecated.

import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

public class ifboG {

  public static void main(String[] args) {

    String url;
    String environment;
    String filename;

    url = "http://server:port/apifw/APIFWAdapterServlet";

    environment = "";

    PostMethod fwPost = new PostMethod(url);

    String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE InboundRequest [<!ELEMENT InboundRequest (APIHeader, BusinessObject)><!ENTITY % APIHeader SYSTEM \"http://apifw//xml/APIHeader.Inbound.Request.V060000.dtd\">%APIHeader;<!ENTITY % BusinessObject SYSTEM \"http://apifw//xml/WorkOrder.Read.Request.V060000.dtd\">%BusinessObject;]><InboundRequest><APIHeader ReplyTypeOK=\"A\" ReplyTypeError=\"N\"><apiAPIVersion>V060000</apiAPIVersion><apiBusinessObject>WorkOrder</apiBusinessObject><apiBusObjectMethod>Read</apiBusObjectMethod><apiBusObjectVersion>V060000</apiBusObjectVersion><apiUserID></apiUserID><apiExtSystemID></apiExtSystemID><apiExtRequestID></apiExtRequestID><apiRoutingInfo></apiRoutingInfo></APIHeader><BusinessObject><WoHeader><ExtSystemId></ExtSystemId><ExternalKey></ExternalKey><WorkOrderNbr>00211997</WorkOrderNbr><SubMethod></SubMethod><WoTaskHeader><WorkOrderTask>01</WorkOrderTask></WoTaskHeader></WoHeader></BusinessObject></InboundRequest>";

    try {

      //Add the Parameters with the data literals URL encoded in UTF-8
      fwPost.addParameter("envName", "UTF8");
      fwPost.addParameter("request", testXML);

      HttpClient httpClient = new HttpClient();
      httpClient.setConnectionTimeout(5000);
      int result = httpClient.executeMethod(fwPost);

      //Get and parse response
      int httpCode = fwPost.getStatusCode();
      String httpResponseText = fwPost.getStatusText();
      Header headr = fwPost.getResponseHeader("ApifwRC");
      headr = fwPost.getResponseHeader("InternalReqID");
      String xmlResult = new String();
      String sTmp;
      BufferedReader bufRdr = null;
      InputStream inStrm = fwPost.getResponseBodyAsStream();
      InputStreamReader ISR = new InputStreamReader(inStrm);
      bufRdr = new BufferedReader(ISR, 3);

      while((sTmp = bufRdr.readLine()) != null)

        xmlResult = xmlResult + sTmp;

      bufRdr.close();

      System.out.println("xmlResult = " + xmlResult);
      fwPost.releaseConnection();

    } catch (Exception e) {
      System.out.println("Exception error" + e.getMessage());
      return;
    }

    return;
  }
}

what exactly is in the multi part POST?

Add this code to see what the multi part POST consists of:

PostMethod fwPost = new PostMethod(url);

//Documentation of "Part".
//http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/multipart/Part.html

Part parts[] = {

  new StringPart("envName", ICEncodingUtils.urlEncode(environment, "UTF8")), new FilePart(input.getName(), input, "text
/xml", "UTF8")

};

java.io.OutputStream ostream = null;
java.io.ByteArrayOutputStream BAOS = new ByteArrayOutputStream();

for (int k = 0; k < parts.length; k++) {

  parts[k].send(BAOS);
  System.out.println("send=" + BAOS.toString());
  BAOS.reset();

  System.out.println("parts k=" + k + " " + parts[k]);
  System.out.println("content type=" + parts[k].getContentType());
  System.out.println("char set=" + parts[k].getCharSet());
  System.out.println("transfer encoding=" + parts[k].getTransferEncoding());
  System.out.println("length=" + Long.toString(parts[k].length()));
  System.out.println("parts k as String=" + parts[k].toString());
  System.out.println("=============================");

}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.