| 1245 | | System.err.println("gliserver URL: " + upload_cgi); |
|---|
| 1246 | | System.err.println("gliserver args: " + cgi_args); |
|---|
| 1247 | | |
|---|
| 1248 | | // Setup the POST method |
|---|
| 1249 | | PostMethod httppost = new PostMethod(upload_cgi+"?"+cgi_args); |
|---|
| 1250 | | |
|---|
| 1251 | | //read the zip file into a byte array |
|---|
| 1252 | | InputStream in = new FileInputStream (file_path); |
|---|
| 1253 | | File f = new File (file_path); // in order to get the length of the bytes array |
|---|
| 1254 | | ByteArrayOutputStream out = new ByteArrayOutputStream ((int)f.length()); |
|---|
| 1255 | | int r = 0; // amount read |
|---|
| 1256 | | byte[] buf = new byte[1024]; |
|---|
| 1257 | | while((r = in.read(buf, 0, buf.length)) != -1) { |
|---|
| 1258 | | out.write(buf, 0, r); |
|---|
| 1259 | | } |
|---|
| 1260 | | in.close(); |
|---|
| 1261 | | byte[] result = out.toByteArray(); |
|---|
| 1262 | | |
|---|
| 1263 | | // construct the multipartrequest form |
|---|
| 1264 | | PartSource partSource = new ByteArrayPartSource("zipFile", result); |
|---|
| 1265 | | FilePart filePart = new FilePart("uploaded_file", partSource); |
|---|
| 1266 | | |
|---|
| 1267 | | String[] cgi_array=cgi_args.split("&");// get parameter-value paires from cgi_args string |
|---|
| 1268 | | Part[] parts=new Part[cgi_array.length+5]; |
|---|
| 1269 | | parts[0]=filePart; |
|---|
| 1270 | | parts[1]= new StringPart("un", remote_greenstone_server_authentication.getUserName()); |
|---|
| 1271 | | parts[2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword())); |
|---|
| 1272 | | parts[3]= new StringPart("ts", String.valueOf(System.currentTimeMillis())); |
|---|
| 1273 | | parts[4]= new StringPart("site", Configuration.site_name); |
|---|
| 1274 | | // find all parameters of cgi-agrs and add them into Part[] |
|---|
| 1275 | | for (int i=0; i<cgi_array.length;i++){ |
|---|
| 1276 | | parts[5+i]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length())); |
|---|
| 1277 | | } |
|---|
| 1278 | | // set MutilartRequestEntity on the POST method |
|---|
| 1279 | | httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams())); |
|---|
| 1280 | | //set up the HttpClient connection |
|---|
| 1281 | | HttpClient client=new HttpClient(); |
|---|
| 1282 | | client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); |
|---|
| 1283 | | client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout |
|---|
| 1284 | | // get the output of the command from the server |
|---|
| 1285 | | client.executeMethod(httppost); |
|---|
| 1286 | | String command_output = ""; |
|---|
| 1287 | | try{ |
|---|
| 1288 | | client.executeMethod(httppost); |
|---|
| 1289 | | if (httppost.getStatusCode() == HttpStatus.SC_OK) { |
|---|
| 1290 | | command_output = httppost.getStatusLine().toString(); |
|---|
| 1291 | | } else { |
|---|
| 1292 | | command_output = httppost.getStatusLine().toString(); |
|---|
| 1293 | | System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); |
|---|
| 1294 | | } |
|---|
| 1295 | | }catch(IOException e){ |
|---|
| 1296 | | e.printStackTrace(); |
|---|
| 1297 | | }finally{ |
|---|
| 1298 | | httppost.releaseConnection(); |
|---|
| 1299 | | } |
|---|
| | 1276 | // Setup the POST method |
|---|
| | 1277 | PostMethod httppost = new PostMethod(upload_cgi); |
|---|
| | 1278 | |
|---|
| | 1279 | // construct the multipartrequest form |
|---|
| | 1280 | String[] cgi_array=cgi_args.split("&");// get parameter-value pairs from cgi_args string |
|---|
| | 1281 | Part[] parts=new Part[cgi_array.length+6]; |
|---|
| | 1282 | |
|---|
| | 1283 | // We insert a dummy value for the first Part. For some reason, the gliserver side doesn't |
|---|
| | 1284 | // read the first Part (whether it's the file or a CGI argument like the cmd=upload-collection-file) |
|---|
| | 1285 | parts[0]= new StringPart("dummy_arg", "dummy_value"); // unread dummy value |
|---|
| | 1286 | |
|---|
| | 1287 | // find all parameters of cgi-args and add them into Part[] |
|---|
| | 1288 | for (int i=0; i<cgi_array.length;i++) { |
|---|
| | 1289 | parts[i+1]=new StringPart(cgi_array[i].substring(0,cgi_array[i].indexOf("=")),cgi_array[i].substring(cgi_array[i].indexOf("=")+1,cgi_array[i].length())); |
|---|
| | 1290 | } |
|---|
| | 1291 | parts[cgi_array.length+1]= new StringPart("un", remote_greenstone_server_authentication.getUserName()); |
|---|
| | 1292 | parts[cgi_array.length+2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword())); |
|---|
| | 1293 | parts[cgi_array.length+3]= new StringPart("ts", String.valueOf(System.currentTimeMillis())); |
|---|
| | 1294 | parts[cgi_array.length+4]= new StringPart("site", Configuration.site_name); |
|---|
| | 1295 | // The FilePart: consisting of the (cgi-arg) name and (optional) filename in the Content-Disposition |
|---|
| | 1296 | // of the POST request Header (see CGI.pm), and the actual zip file itself. It uses the defaults: |
|---|
| | 1297 | // Content-Type: application/octet-stream; charset=ISO-8859-1,Content-Transfer-Encoding: binary |
|---|
| | 1298 | parts[cgi_array.length+5]= new FilePart("uploaded_file", "zipFile", new File(file_path)); |
|---|
| | 1299 | |
|---|
| | 1300 | // set MultipartRequestEntity on the POST method |
|---|
| | 1301 | httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams())); |
|---|
| | 1302 | |
|---|
| | 1303 | // See file gli/request.txt for the multipart request that's been generated: |
|---|
| | 1304 | //httppost.getRequestEntity().writeRequest(new FileOutputStream("request.txt", true)); // true: appends |
|---|
| | 1305 | |
|---|
| | 1306 | |
|---|
| | 1307 | //set up the HttpClient connection |
|---|
| | 1308 | HttpClient client=new HttpClient(); |
|---|
| | 1309 | client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); |
|---|
| | 1310 | client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout |
|---|
| | 1311 | |
|---|
| | 1312 | // get the output of the command from the server |
|---|
| | 1313 | String command_output = ""; |
|---|
| | 1314 | try{ |
|---|
| | 1315 | client.executeMethod(httppost); |
|---|
| | 1316 | if (httppost.getStatusCode() == HttpStatus.SC_OK) { |
|---|
| | 1317 | command_output = httppost.getStatusLine().toString(); |
|---|
| | 1318 | } else { |
|---|
| | 1319 | command_output = httppost.getStatusLine().toString(); |
|---|
| | 1320 | System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); |
|---|
| | 1321 | } |
|---|
| | 1322 | }catch(IOException e){ |
|---|
| | 1323 | e.printStackTrace(); |
|---|
| | 1324 | }finally{ |
|---|
| | 1325 | httppost.releaseConnection(); |
|---|
| | 1326 | } |
|---|