8000 feat: add factory method for UploadHandler (#21400) · vaadin/flow@c17d9cb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit c17d9cb

Browse files
authored
feat: add factory method for UploadHandler (#21400)
Add factory methods for in-memory, File and TemporaryFile for easier handling of upload. fixes #21235
1 parent b9603d5 commit c17d9cb

File tree

10 files changed

+557
-5
lines changed

10 files changed

+557
-5
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.UncheckedIOException;
24+
25+
import com.vaadin.flow.function.SerializableBiConsumer;
26+
import com.vaadin.flow.server.TransferProgressListener;
27+
28+
/**
29+
* Abstract class for file upload handler.
30+
*
31+
* @param <R>
32+
* type of file upload handler
33+
*
34+
* @since 24.8
35+
*/
36+
public abstract class AbstractFileUploadHandler<R extends AbstractFileUploadHandler>
37+
extends TransferProgressAwareHandler<UploadEvent, R>
38+
implements UploadHandler {
39+
private final SerializableBiConsumer<UploadMetadata, File> successHandler;
40+
private final FileFactory fileFactory;
41+
42+
/**
43+
* Creates an upload handler that stores the incoming stream into a
44+
* {@link File} that is generated by the given {@link FileFactory}.
45+
*
46+
* @param successHandler
47+
* consumer to be called for successful upload
48+
* @param fileFactory
49+
* factory that generates the {@link File} to store data
50+
*/
51+
public AbstractFileUploadHandler(
52+
SerializableBiConsumer<UploadMetadata, File> successHandler,
53+
FileFactory fileFactory) {
54+
this.successHandler = successHandler;
55+
this.fileFactory = fileFactory;
56+
}
57+
58+
@Override
59+
public void handleUploadRequest(UploadEvent event) {
60+
File file;
61+
try {
62+
file = fileFactory.createFile(event.getFileName());
63+
try (InputStream inputStream = event.getInputStream();
64+
FileOutputStream outputStream = new FileOutputStream(
65+
file)) {
66+
TransferProgressListener.transfer(inputStream, outputStream,
67+
getTransferContext(event), getListeners());
68+
}
69+
} catch (IOException e) {
70+
notifyError(event, e);
71+
throw new UncheckedIOException(e);
72+
}
73+
successHandler.accept(new UploadMetadata(event.getFileName(),
74+
event.getContentType(), event.getFileSize()), file);
75+
}
76+
77+
@Override
78+
protected TransferContext getTransferContext(UploadEvent transferEvent) {
79+
return new TransferContext(transferEvent.getRequest(),
80+
transferEvent.getResponse(), transferEvent.getSession(),
81+
transferEvent.getFileName(),
82+
transferEvent.getOwningComponent().getElement(),
83+
transferEvent.getFileSize());
84+
}
85+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.Serializable;
22+
23+
/**
24+
* File factory interface for generating file to store the uploaded data into.
25+
*
26+
* @since 24.8
27+
*/
28+
@FunctionalInterface
29+
public interface FileFactory extends Serializable {
30+
31+
/**
32+
* Create a new file for given file name.
33+
*
34+
* @param fileName
35+
* file name to create file for
36+
* @return {@link File} that should be used
37+
*/
38+
File createFile(String fileName) throws IOException;
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.File;
20+
21+
import com.vaadin.flow.function.SerializableBiConsumer;
22+
23+
/**
24+
* Upload handler that stores the data into a file generated by the given
25+
* {@link FileFactory}.
26+
*
27+
* @since 24.8
28+
*/
29+
public class FileUploadHandler
30+
extends AbstractFileUploadHandler<FileUploadHandler> {
31+
32+
/**
33+
* Constructor for file upload handler.
34+
*
35+
* @param successHandler
36+
* consumer to be called when upload successfully completes
37+
* @param fileFactory
38+
* factory for generating file to write to
39+
*/
40+
public FileUploadHandler(
41+
SerializableBiConsumer<UploadMetadata, File> successHandler,
42+
FileFactory fileFactory) {
43+
super(successHandler, fileFactory);
44+
}
45+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.UncheckedIOException;
23+
24+
import com.vaadin.flow.function.SerializableBiConsumer;
25+
import com.vaadin.flow.server.TransferProgressListener;
26+
27+
/**
28+
* Upload handler for storing the upload in-memory. Data is returned as a
29+
* {@code byte[]} to the given successHandler.
30+
*
31+
* @since 24.8
32+
*/
33+
public class InMemoryUploadHandler
34+
extends TransferProgressAwareHandler<UploadEvent, InMemoryUploadHandler>
35+
implements UploadHandler {
36+
private final SerializableBiConsumer<UploadMetadata, byte[]> successHandler;
37+
38+
public InMemoryUploadHandler(
39+
SerializableBiConsumer<UploadMetadata, byte[]> successHandler) {
40+
this.successHandler = successHandler;
41+
}
42+
43+
@Override
44+
public void handleUploadRequest(UploadEvent event) {
45+
byte[] data;
46+
try {
47+
try (InputStream inputStream = event.getInputStream();
48+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
49+
TransferProgressListener.transfer(inputStream, outputStream,
50+
getTransferContext(event), getListeners());
51+
data = outputStream.toByteArray();
52+
}
53+
} catch (IOException e) {
54+
notifyError(event, e);
55+
throw new UncheckedIOException(e);
56+
}
57+
successHandler.accept(new UploadMetadata(event.getFileName(),
58+
event.getContentType(), event.getFileSize()), data);
59+
}
60+
61+
@Override
62+
protected TransferContext getTransferContext(UploadEvent transferEvent) {
63+
return new TransferContext(transferEvent.getRequest(),
64+
transferEvent.getResponse(), transferEvent.getSession(),
65+
transferEvent.getFileName(),
66+
transferEvent.getOwningComponent().getElement(),
67+
transferEvent.getFileSize());
68+
}
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
/**
25+
* File factory to generate a temporary file for given file name
26+
*
27+
* @since 24.8
28+
*/
29+
public class TemporaryFileFactory implements FileFactory {
30+
31+
/**
32+
* Create a new temporary file for filename. Adds the suffix {@code .tmp}
33+
*/
34+
@Override
35+
public File createFile(String fileName) throws IOException {
36+
37+
Path tempDirPath;
38+
try {
39+
tempDirPath = Files.createTempDirectory("temp_dir");
40+
} catch (IOException e) {
41+
throw new IOException("Failed to create temp directory", e);
42+
}
43+
44+
return Files.createTempFile(tempDirPath, fileName, ".tmp").toFile();
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
17+
package com.vaadin.flow.server.streams;
18+
19+
import java.io.File;
20+
21+
import com.vaadin.flow.function.SerializableBiConsumer;
22+
23+
/**
24+
* Upload handler that stores the data into a temporary file. Stored temporary
25+
* file is returned in the successHandler for further use.
26+
*
27+
* @since 24.8
28+
*/
29+
public class TemporaryFileUploadHandler
30+
extends AbstractFileUploadHandler<TemporaryFileUploadHandler> {
31+
32+
public TemporaryFileUploadHandler(
33+
SerializableBiConsumer<UploadMetadata, File> successHandler) {
34+
super(successHandler, new TemporaryFileFactory());
35+
}
36+
}

0 commit comments

Comments
 (0)
0