source: trunk/gli/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java@ 10266

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

Added some stubs for the new remote Greenstone server stuff.

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