From 2d1dc4a87497ef5f22b6b67116ce173867542e19 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 28 Apr 2014 17:34:06 -0400 Subject: [PATCH] application refactor --- .../server/KeycloakServerApplication.java | 84 ------------------- .../src/main/webapp/WEB-INF/web.xml | 46 ++++------ .../server/KeycloakServerApplication.java | 70 ---------------- server/src/main/webapp/WEB-INF/web.xml | 6 +- .../resources/KeycloakApplication.java | 81 +++++++++++++++++- 5 files changed, 99 insertions(+), 188 deletions(-) delete mode 100755 bundled-war-example/src/main/java/org/keycloak/server/KeycloakServerApplication.java delete mode 100755 server/src/main/java/org/keycloak/server/KeycloakServerApplication.java diff --git a/bundled-war-example/src/main/java/org/keycloak/server/KeycloakServerApplication.java b/bundled-war-example/src/main/java/org/keycloak/server/KeycloakServerApplication.java deleted file mode 100755 index 1b14058ae6..0000000000 --- a/bundled-war-example/src/main/java/org/keycloak/server/KeycloakServerApplication.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.keycloak.server; - -import org.jboss.resteasy.core.Dispatcher; -import org.jboss.resteasy.logging.Logger; -import org.keycloak.adapters.AdapterDeploymentContext; -import org.keycloak.models.Config; -import org.keycloak.models.KeycloakSession; -import org.keycloak.models.RealmModel; -import org.keycloak.representations.adapters.config.AdapterConfig; -import org.keycloak.representations.idm.CredentialRepresentation; -import org.keycloak.representations.idm.RealmRepresentation; -import org.keycloak.services.managers.RealmManager; -import org.keycloak.services.resources.KeycloakApplication; -import org.keycloak.util.JsonSerialization; -import org.keycloak.util.KeycloakUriBuilder; - -import javax.servlet.ServletContext; -import javax.ws.rs.core.Context; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; - -public class KeycloakServerApplication extends KeycloakApplication { - - private static final Logger log = Logger.getLogger(KeycloakServerApplication.class); - - public KeycloakServerApplication(@Context ServletContext servletContext,@Context Dispatcher dispatcher) throws FileNotFoundException { - super(servletContext, dispatcher); - KeycloakSession session = factory.createSession(); - session.getTransaction().begin(); - try { - InputStream is = servletContext.getResourceAsStream("/WEB-INF/testrealm.json"); - RealmRepresentation rep = loadJson(is, RealmRepresentation.class); - RealmModel realm = importRealm(session, rep); - AdapterDeploymentContext deploymentContext = (AdapterDeploymentContext)servletContext.getAttribute(AdapterDeploymentContext.class.getName()); - AdapterConfig adapterConfig = new AdapterConfig(); - String host = (String)servletContext.getInitParameter("host-port"); - String uri = KeycloakUriBuilder.fromUri("http://" + host).path(servletContext.getContextPath()).build().toString(); - log.info("**** auth server url: " + uri); - adapterConfig.setRealm("demo"); - adapterConfig.setResource("customer-portal"); - adapterConfig.setRealmKey(realm.getPublicKeyPem()); - Map creds = new HashMap(); - creds.put(CredentialRepresentation.SECRET, "password"); - adapterConfig.setCredentials(creds); - adapterConfig.setAuthServerUrl(uri); - adapterConfig.setSslNotRequired(true); - deploymentContext.updateDeployment(adapterConfig); - session.getTransaction().commit(); - } finally { - session.close(); - } - - } - - public RealmModel importRealm(KeycloakSession session, RealmRepresentation rep) { - RealmManager manager = new RealmManager(session); - - RealmModel realm = manager.getRealmByName(rep.getRealm()); - if (realm != null) { - log.info("Not importing realm " + rep.getRealm() + " realm already exists"); - return realm; - } - - realm = manager.createRealm(rep.getId(), rep.getRealm()); - manager.importRealm(rep, realm); - - log.info("Imported realm " + realm.getName()); - return realm; - } - - private static T loadJson(InputStream is, Class type) { - try { - return JsonSerialization.readValue(is, type); - } catch (IOException e) { - throw new RuntimeException("Failed to parse json", e); - } - } - -} diff --git a/bundled-war-example/src/main/webapp/WEB-INF/web.xml b/bundled-war-example/src/main/webapp/WEB-INF/web.xml index 5d17437d11..b1e4d3d850 100755 --- a/bundled-war-example/src/main/webapp/WEB-INF/web.xml +++ b/bundled-war-example/src/main/webapp/WEB-INF/web.xml @@ -10,16 +10,21 @@ localhost:8080 + + keycloak.import.realm.resources + /WEB-INF/testrealm.json + + Keycloak REST Interface org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher javax.ws.rs.Application - org.keycloak.server.KeycloakServerApplication + org.keycloak.services.resources.KeycloakApplication resteasy.servlet.mapping.prefix - /rest + / 1 true @@ -40,19 +45,10 @@ true - - TmpAdminRedirectServlet - org.keycloak.services.tmp.TmpAdminRedirectServlet - - org.keycloak.services.listeners.KeycloakSessionDestroyListener - - index.html - - Keycloak Client Connection Filter org.keycloak.services.filters.ClientConnectionFilter @@ -75,32 +71,22 @@ Keycloak REST Interface - /rest/* + /* + + + + + + index.html + + Customer REST Interface /database/* - - TmpAdminRedirectServlet - /admin - /admin/ - - - - Customers diff --git a/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java b/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java deleted file mode 100755 index 823b44b395..0000000000 --- a/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.keycloak.server; - -import org.jboss.resteasy.core.Dispatcher; -import org.jboss.resteasy.logging.Logger; -import org.keycloak.models.Config; -import org.keycloak.models.KeycloakSession; -import org.keycloak.models.RealmModel; -import org.keycloak.representations.idm.RealmRepresentation; -import org.keycloak.services.managers.RealmManager; -import org.keycloak.services.resources.KeycloakApplication; -import org.keycloak.util.JsonSerialization; - -import javax.servlet.ServletContext; -import javax.ws.rs.core.Context; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -public class KeycloakServerApplication extends KeycloakApplication { - - private static final Logger log = Logger.getLogger(KeycloakServerApplication.class); - - public KeycloakServerApplication(@Context ServletContext servletContext, @Context Dispatcher dispatcher) throws FileNotFoundException { - super(servletContext, dispatcher); - - String importRealm = System.getProperty("keycloak.import"); - if (importRealm != null) { - KeycloakSession session = factory.createSession(); - session.getTransaction().begin(); - RealmRepresentation rep = loadJson(new FileInputStream(importRealm), RealmRepresentation.class); - importRealm(session, rep); - } - } - - public void importRealm(KeycloakSession session, RealmRepresentation rep) { - try { - RealmManager manager = new RealmManager(session); - - if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { - log.info("Not importing realm " + rep.getRealm() + " realm already exists"); - return; - } - - if (manager.getRealmByName(rep.getRealm()) != null) { - log.info("Not importing realm " + rep.getRealm() + " realm already exists"); - return; - } - - RealmModel realm = manager.createRealm(rep.getId(), rep.getRealm()); - manager.importRealm(rep, realm); - - log.info("Imported realm " + realm.getName()); - - session.getTransaction().commit(); - } finally { - session.close(); - } - } - - private static T loadJson(InputStream is, Class type) { - try { - return JsonSerialization.readValue(is, type); - } catch (IOException e) { - throw new RuntimeException("Failed to parse json", e); - } - } - -} diff --git a/server/src/main/webapp/WEB-INF/web.xml b/server/src/main/webapp/WEB-INF/web.xml index 356425fc87..76b4932632 100755 --- a/server/src/main/webapp/WEB-INF/web.xml +++ b/server/src/main/webapp/WEB-INF/web.xml @@ -7,11 +7,11 @@ auth - Resteasy + Keycloak REST Interface org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher javax.ws.rs.Application - org.keycloak.server.KeycloakServerApplication + org.keycloak.services.resources.KeycloakApplication resteasy.servlet.mapping.prefix @@ -46,7 +46,7 @@ - Resteasy + Keycloak REST Interface /* diff --git a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java index 7c8371bf4f..2cab3d08d4 100755 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -18,27 +18,35 @@ import org.keycloak.models.RealmModel; import org.keycloak.provider.ProviderFactory; import org.keycloak.provider.ProviderFactoryLoader; import org.keycloak.provider.ProviderSession; +import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.DefaultProviderSessionFactory; import org.keycloak.picketlink.IdentityManagerProvider; import org.keycloak.picketlink.IdentityManagerProviderFactory; import org.keycloak.provider.ProviderSessionFactory; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.BruteForceProtector; +import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.SocialRequestManager; import org.keycloak.services.managers.TokenManager; import org.keycloak.services.resources.admin.AdminService; import org.keycloak.models.utils.ModelProviderUtils; import org.keycloak.timer.TimerProvider; import org.keycloak.timer.TimerProviderFactory; +import org.keycloak.util.JsonSerialization; import javax.servlet.ServletContext; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.util.Date; import java.util.HashSet; import java.util.Set; +import java.util.StringTokenizer; /** * @author Bill Burke @@ -66,7 +74,6 @@ public class KeycloakApplication extends Application { context.setAttribute(BruteForceProtector.class.getName(), protector); this.providerSessionFactory = createProviderSessionFactory(); context.setAttribute(KeycloakSessionFactory.class.getName(), factory); - //classes.add(KeycloakSessionCleanupFilter.class); context.setAttribute(ProviderSessionFactory.class.getName(), this.providerSessionFactory); @@ -86,6 +93,7 @@ public class KeycloakApplication extends Application { setupDefaultRealm(context.getContextPath()); setupScheduledTasks(providerSessionFactory, factory); + importRealms(context); } public String getContextPath() { @@ -184,4 +192,75 @@ public class KeycloakApplication extends Application { return singletons; } + public void importRealms(ServletContext context) { + importRealmFile(); + importRealmResources(context); + + } + + public void importRealmResources(ServletContext context) { + String resources = context.getInitParameter("keycloak.import.realm.resources"); + if (resources != null) { + StringTokenizer tokenizer = new StringTokenizer(resources, ","); + while (tokenizer.hasMoreTokens()) { + String resource = tokenizer.nextToken().trim(); + InputStream is = context.getResourceAsStream(resource); + if (is == null) { + log.warn("Could not find realm resource to import: " + resource); + } + RealmRepresentation rep = loadJson(is, RealmRepresentation.class); + importRealm(rep, "resource " + resource); + } + } + } + + public void importRealmFile() { + String file = System.getProperty("keycloak.import"); + if (file != null) { + RealmRepresentation rep = null; + try { + rep = loadJson(new FileInputStream(file), RealmRepresentation.class); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + importRealm(rep, "file " + file); + } + } + + public void importRealm(RealmRepresentation rep, String from) { + KeycloakSession session = factory.createSession(); + try { + session.getTransaction().begin(); + RealmManager manager = new RealmManager(session); + + if (rep.getId() != null && manager.getRealm(rep.getId()) != null) { + log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); + return; + } + + if (manager.getRealmByName(rep.getRealm()) != null) { + log.info("Not importing realm " + rep.getRealm() + " from " + from + ". It already exists."); + return; + } + + RealmModel realm = manager.createRealm(rep.getId(), rep.getRealm()); + manager.importRealm(rep, realm); + + log.info("Imported realm " + realm.getName() + " from " + from); + + session.getTransaction().commit(); + } finally { + session.close(); + } + } + + private static T loadJson(InputStream is, Class type) { + try { + return JsonSerialization.readValue(is, type); + } catch (IOException e) { + throw new RuntimeException("Failed to parse json", e); + } + } + + }