source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/feedback/SendHTTP.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 9.9 KB
Line 
1package org.greenstone.gatherer.feedback;
2
3import java.util.Properties;
4import java.net.*;
5import java.io.*;
6import java.util.Locale;
7import java.util.ResourceBundle;
8import java.text.MessageFormat;
9import javax.swing.*;
10import java.awt.Dimension;
11
12/**
13 * This class will send the 2 jar files to the server using HTTP Post.
14 */
15public class SendHTTP
16{
17 /**
18 * This is variable will hold the arguments provided by user in order to send
19 */
20 private String[] argument;
21 /**
22 * This variable will hold the resource of the words that is stored in Messages.properties file.
23 * The calling using messages.getString(someString) will caused someString to be translated
24 * into some other string that is hold in that file.usually it will caused it to be translated
25 * to the language that the user use or choose to have as stated in Locale.
26 */
27 private static ResourceBundle msg;
28 /**
29 * This variable will hold the jar file unique code.
30 * For example: Jar123File.jar then file_code = 123
31 */
32 private String file_code;
33 /**
34 * This is the flag that will say whether or not the sending is successful.
35 * If failed = false means its succesful otherwise its failed.
36 */
37 private boolean failed = false;
38
39 /**
40 * Crates instance of SendHTTP
41 */
42 public SendHTTP () {}
43
44 /**
45 * This method will send 2 files back to the server.
46 * It will also send the code if the sending is failed then it will give user a message saying whats
47 * the possible error of it, and it will make the jar file changed into
48 * Unsend_Jar123File.jar file.
49 * If the sending is succesful then it will try to send all the other jar files
50 * that were failed to send before.And then it will deletes all the jar files
51 * that are send successfully.
52 * @param args the argument provided to send the email.
53 * @param messages hold the resource of the words that is stored in Messages.properties file.
54 * @param code the code of the email to be send.
55 */
56 public void sendMail(String[] args,ResourceBundle messages,String code)
57 {
58 argument = args;
59 msg = messages;
60 file_code = code;
61
62 try
63 {
64 String BND = "---------------------------7d021a37605f0";
65 String hostURL = "http://kanuka.cs.waikato.ac.nz:8090/glifeedback/Upload";
66
67 URL mURL = new URL(hostURL+"?name=posting");
68 URLConnection conn = mURL.openConnection();
69
70 if (conn instanceof HttpURLConnection)
71 ((HttpURLConnection) conn).setRequestMethod("POST");
72
73 conn.setDoInput(true);
74 conn.setDoOutput(true);
75 conn.setUseCaches(false);
76 conn.setDefaultUseCaches(false);
77 conn.setRequestProperty("Accept","*/*");
78 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BND);
79 conn.setRequestProperty("Connection","Keep-Alive");
80 conn.setRequestProperty("Cache-Control","no-cache");
81
82 DataOutputStream out;
83 out = new DataOutputStream(conn.getOutputStream());
84
85 //--- Sending subject -------
86 String txt;
87 txt = "--" + BND + "\r\n";
88 out.writeBytes(txt);
89
90 txt = "Content-Disposition: form-data; ";
91 out.writeBytes(txt);
92
93 String subject;
94 subject = "Send Feedback" + code;
95 txt = "name=\"" + "Subject" + "\"\r\n";
96 out.writeBytes(txt);
97
98 txt = "\r\n";
99 out.writeBytes(txt);
100
101 txt = subject + "\r\n";
102 out.writeBytes(txt);
103
104 out.flush();
105
106 //--- Sending text ------
107 txt = "--" + BND + "\r\n";
108 out.writeBytes(txt);
109
110 txt = "Content-Disposition: form-data; ";
111 out.writeBytes(txt);
112
113 String attmnt;
114 attmnt = "We have succesfully received your feedback report.\n" +
115 "To check what files we have received go to this link: " + hostURL +
116 "/?name=" + code + "\n" +
117 "Please keep referencing to this ID Code for this feedback report.\n" +
118 "ID Code: " + code;
119
120 txt = "name=\"" + "Text" + "\"\r\n";
121 out.writeBytes(txt);
122
123 txt = "\r\n";
124 out.writeBytes(txt);
125
126 txt = attmnt + "\r\n";
127 out.writeBytes(txt);
128
129 out.flush();
130
131 //--------- Sending xml-jar file
132 FileInputStream fis;
133 File f;
134 String name,path;
135
136 f = new File(args[1]);
137
138 txt = "--" + BND + "\r\n";
139 out.writeBytes(txt);
140
141 txt = "Content-Disposition: form-data; ";
142 out.writeBytes(txt);
143
144 name = f.getName();
145 txt = "name=\"" + name + "\"; ";
146 out.writeBytes(txt);
147
148 path = f.getAbsolutePath();
149 txt = "filename=\"" + path + "\"\r\n";
150 out.writeBytes(txt);
151
152 txt = "Content-Type: aplication/octet-stream\r\n";
153 out.writeBytes(txt);
154
155 txt = "\r\n";
156 out.writeBytes(txt);
157
158 fis = new FileInputStream(f);
159 byte[] data = new byte[1024];
160 int r = 0;
161 while((r = fis.read(data, 0, data.length)) != -1)
162 {
163 out.write(data, 0, r);
164 }
165 fis.close();
166
167 txt = "\r\n";
168 out.writeBytes(txt);
169
170 out.flush();
171
172 //--- Sending parser.jar ----
173 txt = "--" + BND + "\r\n";
174 out.writeBytes(txt);
175
176 txt = "Content-Disposition: form-data; ";
177 out.writeBytes(txt);
178
179 f = new File(args[4]);
180
181 name = f.getName();
182 txt = "name=\"" + name + "\"; ";
183 out.writeBytes(txt);
184
185 path = f.getAbsolutePath();
186 txt = "filename=\"" + path + "\"\r\n";
187 out.writeBytes(txt);
188
189 txt = "Content-Type: application/octet-stream\r\n";
190 out.writeBytes(txt);
191
192 txt = "\r\n";
193 out.writeBytes(txt);
194
195 fis = new FileInputStream(f);
196 data = new byte[1024];
197 r = 0;
198 while((r = fis.read(data, 0, data.length)) != -1)
199 {
200 out.write(data, 0, r);
201 }
202 fis.close();
203
204 txt = "\r\n";
205 out.writeBytes(txt);
206
207 out.flush();
208
209 //----- finished sending -----
210 txt = "--" + BND + "--";
211 out.writeBytes(txt);
212
213 out.flush();
214 out.close();
215
216 //----- getting the response ------
217 BufferedReader reader;
218 reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
219 String responseLine;
220 responseLine = null;
221
222 StringBuffer response;
223 response = new StringBuffer();
224 while ((responseLine = reader.readLine()) != null)
225 response.append(responseLine+"\n");
226
227 reader.close();
228
229
230 //------ our application's response to server's response
231 /*if (response.equals("failed"))
232 {
233 failed = true;
234
235 JOptionPane.showMessageDialog(null,messages.getString("InternalError")+ "\n" +
236 messages.getString("Emaillater"),messages.getString("Error"),
237 JOptionPane.ERROR_MESSAGE);
238 //Conformation.setFinish(true);
239 }
240 else if (response.equals("success"))
241 failed = false;
242 else
243 {*/
244 failed = false;
245 JDialog.setDefaultLookAndFeelDecorated(false);
246 JDialog dlg = new JDialog();
247 JLabel lbl = new JLabel (response.toString());
248
249 dlg.getContentPane().add(lbl);
250 dlg.pack();
251 dlg.setVisible(true);
252 // }
253
254 //Conformation.setFinish(true);
255 }
256 catch (MalformedURLException exp)
257 {
258 failed = true;
259
260 JOptionPane.showMessageDialog(null,"malformed URL" + "\n" +
261 messages.getString("Emaillater"),messages.getString("Error"),
262 JOptionPane.ERROR_MESSAGE);
263 System.out.println("malformed URL");
264 }
265 catch (IOException e)
266 {
267 failed = true;
268
269 JOptionPane.showMessageDialog(null,messages.getString("InternalError")+ "\n" +
270 messages.getString("Emaillater"),messages.getString("Error"),
271 JOptionPane.ERROR_MESSAGE);e.printStackTrace();
272 }
273
274 String dirname;
275 dirname = "xmlfeedback/";
276
277 if (failed == false)
278 {
279 File f;
280 f = new File(dirname + "Jar" + file_code + "File.jar");
281 f.delete();
282
283 File file;
284 file = new File(dirname + ".");
285 File[] fileList;
286 fileList = file.listFiles();
287
288 int y;
289 for (y = 0 ; y < fileList.length ; y++)
290 {
291 String filename;
292 filename = isRightJarFile(fileList[y]);
293 if (filename != null)
294 {
295 renameTo(fileList[y],new File(filename));
296 argument[1] = filename;
297 sendMail(argument,msg,getCode(filename));
298 }
299 }
300 }
301 else
302 {
303 File f2;
304 f2 = new File(dirname + "Jar" + file_code + "File.jar");
305 renameTo(f2,new File(dirname + "Unsend_Jar" + file_code + "File.jar"));
306 }
307
308 }
309
310 /**
311 * This method will rename file.
312 * @param src is the name of the file to be renamed.
313 * @param dest is the desired file name.
314 */
315 private void renameTo (File src, File dest)
316 {
317 if (src.renameTo(dest) == false)
318 {
319 try
320 {
321 FileInputStream in;
322 in = new FileInputStream(src);
323 FileOutputStream out;
324 out = new FileOutputStream(dest);
325 int next;
326
327 while (true)
328 {
329 next = in.read();
330 if (next == -1)
331 break;
332 else
333 out.write((byte) next);
334 }
335
336 in.close();
337 out.close();
338 }
339 catch (IOException exp) {exp.printStackTrace();}
340 }
341 }
342
343 /**
344 * This method will get the extension of the file.
345 * @param f is the file that we want to get the extension.
346 * @return the file extension
347 */
348 private String getExtension(File f)
349 {
350 String ext;
351 ext = null;
352 String s;
353 s = f.getName();
354 int i;
355 i = s.lastIndexOf('.');
356
357 if (i > 0 && i < s.length() - 1)
358 {
359 ext = s.substring(i+1).toLowerCase();
360 }
361 return ext;
362 }
363
364 /**
365 * This method will get all the name of the file after "_" if the file its a jar file.
366 * For example Unsend_Jar123File.jar then this method will return Jar123File, otherwise
367 * file with any other format will return null.
368 * @param f is the file that we want to get the name.
369 */
370 private String isRightJarFile(File f)
371 {
372 String ext;
373 ext = null;
374
375 if (f.isDirectory() == true)
376 return null;
377
378 if (getExtension(f).compareTo("jar") == 0)
379 {
380 String s;
381 s = f.getName();
382 int i;
383 i = s.lastIndexOf("_");
384
385 if (i > 0 && i < s.length() - 1)
386 {
387 ext = s.substring(i+1,s.length());
388 }
389 else
390 ext = null;
391
392 return ext;
393 }
394 else
395 return null;
396 }
397
398 /**
399 * This method will getting the code that inside the Jar filename.
400 * For example Jar123File then this method will return 123.
401 */
402 private String getCode (String s)
403 {
404 int i;
405 i = s.lastIndexOf("Jar");
406 int j;
407 j = s.indexOf("File");
408 String code;
409 code = s.substring((i+3),j);
410 return code;
411 }
412}
413
414
415
416
417
418
419
420
421
Note: See TracBrowser for help on using the repository browser.