Fixed code to be compliant with resteasy REACT

This commit is contained in:
Mauro Mugnaini 2024-03-28 17:17:45 +01:00
parent 07d9d9a927
commit 6474834487
Signed by: mauro.mugnaini
GPG Key ID: 2440CFD0EB321EA8
2 changed files with 11 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import org.jboss.resteasy.annotations.cache.NoCache;
import org.jboss.resteasy.reactive.RestForm;
import org.jboss.resteasy.reactive.multipart.FileUpload;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.keycloak.common.util.Resteasy;
import org.keycloak.http.HttpRequest;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.cors.Cors;
@ -19,7 +20,6 @@ import jakarta.ws.rs.OPTIONS;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
@ -43,8 +43,10 @@ public class AvatarResource extends AbstractAvatarResource {
}
@OPTIONS
public Response handleCorsPreflight(@Context final HttpRequest request) {
logger.info("Received CORS preflight request for ext/user; request is " + request);
public Response handleCorsPreflight() {
logger.info("Received CORS preflight request for ext/user");
KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
HttpRequest request = session.getContext().getHttpRequest();
return Cors.add(request, Response.ok())
.preflight()
.allowAllOrigins()
@ -56,12 +58,14 @@ public class AvatarResource extends AbstractAvatarResource {
@GET
@Produces({ "image/png", "image/jpeg", "image/gif" })
public Response downloadCurrentUserAvatarImage(@Context final HttpRequest request) {
public Response downloadCurrentUserAvatarImage() {
if (auth == null) {
logger.debug("Unhautorized call to get avatar");
throw new NotAuthorizedException("Bearer");
}
logger.debugf("Getting avatar for user %s in realm %s", auth.getUser(), auth.getSession().getRealm());
KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
HttpRequest request = session.getContext().getHttpRequest();
return Cors
.add(request,
Response.fromResponse(fetchAndCreateResponse(auth.getSession().getRealm(), auth.getUser())))
@ -75,7 +79,7 @@ public class AvatarResource extends AbstractAvatarResource {
@POST
@NoCache
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadCurrentUserAvatarImage(@RestForm(AVATAR_IMAGE_PARAMETER) FileUpload image, @Context UriInfo uriInfo) {
public Response uploadCurrentUserAvatarImage(@RestForm(AVATAR_IMAGE_PARAMETER) FileUpload image) {
if (auth == null) {
throw new NotAuthorizedException("Bearer");
}
@ -85,6 +89,8 @@ public class AvatarResource extends AbstractAvatarResource {
// }
logger.debugf("Uploading new avatar for user %s in realm %s", auth.getUser(), auth.getSession().getRealm());
KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
UriInfo uriInfo = session.getContext().getUri();
Response response = null;
try {
saveUserImage(auth.getSession().getRealm(), auth.getUser(), Files.newInputStream(image.uploadedFile()));