Browse Source

Quite a bit of changes: added possibility to better detect _why_ upload failed. Tested all events, should fix #174

Javadoc still contains mention of ProgressEvent, that does not exist.

svn changeset:3934/svn branch:trunk
tags/6.7.0.beta1
Marc Englund 16 years ago
parent
commit
92a209c77b
1 changed files with 99 additions and 11 deletions
  1. 99
    11
      src/com/itmill/toolkit/ui/Upload.java

+ 99
- 11
src/com/itmill/toolkit/ui/Upload.java View File



package com.itmill.toolkit.ui; package com.itmill.toolkit.ui;


import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
// Gets the output target stream // Gets the output target stream
final OutputStream out = receiver.receiveUpload(filename, type); final OutputStream out = receiver.receiveUpload(filename, type);
if (out == null) { if (out == null) {
fireUploadInterrupted(filename, type, 0);
fireNoOutputStream(filename, type, 0);
endUpload(); endUpload();
throw new RuntimeException(
"Error getting outputstream from upload receiver");
return;
} }


final InputStream in = upload.getStream(); final InputStream in = upload.getStream();


if (null == in) { if (null == in) {
// No file, for instance non-existent filename in html upload // No file, for instance non-existent filename in html upload
fireUploadInterrupted(filename, type, 0);
fireNoInputStream(filename, type, 0);
endUpload(); endUpload();
return; return;
} }
endUpload(); endUpload();
requestRepaint(); requestRepaint();


} catch (final IOException e) {
} catch (final Exception e) {


// Download interrupted // Download interrupted
fireUploadInterrupted(filename, type, totalBytes);
fireUploadInterrupted(filename, type, totalBytes, e);
endUpload(); endUpload();
} }
} }
*/ */
private static final long serialVersionUID = 3833746590157386293L; private static final long serialVersionUID = 3833746590157386293L;


private Exception reason = null;

/**
*
* @param source
* @param filename
* @param MIMEType
* @param length
* @param exception
*/
public FailedEvent(Upload source, String filename, String MIMEType,
long length, Exception reason) {
this(source, filename, MIMEType, length);
this.reason = reason;
}

/** /**
* *
* @param source * @param source
* @param filename * @param filename
* @param MIMEType * @param MIMEType
* @param length * @param length
* @param exception
*/ */
public FailedEvent(Upload source, String filename, String MIMEType, public FailedEvent(Upload source, String filename, String MIMEType,
long length) { long length) {
super(source, filename, MIMEType, length); super(source, filename, MIMEType, length);
} }


/**
* Gets the exception that caused the failure.
*
* @return the exception that caused the failure, null if n/a
*/
public Exception getReason() {
return reason;
}

}

/**
* FailedEvent that indicates that an output stream could not be obtained.
*/
public class NoOutputStreamEvent extends FailedEvent {

/**
* Serial generated by eclipse.
*/
private static final long serialVersionUID = 4745219890852396500L;

/**
*
* @param source
* @param filename
* @param MIMEType
* @param length
*/
public NoOutputStreamEvent(Upload source, String filename,
String MIMEType, long length) {
super(source, filename, MIMEType, length);
}
}

/**
* FailedEvent that indicates that an input stream could not be obtained.
*/
public class NoInputStreamEvent extends FailedEvent {

/**
* Serial generated by eclipse.
*/
private static final long serialVersionUID = -529960205445737170L;

/**
*
* @param source
* @param filename
* @param MIMEType
* @param length
*/
public NoInputStreamEvent(Upload source, String filename,
String MIMEType, long length) {
super(source, filename, MIMEType, length);
}

} }


/** /**
* the Listener to be removed. * the Listener to be removed.
*/ */
public void removeListener(StartedListener listener) { public void removeListener(StartedListener listener) {
removeListener(FinishedEvent.class, listener, UPLOAD_STARTED_METHOD);
removeListener(StartedEvent.class, listener, UPLOAD_STARTED_METHOD);
} }


/** /**
* the Listener to be removed. * the Listener to be removed.
*/ */
public void removeListener(FailedListener listener) { public void removeListener(FailedListener listener) {
removeListener(FinishedEvent.class, listener, UPLOAD_FAILED_METHOD);
removeListener(FailedEvent.class, listener, UPLOAD_FAILED_METHOD);
} }


/** /**
} }


/** /**
* Emit upload received event.
* Emit upload finished event.
* *
* @param filename * @param filename
* @param MIMEType * @param MIMEType
} }


/** /**
* Emits the upload interrupted event.
* Emits the upload failed event.
* *
* @param filename * @param filename
* @param MIMEType * @param MIMEType
fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length)); fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length));
} }


protected void fireNoInputStream(String filename, String MIMEType,
long length) {
fireEvent(new Upload.NoInputStreamEvent(this, filename, MIMEType,
length));
}

protected void fireNoOutputStream(String filename, String MIMEType,
long length) {
fireEvent(new Upload.NoOutputStreamEvent(this, filename, MIMEType,
length));
}

protected void fireUploadInterrupted(String filename, String MIMEType,
long length, Exception e) {
fireEvent(new Upload.FailedEvent(this, filename, MIMEType, length, e));
}

/** /**
* Emits the upload success event. * Emits the upload success event.
* *

Loading…
Cancel
Save