Thank you Bas.

Here my implementation: maybe it will help someone:


public class AvatarResourceReference extends ResourceReference {

    private final IUserService userService;

    public AvatarResourceReference(IUserService userService) {
        super(AvatarResourceReference.class.getName());
        this.userService = userService;
    }


    @Override
    public IResource getResource() {

        return new AbstractResource() {
            @Override
            protected ResourceResponse newResourceResponse(Attributes 
attributes) {

                final ResourceResponse res = new ResourceResponse();

                // Check if the user has the role for the DASHBOARD
                // FIXME Add authorization with ROLES
//                if 
(!AuthenticatedWebSession.get().getRoles().contains("DASHBOARD")) {
//                    res.setError(HttpServletResponse.SC_FORBIDDEN);
//                    return res;
//                }

                // If the user PID est missing the request, return an HTTP error
                final StringValue userPidValue = 
attributes.getParameters().get("userPid");
                if (userPidValue.isEmpty()) {
                    res.setError(HttpServletResponse.SC_NOT_FOUND);
                    return res;
                }

                // Search the requested user. If not found, return an error
                final Optional<UserProfile> userProfile = 
userService.findUserByPid(userPidValue.toString());
                if (userProfile.isEmpty()) {
                    res.setError(HttpServletResponse.SC_NOT_FOUND);
                    return res;
                }

                final UserProfile user = userProfile.get();
                if (user.getAvatar() != null) {
                    res.setContentType(user.getAvatar().getContentType());
                } else {
                    res.setContentType("image/png");
                }
                res.setWriteCallback(new WriteCallback() {
                    @Override
                    public void writeData(Attributes attributes) throws 
IOException {
                        attributes.getResponse().write(getUserAvatar(user));
                    }
                });

                return res;
            }
        };
    }

    private byte[] getUserAvatar(UserProfile userProfile) {
        if (userProfile.getAvatar() != null) {
            return userProfile.getAvatar().getData();
        } else {
            try {
                return 
IOUtils.toByteArray(Objects.requireNonNull(UserListPanel.class.getResourceAsStream("avatar-unknown.png")));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

I'm using MarcGiffing framework. Here the initiailization of the resource 
reference:


public void init(WebApplication webApplication) {
    
webApplication.mountResource(String.format("/dashboard/resources/%s/${userPid}",
 AvatarResourceReference.class.getName()), new 
AvatarResourceReference(userService));
}

Stef
________________________________
De : Bas Gooren <b...@iswd.nl>
Envoyé : dimanche 4 août 2024 13:29
À : users@wicket.apache.org <users@wicket.apache.org>
Objet : Re: Image without page versioning

Hi!

Since you are using an image component with a component-local resource, this 
will always require the page to be versioned.
The url that is generated for the image will be page-specific, to be able to 
let the image component call the byte array resource.

If you wish to keep the page stateless, you’ll need to use a mounted resource 
for this.

You can create a mounted resource that gets the active user from the session 
(or return a 404 when the user is not logged in); Or you can build a stateless 
version which has some unique user identifier in the url.

// Bas

Verstuurd vanaf mijn iPhone

> Op 4 aug 2024 om 12:52 heeft Stéphane V <sva...@gmail.com> het volgende 
> geschreven:
>
> Hi,
>
> I'm using a NonCachingImage for the avatar image of my users. I was expecting 
> the page will not be versioned, but it's versioned.
>
> I'm using this code:
>
> new NonCachingImage("avatar", new ByteArrayResource("image/png", 
> getUserAvatar(userProfile)))
>
> How can I avoid to version the page ?
>
> Thank you.
>
> Stef

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to