Changeset 16098


Ignore:
Timestamp:
2008-06-20T19:03:36+12:00 (16 years ago)
Author:
ak19
Message:
  1. RemoteGS3 uploadFileInternal works with GS3's gliserver.pl again: a dummy Part is inserted first into the Multipart post message so that the FilePart will now be recognised successfully on the GS3 server end. 2. Additional method getGreenstoneVersion with corresponding Action.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java

    r15863 r16098  
    161161    }
    162162
     163    static public int getGreenstoneVersion()
     164    {
     165    // returns message "Greenstone version is: <version number of the Greenstone remote server>"
     166    String result = performAction(new RemoteGreenstoneServerVersionAction());
     167    int index = result.indexOf(":") + 1; // includes space after colon
     168    result = result.substring(index);
     169    System.err.println("***** version: " + result);
     170    int greenstoneVersion = Integer.parseInt(result);
     171    return greenstoneVersion;
     172    }
     173
     174
    163175    // ----------------------------------------------------------------------------------------------------
    164176
     
    544556    }
    545557
     558     /**
     559     * --------------------------------------------------------------------------------------------
     560     *    DISCOVERING WHAT VERSION THE REMOTE GREENSTONE SERVER IS (2 or 3)
     561     * --------------------------------------------------------------------------------------------
     562     */
     563
     564    static private class RemoteGreenstoneServerVersionAction
     565    extends RemoteGreenstoneServerAction
     566    {
     567    public void perform()
     568        throws Exception
     569    {
     570        String greenstone_version_command = "cmd=greenstone-server-version";
     571        action_output = sendCommandToServer(greenstone_version_command, null);
     572    }   
     573    }
    546574
    547575    /**
     
    12381266    throws Exception
    12391267    {
     1268    System.err.println("gliserver URL: " + upload_cgi);
     1269    System.err.println("gliserver args: " + cgi_args);
     1270
    12401271    //For a remote GS3
    12411272    //GS3 is running on Tomcat, and Tomcat requires a connection timeout to be set up at the client
     
    12431274    //from Jakarta is applied to solve this problem only for uploading files.
    12441275    if (Gatherer.GS3){
    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        }
    13001327    return command_output;
    13011328    }
    13021329
    13031330    //For a remote GS2
    1304     System.err.println("gliserver URL: " + upload_cgi);
    1305     System.err.println("gliserver args: " + cgi_args);
    13061331    // Add username and password, and a timestamp
    13071332    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
Note: See TracChangeset for help on using the changeset viewer.