You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Stéphane V <sv...@gmail.com> on 2024/08/04 10:52:06 UTC
Image without page versioning
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
Re: Image without page versioning
Posted by Bas Gooren <ba...@iswd.nl>.
Hi all,
To prevent unnecessary requests for unchanged avatars, I recommend adding
the last-modified timestamp to the url; If you have it for the avatar, add
that, if not, add the last-modified of the user.
This way you can let the browser cache the avatar for a year. When it
doesn’t change, it will only get requested once.
Met vriendelijke groet,
Kind regards,
Bas Gooren
Op 5 augustus 2024 bij 21:09:43, Ernesto Reinaldo Barreiro (
reiern70@gmail.com) schreef:
Thanks for sharing. If your users can change the avatar maybe you want at
"client side" to add some request parameter with the timestamp when the
user updated the avatar. This way you might avoid browser caching the
avatar and displaying an outdated one.
On Mon, Aug 5, 2024 at 1:31 PM Stéphane V <sv...@gmail.com> wrote:
> 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 <ba...@iswd.nl>
> Envoyé : dimanche 4 août 2024 13:29
> À : users@wicket.apache.org <us...@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 <sv...@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Regards - Ernesto Reinaldo Barreiro
Re: Image without page versioning
Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Thanks for sharing. If your users can change the avatar maybe you want at
"client side" to add some request parameter with the timestamp when the
user updated the avatar. This way you might avoid browser caching the
avatar and displaying an outdated one.
On Mon, Aug 5, 2024 at 1:31 PM Stéphane V <sv...@gmail.com> wrote:
> 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 <ba...@iswd.nl>
> Envoyé : dimanche 4 août 2024 13:29
> À : users@wicket.apache.org <us...@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 <sv...@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Regards - Ernesto Reinaldo Barreiro
RE: Image without page versioning
Posted by Stéphane V <sv...@gmail.com>.
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 <ba...@iswd.nl>
Envoyé : dimanche 4 août 2024 13:29
À : users@wicket.apache.org <us...@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 <sv...@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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Image without page versioning
Posted by Bas Gooren <ba...@iswd.nl>.
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 <sv...@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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org