source: trunk/gli/src/org/greenstone/gatherer/GathererApplet.java@ 9647

Last change on this file since 9647 was 9217, checked in by mdewsnip, 19 years ago

Fixed up a few problems with setting the wget path manually from the gli.sh file, and getting the default Greenstone wget path on the Mac.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27
28package org.greenstone.gatherer;
29
30import java.applet.*;
31import java.awt.event.ActionEvent;
32import java.awt.event.ActionListener;
33import java.net.*;
34import javax.swing.JApplet;
35
36import java.awt.*;
37import java.io.*;
38import java.util.*;
39import javax.swing.*;
40import org.greenstone.gatherer.Configuration;
41import org.greenstone.gatherer.feedback.ActionRecorderDialog;
42import org.greenstone.gatherer.gui.GUIManager;
43import org.greenstone.gatherer.util.Utility;
44import org.greenstone.gatherer.shell.GShell;
45
46
47public class GathererApplet extends JApplet implements ActionListener
48{
49 Gatherer gatherer;
50 GUIManager g_man;
51 Dimension size = new Dimension(800, 540);
52
53 protected String fullLibraryURL(String address)
54 {
55 String full_address = "";
56
57 // make sure the URL has protocol, host, and file
58 if (address.startsWith("http:")) {
59 // the address has all the necessary components
60 full_address = address;
61 }
62 else if (address.startsWith("/")) {
63 // there is not protocol and host
64 URL document = getDocumentBase();
65 int port_no = document.getPort();
66
67 String port = (port_no>0) ? ":" + port_no : "";
68 full_address = "http://" + document.getHost() + port + address;
69 }
70
71 return full_address;
72 }
73
74 public void init()
75 {
76 try {
77 // Work-around to reduce number of
78 // 'Could not lock user prefs' error messages.
79 // Thanks to Walter Schatz from the java forums.
80 System.setProperty("java.util.prefs.syncInterval","2000000");
81 }
82
83 catch (Exception exception) {
84 // convenient way of finding out if user has agreed to
85 // to requested security settings
86
87 JLabel lab
88 = new JLabel("Greenstone Librarian Interface Applet deactivated",
89 JLabel.CENTER);
90 getContentPane().add(lab);
91 return;
92 }
93
94 // Ensure platform specific LAF
95 try {
96 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
97 }
98 catch (Exception exception) {
99 exception.printStackTrace();
100 }
101
102 // Ensure the GLI user directory exists
103 File gli_user_directory = Utility.getGLIUserFolder();
104 if (!gli_user_directory.exists()) {
105 gli_user_directory.mkdirs();
106 }
107
108 String gwcgi = getParameter("gwcgi");
109 String library_cgi = fullLibraryURL(gwcgi);
110 int cgi_base_cut = library_cgi.lastIndexOf('/');
111 String cgi_base = library_cgi.substring(0,cgi_base_cut+1);
112
113 gatherer = new Gatherer();
114
115 String[] args = { "-gsdl", gli_user_directory.toString(),
116 "-library", library_cgi,
117 };
118
119 GetOpt go = new GetOpt(args);
120
121 if (go.feedback_enabled) {
122 // If feedback is enabled, set up the recorder dialog.
123 // Use the default locale for now - this will be changed by the
124 // Gatherer run method
125 Locale currLocale = Locale.getDefault();
126 ActionRecorderDialog dlg = new ActionRecorderDialog (currLocale);
127 gatherer.feedback_enabled = true;
128 gatherer.feedback_dialog = dlg;
129 }
130
131 Gatherer.isGsdlRemote = true;
132 Gatherer.cgiBase = cgi_base;
133
134 Utility.BASE_DIR = go.gsdl_path;
135 if (!Utility.BASE_DIR.endsWith(File.separator)) {
136 Utility.BASE_DIR += File.separator;
137 }
138
139 Utility.METADATA_DIR = Utility.BASE_DIR + "metadata" + File.separator;
140
141 Configuration.TEMPLATE_CONFIG_XML = "xml/configRemote.xml";
142 Configuration.CONFIG_XML = "configRemote.xml";
143 Configuration.GS3_CONFIG_XML = "config3Remote.xml";
144
145 Gatherer.setCollectDirectoryPath(go.gsdl_path + "collect" + File.separator);
146 File col_dir = new File(Gatherer.getCollectDirectoryPath());
147 if (!col_dir.exists()) {
148 col_dir.mkdir();
149 }
150
151 File metadata_directory = new File(Utility.METADATA_DIR);
152 if (!metadata_directory.exists()) {
153 // digout metadata.zip from JAR file and unzip it
154 Utility.unzipFromJar(Utility.METADATA_ZIP, gli_user_directory.toString());
155 }
156
157
158 File plug_dat = new File(Utility.BASE_DIR + "plugins.dat");
159 if (!plug_dat.exists()) {
160 Utility.extractFromJar("plugins.dat",Utility.BASE_DIR,false);
161 }
162
163 File class_dat = new File(Utility.BASE_DIR + "classifiers.dat");
164 if (!class_dat.exists()) {
165 Utility.extractFromJar("classifiers.dat",Utility.BASE_DIR,false);
166 }
167
168 g_man = gatherer.init(size, go.gsdl_path, go.gsdl3_path, go.local_library_path,
169 go.library_url_string, go.debug, go.perl_path,
170 go.no_load, go.filename, go.site_name,
171 go.servlet_path, go.wget_path);
172
173 JButton bttn = new JButton("Launch Greenstone Librarian Interface ...");
174 bttn.addActionListener(this);
175 getContentPane().add(bttn);
176 }
177
178
179
180 public void start()
181 {
182 System.err.println("Start called");
183 }
184
185
186
187 public void stop()
188 {
189 System.err.println("Stop called");
190
191 }
192
193 public void destroy()
194 {
195 System.err.println("Destroy called");
196 gatherer.exit();
197 System.err.println("Done gatherer exit.");
198 }
199
200 public void actionPerformed(ActionEvent e)
201 {
202 gatherer.run(size, g_man);
203
204 // If there was an open collection last session, reopen it.
205 if (Gatherer.open_collection_file_path != null) {
206 Gatherer.c_man.loadCollection(Gatherer.open_collection_file_path);
207 }
208 }
209
210
211 static public void download_url_zip(String col_name, String dir, GShell source, String accept_expr, String reject_expr)
212 {
213 try {
214 String download_cgi = Gatherer.cgiBase + "download";
215
216 // Send col_name and dir arguments to download cgi
217
218 String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
219 String dir_encoded = URLEncoder.encode(dir,"UTF-8");
220 String cgi_args = "c=" + col_name_encoded;
221 cgi_args += "&dir=" + dir_encoded + "&a=" + accept_expr + "&r=" + reject_expr;
222
223 URL download_url = new URL(download_cgi);
224
225 System.err.println("**** download cgi = '" + download_cgi + "'");
226 System.err.println("**** cgi args = '" + cgi_args + "'");
227
228 URLConnection dl_connection = download_url.openConnection();
229 dl_connection.setDoOutput(true);
230 OutputStream dl_os = dl_connection.getOutputStream();
231
232 PrintWriter dl_out = new PrintWriter(dl_os);
233 dl_out.println(cgi_args);
234 dl_out.close();
235
236 // Download result from running cgi script
237 InputStream dl_is = dl_connection.getInputStream();
238 BufferedInputStream dl_bis = new BufferedInputStream(dl_is);
239 DataInputStream dl_dbis = new DataInputStream(dl_bis);
240
241 // set up output stream for zip download
242 String collect_directory_path = Gatherer.getCollectDirectoryPath();
243 String zip_fname = collect_directory_path + col_name + ".zip";
244 FileOutputStream zip_fos = new FileOutputStream(zip_fname);
245 BufferedOutputStream zip_bfos = new BufferedOutputStream(zip_fos);
246
247 byte[] buf = new byte[1024];
248 int len;
249
250 while ((len = dl_dbis.read(buf)) >= 0 && !source.hasSignalledStop()) {
251 zip_bfos.write(buf,0,len);
252 }
253
254 dl_dbis.close();
255 zip_bfos.close();
256
257 }
258 catch (Exception error) {
259 error.printStackTrace();
260 }
261 DebugStream.println("Exited download_url_zip"); //debug
262 }
263
264
265 static public void upload_url_zip(String col_name, String dir, String delete_type, GShell source)
266 {
267 final String lineEnd = "\r\n";
268 final String twoHyphens = "--";
269 final String boundary = "*****";
270 final int maxBufferSize = 1024;
271
272 String collect_directory_path = Gatherer.getCollectDirectoryPath();
273 String zip_fname = collect_directory_path + col_name + ".zip";
274 String upload_cgi = Gatherer.cgiBase + "upload";
275
276 HttpURLConnection conn = null;
277
278 try {
279 // Send zip file to server
280
281 File file = new File(zip_fname) ;
282
283 FileInputStream fileInputStream = new FileInputStream(file);
284
285 // open a URL connection to the Servlet
286 URL url = new URL(upload_cgi);
287 System.err.println("**** Uploading to: " + upload_cgi);
288
289 // Open a HTTP connection to the URL
290 conn = (HttpURLConnection) url.openConnection();
291
292 conn.setDoInput(true); // Allow Inputs
293 conn.setDoOutput(true); // Allow Outputs
294 conn.setUseCaches(false); // Don't use a cached copy.
295 conn.setRequestMethod("POST"); // Use a post method.
296
297 conn.setRequestProperty("Connection", "Keep-Alive");
298 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
299
300 DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
301
302 dos.writeBytes(twoHyphens + boundary + lineEnd);
303 dos.writeBytes("Content-Disposition: form-data; name=\"c\"" + lineEnd + lineEnd);
304 dos.writeBytes(col_name + lineEnd);
305 System.err.println("**** c="+col_name);
306
307 dos.writeBytes(twoHyphens + boundary + lineEnd);
308 dos.writeBytes("Content-Disposition: form-data; name=\"dir\"" + lineEnd + lineEnd);
309 dos.writeBytes(dir + lineEnd);
310 System.err.println("**** dir="+dir);
311
312 dos.writeBytes(twoHyphens + boundary + lineEnd);
313 dos.writeBytes("Content-Disposition: form-data; name=\"del\"" + lineEnd + lineEnd);
314 dos.writeBytes(delete_type + lineEnd);
315 System.err.println("**** del="+delete_type);
316
317 dos.writeBytes(twoHyphens + boundary + lineEnd);
318 dos.writeBytes("Content-Disposition: form-data; name=\"zip\";"
319 + " filename=\"" + zip_fname +"\"" + lineEnd);
320 dos.writeBytes(lineEnd);
321 System.err.println("**** zip (filename)="+zip_fname);
322
323
324
325 // create a buffer of maximum size
326 int bytesAvailable = fileInputStream.available();
327 int bufferSize = Math.min(bytesAvailable, maxBufferSize);
328 byte[] buffer = new byte[bufferSize];
329
330 // read file and write it into form...
331 int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
332
333 while (bytesRead > 0) {
334 // Check to see if action has been cancelled.
335 if (source != null && source.hasSignalledStop()) {
336 break;
337 }
338 dos.write(buffer, 0, bufferSize);
339 bytesAvailable = fileInputStream.available();
340 bufferSize = Math.min(bytesAvailable, maxBufferSize);
341 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
342 }
343
344 // send multipart form data necesssary after file data...
345
346 dos.writeBytes(lineEnd);
347 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
348
349 // close streams
350
351 fileInputStream.close();
352 dos.flush();
353 dos.close();
354
355
356 }
357 catch (MalformedURLException ex) {
358 System.err.println("Failed on: "+ex);
359 }
360
361 catch (IOException ioe) {
362 System.err.println("Failed on: "+ioe);
363 }
364
365 // Read server response
366
367 try {
368 InputStreamReader isr = new InputStreamReader(conn.getInputStream() );
369 BufferedReader bisr = new BufferedReader (isr);
370 String str;
371 while (( str = bisr.readLine()) != null) {
372 System.err.println(str);
373 }
374 bisr.close();
375
376 }
377 catch (IOException ioex) {
378 System.err.println("From (ServerResponse): "+ioex);
379 }
380 }
381}
Note: See TracBrowser for help on using the repository browser.