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

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

GLI applet: collection building is now much happier about being cancelled. By Matthew Whyte.

  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 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_version_str, 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)
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;
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 }
262
263
264 static public void upload_url_zip(String col_name, String dir, GShell source)
265 {
266 final String lineEnd = "\r\n";
267 final String twoHyphens = "--";
268 final String boundary = "*****";
269 final int maxBufferSize = 1024;
270
271 String collect_directory_path = Gatherer.getCollectDirectoryPath();
272 String zip_fname = collect_directory_path + col_name + ".zip";
273 String upload_cgi = Gatherer.cgiBase + "upload";
274
275 HttpURLConnection conn = null;
276
277 try {
278 // Send zip file to server
279
280 File file = new File(zip_fname) ;
281 FileInputStream fileInputStream = new FileInputStream(file);
282
283 // open a URL connection to the Servlet
284 URL url = new URL(upload_cgi);
285 System.err.println("**** Uploading to: " + upload_cgi);
286
287 // Open a HTTP connection to the URL
288 conn = (HttpURLConnection) url.openConnection();
289
290 conn.setDoInput(true); // Allow Inputs
291 conn.setDoOutput(true); // Allow Outputs
292 conn.setUseCaches(false); // Don't use a cached copy.
293 conn.setRequestMethod("POST"); // Use a post method.
294
295 conn.setRequestProperty("Connection", "Keep-Alive");
296 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
297
298 DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
299
300 dos.writeBytes(twoHyphens + boundary + lineEnd);
301 dos.writeBytes("Content-Disposition: form-data; name=\"c\"" + lineEnd + lineEnd);
302 dos.writeBytes(col_name + lineEnd);
303 System.err.println("**** c="+col_name);
304
305 dos.writeBytes(twoHyphens + boundary + lineEnd);
306 dos.writeBytes("Content-Disposition: form-data; name=\"dir\"" + lineEnd + lineEnd);
307 dos.writeBytes(dir + lineEnd);
308 System.err.println("**** dir="+dir);
309
310 dos.writeBytes(twoHyphens + boundary + lineEnd);
311 dos.writeBytes("Content-Disposition: form-data; name=\"zip\";"
312 + " filename=\"" + zip_fname +"\"" + lineEnd);
313 dos.writeBytes(lineEnd);
314 System.err.println("**** zip (filename)="+zip_fname);
315
316 // create a buffer of maximum size
317 int bytesAvailable = fileInputStream.available();
318 int bufferSize = Math.min(bytesAvailable, maxBufferSize);
319 byte[] buffer = new byte[bufferSize];
320
321 // read file and write it into form...
322 int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
323
324 while (bytesRead > 0) {
325 // Check to see if action has been cancelled.
326 if (source != null && source.hasSignalledStop()) {
327 break;
328 }
329 dos.write(buffer, 0, bufferSize);
330 bytesAvailable = fileInputStream.available();
331 bufferSize = Math.min(bytesAvailable, maxBufferSize);
332 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
333 }
334
335 // send multipart form data necesssary after file data...
336
337 dos.writeBytes(lineEnd);
338 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
339
340 // close streams
341
342 fileInputStream.close();
343 dos.flush();
344 dos.close();
345
346
347 }
348 catch (MalformedURLException ex) {
349 System.err.println("Failed on: "+ex);
350 }
351
352 catch (IOException ioe) {
353 System.err.println("Failed on: "+ioe);
354 }
355
356 // Read server response
357
358 try {
359 InputStreamReader isr = new InputStreamReader(conn.getInputStream() );
360 BufferedReader bisr = new BufferedReader (isr);
361 String str;
362 while (( str = bisr.readLine()) != null) {
363 System.err.println(str);
364 }
365 bisr.close();
366
367 }
368 catch (IOException ioex) {
369 System.err.println("From (ServerResponse): "+ioex);
370 }
371 }
372}
Note: See TracBrowser for help on using the repository browser.