Hello, community. I am new to Java and Vaadin in particular. Please explain to me why addSucceededListener() is triggered earlier (when loading a page) than I download a file?
@Route("")
public class UploadView extends VerticalLayout {
@Autowired
PictureRepository pictureRepository;
@Autowired
GoogleCloudAiAssistantRecognition googleCloudAiAssistantRecognition;
public UploadView() throws IOException {
this.pictureRepository = pictureRepository;
this.googleCloudAiAssistantRecognition = googleCloudAiAssistantRecognition;
add(new H1("Assistant recognition by Google Vision"));
MemoryBuffer buffer = new MemoryBuffer();
Upload upload = new Upload(buffer);
upload.setAcceptedFileTypes("image/jpeg", "image/png", "image/gif");
upload.setMaxFileSize(10485760);
Div output = new Div();
InputStream inputStream = buffer.getInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream);
upload.addSucceededListener(event -> {
add(new H1(event.getMIMEType()));
Component component = createComponent(event.getMIMEType(), event.getFileName(), buffer.getInputStream());
showOutput(event.getFileName(), component, output);
PictureModel pictureModel = new PictureModel(event.getFileName(), event.getMIMEType(), bytes);
Long picId = pictureRepository.save(pictureModel).getId();
List<TagModel> tags = null;
try {
tags = googleCloudAiAssistantRecognition.detect(picId);
} catch (Exception e) {
e.printStackTrace();
}
pictureModel.setTagModel(tags);
pictureRepository.save(pictureModel);
});
add(upload, output);
}
private Component createComponent(String mimeType, String fileName,
InputStream stream) {
if (mimeType.startsWith("text")) {
return createTextComponent(stream);
} else if (mimeType.startsWith("image")) {
Image image = new Image();
try {
byte[] bytes = IOUtils.toByteArray(stream);
image.getElement().setAttribute("src", new StreamResource(
fileName, () -> new ByteArrayInputStream(bytes)));
try (ImageInputStream in = ImageIO.createImageInputStream(
new ByteArrayInputStream(bytes))) {
final Iterator<ImageReader> readers = ImageIO
.getImageReaders(in);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(in);
image.setWidth(reader.getWidth(0) + "px");
image.setHeight(reader.getHeight(0) + "px");
} finally {
reader.dispose();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
Div content = new Div();
String text = String.format("Mime type: '%s'\nSHA-256 hash: '%s'",
mimeType, MessageDigestUtil.sha256(stream.toString()));
content.setText(text);
return content;
}
private Component createTextComponent(InputStream stream) {
String text;
try {
text = IOUtils.toString(stream, StandardCharsets.UTF_8);
} catch (IOException e) {
text = "exception reading stream";
}
return new Text(text);
}
private void showOutput(String text, Component content,
HasComponents outputContainer) {
HtmlComponent p = new HtmlComponent(Tag.P);
p.getElement().setText(text);
outputContainer.add(p);
outputContainer.add(content);
}
}
The problem is that many Listener listeners do not work in principle, all of them are executed before the file is loaded, even addFinishedListener(). I can see that I’m not alone in this problem, I found a workaround so far with upload.getElement().addEventListener(“upload-success” …
I also noticed that everything worked fine initially, until I started adding new Maven dependencies (I’m afraid I can’t say at what point I didn’t use git immediately).