diff options
author | Matti Tahvonen <matti.tahvonen@itmill.com> | 2010-11-03 21:53:53 +0000 |
---|---|---|
committer | Matti Tahvonen <matti.tahvonen@itmill.com> | 2010-11-03 21:53:53 +0000 |
commit | 83c63a773c12541718895329775594e283c3ccb9 (patch) | |
tree | 60aeb89183b0fce973cafc758eb2070b4ecda9d7 /src/com | |
parent | f30ee0dc5252ac742c000fd6cfd50eb8899479aa (diff) | |
download | vaadin-framework-83c63a773c12541718895329775594e283c3ccb9.tar.gz vaadin-framework-83c63a773c12541718895329775594e283c3ccb9.zip |
#5743, #5742, #5741, #4275 related refactoring. Renamed com.vaadin.terminal.Receiver (introduced earlier in this dev branch) to StreamVariable and combined ReceiverOwner methods to it. Resetted Upload.Receiver to 6.4 state, DD wrapper uses StreamVariable based API (breaking change, but very rare code atm)
svn changeset:15849/svn branch:6.5
Diffstat (limited to 'src/com')
22 files changed, 458 insertions, 737 deletions
diff --git a/src/com/vaadin/terminal/PaintTarget.java b/src/com/vaadin/terminal/PaintTarget.java index 2ee69247e7..76ebd19a7d 100644 --- a/src/com/vaadin/terminal/PaintTarget.java +++ b/src/com/vaadin/terminal/PaintTarget.java @@ -152,26 +152,26 @@ public interface PaintTarget extends Serializable { public void addAttribute(String name, Resource value) throws PaintException; /** - * Adds details about {@link Receiver} to the UIDL stream. Eg. in web + * Adds details about {@link StreamVariable} to the UIDL stream. Eg. in web * terminals Receivers are typically rendered for the client side as URLs, * where the client side implementation can do an http post request. * <p> - * Note that a Receiver can only be used once per "paint". The same Receiver + * Note that a StreamVariable can only be used once per "paint". The same StreamVariable * can be used several times, but it must be repainted before the next * stream can be received. * * @param owner * the ReceiverOwner that can track the progress of streaming to - * the given Receiver + * the given StreamVariable * @param name - * an identifying name for the Receiver + * an identifying name for the StreamVariable * @param value - * the Receiver to paint + * the StreamVariable to paint * * @throws PaintException * if the paint operation failed. */ - public void addVariable(ReceiverOwner owner, String name, Receiver value) + public void addVariable(VariableOwner owner, String name, StreamVariable value) throws PaintException; /** diff --git a/src/com/vaadin/terminal/Receiver.java b/src/com/vaadin/terminal/Receiver.java deleted file mode 100644 index 0841ad01e0..0000000000 --- a/src/com/vaadin/terminal/Receiver.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.vaadin.terminal; - -import java.io.OutputStream; -import java.io.Serializable; - -/** - * Receiver is a special kind of variable whose value is streamed to a given - * {@link OutputStream}. E.g. in web terminals Receivers can be used to send - * large files from browsers to the server. - * <p> - * Note, writing to the {@link OutputStream} is not synchronized by the terminal - * (to avoid stalls in other operations when eg. streaming to a slow network - * service). If UI is changed as a side effect of writing to the output stream, - * developer must handle synchronization manually. - * <p> - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 6.5 - * @see PaintTarget#addVariable(ReceiverOwner, String, Receiver) - * @see ReceiverOwner - */ -public interface Receiver extends Serializable { - - /** - * Invoked by the terminal when a new upload arrives. - * - * @param filename - * the filename of the upload if known by the terminal, usually - * as specified by the client. - * @param MIMEType - * the MIME type of the uploaded file. - * @return Stream to which the uploaded file should be written. - */ - public OutputStream receiveUpload(String filename, String MIMEType); -} diff --git a/src/com/vaadin/terminal/ReceiverOwner.java b/src/com/vaadin/terminal/ReceiverOwner.java deleted file mode 100644 index 6f19192081..0000000000 --- a/src/com/vaadin/terminal/ReceiverOwner.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.vaadin.terminal; - -import java.io.Serializable; - -import com.vaadin.Application; -import com.vaadin.terminal.ReceiverOwner.ReceivingController; - -/** - * Special kind of {@link VariableOwner} that can send and receive information - * with the terminal implementation about the progress of receiving data to its - * Receiver. The actual communication happens via {@link ReceivingController} - * which is fetched by the terminal when the Receiving is about to start. - */ -public interface ReceiverOwner extends VariableOwner { - - /* - * The monitor/control is passed to separate ReceivingController because: - * - * - possibly some component in the future may need support for streaming to - * multiple Receivers at the same time. - * - * - we don't want to bloat implementing ReceiverOwner's API. Now only one - * method is published and they can decide what event/methods to publish as - * their public API. - */ - - /** - * Returns a handle for the terminal via the ReceiverOwner can monitor and - * control the steaming of data to {@link Receiver}. - * <p> - * Most commonly ReceiverOwner implementation wants to implement this method - * as final and reveal its own API for the end users. - * - * @param receiver - * the Receiver whose streaming is to be controlled - * @return a {@link ReceivingController} that will be used to control and - * monitor the progress of streaming - */ - ReceivingController getReceivingController(Receiver receiver); - - interface ReceivingEvent extends Serializable { - - /** - * @return the file name of the streamed file if known - */ - String getFileName(); - - /** - * @return the mime type of the streamed file if known - */ - String getMimeType(); - - /** - * @return the Receiver into which the content is being streamed - */ - Receiver getReceiver(); - - /** - * @return the length of the stream (in bytes) if known, else -1 - */ - long getContentLength(); - - /** - * @return then number of bytes streamed to Receiver - */ - long getBytesReceived(); - } - - /** - * Event passed to - * {@link ReceivingController#uploadStarted(ReceivingStartedEvent)} method - * before the streaming of the content to {@link Receiver} starts. - */ - public interface ReceivingStartedEvent extends ReceivingEvent { - } - - /** - * Event passed to - * {@link ReceivingController#onProgress(ReceivingProgressedEvent)} method - * during the streaming progresses. - */ - public interface ReceivingProgressedEvent extends ReceivingEvent { - } - - /** - * Event passed to - * {@link ReceivingController#uploadFinished(ReceivingEndedEvent)} method - * the contents have been streamed to Receiver successfully. - */ - public interface ReceivingEndedEvent extends ReceivingEvent { - } - - /** - * Event passed to - * {@link ReceivingController#uploadFailed(ReceivingFailedEvent)} method - * when the streaming ended before the end of the input. The streaming may - * fail due an interruption by {@link ReceivingController} or due an other - * unknown exception in communication. In the latter case the exception is - * also passed to - * {@link Application#terminalError(com.vaadin.terminal.Terminal.ErrorEvent)} - * . - */ - public interface ReceivingFailedEvent extends ReceivingEvent { - - /** - * @return the exception that caused the receiving not to finish cleanly - */ - Exception getException(); - - } - - public interface ReceivingController extends Serializable { - /** - * Whether the {@link #onProgress(long, long)} method should be called - * during the upload. - * <p> - * {@link #onProgress(long, long)} is called in a synchronized block - * when the content is being received. This is potentially bit slow, so - * we are calling that method only if requested. The value is requested - * after the {@link #uploadStarted(ReceivingStartedEvent)} event, but - * not after reading each buffer. - * - * @return true if this ReceiverOwner wants to by notified during the - * upload of the progress of streaming. - * @see ReceiverOwner#onProgress(int, int) - */ - boolean listenProgress(); - - /** - * This method is called by the terminal if {@link #listenProgress()} - * returns true when the streaming starts. - */ - void onProgress(ReceivingProgressedEvent event); - - void uploadStarted(ReceivingStartedEvent event); - - void uploadFinished(ReceivingEndedEvent event); - - void uploadFailed(ReceivingFailedEvent event); - - /* - * Not synchronized to avoid stalls (caused by UIDL requests) while - * streaming the content. Implementations also most commonly atomic even - * without the restriction. - */ - /** - * ReceiverOwner can set this flag to true if it wants the Terminal to - * stop receiving current upload. - * <p> - * Note, the usage of this method is not synchronized over the - * Application instance by the terminal like other methods. The - * implementation should only return a boolean field and especially not - * modify UI or implement a synchronization by itself. - * - * @return true if the streaming should be interrupted as soon as - * possible. - */ - boolean isInterrupted(); - } - -} diff --git a/src/com/vaadin/terminal/StreamVariable.java b/src/com/vaadin/terminal/StreamVariable.java new file mode 100644 index 0000000000..aeadaad611 --- /dev/null +++ b/src/com/vaadin/terminal/StreamVariable.java @@ -0,0 +1,145 @@ +package com.vaadin.terminal; + +import java.io.OutputStream; +import java.io.Serializable; + +import com.vaadin.Application; + +/** + * StreamVariable is a special kind of variable whose value is streamed to an + * {@link OutputStream} provided by the {@link #getOutputStream()} method. E.g. + * in web terminals {@link StreamVariable} can be used to send large files from + * browsers to the server without consuming large amounts of memory. + * <p> + * Note, writing to the {@link OutputStream} is not synchronized by the terminal + * (to avoid stalls in other operations when eg. streaming to a slow network + * service or file system). If UI is changed as a side effect of writing to the + * output stream, developer must handle synchronization manually. + * <p> + * + * @author IT Mill Ltd. + * @version + * @VERSION@ + * @since 6.5 + * @see PaintTarget#addVariable(VariableOwner, String, StreamVariable) + */ +public interface StreamVariable extends Serializable { + + /** + * Invoked by the terminal when a new upload arrives, after + * {@link #streamingStarted(StreamingStartedEvent)} method has been called. + * The terminal implementation will write the streamed variable to the + * returned output stream. + * + * @return Stream to which the uploaded file should be written. + */ + public OutputStream getOutputStream(); + + /** + * Whether the {@link #onProgress(long, long)} method should be called + * during the upload. + * <p> + * {@link #onProgress(long, long)} is called in a synchronized block when + * the content is being received. This is potentially bit slow, so we are + * calling that method only if requested. The value is requested after the + * {@link #uploadStarted(StreamingStartedEvent)} event, but not after + * reading each buffer. + * + * @return true if this ReceiverOwner wants to by notified during the upload + * of the progress of streaming. + * @see ReceiverOwner#onProgress(int, int) + */ + boolean listenProgress(); + + /** + * This method is called by the terminal if {@link #listenProgress()} + * returns true when the streaming starts. + */ + void onProgress(StreamingProgressedEvent event); + + void streamingStarted(StreamingStartedEvent event); + + void streamingFinished(StreamingEndedEvent event); + + void streamingFailed(StreamingFailedEvent event); + + /* + * Not synchronized to avoid stalls (caused by UIDL requests) while + * streaming the content. Implementations also most commonly atomic even + * without the restriction. + */ + /** + * ReceiverOwner can set this flag to true if it wants the Terminal to stop + * receiving current upload. + * <p> + * Note, the usage of this method is not synchronized over the Application + * instance by the terminal like other methods. The implementation should + * only return a boolean field and especially not modify UI or implement a + * synchronization by itself. + * + * @return true if the streaming should be interrupted as soon as possible. + */ + boolean isInterrupted(); + + interface StreamingEvent extends Serializable { + + /** + * @return the file name of the streamed file if known + */ + String getFileName(); + + /** + * @return the mime type of the streamed file if known + */ + String getMimeType(); + + /** + * @return the length of the stream (in bytes) if known, else -1 + */ + long getContentLength(); + + /** + * @return then number of bytes streamed to StreamVariable + */ + long getBytesReceived(); + } + + /** + * Event passed to {@link #uploadStarted(StreamingStartedEvent)} method + * before the streaming of the content to {@link StreamVariable} starts. + */ + public interface StreamingStartedEvent extends StreamingEvent { + } + + /** + * Event passed to {@link #onProgress(StreamingProgressedEvent)} method + * during the streaming progresses. + */ + public interface StreamingProgressedEvent extends StreamingEvent { + } + + /** + * Event passed to {@link #uploadFinished(StreamingEndedEvent)} method the + * contents have been streamed to StreamVariable successfully. + */ + public interface StreamingEndedEvent extends StreamingEvent { + } + + /** + * Event passed to {@link #uploadFailed(StreamingFailedEvent)} method when + * the streaming ended before the end of the input. The streaming may fail + * due an interruption by {@link } or due an other unknown exception in + * communication. In the latter case the exception is also passed to + * {@link Application#terminalError(com.vaadin.terminal.Terminal.ErrorEvent)} + * . + */ + public interface StreamingFailedEvent extends StreamingEvent { + + /** + * @return the exception that caused the receiving not to finish cleanly + */ + Exception getException(); + + } + +} diff --git a/src/com/vaadin/terminal/UploadStream.java b/src/com/vaadin/terminal/UploadStream.java deleted file mode 100644 index 95351e071d..0000000000 --- a/src/com/vaadin/terminal/UploadStream.java +++ /dev/null @@ -1,50 +0,0 @@ -/* -@ITMillApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal; - -import java.io.InputStream; -import java.io.Serializable; - -/** - * Defines a variable type, that is used for passing uploaded files from - * terminal. Most often, file upload is implented using the - * {@link com.vaadin.ui.Upload Upload} component. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 3.0 - */ -public interface UploadStream extends Serializable { - - /** - * Gets the name of the stream. - * - * @return the name of the stream. - */ - public String getStreamName(); - - /** - * Gets the input stream. - * - * @return the Input stream. - */ - public InputStream getStream(); - - /** - * Gets the input stream content type. - * - * @return the content type of the input stream. - */ - public String getContentType(); - - /** - * Gets stream content name. Stream content name usually differs from the - * actual stream name. It is used to identify the content of the stream. - * - * @return the Name of the stream content. - */ - public String getContentName(); -} diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index 6102505883..73b83957c2 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -51,12 +51,10 @@ import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Paintable; import com.vaadin.terminal.Paintable.RepaintRequestEvent; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner; -import com.vaadin.terminal.ReceiverOwner.ReceivingController; -import com.vaadin.terminal.ReceiverOwner.ReceivingEndedEvent; -import com.vaadin.terminal.ReceiverOwner.ReceivingFailedEvent; -import com.vaadin.terminal.ReceiverOwner.ReceivingStartedEvent; +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingEndedEvent; +import com.vaadin.terminal.StreamVariable.StreamingFailedEvent; +import com.vaadin.terminal.StreamVariable.StreamingStartedEvent; import com.vaadin.terminal.Terminal.ErrorEvent; import com.vaadin.terminal.Terminal.ErrorListener; import com.vaadin.terminal.URIHandler; @@ -369,18 +367,18 @@ public abstract class AbstractCommunicationManager implements /** * Method used to stream content from a multipart request (either from - * servlet or portlet request) to given Receiver + * servlet or portlet request) to given StreamVariable * * * @param request * @param response - * @param receiver + * @param streamVariable * @param owner * @param boundary * @throws IOException */ protected void doHandleSimpleMultipartFileUpload(Request request, - Response response, Receiver receiver, ReceiverOwner owner, + Response response, StreamVariable streamVariable, VariableOwner owner, String boundary) throws IOException { boundary = CRLF + "--" + boundary + "--"; @@ -530,7 +528,7 @@ public abstract class AbstractCommunicationManager implements throw new UploadException( "Warning: file upload ignored because the componente was read-only"); } - streamToReceiver(simpleMultiPartReader, receiver, owner, filename, + streamToReceiver(simpleMultiPartReader, streamVariable, filename, mimeType, contentLength); } catch (Exception e) { synchronized (application) { @@ -547,13 +545,13 @@ public abstract class AbstractCommunicationManager implements * * @param request * @param response - * @param receiver + * @param streamVariable * @param owner * @param contentLength * @throws IOException */ protected void doHandleXhrFilePost(Request request, Response response, - Receiver receiver, ReceiverOwner owner, int contentLength) + StreamVariable streamVariable, VariableOwner owner, int contentLength) throws IOException { // These are unknown in filexhr ATM, maybe add to Accept header that @@ -569,9 +567,9 @@ public abstract class AbstractCommunicationManager implements Component component = (Component) owner; if (component.isReadOnly()) { throw new UploadException( - "Warning: file upload ignored because the componente was read-only"); + "Warning: file upload ignored because the component was read-only"); } - streamToReceiver(stream, receiver, owner, filename, mimeType, + streamToReceiver(stream, streamVariable, filename, mimeType, contentLength); } catch (Exception e) { synchronized (application) { @@ -583,15 +581,12 @@ public abstract class AbstractCommunicationManager implements } protected final void streamToReceiver(final InputStream in, - Receiver receiver, ReceiverOwner source, String filename, + StreamVariable streamVariable, String filename, String type, int contentLength) throws UploadException { - if (receiver == null) { - throw new IllegalStateException("Receiver for the post not found"); + if (streamVariable == null) { + throw new IllegalStateException("StreamVariable for the post not found"); } - ReceivingController controller = source - .getReceivingController(receiver); - final Application application = getApplication(); OutputStream out = null; @@ -599,11 +594,11 @@ public abstract class AbstractCommunicationManager implements try { boolean listenProgress; synchronized (application) { - ReceivingStartedEvent startedEvent = new ReceivingStartedEventImpl( - receiver, filename, type, contentLength); - controller.uploadStarted(startedEvent); - out = receiver.receiveUpload(filename, type); - listenProgress = controller.listenProgress(); + StreamingStartedEvent startedEvent = new StreamingStartedEventImpl( + streamVariable, filename, type, contentLength); + streamVariable.streamingStarted(startedEvent); + out = streamVariable.getOutputStream(); + listenProgress = streamVariable.listenProgress(); } // Gets the output target stream @@ -625,42 +620,42 @@ public abstract class AbstractCommunicationManager implements // update progress if listener set and contentLength // received synchronized (application) { - ReceivingProgressedEventImpl progressEvent = new ReceivingProgressedEventImpl( - receiver, filename, type, contentLength, + StreamingProgressedEventImpl progressEvent = new StreamingProgressedEventImpl( + streamVariable, filename, type, contentLength, totalBytes); - controller.onProgress(progressEvent); + streamVariable.onProgress(progressEvent); } } - if (controller.isInterrupted()) { + if (streamVariable.isInterrupted()) { throw new UploadInterruptedException(); } } // upload successful out.close(); - ReceivingEndedEvent event = new ReceivingEndedEventImpl(receiver, + StreamingEndedEvent event = new StremingEndedEventImpl(streamVariable, filename, type, totalBytes); synchronized (application) { - controller.uploadFinished(event); + streamVariable.streamingFinished(event); } } catch (UploadInterruptedException e) { // Download interrupted by application code tryToCloseStream(out); - ReceivingFailedEvent event = new ReceivingFailedEventImpl(receiver, + StreamingFailedEvent event = new StreamingFailedEventImpl(streamVariable, filename, type, contentLength, totalBytes, e); synchronized (application) { - controller.uploadFailed(event); + streamVariable.streamingFailed(event); } // Note, we are not throwing interrupted exception forward as it is // not a terminal level error like all other exception. } catch (final Exception e) { tryToCloseStream(out); synchronized (application) { - ReceivingFailedEvent event = new ReceivingFailedEventImpl( - receiver, filename, type, contentLength, totalBytes, e); + StreamingFailedEvent event = new StreamingFailedEventImpl( + streamVariable, filename, type, contentLength, totalBytes, e); synchronized (application) { - controller.uploadFailed(event); + streamVariable.streamingFailed(event); } // throw exception for terminal to be handled (to be passed to // terminalErrorHandler) @@ -2186,6 +2181,6 @@ public abstract class AbstractCommunicationManager implements } - abstract String createReceiverUrl(ReceiverOwner owner, String name, - Receiver value); + abstract String createReceiverUrl(VariableOwner owner, String name, + StreamVariable value); } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractReceivingEvent.java b/src/com/vaadin/terminal/gwt/server/AbstractStreamingEvent.java index a3ada4ea53..91039d05e5 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractReceivingEvent.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractStreamingEvent.java @@ -1,16 +1,16 @@ package com.vaadin.terminal.gwt.server; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingEvent; +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingEvent; /** - * Abstract base class for ReceivingEvent implementations. + * Abstract base class for StreamingEvent implementations. */ @SuppressWarnings("serial") -abstract class AbstractReceivingEvent implements ReceivingEvent { +abstract class AbstractStreamingEvent implements StreamingEvent { private final String type; private final String filename; - private final Receiver receiver; + private final StreamVariable streamVariable; private final long contentLength; private final long bytesReceived; @@ -22,17 +22,17 @@ abstract class AbstractReceivingEvent implements ReceivingEvent { return type; } - protected AbstractReceivingEvent(Receiver receiver, String filename, + protected AbstractStreamingEvent(StreamVariable streamVariable, String filename, String type, long length, long bytesReceived) { - this.receiver = receiver; + this.streamVariable = streamVariable; this.filename = filename; this.type = type; contentLength = length; this.bytesReceived = bytesReceived; } - public final Receiver getReceiver() { - return receiver; + public final StreamVariable getReceiver() { + return streamVariable; } public final long getContentLength() { diff --git a/src/com/vaadin/terminal/gwt/server/CommunicationManager.java b/src/com/vaadin/terminal/gwt/server/CommunicationManager.java index df937688cb..73f6732cf0 100644 --- a/src/com/vaadin/terminal/gwt/server/CommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/CommunicationManager.java @@ -20,8 +20,8 @@ import com.vaadin.Application; import com.vaadin.terminal.ApplicationResource; import com.vaadin.terminal.DownloadStream; import com.vaadin.terminal.Paintable; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner; +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.VariableOwner; import com.vaadin.ui.Component; import com.vaadin.ui.Window; @@ -208,7 +208,7 @@ public class CommunicationManager extends AbstractCommunicationManager { /** * Handles file upload request submitted via Upload component. * - * @see #createReceiverUrl(ReceiverOwner, String, Receiver) + * @see #createReceiverUrl(ReceiverOwner, String, StreamVariable) * * @param request * @param response @@ -231,23 +231,23 @@ public class CommunicationManager extends AbstractCommunicationManager { String uppUri = pathInfo.substring(startOfData); String[] parts = uppUri.split("/", 3); // 0 = pid, 1= name, 2 = sec key - Receiver receiver = pidToNameToReceiver.get(parts[0]).remove(parts[1]); - String secKey = receiverToSeckey.remove(receiver); + StreamVariable streamVariable = pidToNameToReceiver.get(parts[0]).remove(parts[1]); + String secKey = receiverToSeckey.remove(streamVariable); if (secKey.equals(parts[2])) { - ReceiverOwner source = (ReceiverOwner) getVariableOwner(parts[0]); + VariableOwner source = (VariableOwner) getVariableOwner(parts[0]); String contentType = request.getContentType(); if (request.getContentType().contains("boundary")) { // Multipart requests contain boundary string doHandleSimpleMultipartFileUpload( new HttpServletRequestWrapper(request), - new HttpServletResponseWrapper(response), receiver, + new HttpServletResponseWrapper(response), streamVariable, source, contentType.split("boundary=")[1]); } else { // if boundary string does not exist, the posted file is from // XHR2.post(File) doHandleXhrFilePost(new HttpServletRequestWrapper(request), - new HttpServletResponseWrapper(response), receiver, + new HttpServletResponseWrapper(response), streamVariable, source, request.getContentLength()); } } else { @@ -336,8 +336,8 @@ public class CommunicationManager extends AbstractCommunicationManager { @Override protected void unregisterPaintable(Component p) { /* Cleanup possible receivers */ - if (pidToNameToReceiver != null && p instanceof ReceiverOwner) { - Map<String, Receiver> removed = pidToNameToReceiver + if (pidToNameToReceiver != null) { + Map<String, StreamVariable> removed = pidToNameToReceiver .remove(getPaintableId(p)); if (removed != null) { for (String key : removed.keySet()) { @@ -349,12 +349,16 @@ public class CommunicationManager extends AbstractCommunicationManager { } - private Map<String, Map<String, Receiver>> pidToNameToReceiver; + private Map<String, Map<String, StreamVariable>> pidToNameToReceiver; - private Map<Receiver, String> receiverToSeckey; + private Map<StreamVariable, String> receiverToSeckey; @Override - String createReceiverUrl(ReceiverOwner owner, String name, Receiver value) { + String createReceiverUrl(VariableOwner owner, String name, StreamVariable value) { + + /* + * TODO figure out how this can be simplified now that ReceiverOwner is removed. + */ /* * We will use the same APP/* URI space as ApplicationResources but @@ -365,25 +369,25 @@ public class CommunicationManager extends AbstractCommunicationManager { * SECKEY is created on each paint to make URL's unpredictable (to * prevent CSRF attacks). * - * NAME and PID from URI forms a key to fetch Receiver when handling + * NAME and PID from URI forms a key to fetch StreamVariable when handling * post */ String paintableId = getPaintableId((Paintable) owner); String key = paintableId + "/" + name; if (pidToNameToReceiver == null) { - pidToNameToReceiver = new HashMap<String, Map<String, Receiver>>(); + pidToNameToReceiver = new HashMap<String, Map<String, StreamVariable>>(); } - Map<String, Receiver> nameToReceiver = pidToNameToReceiver + Map<String, StreamVariable> nameToReceiver = pidToNameToReceiver .get(paintableId); if (nameToReceiver == null) { - nameToReceiver = new HashMap<String, Receiver>(); + nameToReceiver = new HashMap<String, StreamVariable>(); pidToNameToReceiver.put(paintableId, nameToReceiver); } nameToReceiver.put(name, value); if (receiverToSeckey == null) { - receiverToSeckey = new HashMap<Receiver, String>(); + receiverToSeckey = new HashMap<StreamVariable, String>(); } String seckey = UUID.randomUUID().toString(); receiverToSeckey.put(value, seckey); diff --git a/src/com/vaadin/terminal/gwt/server/HttpUploadStream.java b/src/com/vaadin/terminal/gwt/server/HttpUploadStream.java deleted file mode 100644 index 40b5d41004..0000000000 --- a/src/com/vaadin/terminal/gwt/server/HttpUploadStream.java +++ /dev/null @@ -1,90 +0,0 @@ -/* -@ITMillApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal.gwt.server; - -import java.io.InputStream; - -/** - * AjaxAdapter implementation of the UploadStream interface. - * - * @author IT Mill Ltd. - * @version - * @VERSION@ - * @since 5.0 - */ -@SuppressWarnings("serial") -public class HttpUploadStream implements com.vaadin.terminal.UploadStream { - - /** - * Holds value of property variableName. - */ - private final String streamName; - - private final String contentName; - - private final String contentType; - - /** - * Holds value of property variableValue. - */ - private final InputStream stream; - - /** - * Creates a new instance of UploadStreamImpl. - * - * @param name - * the name of the stream. - * @param stream - * the input stream. - * @param contentName - * the name of the content. - * @param contentType - * the type of the content. - */ - public HttpUploadStream(String name, InputStream stream, - String contentName, String contentType) { - streamName = name; - this.stream = stream; - this.contentName = contentName; - this.contentType = contentType; - } - - /** - * Gets the name of the stream. - * - * @return the name of the stream. - */ - public String getStreamName() { - return streamName; - } - - /** - * Gets the input stream. - * - * @return the Input stream. - */ - public InputStream getStream() { - return stream; - } - - /** - * Gets the input stream content type. - * - * @return the content type of the input stream. - */ - public String getContentType() { - return contentType; - } - - /** - * Gets the stream content name. Stream content name usually differs from - * the actual stream name. It is used to identify the content of the stream. - * - * @return the Name of the stream content. - */ - public String getContentName() { - return contentName; - } -} diff --git a/src/com/vaadin/terminal/gwt/server/JsonPaintTarget.java b/src/com/vaadin/terminal/gwt/server/JsonPaintTarget.java index 27b9ea4ae9..2ea372264d 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonPaintTarget.java +++ b/src/com/vaadin/terminal/gwt/server/JsonPaintTarget.java @@ -29,8 +29,7 @@ import com.vaadin.terminal.ExternalResource; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Paintable; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner; +import com.vaadin.terminal.StreamVariable; import com.vaadin.terminal.Resource; import com.vaadin.terminal.ThemeResource; import com.vaadin.terminal.VariableOwner; @@ -1112,7 +1111,7 @@ public class JsonPaintTarget implements PaintTarget { return usedPaintableTypes; } - public void addVariable(ReceiverOwner owner, String name, Receiver value) + public void addVariable(VariableOwner owner, String name, StreamVariable value) throws PaintException { String url = manager.createReceiverUrl(owner, name, value); addVariable(owner, name, url); diff --git a/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java index 37beafa172..cf2cab0edf 100644 --- a/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java @@ -24,8 +24,8 @@ import javax.servlet.http.HttpServletRequestWrapper; import com.vaadin.Application; import com.vaadin.terminal.DownloadStream; import com.vaadin.terminal.Paintable; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner; +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.VariableOwner; import com.vaadin.ui.Component; import com.vaadin.ui.Window; @@ -197,8 +197,8 @@ public class PortletCommunicationManager extends AbstractCommunicationManager { String contentType = request.getContentType(); String name = request.getParameter("name"); String ownerId = request.getParameter("rec-owner"); - ReceiverOwner variableOwner = (ReceiverOwner) getVariableOwner(ownerId); - Receiver receiver = ownerToNameToReceiver.get(variableOwner).remove( + VariableOwner variableOwner = (VariableOwner) getVariableOwner(ownerId); + StreamVariable streamVariable = ownerToNameToReceiver.get(variableOwner).remove( name); // clean up, may be re added on next paint @@ -207,11 +207,11 @@ public class PortletCommunicationManager extends AbstractCommunicationManager { if (contentType.contains("boundary")) { doHandleSimpleMultipartFileUpload( new PortletRequestWrapper(request), - new PortletResponseWrapper(response), receiver, + new PortletResponseWrapper(response), streamVariable, variableOwner, contentType.split("boundary=")[1]); } else { doHandleXhrFilePost(new PortletRequestWrapper(request), - new PortletResponseWrapper(response), receiver, + new PortletResponseWrapper(response), streamVariable, variableOwner, request.getContentLength()); } @@ -271,16 +271,16 @@ public class PortletCommunicationManager extends AbstractCommunicationManager { application, assumedWindow); } - private Map<ReceiverOwner, Map<String, Receiver>> ownerToNameToReceiver; + private Map<VariableOwner, Map<String, StreamVariable>> ownerToNameToReceiver; @Override - String createReceiverUrl(ReceiverOwner owner, String name, Receiver value) { + String createReceiverUrl(VariableOwner owner, String name, StreamVariable value) { if (ownerToNameToReceiver == null) { - ownerToNameToReceiver = new HashMap<ReceiverOwner, Map<String, Receiver>>(); + ownerToNameToReceiver = new HashMap<VariableOwner, Map<String, StreamVariable>>(); } - Map<String, Receiver> nameToReceiver = ownerToNameToReceiver.get(owner); + Map<String, StreamVariable> nameToReceiver = ownerToNameToReceiver.get(owner); if (nameToReceiver == null) { - nameToReceiver = new HashMap<String, Receiver>(); + nameToReceiver = new HashMap<String, StreamVariable>(); ownerToNameToReceiver.put(owner, nameToReceiver); } nameToReceiver.put(name, value); diff --git a/src/com/vaadin/terminal/gwt/server/ReceivingEndedEventImpl.java b/src/com/vaadin/terminal/gwt/server/ReceivingEndedEventImpl.java deleted file mode 100644 index 06609f058e..0000000000 --- a/src/com/vaadin/terminal/gwt/server/ReceivingEndedEventImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.vaadin.terminal.gwt.server; - -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingEndedEvent; - -@SuppressWarnings("serial") -final class ReceivingEndedEventImpl extends AbstractReceivingEvent implements - ReceivingEndedEvent { - - public ReceivingEndedEventImpl(Receiver receiver, String filename, - String type, long totalBytes) { - super(receiver, filename, type, totalBytes, totalBytes); - } - -} diff --git a/src/com/vaadin/terminal/gwt/server/ReceivingFailedEventImpl.java b/src/com/vaadin/terminal/gwt/server/ReceivingFailedEventImpl.java deleted file mode 100644 index c732e69cc9..0000000000 --- a/src/com/vaadin/terminal/gwt/server/ReceivingFailedEventImpl.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.vaadin.terminal.gwt.server; - -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingFailedEvent; - -@SuppressWarnings("serial") -final class ReceivingFailedEventImpl extends AbstractReceivingEvent implements - ReceivingFailedEvent { - - private final Exception exception; - - public ReceivingFailedEventImpl(Receiver receiver, final String filename, - final String type, long contentLength, long bytesReceived, - final Exception exception) { - super(receiver, filename, type, contentLength, bytesReceived); - this.exception = exception; - } - - public final Exception getException() { - return exception; - } - -} diff --git a/src/com/vaadin/terminal/gwt/server/ReceivingProgressedEventImpl.java b/src/com/vaadin/terminal/gwt/server/ReceivingProgressedEventImpl.java deleted file mode 100644 index f28f0ad453..0000000000 --- a/src/com/vaadin/terminal/gwt/server/ReceivingProgressedEventImpl.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.vaadin.terminal.gwt.server; - -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingProgressedEvent; - -@SuppressWarnings("serial") -final class ReceivingProgressedEventImpl extends AbstractReceivingEvent - implements ReceivingProgressedEvent { - - public ReceivingProgressedEventImpl(Receiver receiver, - final String filename, final String type, long contentLength, - long bytesReceived) { - super(receiver, filename, type, contentLength, bytesReceived); - } - -} diff --git a/src/com/vaadin/terminal/gwt/server/ReceivingStartedEventImpl.java b/src/com/vaadin/terminal/gwt/server/ReceivingStartedEventImpl.java deleted file mode 100644 index beb2ac8e25..0000000000 --- a/src/com/vaadin/terminal/gwt/server/ReceivingStartedEventImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.vaadin.terminal.gwt.server; - -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingStartedEvent; - -@SuppressWarnings("serial") -final class ReceivingStartedEventImpl extends AbstractReceivingEvent implements - ReceivingStartedEvent { - - public ReceivingStartedEventImpl(Receiver receiver, final String filename, - final String type, long contentLength) { - super(receiver, filename, type, contentLength, 0); - } - -} diff --git a/src/com/vaadin/terminal/gwt/server/StreamingFailedEventImpl.java b/src/com/vaadin/terminal/gwt/server/StreamingFailedEventImpl.java new file mode 100644 index 0000000000..23fb3b0e86 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/StreamingFailedEventImpl.java @@ -0,0 +1,23 @@ +package com.vaadin.terminal.gwt.server; + +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingFailedEvent; + +@SuppressWarnings("serial") +final class StreamingFailedEventImpl extends AbstractStreamingEvent implements + StreamingFailedEvent { + + private final Exception exception; + + public StreamingFailedEventImpl(StreamVariable streamVariable, final String filename, + final String type, long contentLength, long bytesReceived, + final Exception exception) { + super(streamVariable, filename, type, contentLength, bytesReceived); + this.exception = exception; + } + + public final Exception getException() { + return exception; + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/StreamingProgressedEventImpl.java b/src/com/vaadin/terminal/gwt/server/StreamingProgressedEventImpl.java new file mode 100644 index 0000000000..6ca751d60c --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/StreamingProgressedEventImpl.java @@ -0,0 +1,16 @@ +package com.vaadin.terminal.gwt.server; + +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingProgressedEvent; + +@SuppressWarnings("serial") +final class StreamingProgressedEventImpl extends AbstractStreamingEvent + implements StreamingProgressedEvent { + + public StreamingProgressedEventImpl(StreamVariable streamVariable, + final String filename, final String type, long contentLength, + long bytesReceived) { + super(streamVariable, filename, type, contentLength, bytesReceived); + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/StreamingStartedEventImpl.java b/src/com/vaadin/terminal/gwt/server/StreamingStartedEventImpl.java new file mode 100644 index 0000000000..f0b37479a5 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/StreamingStartedEventImpl.java @@ -0,0 +1,15 @@ +package com.vaadin.terminal.gwt.server; + +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingStartedEvent; + +@SuppressWarnings("serial") +final class StreamingStartedEventImpl extends AbstractStreamingEvent implements + StreamingStartedEvent { + + public StreamingStartedEventImpl(StreamVariable streamVariable, final String filename, + final String type, long contentLength) { + super(streamVariable, filename, type, contentLength, 0); + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/StremingEndedEventImpl.java b/src/com/vaadin/terminal/gwt/server/StremingEndedEventImpl.java new file mode 100644 index 0000000000..9117ce95a1 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/StremingEndedEventImpl.java @@ -0,0 +1,15 @@ +package com.vaadin.terminal.gwt.server; + +import com.vaadin.terminal.StreamVariable; +import com.vaadin.terminal.StreamVariable.StreamingEndedEvent; + +@SuppressWarnings("serial") +final class StremingEndedEventImpl extends AbstractStreamingEvent implements + StreamingEndedEvent { + + public StremingEndedEventImpl(StreamVariable streamVariable, String filename, + String type, long totalBytes) { + super(streamVariable, filename, type, totalBytes, totalBytes); + } + +} diff --git a/src/com/vaadin/ui/DragAndDropWrapper.java b/src/com/vaadin/ui/DragAndDropWrapper.java index d2a769c591..d79402217c 100644 --- a/src/com/vaadin/ui/DragAndDropWrapper.java +++ b/src/com/vaadin/ui/DragAndDropWrapper.java @@ -3,8 +3,11 @@ */ package com.vaadin.ui; +import java.io.OutputStream; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import com.vaadin.event.Transferable; import com.vaadin.event.TransferableImpl; @@ -15,18 +18,16 @@ import com.vaadin.event.dd.TargetDetails; import com.vaadin.event.dd.TargetDetailsImpl; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner; +import com.vaadin.terminal.StreamVariable; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.ui.VDragAndDropWrapper; import com.vaadin.terminal.gwt.client.ui.dd.HorizontalDropLocation; import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation; -import com.vaadin.ui.Html5File.ProxyReceiver; @SuppressWarnings("serial") @ClientWidget(VDragAndDropWrapper.class) public class DragAndDropWrapper extends CustomComponent implements DropTarget, - DragSource, ReceiverOwner { + DragSource { public class WrapperTransferable extends TransferableImpl { @@ -185,22 +186,26 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, getDropHandler().getAcceptCriterion().paint(target); } if (receivers != null && receivers.size() > 0) { - for (String id : receivers.keySet()) { + for (Iterator<Entry<String, Html5File>> it = receivers.entrySet() + .iterator(); it.hasNext();) { + String id = it.next().getKey(); Html5File html5File = receivers.get(id); if (html5File.getReceiver() != null) { - target.addVariable(this, "rec-" + id, - html5File.getProxyReceiver()); + target.addVariable(this, "rec-" + id, new ProxyReceiver( + html5File)); + // these are cleaned from receivers once the upload has + // started } else { // instructs the client side not to send the file target.addVariable(this, "rec-" + id, (String) null); + // forget the file from subsequent paints + it.remove(); } } } } private DropHandler dropHandler; - private Html5File currentlyUploadedFile; - private boolean listenProgressOfUploadedFile; public DropHandler getDropHandler() { return dropHandler; @@ -229,98 +234,103 @@ public class DragAndDropWrapper extends CustomComponent implements DropTarget, return dragStartMode; } - /* - * Single controller is enough for atm as files are transferred in serial. - * If parallel transfer is needed, this logic needs to go to Html5File - */ - private ReceivingController controller = new ReceivingController() { - /* - * With XHR2 file posts we can't provide as much information from the - * terminal as with multipart request. This helper class wraps the - * terminal event and provides the lacking information from the - * Html5File. - */ - class ReceivingEventWrapper implements ReceivingFailedEvent, - ReceivingEndedEvent, ReceivingStartedEvent, - ReceivingProgressedEvent { - private ReceivingEvent wrappedEvent; + final class ProxyReceiver implements StreamVariable { - ReceivingEventWrapper(ReceivingEvent e) { - wrappedEvent = e; - } - - public String getMimeType() { - return currentlyUploadedFile.getType(); - } - - public String getFileName() { - return currentlyUploadedFile.getFileName(); - } + private Html5File file; - public long getContentLength() { - return currentlyUploadedFile.getFileSize(); - } + public ProxyReceiver(Html5File file) { + this.file = file; + } - public Receiver getReceiver() { - return currentlyUploadedFile.getReceiver(); - } + private boolean listenProgressOfUploadedFile; - public Exception getException() { - if (wrappedEvent instanceof ReceivingFailedEvent) { - return ((ReceivingFailedEvent) wrappedEvent).getException(); - } + public OutputStream getOutputStream() { + if (file.getReceiver() == null) { return null; } - - public long getBytesReceived() { - return wrappedEvent.getBytesReceived(); - } + return file.getReceiver().getOutputStream(); } public boolean listenProgress() { - return listenProgressOfUploadedFile; + return file.getReceiver().listenProgress(); } - public void onProgress(ReceivingProgressedEvent event) { - currentlyUploadedFile.getUploadListener().onProgress( - new ReceivingEventWrapper(event)); + public void onProgress(StreamingProgressedEvent event) { + file.getReceiver().onProgress(new ReceivingEventWrapper(event)); } - public void uploadStarted(ReceivingStartedEvent event) { - currentlyUploadedFile = ((ProxyReceiver) event.getReceiver()) - .getFile(); - listenProgressOfUploadedFile = currentlyUploadedFile - .getUploadListener() != null; + public void streamingStarted(StreamingStartedEvent event) { + listenProgressOfUploadedFile = file.getReceiver() != null; if (listenProgressOfUploadedFile) { - currentlyUploadedFile.getUploadListener().uploadStarted( + file.getReceiver().streamingStarted( new ReceivingEventWrapper(event)); } + // no need tell to the client about this receiver on next paint + receivers.remove(file); } - public void uploadFinished(ReceivingEndedEvent event) { + public void streamingFinished(StreamingEndedEvent event) { if (listenProgressOfUploadedFile) { - currentlyUploadedFile.getUploadListener().uploadFinished( + file.getReceiver().streamingFinished( new ReceivingEventWrapper(event)); } - receivers.remove(event.getReceiver()); } - public void uploadFailed(final ReceivingFailedEvent event) { + public void streamingFailed(final StreamingFailedEvent event) { if (listenProgressOfUploadedFile) { - currentlyUploadedFile.getUploadListener().uploadFailed( + file.getReceiver().streamingFailed( new ReceivingEventWrapper(event)); } - receivers.remove(event.getReceiver()); } public boolean isInterrupted() { - return currentlyUploadedFile.isInterrupted(); + return file.getReceiver().isInterrupted(); } - }; + /* + * With XHR2 file posts we can't provide as much information from the + * terminal as with multipart request. This helper class wraps the + * terminal event and provides the lacking information from the + * Html5File. + */ + class ReceivingEventWrapper implements StreamingFailedEvent, + StreamingEndedEvent, StreamingStartedEvent, + StreamingProgressedEvent { + + private StreamingEvent wrappedEvent; + + ReceivingEventWrapper(StreamingEvent e) { + wrappedEvent = e; + } + + public String getMimeType() { + return file.getType(); + } + + public String getFileName() { + return file.getFileName(); + } + + public long getContentLength() { + return file.getFileSize(); + } + + public StreamVariable getReceiver() { + return ProxyReceiver.this; + } + + public Exception getException() { + if (wrappedEvent instanceof StreamingFailedEvent) { + return ((StreamingFailedEvent) wrappedEvent).getException(); + } + return null; + } + + public long getBytesReceived() { + return wrappedEvent.getBytesReceived(); + } + } - public ReceivingController getReceivingController(Receiver receiver) { - return controller; } } diff --git a/src/com/vaadin/ui/Html5File.java b/src/com/vaadin/ui/Html5File.java index 6c3a91f3b0..7c01f9b0a4 100644 --- a/src/com/vaadin/ui/Html5File.java +++ b/src/com/vaadin/ui/Html5File.java @@ -1,14 +1,9 @@ package com.vaadin.ui; -import java.io.OutputStream; import java.io.Serializable; import com.vaadin.event.dd.DropHandler; -import com.vaadin.terminal.Receiver; -import com.vaadin.terminal.ReceiverOwner.ReceivingEndedEvent; -import com.vaadin.terminal.ReceiverOwner.ReceivingFailedEvent; -import com.vaadin.terminal.ReceiverOwner.ReceivingProgressedEvent; -import com.vaadin.terminal.ReceiverOwner.ReceivingStartedEvent; +import com.vaadin.terminal.StreamVariable; /** * {@link DragAndDropWrapper} can receive also files from client computer if @@ -16,23 +11,10 @@ import com.vaadin.terminal.ReceiverOwner.ReceivingStartedEvent; * information about dragged file on server side. */ public class Html5File implements Serializable { - - final class ProxyReceiver implements Receiver { - public OutputStream receiveUpload(String filename, String MIMEType) { - if (receiver == null) { - return null; - } - return receiver.receiveUpload(filename, MIMEType); - } - - Html5File getFile() { - return Html5File.this; - } - } - + private String name; private long size; - private Receiver receiver; + private StreamVariable streamVariable; private String type; Html5File(String name, long size, String mimeType) { @@ -41,14 +23,6 @@ public class Html5File implements Serializable { type = mimeType; } - /** - * The receiver that is registered to the terminal. Wraps the actual - * Receiver set later by Html5File user. - */ - private ProxyReceiver proxyReceiver = new ProxyReceiver(); - private boolean interrupted = false; - private Html5FileUploadListener listener; - public String getFileName() { return name; } @@ -62,10 +36,10 @@ public class Html5File implements Serializable { } /** - * Sets the {@link Receiver} that into which the file contents will be - * written. Usage of Receiver is similar to {@link Upload} component. + * Sets the {@link StreamVariable} that into which the file contents will be + * written. Usage of StreamVariable is similar to {@link Upload} component. * <p> - * If the {@link Receiver} is not set in the {@link DropHandler} the file + * If the {@link StreamVariable} is not set in the {@link DropHandler} the file * contents will not be sent to server. * <p> * <em>Note!</em> receiving file contents is experimental feature depending @@ -73,65 +47,17 @@ public class Html5File implements Serializable { * 3.6 and above and recent webkit based browsers (Safari 5, Chrome 6) at * this time. * - * @param receiver + * @param streamVariable * the callback that returns stream where the implementation * writes the file contents as it arrives. */ - public void setReceiver(Receiver receiver) { - this.receiver = receiver; - } - - public Receiver getReceiver() { - return receiver; - } - - ProxyReceiver getProxyReceiver() { - return proxyReceiver; - } - - /** - * Gets the {@link Html5FileUploadListener} that is used to track the - * progress of streaming the file contents to given {@link Receiver}. - * - * @return - */ - public Html5FileUploadListener getUploadListener() { - return listener; + public void setReceiver(StreamVariable streamVariable) { + this.streamVariable = streamVariable; } - /** - * Sets the {@link Html5FileUploadListener} that can be used to track the - * progress of streaming the file contents to given {@link Receiver}. - * - * @param listener - * @see #setReceiver(Receiver) - */ - public void setUploadListener(Html5FileUploadListener listener) { - this.listener = listener; - } - - public boolean isInterrupted() { - return interrupted; - } - - /** - * Interrupts uploading this file. - * - * @param interrupted - */ - public void setInterrupted(boolean interrupted) { - this.interrupted = interrupted; - } - - public interface Html5FileUploadListener extends Serializable { - - void onProgress(ReceivingProgressedEvent event); - - void uploadStarted(ReceivingStartedEvent event); - - void uploadFinished(ReceivingEndedEvent event); - - void uploadFailed(ReceivingFailedEvent event); + public StreamVariable getReceiver() { + return streamVariable; } + }
\ No newline at end of file diff --git a/src/com/vaadin/ui/Upload.java b/src/com/vaadin/ui/Upload.java index 413c0aab52..8e1a859f5f 100644 --- a/src/com/vaadin/ui/Upload.java +++ b/src/com/vaadin/ui/Upload.java @@ -4,6 +4,7 @@ package com.vaadin.ui; +import java.io.OutputStream; import java.io.Serializable; import java.lang.reflect.Method; import java.util.Iterator; @@ -12,7 +13,7 @@ import java.util.Map; import com.vaadin.terminal.PaintException; import com.vaadin.terminal.PaintTarget; -import com.vaadin.terminal.ReceiverOwner; +import com.vaadin.terminal.StreamVariable.StreamingStartedEvent; import com.vaadin.terminal.gwt.client.ui.VUpload; import com.vaadin.terminal.gwt.server.NoInputStreamException; import com.vaadin.terminal.gwt.server.NoOutputStreamException; @@ -60,8 +61,7 @@ import com.vaadin.ui.ClientWidget.LoadStyle; */ @SuppressWarnings("serial") @ClientWidget(value = VUpload.class, loadStyle = LoadStyle.LAZY) -public class Upload extends AbstractComponent implements Component.Focusable, - ReceiverOwner { +public class Upload extends AbstractComponent implements Component.Focusable { /** * Should the field be focused on next repaint? @@ -76,7 +76,7 @@ public class Upload extends AbstractComponent implements Component.Focusable, /** * The output of the upload is redirected to this receiver. */ - private com.vaadin.terminal.Receiver receiver; + private Receiver receiver; private boolean isUploading; @@ -106,33 +106,12 @@ public class Upload extends AbstractComponent implements Component.Focusable, public Upload() { } - /** - * @deprecated use - * {@link Upload#Upload(String, com.vaadin.terminal.Receiver)} - * instead - */ - @Deprecated public Upload(String caption, Receiver uploadReceiver) { setCaption(caption); receiver = uploadReceiver; } /** - * Creates a new instance of Upload that redirects the uploaded data to - * stream given by the Receiver. - * - * @param caption - * Normal component caption. You can set the caption of the - * upload submit button with setButtonCaption(). - * @param uploadReceiver - * Receiver to call to retrieve output stream when upload starts. - */ - public Upload(String caption, com.vaadin.terminal.Receiver uploadReceiver) { - setCaption(caption); - receiver = uploadReceiver; - } - - /** * Invoked when the value of a variable has changed. * * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object, @@ -181,8 +160,8 @@ public class Upload extends AbstractComponent implements Component.Focusable, target.addAttribute("nextid", nextid); - // Post file to this receiver - target.addVariable(this, "action", getReceiver()); + // Post file to this strean variable + target.addVariable(this, "action", getStreamVariable()); } @@ -194,11 +173,9 @@ public class Upload extends AbstractComponent implements Component.Focusable, * @version * @VERSION@ * @since 3.0 - * @deprecated use {@link com.vaadin.terminal.Receiver} instead. A "copy" - * here is kept for backwards compatibility. */ - @Deprecated - public interface Receiver extends com.vaadin.terminal.Receiver { + public interface Receiver extends Serializable { + public OutputStream receiveUpload(String filename, String mimetype); } /* Upload events */ @@ -753,36 +730,23 @@ public class Upload extends AbstractComponent implements Component.Focusable, /** * Returns the current receiver. * - * @return the Receiver. + * @return the StreamVariable. */ - public com.vaadin.terminal.Receiver getReceiver() { + public Receiver getReceiver() { return receiver; } /** * Sets the receiver. * - * @deprecated use {@link #setReceiver(com.vaadin.terminal.Receiver)} - * instead * @param receiver * the receiver to set. */ - @Deprecated public void setReceiver(Receiver receiver) { this.receiver = receiver; } /** - * Sets the receiver. - * - * @param receiver - * the receiver to set. - */ - public void setReceiver(com.vaadin.terminal.Receiver receiver) { - this.receiver = receiver; - } - - /** * {@inheritDoc} */ @Override @@ -942,50 +906,66 @@ public class Upload extends AbstractComponent implements Component.Focusable, * Handle to terminal via Upload monitors and controls the upload during it * is being streamed. */ - private final ReceivingController controller = new ReceivingController() { - public boolean listenProgress() { - return (progressListeners != null && !progressListeners.isEmpty()); - } - - public void onProgress(ReceivingProgressedEvent event) { - fireUpdateProgress(event.getBytesReceived(), - event.getContentLength()); - } - - public void uploadStarted(ReceivingStartedEvent event) { - startUpload(); - contentLength = event.getContentLength(); - fireStarted(event.getFileName(), event.getMimeType()); - } - - public void uploadFinished(ReceivingEndedEvent event) { - fireUploadSuccess(event.getFileName(), event.getMimeType(), - event.getContentLength()); - endUpload(); - requestRepaint(); - } - - public void uploadFailed(ReceivingFailedEvent event) { - Exception exception = event.getException(); - if (exception instanceof NoInputStreamException) { - fireNoInputStream(event.getFileName(), event.getMimeType(), 0); - } else if (exception instanceof NoOutputStreamException) { - fireNoOutputStream(event.getFileName(), event.getMimeType(), 0); - } else { - fireUploadInterrupted(event.getFileName(), event.getMimeType(), - 0, exception); - } - endUpload(); - } - - public boolean isInterrupted() { - return interrupted; - } - }; - - public ReceivingController getReceivingController( - com.vaadin.terminal.Receiver receiver) { - return controller; + private com.vaadin.terminal.StreamVariable streamVariable; + + protected com.vaadin.terminal.StreamVariable getStreamVariable() { + if (streamVariable == null) { + streamVariable = new com.vaadin.terminal.StreamVariable() { + private StreamingStartedEvent lastStartedEvent; + + public boolean listenProgress() { + return (progressListeners != null && !progressListeners + .isEmpty()); + } + + public void onProgress(StreamingProgressedEvent event) { + fireUpdateProgress(event.getBytesReceived(), + event.getContentLength()); + } + + public boolean isInterrupted() { + return interrupted; + } + + public OutputStream getOutputStream() { + OutputStream receiveUpload = receiver.receiveUpload( + lastStartedEvent.getFileName(), + lastStartedEvent.getMimeType()); + lastStartedEvent = null; + return receiveUpload; + } + + public void streamingStarted(StreamingStartedEvent event) { + startUpload(); + contentLength = event.getContentLength(); + fireStarted(event.getFileName(), event.getMimeType()); + lastStartedEvent = event; + } + + public void streamingFinished(StreamingEndedEvent event) { + fireUploadSuccess(event.getFileName(), event.getMimeType(), + event.getContentLength()); + endUpload(); + requestRepaint(); + } + + public void streamingFailed(StreamingFailedEvent event) { + Exception exception = event.getException(); + if (exception instanceof NoInputStreamException) { + fireNoInputStream(event.getFileName(), + event.getMimeType(), 0); + } else if (exception instanceof NoOutputStreamException) { + fireNoOutputStream(event.getFileName(), + event.getMimeType(), 0); + } else { + fireUploadInterrupted(event.getFileName(), + event.getMimeType(), 0, exception); + } + endUpload(); + } + }; + } + return streamVariable; } } |