Java Batch Client

From Textserver wiki
Revision as of 13:12, 20 January 2016 by Padro (Talk | contribs)

Jump to: navigation, search
  1 ////////////////////////////////////////////////////////////////////////////
  2 //
  3 //  Example client to submit a batch request to TextServer 
  4 // SERVICENAME service, and wait for the job to be finished.
  5 //
  6 //  Input must be a ZIP file containing one or more text files to analyze
  7 //  Output will be a ZIP file containing results for each input file in
  8 // the requested format (XML, json, conll)
  9 //
 10 ////////////////////////////////////////////////////////////////////////////
 11 
 12 
 13 import org.apache.http.client.HttpClient;
 14 import org.apache.http.HttpResponse;
 15 import org.apache.http.HttpStatus;
 16 import org.apache.http.entity.mime.content.FileBody;
 17    
 18 import org.apache.http.client.methods.HttpPost;
 19 import org.apache.http.entity.mime.MultipartEntityBuilder;
 20 import org.apache.http.impl.client.HttpClientBuilder;
 21 import org.apache.http.util.EntityUtils;
 22 
 23 import java.io.File;
 24 import java.io.InputStream;
 25 import java.io.FileOutputStream;
 26 
 27 import java.util.regex.Matcher;
 28 import java.util.regex.Pattern;
 29 
 30 public class SERVICENAMEClientBatch {
 31     
 32     // service base URL
 33     static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
 34     static final String service = "SERVICENAME";
 35     
 36     public static void main(String[] args) throws Exception {
 37 
 38         // Get request parameters
 39         System.out.print("Input ZIP file: ");
 40         String fname = System.console().readLine();
 41         System.out.print("Output ZIP file: ");
 42         String outfname = System.console().readLine();
 43         System.out.print("Language: ");
 44         String lang = System.console().readLine();
 45         System.out.print("Output format (xml,json,conll,naf): ");
 46         String out = System.console().readLine();
 47         System.out.print("TextServer Username: ");
 48         String user = System.console().readLine();
 49         System.out.print("TextServer Password: ");
 50         String pwd = System.console().readLine();
 51 
 52         // Create request, fill query parameters
 53         HttpPost request = new HttpPost(textServer_URL+"/processQuery/"+service);
 54         request.setEntity(MultipartEntityBuilder
 55                           .create()
 56                           .addTextBody("username", user)
 57                           .addTextBody("password", pwd)
 58                           .addPart("file", new FileBody(new File(fname)))
 59                           .addTextBody("language", lang)
 60                           .addTextBody("output", out)
 61                           .addTextBody("interactive", "0")
 62                           .build() );
 63         
 64         // create client, send request, get response
 65         HttpClient client = HttpClientBuilder.create().build();
 66         HttpResponse response = client.execute(request);
 67         String content = EntityUtils.toString(response.getEntity());
 68 
 69         // check for communication errors
 70         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
 71             System.out.println(response.getStatusLine() + " - " + content);
 72             System.exit(1);
 73         }
 74 
 75         // Server response should include a job tokenID, retrieve it
 76         Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
 77         Matcher matcher = pattern.matcher(content);
 78         String tokenID="";
 79         if (matcher.find()) 
 80             tokenID = matcher.group(1);
 81         else {
 82             // unexpected response
 83             System.out.println(content);
 84             System.exit(1);
 85         }
 86         
 87         System.out.println("Job submitted. Token ID= "+tokenID);
 88 
 89         // prepare request to poll for completion and retrieve results
 90         request = new HttpPost(textServer_URL+"/resultRetrieve");
 91         request.setEntity(MultipartEntityBuilder
 92                           .create()
 93                           .addTextBody("username", user)
 94                           .addTextBody("tokenID", tokenID)
 95                           .build() );
 96 
 97         int seconds=20;
 98         while (true) {
 99             
100             System.out.print("Sleeping "); System.out.print(seconds); System.out.println(" seconds");
101             Thread.sleep(seconds*1000);
102             
103             System.out.println("Polling server for job completion");
104             response = client.execute(request);
105             content = EntityUtils.toString(response.getEntity());
106 
107             // if status is OK, we got response, end waiting loop
108             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
109                 break;
110             
111             // we got an error code, lets see which one
112             if (response.getStatusLine().getStatusCode()==HttpStatus.SC_SERVICE_UNAVAILABLE &&
113                 content.substring(0,8).equals("[TS-125]") ) {
114                 // SC=503 and TS_code=TS-125 mean job is not finished. keep waiting
115                 System.out.println("Job not finished yet.");
116                 continue;
117             }
118             else {
119                 // some unexpected error happened
120                 System.out.println(content);
121                 System.exit(1);
122             }
123         }
124 
125 	// write the received ZIP result to output file
126         System.out.println("Job finished. Saving results to "+outfname);
127 
128         InputStream sin = response.getEntity().getContent();
129         byte data[] = new byte[sin.available()];
130         sin.read(data);
131 
132         FileOutputStream outf = new FileOutputStream(outfname);
133         outf.write(data); 
134         outf.close();
135     }
136 }