Changeset 14308


Ignore:
Timestamp:
2007-08-06T10:55:19+12:00 (17 years ago)
Author:
qq6
Message:

download file 'web.xml' and get all names of sites avaliable if Gatherer.GS3. Reimplemented uploadFileInternal for GS3 on tomcat

File:
1 edited

Legend:

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

    r14261 r14308  
    3333import java.util.zip.*;
    3434import javax.swing.*;
     35import java.io.ByteArrayOutputStream;
    3536import org.greenstone.gatherer.Configuration;
    3637import org.greenstone.gatherer.DebugStream;
     
    4243import org.greenstone.gatherer.util.UnzipTools;
    4344import org.greenstone.gatherer.util.Utility;
    44 
     45import org.apache.commons.httpclient.HttpClient;
     46import org.apache.commons.httpclient.methods.PostMethod;
     47import org.apache.commons.httpclient.methods.GetMethod;
     48import org.apache.commons.httpclient.HttpException;
     49import org.apache.commons.httpclient.methods.multipart.FilePart;
     50import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
     51import org.apache.commons.httpclient.methods.multipart.Part;
     52import org.apache.commons.httpclient.methods.multipart.*;
     53import org.apache.commons.httpclient.params.*;
     54import org.apache.commons.httpclient.HttpStatus;
    4555
    4656public class RemoteGreenstoneServer
     
    8595    return performAction(new RemoteGreenstoneServerDownloadCollectionFileAction(collection_name, collection_file));
    8696    }
    87 
     97   
     98    // get web.xml from the server -- for a remote gli of GS3
     99    static public String downloadWebXMLFile()
     100    {
     101    return performAction(new RemoteGreenstoneServerDownloadWebXMLFileAction());
     102    }
    88103
    89104    static public String getScriptOptions(String script_name, String script_arguments)
     
    91106    return performAction(new RemoteGreenstoneServerGetScriptOptionsAction(script_name, script_arguments));
    92107    }
    93 
     108   
     109    //get all available site names from the server -- for a remote gli of GS3
     110    static public String getSiteNames()
     111    {
     112    return performAction(new RemoteGreenstoneServerGetSiteNamesAction());
     113    }
    94114
    95115    static public String moveCollectionFile(String collection_name, File source_collection_file, File target_collection_file)
     
    532552    }
    533553
     554    /**
     555     * --------------------------------------------------------------------------------------------
     556     *    DOWNLOAD web.xml FILE --for a remote GS3
     557     * --------------------------------------------------------------------------------------------
     558     */
     559    static private class RemoteGreenstoneServerDownloadWebXMLFileAction
     560    extends RemoteGreenstoneServerAction
     561    {
     562    public RemoteGreenstoneServerDownloadWebXMLFileAction()
     563    {}
     564
     565    public void perform()
     566        throws Exception
     567    {
     568        String web_xml_directory_path=(Configuration.gli_user_directory_path);
     569        String download_web_xml_file_command = "cmd=download-web-xml-file";
     570        download_web_xml_file_command += "&file=" + URLEncoder.encode("web.xml", "UTF-8");
     571        String zip_file_name = "web-xml.zip";
     572        String zip_file_path = web_xml_directory_path + zip_file_name;
     573        action_output = downloadFile(download_web_xml_file_command, zip_file_path);
     574
     575        // Unzip the web.xml file just downloaded
     576        UnzipTools.unzipFile(zip_file_path,web_xml_directory_path);
     577    }
     578    }
    534579
    535580    /**
     
    564609    }
    565610
     611    /**
     612     * --------------------------------------------------------------------------------------------
     613     *    GET ALL NAMES OF SITES // for a remote GS3
     614     * --------------------------------------------------------------------------------------------
     615     */
     616    static private class RemoteGreenstoneServerGetSiteNamesAction
     617    extends RemoteGreenstoneServerAction
     618    {
     619    public RemoteGreenstoneServerGetSiteNamesAction()
     620    {}
     621
     622    public void perform()
     623        throws Exception
     624    {
     625        progress_bar.setAction("Getting names of sites ...");
     626
     627        String get_script_options_command = "cmd=get-site-names";
     628        action_output = sendCommandToServer(get_script_options_command, null);
     629    }
     630    }
    566631
    567632    /**
     
    780845    // static private PasswordAuthentication remote_greenstone_server_authentication = new PasswordAuthentication(System.getProperty("user.name"), new char[] { });
    781846
     847    static public void set_remote_greenstone_server_authentication_to_null(){
     848    remote_greenstone_server_authentication = null;
     849    }
    782850
    783851    static private class RemoteGreenstoneServerAuthenticateTask
     
    800868        protected String getMessageString()
    801869        {
     870        if (Gatherer.GS3){
     871            return (Dictionary.get("RemoteGreenstoneServer.Authentication_Message_gs3") + " " + Configuration.site_name);
     872        }
    802873        return Dictionary.get("RemoteGreenstoneServer.Authentication_Message");
    803874        }
     
    9891060    cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
    9901061    cgi_args += "&ts=" + System.currentTimeMillis();
     1062    if (Gatherer.GS3){
     1063        cgi_args += "&site=" + Configuration.site_name;
     1064    }
    9911065
    9921066    URL download_url = new URL(download_cgi);
     
    10421116        cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
    10431117    cgi_args += "&ts=" + System.currentTimeMillis();
     1118    if (Gatherer.GS3){
     1119        cgi_args += "&site=" + Configuration.site_name;
     1120    }
    10441121
    10451122    URL gliserver_url = new URL(gliserver_url_string + "?" + cgi_args);
     
    10711148    throws Exception
    10721149    {
    1073     DebugStream.println("gliserver URL: " + upload_cgi);
     1150    //For a remote GS3
     1151    //GS3 is running on Tomcat, and Tomcat requires a connection timeout to be set up at the client
     1152    //side while uploading files. As HttpURLConection couldn't set the connection timeout, HttpClient.jar
     1153    //from Jakarta is applied to solve this problem only for uploading files.
     1154    if (Gatherer.GS3){
     1155        System.err.println("gliserver URL: " + upload_cgi);
     1156        System.err.println("gliserver args: " + cgi_args);
     1157
     1158    // Setup the POST method
     1159    PostMethod httppost = new PostMethod(upload_cgi+"?"+cgi_args);
     1160
     1161    //read the zip file into a byte array
     1162    InputStream in = new FileInputStream (file_path);
     1163    File f = new File (file_path); // in order to get the length of the bytes array
     1164    ByteArrayOutputStream out = new ByteArrayOutputStream ((int)f.length());
     1165    int r = 0; // amount read
     1166    byte[] buf = new byte[1024];
     1167    while((r = in.read(buf, 0, buf.length)) != -1) {
     1168        out.write(buf, 0, r);
     1169    }
     1170    in.close();
     1171    byte[] result = out.toByteArray();
     1172
     1173    // construct the multipartrequest form
     1174    PartSource partSource = new ByteArrayPartSource("zipFile", result);
     1175        FilePart filePart = new FilePart("uploaded_file", partSource);
     1176
     1177    String[] cgi_array=cgi_args.split("&");// get parameter-value paires from cgi_args string
     1178    Part[] parts=new Part[cgi_array.length+5];
     1179    parts[0]=filePart;
     1180    parts[1]= new StringPart("un", remote_greenstone_server_authentication.getUserName());
     1181    parts[2]= new StringPart("pw", new String(remote_greenstone_server_authentication.getPassword()));
     1182    parts[3]= new StringPart("ts", String.valueOf(System.currentTimeMillis()));
     1183    parts[4]= new StringPart("site", Configuration.site_name);
     1184    // find all parameters of cgi-agrs and add them into Part[]
     1185    for (int i=0; i<cgi_array.length;i++){
     1186        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()));
     1187    }
     1188    // set MutilartRequestEntity on the POST method
     1189        httppost.setRequestEntity(new MultipartRequestEntity(parts, httppost.getParams()));
     1190    //set up the HttpClient connection
     1191    HttpClient client=new HttpClient();
     1192        client.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
     1193    client.getParams().setConnectionManagerTimeout(200000); //set the connection timeout
     1194    // get the output of the command from the server
     1195    client.executeMethod(httppost);
     1196    String command_output = "";
     1197    try{
     1198        client.executeMethod(httppost);
     1199        if (httppost.getStatusCode() == HttpStatus.SC_OK) {
     1200        command_output = httppost.getStatusLine().toString();
     1201        } else {
     1202        command_output = httppost.getStatusLine().toString();
     1203        System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
     1204        }
     1205    }catch(IOException e){
     1206        e.printStackTrace();
     1207    }finally{
     1208        httppost.releaseConnection();
     1209    }
     1210    return command_output;
     1211    }
     1212
     1213    //For a remote GS2
     1214    System.err.println("gliserver URL: " + upload_cgi);
    10741215    System.err.println("gliserver args: " + cgi_args);
    1075 
    10761216    // Add username and password, and a timestamp
    10771217    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
Note: See TracChangeset for help on using the changeset viewer.