Added keycloak.import to server

This commit is contained in:
Stian Thorgersen 2013-11-16 12:20:51 +00:00
parent 19d4b09626
commit 80b92dc723
2 changed files with 70 additions and 1 deletions

View file

@ -103,6 +103,25 @@
<artifactId>jboss-servlet-api_3.0_spec</artifactId> <artifactId>jboss-servlet-api_3.0_spec</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -1,6 +1,7 @@
package org.keycloak.server; package org.keycloak.server;
import org.jboss.resteasy.jwt.JsonSerialization; import org.jboss.resteasy.jwt.JsonSerialization;
import org.jboss.resteasy.logging.Logger;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.ApplianceBootstrap;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
@ -11,18 +12,67 @@ import org.keycloak.services.resources.KeycloakApplication;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
public class KeycloakServerApplication extends KeycloakApplication { public class KeycloakServerApplication extends KeycloakApplication {
public KeycloakServerApplication(@Context ServletContext servletContext) { private static final Logger log = Logger.getLogger(KeycloakServerApplication.class);
public KeycloakServerApplication(@Context ServletContext servletContext) throws FileNotFoundException {
super(servletContext); super(servletContext);
KeycloakSession session = factory.createSession(); KeycloakSession session = factory.createSession();
session.getTransaction().begin(); session.getTransaction().begin();
ApplianceBootstrap bootstrap = new ApplianceBootstrap(); ApplianceBootstrap bootstrap = new ApplianceBootstrap();
bootstrap.bootstrap(session); bootstrap.bootstrap(session);
String importRealm = System.getProperty("keycloak.import");
if (importRealm != null) {
RealmRepresentation rep = loadJson(new FileInputStream(importRealm), RealmRepresentation.class);
importRealm(session, rep);
}
session.getTransaction().commit(); session.getTransaction().commit();
} }
public void importRealm(KeycloakSession session, RealmRepresentation rep ) {
try {
RealmManager manager = new RealmManager(session);
if (rep.getId() == null) {
throw new RuntimeException("Realm id not specified");
}
if (manager.getRealm(rep.getId()) != 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> T loadJson(InputStream is, Class<T> type) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int c;
while ((c = is.read()) != -1) {
os.write(c);
}
byte[] bytes = os.toByteArray();
return JsonSerialization.fromBytes(type, bytes);
} catch (IOException e) {
throw new RuntimeException("Failed to parse json", e);
}
}
} }