/** * */ package org.cipres.restapi; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; /** * This the client program to send multipart message to the web server using HttpClient * * You can also use apache's TCPMon to monitor the TCP traffic between client and server * + From the Admin tab: * + Listen Port # 3333 (acts as the proxy, all traffic redirects to this port) * + Listener: * + Target Hostname: lcchan-linux.sdsc.edu * + Target Port #: 8888 * + sent the Listener port 3333 in your client program (i.e. this program) to watch the traffic packages * * @author lcchan * */ public class SubmitClient { /** * @param args */ public static void main(String[] args) { // step 1: set URL String url = "http://lcchan-linux.sdsc.edu:8080/cipres-web/restapi/job"; //String url = "http://localhost:3333"; // step 2: make connection using post mode PostMethod method = new PostMethod(url); HttpClient client = new HttpClient(); BufferedReader reader = null; try { // Send multiple data to servlet File file1 = new File("/tmp/input/12Tx432C.nex"); File file2 = new File("/tmp/input/RAxML-Boot.xml"); Part[] parts = { new StringPart("email","lcchan.temp@gmail.com"), new StringPart("analysis", "MP"), new StringPart("tool", "PAUP"), new FilePart("datafile", file1), //new FilePart("configfile", file2), //new StringPart("analysis", "MP"), //new StringPart("tool", "PAUP"), }; // step 3: send query method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); client.executeMethod(method); // read response from servlet System.out.println("----------------- Begin Response ------------------------------------"); System.out.println("status code: " + method.getStatusCode()); System.out.println("status line: " + method.getStatusLine()); System.out.println("status text: " + method.getStatusText()); System.out.println("body\n: " + method.getResponseBodyAsString()); System.out.println("----------------- End Response ------------------------------------"); // if multiple lines have been sent InputStream in = method.getResponseBodyAsStream(); reader = new BufferedReader(new InputStreamReader(in)); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line + "\n"); } System.out.println(buffer.toString()); } catch (Exception ex) { System.out.println("POST Exception: " + ex.toString()); } finally { // clean up method.releaseConnection(); try {reader.close();} catch(Exception ex){} } } }