Difference between revisions of "Batch Results Retrieval in Java"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="java" line="1" > </syntaxhighlight>")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<syntaxhighlight lang="java" line="1" >
 
<syntaxhighlight lang="java" line="1" >
 +
////////////////////////////////////////////////////////////////////////////
 +
//
 +
//  Example client to retrieve the results of a batch request to TextServer
 +
//
 +
//  Output will be a ZIP file containing results for each input file in
 +
// the requested format (XML, json, conll)
 +
//
 +
////////////////////////////////////////////////////////////////////////////
 +
 +
import org.apache.http.client.HttpClient;
 +
import org.apache.http.HttpResponse;
 +
import org.apache.http.HttpStatus;
 +
import org.apache.http.entity.mime.content.FileBody;
 +
 
 +
import org.apache.http.client.methods.HttpPost;
 +
import org.apache.http.entity.mime.MultipartEntityBuilder;
 +
import org.apache.http.impl.client.HttpClientBuilder;
 +
import org.apache.http.util.EntityUtils;
 +
 +
import java.io.File;
 +
import java.io.InputStream;
 +
import java.io.FileOutputStream;
 +
 +
public class RetrieveResults {
 +
   
 +
    // service base URL
 +
    static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
 +
   
 +
    public static void main(String[] args) throws Exception {
 +
 +
        // Get request parameters
 +
        System.out.print("TextServer Username: ");
 +
        String user = System.console().readLine();
 +
        System.out.print("TextServer Job Token ID: ");
 +
        String tokenID = System.console().readLine();
 +
        System.out.print("Output ZIP file: ");
 +
        String outfname = System.console().readLine();
 +
     
 +
 +
        // prepare request to poll for completion and retrieve results
 +
        request = new HttpPost(textServer_URL+"/resultRetrieve");
 +
        request.setEntity(MultipartEntityBuilder
 +
                          .create()
 +
                          .addTextBody("username", user)
 +
                          .addTextBody("tokenID", tokenID)
 +
                          .build() );
 +
   
 +
        System.out.println("Polling server for job completion");
 +
        response = client.execute(request);
 +
        content = EntityUtils.toString(response.getEntity());
 +
           
 +
        // we got an error code, lets see which one
 +
        if (response.getStatusLine().getStatusCode()==HttpStatus.SC_SERVICE_UNAVAILABLE &&
 +
            content.substring(0,8).equals("[TS-125]") ) {
 +
            // SC=503 and TS_code=TS-125 mean job is not finished. keep waiting
 +
            System.out.println("Job not finished yet.");
 +
            System.exit(0);
 +
        }
 +
        else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
 +
            // some unexpected error happened
 +
            System.out.println(content);
 +
            System.exit(1);
 +
        }
 +
 +
        // No errors, we got response.
 +
        // write the received ZIP result to output file
 +
        InputStream sin = response.getEntity().getContent();
 +
        byte data[] = new byte[sin.available()];
 +
        sin.read(data);
 +
 +
        FileOutputStream outf = new FileOutputStream(outfname);
 +
        outf.write(data);
 +
        outf.close();
 +
 +
        System.out.println("Job finished. Results saved to "+outfname);
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

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 }