KEYCLOAK-6032 Fix error page when internationalization is enabled
This commit is contained in:
parent
2b11938084
commit
b672229efc
2 changed files with 24 additions and 1 deletions
|
@ -16,6 +16,7 @@ import org.keycloak.services.util.LocaleHelper;
|
||||||
import org.keycloak.theme.FreeMarkerUtil;
|
import org.keycloak.theme.FreeMarkerUtil;
|
||||||
import org.keycloak.theme.Theme;
|
import org.keycloak.theme.Theme;
|
||||||
import org.keycloak.theme.ThemeProvider;
|
import org.keycloak.theme.ThemeProvider;
|
||||||
|
import org.keycloak.theme.beans.LocaleBean;
|
||||||
import org.keycloak.theme.beans.MessageBean;
|
import org.keycloak.theme.beans.MessageBean;
|
||||||
import org.keycloak.theme.beans.MessageFormatterMethod;
|
import org.keycloak.theme.beans.MessageFormatterMethod;
|
||||||
import org.keycloak.theme.beans.MessageType;
|
import org.keycloak.theme.beans.MessageType;
|
||||||
|
@ -123,13 +124,14 @@ public class KeycloakErrorHandler implements ExceptionMapper<Throwable> {
|
||||||
|
|
||||||
private Map<String, Object> initAttributes(RealmModel realm, Theme theme, Locale locale, int statusCode) throws IOException {
|
private Map<String, Object> initAttributes(RealmModel realm, Theme theme, Locale locale, int statusCode) throws IOException {
|
||||||
Map<String, Object> attributes = new HashMap<>();
|
Map<String, Object> attributes = new HashMap<>();
|
||||||
|
Properties messagesBundle = theme.getMessages(locale);
|
||||||
|
|
||||||
attributes.put("statusCode", statusCode);
|
attributes.put("statusCode", statusCode);
|
||||||
|
|
||||||
attributes.put("realm", realm);
|
attributes.put("realm", realm);
|
||||||
attributes.put("url", new UrlBean(realm, theme, uriInfo.getBaseUri(), null));
|
attributes.put("url", new UrlBean(realm, theme, uriInfo.getBaseUri(), null));
|
||||||
|
attributes.put("locale", new LocaleBean(realm, locale, uriInfo.getBaseUriBuilder(), messagesBundle));
|
||||||
|
|
||||||
Properties messagesBundle = theme.getMessages(locale);
|
|
||||||
|
|
||||||
String errorKey = statusCode == 404 ? Messages.PAGE_NOT_FOUND : Messages.INTERNAL_SERVER_ERROR;
|
String errorKey = statusCode == 404 ? Messages.PAGE_NOT_FOUND : Messages.INTERNAL_SERVER_ERROR;
|
||||||
String errorMessage = messagesBundle.getProperty(errorKey);
|
String errorMessage = messagesBundle.getProperty(errorKey);
|
||||||
|
|
|
@ -2,13 +2,16 @@ package org.keycloak.testsuite.error;
|
||||||
|
|
||||||
import org.jboss.arquillian.graphene.page.Page;
|
import org.jboss.arquillian.graphene.page.Page;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.keycloak.admin.client.resource.RealmResource;
|
||||||
import org.keycloak.representations.idm.RealmRepresentation;
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.testsuite.AbstractKeycloakTest;
|
import org.keycloak.testsuite.AbstractKeycloakTest;
|
||||||
import org.keycloak.testsuite.pages.ErrorPage;
|
import org.keycloak.testsuite.pages.ErrorPage;
|
||||||
|
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -65,6 +68,24 @@ public class UncaughtErrorPageTest extends AbstractKeycloakTest {
|
||||||
assertEquals("Client not found.", errorPage.getError());
|
assertEquals("Client not found.", errorPage.getError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void internationalisationEnabled() throws MalformedURLException {
|
||||||
|
RealmResource testRealm = realmsResouce().realm("master");
|
||||||
|
RealmRepresentation rep = testRealm.toRepresentation();
|
||||||
|
rep.setInternationalizationEnabled(true);
|
||||||
|
rep.setDefaultLocale("en");
|
||||||
|
rep.setSupportedLocales(Collections.singleton("en"));
|
||||||
|
testRealm.update(rep);
|
||||||
|
|
||||||
|
try {
|
||||||
|
checkPageNotFound("/auth/realms/master/nosuch");
|
||||||
|
checkPageNotFound("/auth/nosuch");
|
||||||
|
} finally {
|
||||||
|
rep.setInternationalizationEnabled(false);
|
||||||
|
testRealm.update(rep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void checkPageNotFound(String path) throws MalformedURLException {
|
private void checkPageNotFound(String path) throws MalformedURLException {
|
||||||
URI uri = suiteContext.getAuthServerInfo().getUriBuilder().path(path).build();
|
URI uri = suiteContext.getAuthServerInfo().getUriBuilder().path(path).build();
|
||||||
driver.navigate().to(uri.toURL());
|
driver.navigate().to(uri.toURL());
|
||||||
|
|
Loading…
Reference in a new issue