Java Batch Client
From Textserver wiki
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 Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
75 Matcher matcher = pattern.matcher(content);
76 String tokenID="";
77 if (matcher.find())
78 tokenID = matcher.group(1);
79 else {
80 // unexpected response
81 System.out.println(content);
82 System.exit(1);
83 }
84
85 System.out.println("Job submitted. Token ID="+tokenID);
86 }
87 }