Search in sources :

Example 16 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class AdminRoot method getMessages.

public static Properties getMessages(KeycloakSession session, RealmModel realm, String lang) {
    try {
        Theme theme = getTheme(session, realm);
        Locale locale = lang != null ? Locale.forLanguageTag(lang) : Locale.ENGLISH;
        return theme.getMessages(locale);
    } catch (IOException e) {
        logger.error("Failed to load messages from theme", e);
        return new Properties();
    }
}
Also used : Locale(java.util.Locale) Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Properties(java.util.Properties)

Example 17 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class KeycloakErrorHandler method toResponse.

@Override
public Response toResponse(Throwable throwable) {
    KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
    KeycloakTransaction tx = session.getTransactionManager();
    tx.setRollbackOnly();
    int statusCode = getStatusCode(throwable);
    if (statusCode >= 500 && statusCode <= 599) {
        logger.error(UNCAUGHT_SERVER_ERROR_TEXT, throwable);
    } else {
        logger.debugv(throwable, ERROR_RESPONSE_TEXT, statusCode);
    }
    if (!MediaTypeMatcher.isHtmlRequest(headers)) {
        OAuth2ErrorRepresentation error = new OAuth2ErrorRepresentation();
        error.setError(getErrorCode(throwable));
        return Response.status(statusCode).header(HttpHeaders.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE.toString()).entity(error).build();
    }
    try {
        RealmModel realm = resolveRealm(session);
        Theme theme = session.theme().getTheme(Theme.Type.LOGIN);
        Locale locale = session.getContext().resolveLocale(null);
        FreeMarkerUtil freeMarker = new FreeMarkerUtil();
        Map<String, Object> attributes = initAttributes(session, realm, theme, locale, statusCode);
        String templateName = "error.ftl";
        String content = freeMarker.processTemplate(attributes, templateName, theme);
        return Response.status(statusCode).type(MediaType.TEXT_HTML_UTF_8_TYPE).entity(content).build();
    } catch (Throwable t) {
        logger.error("Failed to create error page", t);
        return Response.serverError().build();
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) Locale(java.util.Locale) FreeMarkerUtil(org.keycloak.theme.FreeMarkerUtil) KeycloakSession(org.keycloak.models.KeycloakSession) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) Theme(org.keycloak.theme.Theme) KeycloakTransaction(org.keycloak.models.KeycloakTransaction)

Example 18 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class ThemeResourceProviderTest method getResourceIllegalTraversal.

@Test
public void getResourceIllegalTraversal() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme("base", Theme.Type.LOGIN);
            Assert.assertNull(theme.getResourceAsStream("../templates/test.ftl"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 19 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class ThemeResourceProviderTest method getMessages.

@Test
public void getMessages() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme("base", Theme.Type.LOGIN);
            Assert.assertNotNull(theme.getMessages("messages", Locale.ENGLISH).get("test.keycloak-8818"));
            Assert.assertNotEquals("Full name (Theme-resources)", theme.getMessages("messages", Locale.ENGLISH).get("fullName"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 20 with Theme

use of org.keycloak.theme.Theme in project keycloak by keycloak.

the class DefaultThemeManagerTest method systemPropertiesSubstitutionInThemeProperties.

// KEYCLOAK-6698
@Test
public void systemPropertiesSubstitutionInThemeProperties() {
    testingClient.server().run(session -> {
        try {
            Theme theme = session.theme().getTheme(THEME_NAME, Theme.Type.LOGIN);
            Assert.assertEquals("getTheme(...) returns default theme when no matching theme found, but we need " + THEME_NAME + " theme deployed.", THEME_NAME, theme.getName());
            Assert.assertEquals("Keycloak is awesome", theme.getProperties().getProperty("system.property.found"));
            Assert.assertEquals("${missing_system_property}", theme.getProperties().getProperty("system.property.missing"));
            Assert.assertEquals("defaultValue", theme.getProperties().getProperty("system.property.missing.with.default"));
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    });
}
Also used : Theme(org.keycloak.theme.Theme) IOException(java.io.IOException) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Aggregations

Theme (org.keycloak.theme.Theme)21 IOException (java.io.IOException)17 Locale (java.util.Locale)9 Properties (java.util.Properties)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)5 FreeMarkerUtil (org.keycloak.theme.FreeMarkerUtil)5 UriBuilder (javax.ws.rs.core.UriBuilder)4 URI (java.net.URI)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Response (javax.ws.rs.core.Response)3 RealmModel (org.keycloak.models.RealmModel)3 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 Map (java.util.Map)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2