Difference between revisions of "Batch Results Retrieval in Java"

From Textserver wiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
 
////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////
 
//
 
//
//  Example client to submit a batch request to TextServer  
+
//  Example client to retrieve the results of a batch request to TextServer  
// SERVICENAME service, and wait for the job to be finished.
+
 
//
 
//
//  Input must be a ZIP file containing one or more text files to analyze
 
 
//  Output will be a ZIP file containing results for each input file in
 
//  Output will be a ZIP file containing results for each input file in
 
// the requested format (XML, json, conll)
 
// the requested format (XML, json, conll)
 
//
 
//
 
////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////
 
  
 
import org.apache.http.client.HttpClient;
 
import org.apache.http.client.HttpClient;

Latest revision as of 12:31, 21 January 2016

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