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