Difference between revisions of "Batch Results Retrieval in Java"

From Textserver wiki
Jump to: navigation, search
Line 10: Line 10:
 
//
 
//
 
////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////
 
  
 
import org.apache.http.client.HttpClient;
 
import org.apache.http.client.HttpClient;

Revision as of 12:27, 21 January 2016

 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 import org.apache.http.client.HttpClient;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.HttpStatus;
15 import org.apache.http.entity.mime.content.FileBody;
16    
17 import org.apache.http.client.methods.HttpPost;
18 import org.apache.http.entity.mime.MultipartEntityBuilder;
19 import org.apache.http.impl.client.HttpClientBuilder;
20 import org.apache.http.util.EntityUtils;
21 
22 import java.io.File;
23 import java.io.InputStream;
24 import java.io.FileOutputStream;
25 
26 public class RetrieveResults {
27     
28     // service base URL
29     static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
30     
31     public static void main(String[] args) throws Exception {
32 
33         // Get request parameters
34         System.out.print("TextServer Username: ");
35         String user = System.console().readLine();
36         System.out.print("TextServer Job Token ID: ");
37         String tokenID = System.console().readLine();
38         System.out.print("Output ZIP file: ");
39         String outfname = System.console().readLine();
40        
41 
42         // prepare request to poll for completion and retrieve results
43         request = new HttpPost(textServer_URL+"/resultRetrieve");
44         request.setEntity(MultipartEntityBuilder
45                           .create()
46                           .addTextBody("username", user)
47                           .addTextBody("tokenID", tokenID)
48                           .build() );
49     
50         System.out.println("Polling server for job completion");
51         response = client.execute(request);
52         content = EntityUtils.toString(response.getEntity());
53             
54         // we got an error code, lets see which one
55         if (response.getStatusLine().getStatusCode()==HttpStatus.SC_SERVICE_UNAVAILABLE &&
56             content.substring(0,8).equals("[TS-125]") ) {
57             // SC=503 and TS_code=TS-125 mean job is not finished. keep waiting
58             System.out.println("Job not finished yet.");
59             System.exit(0);
60         }
61         else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
62             // some unexpected error happened
63             System.out.println(content);
64             System.exit(1);
65         }
66 
67         // No errors, we got response.
68         // write the received ZIP result to output file
69         InputStream sin = response.getEntity().getContent();
70         byte data[] = new byte[sin.available()];
71         sin.read(data);
72 
73         FileOutputStream outf = new FileOutputStream(outfname);
74         outf.write(data); 
75         outf.close();
76 
77         System.out.println("Job finished. Results saved to "+outfname);
78     }
79 }