KEYCLOAK-6032 Fix error page when internationalization is enabled

This commit is contained in:
stianst 2017-12-14 15:22:13 +01:00 committed by Stian Thorgersen
parent 2b11938084
commit b672229efc
2 changed files with 24 additions and 1 deletions

View file

@ -16,6 +16,7 @@ import org.keycloak.services.util.LocaleHelper;
import org.keycloak.theme.FreeMarkerUtil;
import org.keycloak.theme.Theme;
import org.keycloak.theme.ThemeProvider;
import org.keycloak.theme.beans.LocaleBean;
import org.keycloak.theme.beans.MessageBean;
import org.keycloak.theme.beans.MessageFormatterMethod;
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 {
Map<String, Object> attributes = new HashMap<>();
Properties messagesBundle = theme.getMessages(locale);
attributes.put("statusCode", statusCode);
attributes.put("realm", realm);
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 errorMessage = messagesBundle.getProperty(errorKey);

View file

@ -2,13 +2,16 @@ package org.keycloak.testsuite.error;
import org.jboss.arquillian.graphene.page.Page;
import org.junit.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.pages.ErrorPage;
import javax.ws.rs.core.Response;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
@ -65,6 +68,24 @@ public class UncaughtErrorPageTest extends AbstractKeycloakTest {
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 {
URI uri = suiteContext.getAuthServerInfo().getUriBuilder().path(path).build();
driver.navigate().to(uri.toURL());