/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson, Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.JApplet; import java.applet.*; import java.awt.*; import java.util.*; import javax.swing.*; import org.greenstone.gatherer.Configuration; import org.greenstone.gatherer.feedback.ActionRecorderDialog; import org.greenstone.gatherer.gui.GUIManager; import org.greenstone.gatherer.gui.Splash; import org.greenstone.gatherer.util.Utility; public class GathererApplet extends JApplet implements ActionListener { Gatherer gatherer; GUIManager g_man; Dimension size = new Dimension(800, 540); protected String fullLibraryURL(String address) { String full_address = ""; // make sure the URL has protocol, host, and file if (address.startsWith("http:")) { // the address has all the necessary components full_address = address; } else if (address.startsWith("/")) { // there is not protocol and host URL document = getDocumentBase(); int port_no = document.getPort(); String port = (port_no>0) ? ":" + port_no : ""; full_address = "http://" + document.getHost() + port + address; } return full_address; } public void init() { try { // Work-around to reduce number of // 'Could not lock user prefs' error messages. // Thanks to Walter Schatz from the java forums. System.setProperty("java.util.prefs.syncInterval","2000000"); } catch (Exception exception) { // convenient way of finding out if user has agreed to // to requested security settings JLabel lab = new JLabel("Greenstone Librarian Interface Applet deactivated", JLabel.CENTER); getContentPane().add(lab); return; } // Ensure platform specific LAF try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception exception) { exception.printStackTrace(); } String gwcgi = getParameter("gwcgi"); String library_cgi = fullLibraryURL(gwcgi); int cgi_base_cut = library_cgi.lastIndexOf('/'); String cgi_base = library_cgi.substring(0,cgi_base_cut+1); gatherer = new Gatherer(); String gli_user_folder = Utility.getGLIUserFolder().toString(); String[] args = { "-gsdl", gli_user_folder, "-library", library_cgi, }; GetOpt go = new GetOpt(args); if (go.feedback_enabled) { // If feedback is enabled, set up the recorder dialog. // Use the default locale for now - this will be changed by the // Gatherer run method Locale currLocale = Locale.getDefault(); ActionRecorderDialog dlg = new ActionRecorderDialog (currLocale); gatherer.feedback_enabled = true; gatherer.feedback_dialog = dlg; } Gatherer.isGsdlRemote = true; Gatherer.cgiBase = cgi_base; Utility.BASE_DIR = go.gsdl_path; if (!Utility.BASE_DIR.endsWith(File.separator)) { Utility.BASE_DIR += File.separator; } Utility.CACHE_DIR = Utility.BASE_DIR + "cache" + File.separator; Utility.HELP_DIR = Utility.BASE_DIR + "help" + File.separator; Utility.METADATA_DIR = Utility.BASE_DIR + "metadata" + File.separator; Utility.RECYCLE = Utility.BASE_DIR + "recycle" + File.separator; Utility.RES_DIR = Utility.BASE_DIR + "resource" + File.separator; Utility.WELCOME_DIR = Utility.BASE_DIR + "welcome" + File.separator; Configuration.TEMPLATE_CONFIG_XML = "xml/configRemote.xml"; Configuration.CONFIG_XML = "configRemote.xml"; Configuration.GS3_CONFIG_XML = "config3Remote.xml"; File gli_user_dir = new File(gli_user_folder); if (!gli_user_dir.exists()) { gli_user_dir.mkdirs(); } File col_dir = new File(Utility.getCollectDir(go.gsdl_path)); if (!col_dir.exists()) { col_dir.mkdir(); } File metadata_directory = new File(Utility.METADATA_DIR); if (!metadata_directory.exists()) { // digout metadata.zip from JAR file and unzip it Utility.unzipFromJar(Utility.METADATA_ZIP,gli_user_folder); } File plug_dat = new File(Utility.BASE_DIR + "plugins.dat"); if (!plug_dat.exists()) { Utility.extractFromJar("plugins.dat",Utility.BASE_DIR,false); } File class_dat = new File(Utility.BASE_DIR + "classifiers.dat"); if (!class_dat.exists()) { Utility.extractFromJar("classifiers.dat",Utility.BASE_DIR,false); } g_man = gatherer.init(size, go.gsdl_path, go.gsdl3_path, go.exec_path, go.debug, go.perl_path, go.no_load, go.filename, go.site_name, go.servlet_path, go.mirroring_enabled, go.wget_version_str, go.wget_path); JButton bttn = new JButton("Launch Greenstone Librarian Interface ..."); bttn.addActionListener(this); getContentPane().add(bttn); } public void start() { System.err.println("Start called"); } public void stop() { System.err.println("Stop called"); } public void destroy() { System.err.println("Destroy called"); gatherer.exit(); System.err.println("Done gatherer exit."); } public void actionPerformed(ActionEvent e) { Splash splash = null; gatherer.run(size, splash, g_man); } static public void download_url_zip(String col_name, String dir) { try { String download_cgi = Gatherer.cgiBase + "download"; // Send col_name and dir arguments to download cgi String col_name_encoded = URLEncoder.encode(col_name,"UTF-8"); String dir_encoded = URLEncoder.encode(dir,"UTF-8"); String cgi_args = "c=" + col_name_encoded; cgi_args += "&dir=" + dir_encoded; URL download_url = new URL(download_cgi); System.err.println("**** download cgi = '" + download_cgi + "'"); System.err.println("**** cgi args = '" + cgi_args + "'"); URLConnection dl_connection = download_url.openConnection(); dl_connection.setDoOutput(true); OutputStream dl_os = dl_connection.getOutputStream(); PrintWriter dl_out = new PrintWriter(dl_os); dl_out.println(cgi_args); dl_out.close(); // Download result from running cgi script InputStream dl_is = dl_connection.getInputStream(); BufferedInputStream dl_bis = new BufferedInputStream(dl_is); DataInputStream dl_dbis = new DataInputStream(dl_bis); // set up output stream for zip download String col_dir = Utility.getCollectDir(Configuration.gsdl_path); String zip_fname = col_dir + col_name + ".zip"; FileOutputStream zip_fos = new FileOutputStream(zip_fname); BufferedOutputStream zip_bfos = new BufferedOutputStream(zip_fos); byte[] buf = new byte[1024]; int len; while ((len = dl_dbis.read(buf)) >= 0) { zip_bfos.write(buf,0,len); } dl_dbis.close(); zip_bfos.close(); } catch (Exception error) { error.printStackTrace(); } } static public void upload_url_zip(String col_name, String dir) { final String lineEnd = "\r\n"; final String twoHyphens = "--"; final String boundary = "*****"; final int maxBufferSize = 1024; String col_dir = Utility.getCollectDir(Configuration.gsdl_path); String zip_fname = col_dir + col_name + ".zip"; String upload_cgi = Gatherer.cgiBase + "upload"; HttpURLConnection conn = null; try { // Send zip file to server File file = new File(zip_fname) ; FileInputStream fileInputStream = new FileInputStream(file); // open a URL connection to the Servlet URL url = new URL(upload_cgi); System.err.println("**** Uploading to: " + upload_cgi); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a cached copy. conn.setRequestMethod("POST"); // Use a post method. conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); DataOutputStream dos = new DataOutputStream( conn.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"c\"" + lineEnd + lineEnd); dos.writeBytes(col_name + lineEnd); System.err.println("**** c="+col_name); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"dir\"" + lineEnd + lineEnd); dos.writeBytes(dir + lineEnd); System.err.println("**** dir="+dir); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"zip\";" + " filename=\"" + zip_fname +"\"" + lineEnd); dos.writeBytes(lineEnd); System.err.println("**** zip (filename)="+zip_fname); // create a buffer of maximum size int bytesAvailable = fileInputStream.available(); int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new byte[bufferSize]; // read file and write it into form... int bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close streams fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { System.err.println("Failed on: "+ex); } catch (IOException ioe) { System.err.println("Failed on: "+ioe); } // Read server response try { InputStreamReader isr = new InputStreamReader(conn.getInputStream() ); BufferedReader bisr = new BufferedReader (isr); String str; while (( str = bisr.readLine()) != null) { System.err.println(str); } bisr.close(); } catch (IOException ioex) { System.err.println("From (ServerResponse): "+ioex); } } }