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

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

New class for functions needed to use a remote Greenstone server.

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