|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component.upload.tests; |
| 17 | + |
| 18 | +import java.io.FilterOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | + |
| 22 | +import com.vaadin.flow.component.html.Div; |
| 23 | +import com.vaadin.flow.component.upload.Upload; |
| 24 | +import com.vaadin.flow.component.upload.receivers.MultiFileMemoryBuffer; |
| 25 | +import com.vaadin.flow.router.Route; |
| 26 | + |
| 27 | +@Route("vaadin-upload/interrupt") |
| 28 | +public class UploadInterruptView extends Div { |
| 29 | + |
| 30 | + public UploadInterruptView() { |
| 31 | + Div output = new Div(); |
| 32 | + output.setId("test-output"); |
| 33 | + Div eventsOutput = new Div(); |
| 34 | + eventsOutput.setId("test-events-output"); |
| 35 | + |
| 36 | + MultiFileMemoryBuffer buffer = new SlowMultiFileMemoryBuffer(); |
| 37 | + Upload upload = new Upload(buffer); |
| 38 | + upload.setAcceptedFileTypes(".txt"); |
| 39 | + upload.addStartedListener(event -> { |
| 40 | + if (isInterruptableFile(event.getFileName())) { |
| 41 | + event.getUpload().interruptUpload(); |
| 42 | + } |
| 43 | + }); |
| 44 | + upload.addFailedListener(event -> { |
| 45 | + eventsOutput.add("-failed"); |
| 46 | + output.add("FAILED:" + event.getFileName() + "," |
| 47 | + + event.getReason().getMessage()); |
| 48 | + }); |
| 49 | + upload.addSucceededListener(event -> eventsOutput.add("-succeeded")); |
| 50 | + upload.addAllFinishedListener(event -> eventsOutput.add("-finished")); |
| 51 | + |
| 52 | + add(upload, output, eventsOutput); |
| 53 | + } |
| 54 | + |
| 55 | + private static boolean isInterruptableFile(String fileName) { |
| 56 | + return fileName != null && fileName.endsWith(".interrupt.txt"); |
| 57 | + } |
| 58 | + |
| 59 | + // Returns an OutputStream that delays write operations for uploads. The |
| 60 | + // delay ensures that the interruption flag is set before uploads completion |
| 61 | + // so that the test can verify all uploads failed. |
| 62 | + private static class SlowMultiFileMemoryBuffer |
| 63 | + extends MultiFileMemoryBuffer { |
| 64 | + @Override |
| 65 | + public OutputStream receiveUpload(String fileName, String MIMEType) { |
| 66 | + OutputStream outputStream = super.receiveUpload(fileName, MIMEType); |
| 67 | + // Also delay the interrupted file to allow other uploads to start |
| 68 | + int delay = isInterruptableFile(fileName) ? 500 : 1000; |
| 69 | + return new SlowOutputStream(outputStream, delay); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static class SlowOutputStream extends FilterOutputStream { |
| 74 | + |
| 75 | + private final int delay; |
| 76 | + |
| 77 | + SlowOutputStream(OutputStream delegate, int delay) { |
| 78 | + super(delegate); |
| 79 | + this.delay = delay; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void write(byte[] b, int off, int len) throws IOException { |
| 84 | + try { |
| 85 | + Thread.sleep(delay); |
| 86 | + } catch (InterruptedException e) { |
| 87 | + Thread.currentThread().interrupt(); |
| 88 | + throw new IOException("Interrupted", e); |
| 89 | + } |
| 90 | + super.write(b, off, len); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments