package com.ilink.whlsl.util; /** * Title: Wholesale XML API * Description: This project contains the classes necessary to receive an HTTP Post of an XML document. Parse that document and make calls to the Wholesale API in Oracle * Copyright: Copyright (c) 2001 * Company: I-Link Incorporated * @author John Muhlestein * @version 1.0 */ import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class WhlslPostTest { private static URL whlslURL; public InputStream send (ByteArrayOutputStream content) throws Exception { //Open connection HttpURLConnection cxn = (HttpURLConnection)whlslURL.openConnection(); cxn.setDoOutput(true); cxn.setUseCaches(false); cxn.setRequestMethod("POST"); cxn.setRequestProperty("Content-Type", "text/xml"); cxn.setRequestProperty("Content-Length", content.size()+""); //Send Request OutputStream toServlet = new BufferedOutputStream(cxn.getOutputStream()); try { content.writeTo(toServlet); } finally { toServlet.close(); } //Response OK? int code = cxn.getResponseCode(); if(code != HttpURLConnection.HTTP_OK) { String msg = cxn.getResponseMessage(); throw new Exception("Http Error " + code + ": " + msg); } //Return InputStream byte[] buf = new byte[cxn.getContentLength()]; new DataInputStream(cxn.getInputStream()).readFully(buf); InputStream fromServlet = new ByteArrayInputStream(buf); cxn.disconnect(); return fromServlet; } public static void main(String[] args) { if (args.length != 2) { throw new IllegalArgumentException("Wrong number of arguments"); } try { WhlslPostTest whlslPostTest1 = new WhlslPostTest(); File infile = new File(args[0]); File outfile = new File(args[1]); whlslURL = new URL("http://devboard/xml/servlet/whlsl"); String currentLine; //read the file into a byte array ByteArrayOutputStream bytebuf = new ByteArrayOutputStream(400); BufferedReader br = new BufferedReader(new FileReader(infile)); PrintWriter writer = new PrintWriter(bytebuf, true); String curline; while((curline = br.readLine()) != null) { writer.print(curline); } writer.flush(); br.close(); //clean up resources InputStream results = whlslPostTest1.send(bytebuf); //receive the results from the servlet BufferedReader in = new BufferedReader(new InputStreamReader(results)); //print to an output file FileWriter out = new FileWriter(outfile); while((currentLine = in.readLine()) != null) { out.write(currentLine); } out.flush(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(System.out); } } }