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

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

Added a couple more stubs for the West Yorkshire functionality -- storing collections on the server.

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