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;
}

}

}

Reply