8000 fix: Preserve handler type when chaining listener methods (#21398) · vaadin/flow@cdaa758 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit cdaa758

Browse files
authored
fix: Preserve handler type when chaining listener methods (#21398)
Allows getting the proper type instead of TransferProgressAwareHandler to pass to component. Related-to #21343
1 parent ac743ca commit cdaa758

8 files changed

+73
-69
lines changed

flow-server/src/main/java/com/vaadin/flow/server/TransferProgressListener.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
* Implementations of this interface can be used to monitor the progress of file
3434
* transfers, such as downloads or uploads.
3535
* <p>
36-
* It uses {@link com.vaadin.flow.component.UI#access} to send UI changes from
37-
* progress listeners when the download or upload request is being handled.
38-
* Thus, it needs {@link com.vaadin.flow.component.page.Push} to be enabled in
39-
* the application.
36+
* It uses {@link com.vaadin.flow.component.UI#access(Command)} to send UI
37+
* changes from progress listeners when the download or upload request is being
38+
* handled. Thus, it needs {@link com.vaadin.flow.component.page.Push} to be
39+
* enabled in the application.
4040
*
4141
* @since 24.8
4242
*/
@@ -61,11 +61,12 @@ public interface TransferProgressListener extends Serializable {
6161
* Called when the data transfer is started.
6262
* <p>
6363
* The call of this method is wrapped by the
64-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
65-
* here when the download or upload request is being handled. Thus, no need
66-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
67-
* of this method. This needs {@link com.vaadin.flow.component.page.Push} to
68-
* be enabled in the application to properly send the UI changes to client.
64+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
65+
* defined here when the download or upload request is being handled. Thus,
66+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
67+
* the implementation of this method. This needs
68+
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
69+
* application to properly send the UI changes to client.
6970
*
7071
* @param context
7172
* the context of the transfer
@@ -78,11 +79,12 @@ default void onStart(TransferContext context) {
7879
* Called periodically during the transfer to report progress.
7980
* <p>
8081
* The call of this method is wrapped by the
81-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
82-
* here when the download or upload request is being handled. Thus, no need
83-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
84-
* of this method. This needs {@link com.vaadin.flow.component.page.Push} to
85-
* be enabled in the application to properly send the UI changes to client.
82+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
83+
* defined here when the download or upload request is being handled. Thus,
84+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
85+
* the implementation of this method. This needs
86+
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
87+
* application to properly send the UI changes to client.
8688
*
8789
* @param context
8890
* the context of the transfer
@@ -102,11 +104,12 @@ default void onProgress(TransferContext context, long transferredBytes,
102104
* Called when the transfer is failed.
103105
* <p>
104106
* The call of this method is wrapped by the
105-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
106-
* here when the download or upload request is being handled. Thus, no need
107-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
108-
* of this method. This needs {@link com.vaadin.flow.component.page.Push} to
109-
* be enabled in the application to properly send the UI changes to client.
107+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
108+
* defined here when the download or upload request is being handled. Thus,
109+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
110+
* the implementation of this method. This needs
111+
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
112+
* application to properly send the UI changes to client.
110113
*
111114
* @param context
112115
* the context of the transfer
@@ -121,11 +124,12 @@ default void onError(TransferContext context, IOException reason) {
121124
* Called when the transfer is started.
122125
* <p>
123126
* The call of this method is wrapped by the
124-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
125-
* here when the download or upload request is being handled. Thus, no need
126-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
127-
* of this method. This needs {@link com.vaadin.flow.component.page.Push} to
128-
* be enabled in the application to properly send the UI changes to client.
127+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
128+
* defined here when the download or upload request is being handled. Thus,
129+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
130+
* the implementation of this method. This needs
131+
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
132+
* application to properly send the UI changes to client.
129133
*
130134
* @param context
131135
* the context of the transfer

flow-server/src/main/java/com/vaadin/flow/server/streams/AbstractDownloadHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
/**
2323
* Abstract class for common methods used in pre-made download handlers.
2424
*
25+
* @param <R>
26+
* the type of the subclass implementing this abstract class
2527
* @since 24.8
2628
*/
27-
public abstract class AbstractDownloadHandler extends
28-
TransferProgressAwareHandler<DownloadEvent> implements DownloadHandler {
29+
public abstract class AbstractDownloadHandler<R extends AbstractDownloadHandler>
30+
extends TransferProgressAwareHandler<DownloadEvent, R>
31+
implements DownloadHandler {
2932

3033
@Override
3134
protected TransferContext getTransferContext(DownloadEvent transferEvent) {

flow-server/src/main/java/com/vaadin/flow/server/streams/ClassDownloadHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
*
3737
* @since 24.8
3838
*/
39-
public class ClassDownloadHandler extends AbstractDownloadHandler {
39+
public class ClassDownloadHandler
40+
extends AbstractDownloadHandler<ClassDownloadHandler> {
4041

4142
private final Class<?> clazz;
4243
private final String resourceName;

flow-server/src/main/java/com/vaadin/flow/server/streams/FileDownloadHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
*
3434
* @since 24.8
3535
*/
36-
public class FileDownloadHandler extends AbstractDownloadHandler {
36+
public class FileDownloadHandler
37+
extends AbstractDownloadHandler<FileDownloadHandler> {
3738

3839
private final File file;
3940
private final String name;

flow-server/src/main/java/com/vaadin/flow/server/streams/InputStreamDownloadHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
*
3333
* @since 24.8
3434
*/
35-
public class InputStreamDownloadHandler extends AbstractDownloadHandler {
35+
public class InputStreamDownloadHandler
36+
extends AbstractDownloadHandler<InputStreamDownloadHandler> {
3637

3738
private final SerializableFunction<DownloadEvent, DownloadResponse> handler;
3839
private final String name;

flow-server/src/main/java/com/vaadin/flow/server/streams/ServletResourceDownloadHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
*
3636
* @since 24.8
3737
*/
38-
public class ServletResourceDownloadHandler extends AbstractDownloadHandler {
38+
public class ServletResourceDownloadHandler
39+
extends AbstractDownloadHandler<ServletResourceDownloadHandler> {
3940

4041
private final String path;
4142
private final String name;

flow-server/src/main/java/com/vaadin/flow/server/streams/TransferProgressAwareHandler.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
* @param <T>
4242
* type of transfer event, e.g.
4343
* {@link com.vaadin.flow.server.DownloadHandler}
44+
* @param <R>
45+
* type of the subclass implementing this abstract class, needed for
46+
* revealing a proper type when you chain the methods
4447
*/
45-
public abstract class TransferProgressAwareHandler<T> implements Serializable {
48+
public abstract class TransferProgressAwareHandler<T, R extends TransferProgressAwareHandler>
49+
implements Serializable {
4650

4751
private List<TransferProgressListener> listeners;
4852

@@ -66,10 +70,10 @@ public abstract class TransferProgressAwareHandler<T> implements Serializable {
6670
* </ul>
6771
* <p>
6872
* The calls of the given listener's methods are wrapped by the
69-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
70-
* here when the download or upload request is being handled. Thus, no need
71-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
72-
* of the given listener. This needs
73+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
74+
* defined here when the download or upload request is being handled. Thus,
75+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
76+
* the implementation of the given listener. This needs
7377
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
7478
* application to properly send the UI changes to client.
7579
*
@@ -90,21 +94,18 @@ public Registration addTransferProgressListener(
9094
* Adds a listener to be notified when the transfer starts.
9195
* <p>
9296
* The call of the given callback is wrapped by the
93-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
94-
* here when the download or upload request is being handled. Thus, no need
95-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
96-
* of the given handler. This needs
97+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
98+
* defined here when the download or upload request is being handled. Thus,
99+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
100+
* the implementation of the given handler. This needs
97101
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
98102
* application to properly send the UI changes to client.
99103
*
100104
* @param startHandler
101105
* the handler to be called when the transfer starts
102106
* @return this instance for method chaining
103-
* @param <R>
104-
* the type of this transfer progress aware handler
105107
*/
106-
public <R extends TransferProgressAwareHandler<T>> R whenStart(
107-
SerializableRunnable startHandler) {
108+
public R whenStart(SerializableRunnable startHandler) {
108109
Objects.requireNonNull(startHandler, "Start handler cannot be null");
109110
addTransferProgressListenerInternal(new TransferProgressListener() {
110111
@Override
@@ -121,10 +122,10 @@ public void onStart(TransferContext context) {
121122
* Adds a listener to be notified of transfer progress.
122123
* <p>
123124
* The call of the given callback is wrapped by the
124-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
125-
* here when the download or upload request is being handled. Thus, no need
126-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
127-
* of the given handler. This needs
125+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
126+
* defined here when the download or upload request is being handled. Thus,
127+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
128+
* the implementation of the given handler. This needs
128129
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
129130
* application to properly send the UI changes to client.
130131
*
@@ -133,11 +134,8 @@ public void onStart(TransferContext context) {
133134
* @param progressIntervalInBytes
134135
* the interval in bytes for reporting progress
135136
* @return this instance for method chaining
136-
* @param <R>
137-
* the type of this transfer progress aware handler
138137
*/
139-
public <R extends TransferProgressAwareHandler<T>> R onProgress(
140-
SerializableBiConsumer<Long, Long> progressHandler,
138+
public R onProgress(SerializableBiConsumer<Long, Long> progressHandler,
141139
long progressIntervalInBytes) {
142140
Objects.requireNonNull(progressHandler,
143141
"Progress handler cannot be null");
@@ -166,21 +164,18 @@ public long progressReportInterval() {
166164
* the second is the total number of bytes.
167165
* <p>
168166
* The call of the given callback is wrapped by the
169-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
170-
* here when the download or upload request is being handled. Thus, no need
171-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
172-
* of the given handler. This needs
167+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
168+
* defined here when the download or upload request is being handled. Thus,
169+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
170+
* the implementation of the given handler. This needs
173171
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
174172
* application to properly send the UI changes to client.
175173
*
176174
* @param progressHandler
177175
* the handler to be called with the current and total bytes
178176
* @return this instance for method chaining
179-
* @param <R>
180-
* the type of this transfer progress aware handler
181177
*/
182-
public <R extends TransferProgressAwareHandler<T>> R onProgress(
183-
SerializableBiConsumer<Long, Long> progressHandler) {
178+
public R onProgress(SerializableBiConsumer<Long, Long> progressHandler) {
184179
return onProgress(progressHandler,
185180
TransferProgressListener.DEFAULT_PROGRESS_REPORT_INTERVAL_IN_BYTES);
186181
}
@@ -191,20 +186,18 @@ public <R extends TransferProgressAwareHandler<T>> R onProgress(
191186
* whether the transfer was completed successfully (true) or not (false).
192187
* <p>
193188
* The call of the given callback is wrapped by the
194-
* {@link com.vaadin.flow.component.UI#access} to send UI changes defined
195-
* here when the download or upload request is being handled. Thus, no need
196-
* to call {@link com.vaadin.flow.component.UI#access} in the implementation
197-
* of the given handler. This needs
189+
* {@link com.vaadin.flow.component.UI#access(Command)} to send UI changes
190+
* defined here when the download or upload request is being handled. Thus,
191+
* no need to call {@link com.vaadin.flow.component.UI#access(Command)} in
192+
* the implementation of the given handler. This needs
198193
* {@link com.vaadin.flow.component.page.Push} to be enabled in the
199194
* application to properly send the UI changes to client.
200195
*
201196
* @param completeOrTerminateHandler
202197
* the handler to be called when the transfer is completed
203198
* @return this instance for method chaining
204-
* @param <R>
205-
* the type of this transfer progress aware handler
206199
*/
207-
public <R extends TransferProgressAwareHandler<T>> R whenComplete(
200+
public R whenComplete(
208201
SerializableConsumer<Boolean> completeOrTerminateHandler) {
209202
Objects.requireNonNull(completeOrTerminateHandler,
210203
"Complete or terminate handler cannot be null");

flow-server/src/test/java/com/vaadin/flow/server/streams/AbstractDownloadHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class AbstractDownloadHandlerTest {
5050
private static final long TRANSFERRED_BYTES = 42L;
5151
private static final IOException EXCEPTION = new IOException("Test error");
5252

53-
private AbstractDownloadHandler handler;
53+
private AbstractDownloadHandler<?> handler;
5454
private TransferContext mockContext;
5555
private TransferProgressListener listener;
5656

@@ -84,7 +84,7 @@ public void setUp() throws IOException {
8484
downloadEvent = new DownloadEvent(request, response, session,
8585
"download", "application/octet-stream", owner);
8686

87-
handler = new AbstractDownloadHandler() {
87+
handler = new AbstractDownloadHandler<>() {
8888
@Override
8989
public void handleDownloadRequest(DownloadEvent event) {
9090
}
@@ -162,7 +162,7 @@ public void whenComplete() {
162162
public void customHandlerWithShorthandCompleteListener_noErrorInTransfer_success_errorInTransfer_failure()
163163
throws IOException {
164164
AtomicBoolean successAtomic = new AtomicBoolean(false);
165-
AbstractDownloadHandler customHandler = new AbstractDownloadHandler() {
165+
AbstractDownloadHandler customHandler = new AbstractDownloadHandler<>() {
166166
@Override
167167
public void handleDownloadRequest(DownloadEvent event) {
168168
ByteArrayInputStream inputStream = new ByteArrayInputStream(

0 commit comments

Comments
 (0)
0