Skip to content

Replace StreamResource with DownloadHandler in Collaboration Kit docs #4444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: v24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ To display an image inside the avatar, you have two options. First, if the image

=== Images from Backend

The way to load images from a database into components such as `Image` or `Avatar`, is to use `StreamResources`. `CollaborationAvatarGroup` supports stream resources with the `setImageProvider` method. It takes a function that generates `StreamResources` for users based on their `UserInfo`.
The way to load images from a database into components such as `Image` or `Avatar`, is to use a `DownloadHandler`. `CollaborationAvatarGroup` supports download handlers with the `setImageHandler` method. It takes a function that generates a `DownloadHandler` for users based on their `UserInfo`.

For example, if the user entity contains the image as a byte array, you can create a `StreamResource` that loads those bytes and let the framework take care of hosting the image like so:
For example, if the user entity contains the image as a byte array, you can create a `DownloadHandler` that loads those bytes and let the framework take care of hosting the image like so:

[source,java]
----
include::{root}/src/main/java/com/vaadin/demo/collaboration/AvatarGroupDocumentation.java[tags=avatar-group-images,indent=0]
----

Regarding this image provider pattern, `StreamResource` isn't serializable as JSON. As a result, it can't be included in `UserInfo` or sent back and forth through Collaboration Kit.
Regarding this image provider pattern, `DownloadHandler` isn't serializable as JSON. As a result, it can't be included in `UserInfo` or sent back and forth through Collaboration Kit.


== Scoping Avatars with Topics
Expand Down Expand Up @@ -94,7 +94,7 @@ It's a common design pattern to display the user's avatar separately from the ot
include::{root}/src/main/java/com/vaadin/demo/collaboration/AvatarGroupDocumentation.java[tags=avatar-group-own,indent=0]
----

You can set the same `name`, `abbreviation`, and `image` properties as contained in the `UserInfo`. To load the image from a backend, you can pass a `StreamResource` directly to the `Avatar::setImageResource` method.
You can set the same `name`, `abbreviation`, and `image` properties as contained in the `UserInfo`. To load the image from a backend, you can pass a `DownloadHandler` directly to the `Avatar::setImageHandler` method.


[discussion-id]`2374106C-8FD4-4AED-B1F3-5045FFF81F55`
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ include::{root}/src/main/java/com/vaadin/demo/collaboration/MessageListDocumenta
[NOTE]
[classname]`User` and [classname]`UserService` are application-specific example classes. They're not part of the Collaboration Kit API.

The [classname]`CollaborationMessageList` constructor takes two arguments: information about the end user associated with this session; and a topic identifier. The user information is used to render the user name and avatar in the messages submitted by the user. See the <<collaboration-avatar-group#ce.configuring-avatars, `CollaborationAvatarGroup` documentation>> for more details on how the avatars are rendered, or how to load images from a backend with the [methodname]`setImageProvider()` method.
The [classname]`CollaborationMessageList` constructor takes two arguments: information about the end user associated with this session; and a topic identifier. The user information is used to render the user name and avatar in the messages submitted by the user. See the <<collaboration-avatar-group#ce.configuring-avatars, `CollaborationAvatarGroup` documentation>> for more details on how the avatars are rendered, or how to load images from a backend with the [methodname]`setImageHandler()` method.

The topic identifier works the same as for other Collaboration Kit features. The data in Collaboration Kit is shared among those users who are connected to the same topic. With these components, the topic acts like a chat room.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import com.vaadin.demo.domain.User.UserService;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.server.streams.DownloadHandler;
import com.vaadin.flow.server.streams.DownloadResponse;

/**
* Code snippets used in CollaborationAvatarGroup's reference documentation.
Expand Down Expand Up @@ -41,19 +42,19 @@ public AvatarGroupDocumentation() {
// end::avatar-group-own[]
}

private void imageProvider() {
private void imageHandler() {
// tag::avatar-group-images[]
avatarGroup.setImageProvider(userInfo -> {
StreamResource streamResource = new StreamResource(
"avatar_" + userInfo.getId(), () -> {
avatarGroup.setImageHandler(userInfo ->
DownloadHandler.fromInputStream(
event -> {
User userEntity = userService
.findById(userInfo.getId());
byte[] imageBytes = userEntity.getImage();
return new ByteArrayInputStream(imageBytes);
});
streamResource.setContentType("image/png");
return streamResource;
});
return new DownloadResponse(new ByteArrayInputStream(imageBytes),
"avatar_" + userInfo.getId() + ".png",
"image/png", imageBytes.length);
})
);
// end::avatar-group-images[]
}

Expand Down
Loading