Difference between revisions of "Java Batch Client"

From Textserver wiki
Jump to: navigation, search
(Created page with "<syntaxhighlight lang="java" line="1" > </syntaxhighlight>")
 
 
(3 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 submit 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
 +
// 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;
 +
 +
import java.util.regex.Matcher;
 +
import java.util.regex.Pattern;
 +
 +
public class SERVICENAMEClientBatch {
 +
   
 +
    // service base URL
 +
    static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
 +
    static final String service = "SERVICENAME";
 +
   
 +
    public static void main(String[] args) throws Exception {
 +
 +
        // Get request parameters
 +
        System.out.print("Input ZIP file: ");
 +
        String fname = System.console().readLine();
 +
        System.out.print("Language: ");
 +
        String lang = System.console().readLine();
 +
        System.out.print("Output format (xml,json,conll,naf): ");
 +
        String out = System.console().readLine();
 +
        System.out.print("TextServer Username: ");
 +
        String user = System.console().readLine();
 +
        System.out.print("TextServer Password: ");
 +
        String pwd = System.console().readLine();
 +
 +
        // Create request, fill query parameters
 +
        HttpPost request = new HttpPost(textServer_URL+"/processQuery/"+service);
 +
        request.setEntity(MultipartEntityBuilder
 +
                          .create()
 +
                          .addTextBody("username", user)
 +
                          .addTextBody("password", pwd)
 +
                          .addPart("file", new FileBody(new File(fname)))
 +
                          .addTextBody("language", lang)
 +
                          .addTextBody("output", out)
 +
                          .addTextBody("interactive", "0")
 +
                          .build() );
 +
       
 +
        // create client, send request, get response
 +
        HttpClient client = HttpClientBuilder.create().build();
 +
        HttpResponse response = client.execute(request);
 +
        String content = EntityUtils.toString(response.getEntity());
 +
 +
        // check for communication errors
 +
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
 +
            System.out.println(response.getStatusLine() + " - " + content);
 +
            System.exit(1);
 +
        }
 +
 +
        // Server response should include a job tokenID, retrieve it
 +
        // (You can use an XML parser to extract it if you want to do it nicer)
 +
        Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
 +
        Matcher matcher = pattern.matcher(content);
 +
        String tokenID="";
 +
        if (matcher.find())
 +
            tokenID = matcher.group(1);
 +
        else {
 +
            // unexpected response
 +
            System.out.println(content);
 +
            System.exit(1);
 +
        }
 +
       
 +
        System.out.println("Job submitted. Token ID="+tokenID);
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:05, 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 
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("Language: ");
42         String lang = System.console().readLine();
43         System.out.print("Output format (xml,json,conll,naf): ");
44         String out = System.console().readLine();
45         System.out.print("TextServer Username: ");
46         String user = System.console().readLine();
47         System.out.print("TextServer Password: ");
48         String pwd = System.console().readLine();
49 
50         // Create request, fill query parameters
51         HttpPost request = new HttpPost(textServer_URL+"/processQuery/"+service);
52         request.setEntity(MultipartEntityBuilder
53                           .create()
54                           .addTextBody("username", user)
55                           .addTextBody("password", pwd)
56                           .addPart("file", new FileBody(new File(fname)))
57                           .addTextBody("language", lang)
58                           .addTextBody("output", out)
59                           .addTextBody("interactive", "0")
60                           .build() );
61         
62         // create client, send request, get response
63         HttpClient client = HttpClientBuilder.create().build();
64         HttpResponse response = client.execute(request);
65         String content = EntityUtils.toString(response.getEntity());
66 
67         // check for communication errors
68         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
69             System.out.println(response.getStatusLine() + " - " + content);
70             System.exit(1);
71         }
72 
73         // Server response should include a job tokenID, retrieve it
74         // (You can use an XML parser to extract it if you want to do it nicer)
75         Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
76         Matcher matcher = pattern.matcher(content);
77         String tokenID="";
78         if (matcher.find()) 
79             tokenID = matcher.group(1);
80         else {
81             // unexpected response
82             System.out.println(content);
83             System.exit(1);
84         }
85         
86         System.out.println("Job submitted. Token ID="+tokenID);
87     }
88 }