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: David Bainbridge, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 2005 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.remote;
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import java.net.*;
|
---|
32 | import org.greenstone.gatherer.Configuration;
|
---|
33 | import org.greenstone.gatherer.DebugStream;
|
---|
34 | import org.greenstone.gatherer.Gatherer;
|
---|
35 | import org.greenstone.gatherer.shell.GShell;
|
---|
36 |
|
---|
37 |
|
---|
38 | public class RemoteGreenstoneServer
|
---|
39 | {
|
---|
40 | static public void download_url_zip(String col_name, String dir, GShell source, String accept_expr, String reject_expr)
|
---|
41 | {
|
---|
42 | try {
|
---|
43 | String download_cgi = Gatherer.cgiBase + "download";
|
---|
44 |
|
---|
45 | // Send col_name and dir arguments to download cgi
|
---|
46 | String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
|
---|
47 | String dir_encoded = URLEncoder.encode(dir,"UTF-8");
|
---|
48 | String cgi_args = "c=" + col_name_encoded;
|
---|
49 | cgi_args += "&dir=" + dir_encoded + "&a=" + accept_expr + "&r=" + reject_expr;
|
---|
50 |
|
---|
51 | URL download_url = new URL(download_cgi);
|
---|
52 |
|
---|
53 | System.err.println("**** download cgi = '" + download_cgi + "'");
|
---|
54 | System.err.println("**** cgi args = '" + cgi_args + "'");
|
---|
55 |
|
---|
56 | URLConnection dl_connection = download_url.openConnection();
|
---|
57 | dl_connection.setDoOutput(true);
|
---|
58 | OutputStream dl_os = dl_connection.getOutputStream();
|
---|
59 |
|
---|
60 | PrintWriter dl_out = new PrintWriter(dl_os);
|
---|
61 | dl_out.println(cgi_args);
|
---|
62 | dl_out.close();
|
---|
63 |
|
---|
64 | // Download result from running cgi script
|
---|
65 | InputStream dl_is = dl_connection.getInputStream();
|
---|
66 | BufferedInputStream dl_bis = new BufferedInputStream(dl_is);
|
---|
67 | DataInputStream dl_dbis = new DataInputStream(dl_bis);
|
---|
68 |
|
---|
69 | // set up output stream for zip download
|
---|
70 | String col_dir;
|
---|
71 | if (col_name.startsWith("/")) {
|
---|
72 | col_dir = Configuration.gsdl_path;
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | col_dir = Gatherer.getCollectDirectoryPath();
|
---|
76 | }
|
---|
77 |
|
---|
78 | String zip_fname = col_dir + col_name + ".zip";
|
---|
79 | FileOutputStream zip_fos = new FileOutputStream(zip_fname);
|
---|
80 | BufferedOutputStream zip_bfos = new BufferedOutputStream(zip_fos);
|
---|
81 |
|
---|
82 | byte[] buf = new byte[1024];
|
---|
83 | int len;
|
---|
84 |
|
---|
85 | while ((len = dl_dbis.read(buf)) >= 0 && !source.hasSignalledStop()) {
|
---|
86 | zip_bfos.write(buf,0,len);
|
---|
87 | }
|
---|
88 |
|
---|
89 | dl_dbis.close();
|
---|
90 | dl_bis.close();
|
---|
91 | dl_is.close();
|
---|
92 |
|
---|
93 | zip_bfos.close();
|
---|
94 | zip_fos.close();
|
---|
95 |
|
---|
96 | if(source.hasSignalledStop()) {
|
---|
97 | //A cancel has been called. Delete the zip file.
|
---|
98 | DebugStream.println("download_url_zip() cancelled. Cleaning up.");
|
---|
99 | if(new File(zip_fname).delete()) {
|
---|
100 | DebugStream.println("Zip file " + zip_fname + " deleted");
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | DebugStream.println("Zip file " + zip_fname + " NOT deleted (no big deal). Does it exist?");
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | catch (Exception error) {
|
---|
108 | error.printStackTrace();
|
---|
109 | }
|
---|
110 | DebugStream.println("Exited download_url_zip");
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | static public void upload_url_zip(String col_name, String dir, String delete_type, GShell source)
|
---|
115 | {
|
---|
116 | final String lineEnd = "\r\n";
|
---|
117 | final String twoHyphens = "--";
|
---|
118 | final String boundary = "*****";
|
---|
119 | final int maxBufferSize = 1024;
|
---|
120 |
|
---|
121 | String col_dir;
|
---|
122 | if (col_name.startsWith("/")) {
|
---|
123 | col_dir = Configuration.gsdl_path;
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | col_dir = Gatherer.getCollectDirectoryPath();
|
---|
127 | }
|
---|
128 |
|
---|
129 | String zip_fname = col_dir + col_name + ".zip";
|
---|
130 | String upload_cgi = Gatherer.cgiBase + "upload";
|
---|
131 |
|
---|
132 | HttpURLConnection conn = null;
|
---|
133 |
|
---|
134 | try {
|
---|
135 | // Send zip file to server
|
---|
136 | File file = new File(zip_fname);
|
---|
137 | FileInputStream fileInputStream = new FileInputStream(file);
|
---|
138 |
|
---|
139 | // open a URL connection to the Servlet
|
---|
140 | URL url = new URL(upload_cgi);
|
---|
141 | System.err.println("**** Uploading to: " + upload_cgi);
|
---|
142 |
|
---|
143 | // Open a HTTP connection to the URL
|
---|
144 | conn = (HttpURLConnection) url.openConnection();
|
---|
145 |
|
---|
146 | conn.setDoInput(true); // Allow Inputs
|
---|
147 | conn.setDoOutput(true); // Allow Outputs
|
---|
148 | conn.setUseCaches(false); // Don't use a cached copy.
|
---|
149 | conn.setRequestMethod("POST"); // Use a post method.
|
---|
150 |
|
---|
151 | conn.setRequestProperty("Connection", "Keep-Alive");
|
---|
152 | conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
|
---|
153 |
|
---|
154 | DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
|
---|
155 |
|
---|
156 | dos.writeBytes(twoHyphens + boundary + lineEnd);
|
---|
157 | dos.writeBytes("Content-Disposition: form-data; name=\"c\"" + lineEnd + lineEnd);
|
---|
158 | dos.writeBytes(col_name + lineEnd);
|
---|
159 | System.err.println("**** c="+col_name);
|
---|
160 |
|
---|
161 | dos.writeBytes(twoHyphens + boundary + lineEnd);
|
---|
162 | dos.writeBytes("Content-Disposition: form-data; name=\"dir\"" + lineEnd + lineEnd);
|
---|
163 | dos.writeBytes(dir + lineEnd);
|
---|
164 | System.err.println("**** dir="+dir);
|
---|
165 |
|
---|
166 | dos.writeBytes(twoHyphens + boundary + lineEnd);
|
---|
167 | dos.writeBytes("Content-Disposition: form-data; name=\"del\"" + lineEnd + lineEnd);
|
---|
168 | dos.writeBytes(delete_type + lineEnd);
|
---|
169 | System.err.println("**** del="+delete_type);
|
---|
170 |
|
---|
171 | dos.writeBytes(twoHyphens + boundary + lineEnd);
|
---|
172 | dos.writeBytes("Content-Disposition: form-data; name=\"zip\";"
|
---|
173 | + " filename=\"" + zip_fname +"\"" + lineEnd);
|
---|
174 | dos.writeBytes(lineEnd);
|
---|
175 | System.err.println("**** zip (filename)="+zip_fname);
|
---|
176 |
|
---|
177 | // create a buffer of maximum size
|
---|
178 | int bytesAvailable = fileInputStream.available();
|
---|
179 | int bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
---|
180 | byte[] buffer = new byte[bufferSize];
|
---|
181 |
|
---|
182 | // read file and write it into form...
|
---|
183 | int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
---|
184 |
|
---|
185 | while (bytesRead > 0) {
|
---|
186 | // Check to see if action has been cancelled.
|
---|
187 | if (source != null && source.hasSignalledStop()) {
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | dos.write(buffer, 0, bufferSize);
|
---|
191 | bytesAvailable = fileInputStream.available();
|
---|
192 | bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
---|
193 | bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
---|
194 | }
|
---|
195 |
|
---|
196 | // send multipart form data necesssary after file data...
|
---|
197 |
|
---|
198 | dos.writeBytes(lineEnd);
|
---|
199 | dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
---|
200 |
|
---|
201 | // close streams
|
---|
202 |
|
---|
203 | fileInputStream.close();
|
---|
204 | dos.flush();
|
---|
205 | dos.close();
|
---|
206 |
|
---|
207 | // delete zip file
|
---|
208 | boolean file_deleted = file.delete();
|
---|
209 | if (file_deleted) {
|
---|
210 | System.err.println("Zip file " + file.toString() + " deleted");
|
---|
211 | }
|
---|
212 | else {
|
---|
213 | System.err.println("Zip file " + file.toString() + " NOT deleted");
|
---|
214 | }
|
---|
215 | }
|
---|
216 | catch (MalformedURLException ex) {
|
---|
217 | System.err.println("Failed on: "+ex);
|
---|
218 | }
|
---|
219 |
|
---|
220 | catch (IOException ioe) {
|
---|
221 | System.err.println("Failed on: "+ioe);
|
---|
222 | }
|
---|
223 |
|
---|
224 | // Read server response
|
---|
225 | try {
|
---|
226 | InputStreamReader isr = new InputStreamReader(conn.getInputStream() );
|
---|
227 | BufferedReader bisr = new BufferedReader (isr);
|
---|
228 | String str;
|
---|
229 | while (( str = bisr.readLine()) != null) {
|
---|
230 | System.err.println(str);
|
---|
231 | }
|
---|
232 | bisr.close();
|
---|
233 |
|
---|
234 | }
|
---|
235 | catch (IOException ioex) {
|
---|
236 | System.err.println("From (ServerResponse): "+ioex);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|