/* compile the following in eclipse : run as java application with the simpleHTTPServer listening on port 5000 (from previous example) */
/* to compile the following external jars are required: commons-codec-1.5.jar, commons-httpclient-3.0.1.jar
commons-logging-1.1.1.jar, commons-logging-1.1.1-adapters.jar, commons-logging-1.1.1-api.jar
commons-logging-1.1.1-tests.jar, jackson-core-asl-1.9.2.jar, json-20080701.jar, log4j-1.2.15.jar
and log4j-1.2.16-sources.jar */
import org.apache.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.*;
import org.json.*;
import org.xml.sax.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.http.HTTPException;
import org.apache.log4j.*;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class myClient {
public myClient() {
System.out.println("myclient");
Logger log = Logger.getLogger("wbs");
}
public Document getTemplateDocument(String fName) {
Document doc = null;
try {
DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBld = docFac.newDocumentBuilder();
if (!fName.equals(null)) {
File file = new File(fName);
doc = docBld.parse(file);
}
else {
System.out.println("null file");
}
}catch (ParserConfigurationException pce){
pce.printStackTrace();
}catch (IOException ioe) {
ioe.printStackTrace();
}catch (SAXException sxe) {
sxe.printStackTrace();
}
return doc;
}
public String convertDocToString(Document doc){
String xmls = null;
try {
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult res = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
trans.transform(source, res);
xmls = res.getWriter().toString();
}catch(TransformerConfigurationException tce){
tce.printStackTrace();
}catch(TransformerException te) {
te.printStackTrace();
}
return xmls;
}
public static String execReq(String xmls, String url) {
HttpClientParams cparam = new HttpClientParams();
DefaultHttpMethodRetryHandler retry_H = new DefaultHttpMethodRetryHandler();
cparam.setParameter(HttpClientParams.RETRY_HANDLER, retry_H);
cparam.setParameter(HttpClientParams.HEAD_BODY_CHECK_TIMEOUT, 600000);
cparam.setSoTimeout(600000);
cparam.setConnectionManagerTimeout(600000);
String strXMLFile = xmls;
File input = new File(xmls);
System.out.println("File length: " + input.length());
PostMethod post = null;
try {
post = new PostMethod(url);
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(600000);
post.setRequestBody(new FileInputStream(input));
if (input.lastModified() < Integer.MAX_VALUE){
post.setRequestContentLength((int)input.length());
}
else {
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
int stc = hc.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
System.out.println("statusline: " + post.getStatusLine());
try {
Thread.sleep(100);
}catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
if(stc == HttpStatus.SC_OK) {
xmls = post.getResponseBodyAsString();
}else {
System.out.println(post.getStatusCode());
}
}
}catch(FileNotFoundException ffe) {
ffe.printStackTrace();
}catch(HTTPException he) {
he.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally {
post.releaseConnection();
}
return xmls;
}
public void sendtest() {
Document doc = getTemplateDocument("\\test.xml");
//String request = convertDocToString(doc);
//String response = execReq(request, "http://localhost:5000");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
myClient mc = new myClient();
Document d = mc.getTemplateDocument("\\test.xml");
String request = mc.convertDocToString(d);
System.out.println(request);
//File inp = new File(request);
PostMethod pos = null;
pos = new PostMethod("http://localhost:5000");
HttpClient hc = new HttpClient();
hc.setConnectionTimeout(600000);
try{
//pos.setRequestBody(new FileInputStream(inp));
//pos.setRequestContentLength((int)inp.length());
int stc = hc.executeMethod(pos);
System.out.println(pos.getResponseBodyAsString());
pos.releaseConnection();
System.out.println("statusline: " + pos.getStatusLine());
}catch(HTTPException he) {
he.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally {
pos.releaseConnection();
}
}
}
/* sample test message test.xml file :
*/
No comments:
Post a Comment