From 8b00fff5ee140ef684975edc764858be4b11cd51 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Fri, 2 Aug 2013 23:36:25 -0400 Subject: [PATCH] remove filters --- .../services/resources/KeycloakApplication.java | 6 +++--- ...ubResource.java => PublicRealmResource.java} | 6 +++--- .../services/resources/RealmsResource.java | 8 ++++---- .../services/resources/SaaSService.java | 1 - .../services/resources/Transaction.java | 17 +++++++++++++++-- 5 files changed, 25 insertions(+), 13 deletions(-) rename services/src/main/java/org/keycloak/services/resources/{RealmSubResource.java => PublicRealmResource.java} (92%) 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 b84eb7abef..c6ba0e723e 100755 --- a/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java +++ b/services/src/main/java/org/keycloak/services/resources/KeycloakApplication.java @@ -49,10 +49,10 @@ public class KeycloakApplication extends Application { public KeycloakApplication() { KeycloakSessionFactory f = createSessionFactory(); this.factory = f; - KeycloakSessionRequestFilter filter = new KeycloakSessionRequestFilter(factory); + //KeycloakSessionRequestFilter filter = new KeycloakSessionRequestFilter(factory); + //singletons.add(filter); + //classes.add(KeycloakSessionResponseFilter.class); singletons.add(new RealmsResource(new TokenManager(), new SocialRequestManager())); - singletons.add(filter); - classes.add(KeycloakSessionResponseFilter.class); classes.add(SkeletonKeyContextResolver.class); classes.add(SaasService.class); } diff --git a/services/src/main/java/org/keycloak/services/resources/RealmSubResource.java b/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java similarity index 92% rename from services/src/main/java/org/keycloak/services/resources/RealmSubResource.java rename to services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java index 9e0fd3bd6e..bef77ff8ed 100755 --- a/services/src/main/java/org/keycloak/services/resources/RealmSubResource.java +++ b/services/src/main/java/org/keycloak/services/resources/PublicRealmResource.java @@ -17,8 +17,8 @@ import javax.ws.rs.core.UriInfo; * @author Bill Burke * @version $Revision: 1 $ */ -public class RealmSubResource { - protected static final Logger logger = Logger.getLogger(RealmSubResource.class); +public class PublicRealmResource { + protected static final Logger logger = Logger.getLogger(PublicRealmResource.class); public static final String ADMIN_ROLE = "$REALM-ADMIN$"; @Context @@ -26,7 +26,7 @@ public class RealmSubResource { protected RealmModel realm; - public RealmSubResource(RealmModel realm) { + public PublicRealmResource(RealmModel realm) { this.realm = realm; } diff --git a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java index d9319ca307..84f8b59408 100755 --- a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java +++ b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java @@ -92,17 +92,17 @@ public class RealmsResource { } @Path("{realm}") - public RealmSubResource getRealmResource(final @PathParam("realm") String id) { + public PublicRealmResource getRealmResource(final @PathParam("realm") String id) { return new Transaction(false) { @Override - protected RealmSubResource callImpl() { + protected PublicRealmResource callImpl() { RealmManager realmManager = new RealmManager(session); RealmModel realm = realmManager.getRealm(id); if (realm == null) { logger.debug("realm not found"); throw new NotFoundException(); } - RealmSubResource realmResource = new RealmSubResource(realm); + PublicRealmResource realmResource = new PublicRealmResource(realm); resourceContext.initResource(realmResource); return realmResource; } @@ -127,7 +127,7 @@ public class RealmsResource { RealmModel realm = realmManager.importRealm(rep, realmCreator); UriBuilder builder = uriInfo.getRequestUriBuilder().path(realm.getId()); return Response.created(builder.build()) - .entity(RealmSubResource.realmRep(realm, uriInfo)) + .entity(PublicRealmResource.realmRep(realm, uriInfo)) .type(MediaType.APPLICATION_JSON_TYPE).build(); } }.call(); diff --git a/services/src/main/java/org/keycloak/services/resources/SaaSService.java b/services/src/main/java/org/keycloak/services/resources/SaaSService.java index 93a3e48eb5..05eb3a1961 100755 --- a/services/src/main/java/org/keycloak/services/resources/SaaSService.java +++ b/services/src/main/java/org/keycloak/services/resources/SaaSService.java @@ -9,7 +9,6 @@ import org.keycloak.representations.idm.RequiredCredentialRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.services.managers.AuthenticationManager; import org.keycloak.services.managers.RealmManager; -import org.keycloak.services.models.KeycloakSession; import org.keycloak.services.models.RealmModel; import org.keycloak.services.models.RoleModel; import org.keycloak.services.models.UserModel; diff --git a/services/src/main/java/org/keycloak/services/resources/Transaction.java b/services/src/main/java/org/keycloak/services/resources/Transaction.java index 03bb3b750d..32f4319d1b 100755 --- a/services/src/main/java/org/keycloak/services/resources/Transaction.java +++ b/services/src/main/java/org/keycloak/services/resources/Transaction.java @@ -5,6 +5,8 @@ import org.keycloak.services.models.KeycloakSession; import org.keycloak.services.models.KeycloakSessionFactory; import org.keycloak.services.models.KeycloakTransaction; +import javax.ws.rs.core.Application; + /** * Meant to be used as an inner class wrapper (I forget the pattern name, its been awhile). * @@ -15,6 +17,7 @@ public class Transaction { protected KeycloakSession session; protected KeycloakTransaction transaction; protected boolean closeSession; + protected boolean created; /** * Pull KeycloakSession from @Context @@ -29,10 +32,16 @@ public class Transaction { /** * Pull KeycloakSession from @Context * - * @param close whether to close the session or not after completion + * @param close whether to close the session or not after successful completion */ public Transaction(boolean close) { this.session = ResteasyProviderFactory.getContextData(KeycloakSession.class); + if (session == null) { + KeycloakApplication app = (KeycloakApplication)ResteasyProviderFactory.getContextData(Application.class); + session = app.getFactory().createSession(); + created = true; + ResteasyProviderFactory.pushContext(KeycloakSession.class, session); + } transaction = session.getTransaction(); closeSession = close; @@ -65,9 +74,12 @@ public class Transaction { if (!wasActive && transaction.isActive()) transaction.commit(); } catch (RuntimeException e) { if (!wasActive && transaction.isActive()) transaction.rollback(); + if (created) closeSession = true; throw e; } finally { - if (!wasActive && closeSession) session.close(); + if (!wasActive && closeSession) { + session.close(); + } } } @@ -88,6 +100,7 @@ public class Transaction { return rtn; } catch (RuntimeException e) { if (!wasActive && transaction.isActive()) transaction.rollback(); + if (created) closeSession = true; // close if there was a failure throw e; } finally { if (!wasActive && closeSession) session.close();