4 import java.awt.event.ActionListener;
5 import java.awt.event.ActionEvent;
7 import ij.plugin.JpegWriter;
15 public static final String CONFIRM_PREFIX =
"OK";
16 public static final String ERROR_PREFIX =
"ERROR";
22 private int progress = 0;
24 private String tempFileName = null;
43 byte[] fileData = null;
44 String fileType = ij.getFileType();
45 if (fileType ==
".gif") {
46 fileData =
getGif(ij.getImagePlus());
47 }
else if (fileType ==
".jpg") {
48 fileData =
getJpeg(ij.getImagePlus());
50 showError(
"Undefined file type " + fileType);
52 String fileName = ij.getFilename();
53 if (fileName.length() < 5) {
54 showError(
"You must give a filename before committing");
57 final int length = fileData.length;
62 submitURL =
new URL(ij.getParameter(
"SUBMIT_URL"));
63 }
catch (MalformedURLException e) {
64 showError(
"Malformed URL: "+ij.getParameter(
"SUBMIT_URL"));
66 }
catch (Exception e) {
67 showError(
"Problem getting SUBMIT_URL parameter");
72 Thread progressThread =
new Thread() {
74 ServerSubmitProgressDialog progressDialog =
new ServerSubmitProgressDialog(length);
75 int p = getProgress();
77 progressDialog.setValue(p);
78 try { sleep(50); }
catch (Exception e) {}
81 progressDialog.setTitle(
"Waiting for server response...");
82 progressDialog.goIndeterminate();
83 while (progress != -1) {
84 try { sleep(100); }
catch (Exception e) {}
86 progressDialog.setVisible(
false);
89 progressThread.start();
92 ServerSubmitterThread submitterThread =
new ServerSubmitterThread(
this, fileData, fileName, submitURL);
93 submitterThread.start();
103 return ij.getParameter(name);
115 GifEncoder encoder =
new GifEncoder(fi.width, fi.height, pixels, fi.reds, fi.greens, fi.blues);
116 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
117 OutputStream output =
new BufferedOutputStream(baos);
118 encoder.write(output);
119 return baos.toByteArray();
121 catch (IOException e) {
122 showError(e.getMessage());
135 return jpr.getJpegContents(imp);
143 void showError(String msg)
145 JOptionPane.showMessageDialog(ij, msg,
"Upload Error", JOptionPane.ERROR_MESSAGE);
153 synchronized void setProgress(
int val)
163 synchronized int getProgress()
173 synchronized void finish(String tempFileName)
176 this.tempFileName = tempFileName;
180 String getTempFileName()
182 while (getProgress() != -1)
try { Thread.sleep(333); }
catch (Exception e) {}
183 return this.tempFileName;
192 class ServerSubmitterThread
extends Thread
213 ServerSubmitterThread(ServerSubmitter ms, byte[] fileData, String fileName, URL submitURL)
216 this.fileData = fileData;
217 this.fileName = fileName;
218 this.submitURL = submitURL;
219 this.boundary = getBoundary();
227 private String getPostComponent(String name, String val)
229 return "\r\n--"+boundary+
"\r\nContent-Disposition: form-data; name=\""+name+
"\"\r\n\r\n"+val;
240 String fileFieldName = ms.getParameter(
"FILE_FIELD_NAME");
241 if (fileFieldName == null) fileFieldName =
"file_0";
242 StringBuffer fileHeader =
new StringBuffer();
243 fileHeader.append(
"--" + boundary +
"\r\n");
244 fileHeader.append(
"Content-Disposition: form-data; name=\""+fileFieldName+
"\"; fileName=\""+fileName+
"\"\r\n");
245 fileHeader.append(
"Content-Type: application/octet-stream");
246 fileHeader.append(
"\r\n\r\n");
249 StringBuffer tail =
new StringBuffer();
251 String fieldName = ms.getParameter(
"FIELD_NAME_"+i);
252 String fieldValue = ms.getParameter(
"FIELD_VALUE_"+i);
253 while (fieldName != null) {
255 tail.append(getPostComponent(fieldName, fieldValue));
257 fieldName = ms.getParameter(
"FIELD_NAME_"+i);
258 fieldValue = ms.getParameter(
"FIELD_VALUE_"+i);
260 tail.append(
"\r\n--");
261 tail.append(boundary);
262 tail.append(
"--\r\n");
265 StringBuffer header =
new StringBuffer();
266 header.append(
"POST "+submitURL.getPath()+
"?"+submitURL.getQuery()+
" HTTP/1.1\r\n");
267 header.append(
"Host: "+submitURL.getHost()+
"\r\n");
268 header.append(
"Content-type: multipart/form-data; boundary="+boundary+
"\r\n");
269 header.append(
"Content-length: "+((
int)(fileData.length + fileHeader.length() + tail.length()))+
"\r\n");
270 header.append(
"Connection: Keep-Alive\r\n");
271 header.append(
"\r\n");
275 DataOutputStream dataout = null;
276 BufferedReader datain = null;
280 sock =
new Socket(submitURL.getHost(), (-1 == submitURL.getPort())?80:submitURL.getPort());
281 dataout =
new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
282 datain =
new BufferedReader(
new InputStreamReader(sock.getInputStream()));
285 dataout.writeBytes(header.toString());
288 dataout.writeBytes(fileHeader.toString());
289 byte[] byteBuff =
new byte[1024];
292 ByteArrayInputStream is =
new ByteArrayInputStream(fileData);
293 while(-1 != (numBytes = is.read(byteBuff))) {
295 dataout.write(byteBuff, 0, numBytes);
296 totalBytes += numBytes;
297 ms.setProgress(totalBytes);
298 try { Thread.sleep(20); }
catch (Exception e) {}
302 dataout.writeBytes(tail.toString());
305 }
catch (Exception e) {
306 ms.showError(
"Error while writing to server: "+e.getMessage());
313 String line = datain.readLine();
314 String confirmLine = null;
315 URL redirectLocation;
318 while ((line != null)) {
320 if (line.indexOf(
"403 Forbidden") != -1) {
321 error =
"Server returned 403 forbidden. You don't seem to have access to the system";
324 if (line.indexOf(
"MySource Warning") != -1) {
325 error =
"MySource Warning: \n" + getMysourceMessage(datain);
328 if (line.indexOf(
"MySource Error") != -1) {
329 error =
"MySource Error: \n" + getMysourceMessage(datain);
332 if (line.trim().startsWith(ServerSubmitter.CONFIRM_PREFIX)) {
333 confirmLine = line.trim();
336 if (line.trim().startsWith(ServerSubmitter.ERROR_PREFIX)) {
341 line = datain.readLine();
350 if (confirmLine == null) {
351 ms.showError(
"No confirm line found");
355 ms.finish(confirmLine.substring(ms.CONFIRM_PREFIX.length()+1));
356 }
catch (Exception e) {
357 ms.showError(
"Error while reading server response: "+e.getMessage());
365 private String getMysourceMessage(BufferedReader datain)
367 StringBuffer msg =
new StringBuffer();
369 String line = datain.readLine();
370 while (line.indexOf(
"File:</td>") == -1) {
371 line = datain.readLine();
373 line = datain.readLine();
374 line = line.substring(0, line.lastIndexOf(
"</td>"));
375 line = line.substring(line.lastIndexOf(
">")+1);
376 msg.append(
"File: ");
380 while (line.indexOf(
"Line:</td>") == -1) {
381 line = datain.readLine();
383 line = datain.readLine();
384 line = line.substring(0, line.lastIndexOf(
"</td>"));
385 line = line.substring(line.lastIndexOf(
">")+1);
386 msg.append(
"Line: ");
390 while (line.indexOf(
"Message:</td>") == -1) {
391 line = datain.readLine();
393 line = datain.readLine();
394 line = line.substring(0, line.lastIndexOf(
"</td>"));
395 line = line.substring(line.lastIndexOf(
">")+1);
396 msg.append(
"Message: ");
397 msg.append(line.replaceAll(
""",
"\""));
398 }
catch (Exception e) {
399 msg.append(
"...Exception reading error response...");
401 return msg.toString();
409 private String getBoundary()
411 char[] allChars =
new String(
"1234567890abcdefghijklmnopqrstuvwxyz").toCharArray();
412 int len = allChars.length - 1;
413 return "-----------------------------"
414 + allChars[(int)(Math.random() * len)]
415 + allChars[(
int)(Math.random() * len)]
416 + allChars[(
int)(Math.random() * len)]
417 + allChars[(
int)(Math.random() * len)]
418 + allChars[(
int)(Math.random() * len)]
419 + allChars[(
int)(Math.random() * len)]
420 + allChars[(
int)(Math.random() * len)]
421 + allChars[(
int)(Math.random() * len)]
422 + allChars[(
int)(Math.random() * len)]
423 + allChars[(
int)(Math.random() * len)]
424 + allChars[(
int)(Math.random() * len)];
433 class ServerSubmitProgressDialog
extends JFrame
436 JProgressBar progressBar;
443 ServerSubmitProgressDialog(
int max)
446 progressBar =
new JProgressBar(0, max);
447 progressBar.setStringPainted(
true);
448 progressBar.setValue(0);
449 getContentPane().add(progressBar);
452 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
453 setLocation((screen.width / 2) - 200, (screen.height / 2) - 40);
464 void setValue(
int val)
466 progressBar.setValue(val);
475 void goIndeterminate()
477 progressBar.setIndeterminate(
true);
478 progressBar.setStringPainted(
false);