From a99df49bc932899085e4046eddedd4203acba31d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 26 Nov 2015 11:37:36 -0200 Subject: [PATCH] KEYCLOAK-2136: Unexpected error when trying to create mapper with already existing name --- .../org/keycloak/models/jpa/ClientAdapter.java | 3 ++- .../mongo/keycloak/adapters/ClientAdapter.java | 3 ++- .../resources/admin/ProtocolMappersResource.java | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java index a7dc0f46d3..6cb24f8dce 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java @@ -3,6 +3,7 @@ package org.keycloak.models.jpa; import org.keycloak.connections.jpa.util.JpaUtils; import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleContainerModel; @@ -328,7 +329,7 @@ public class ClientAdapter implements ClientModel { @Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { - throw new RuntimeException("protocol mapper name must be unique per protocol"); + throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); ProtocolMapperEntity entity = new ProtocolMapperEntity(); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java index 8eb562f96a..fc1f13defb 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java @@ -5,6 +5,7 @@ import com.mongodb.QueryBuilder; import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; @@ -342,7 +343,7 @@ public class ClientAdapter extends AbstractMongoAdapter imple @Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { - throw new RuntimeException("protocol mapper name must be unique per protocol"); + throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } ProtocolMapperEntity entity = new ProtocolMapperEntity(); String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/ProtocolMappersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/ProtocolMappersResource.java index 306d350b65..f53d2564b8 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/ProtocolMappersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/ProtocolMappersResource.java @@ -6,10 +6,12 @@ import org.jboss.resteasy.spi.NotFoundException; import org.keycloak.events.admin.OperationType; import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ProtocolMapperModel; import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.ProtocolMapperRepresentation; +import org.keycloak.services.ErrorResponse; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -86,9 +88,17 @@ public class ProtocolMappersResource { @Consumes(MediaType.APPLICATION_JSON) public Response createMapper(ProtocolMapperRepresentation rep) { auth.requireManage(); - ProtocolMapperModel model = RepresentationToModel.toModel(rep); - model = client.addProtocolMapper(model); - adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, model.getId()).representation(rep).success(); + + ProtocolMapperModel model = null; + try { + model = RepresentationToModel.toModel(rep); + model = client.addProtocolMapper(model); + adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, model.getId()).representation(rep).success(); + + } catch (ModelDuplicateException e) { + return ErrorResponse.exists("Protocol mapper exists with same name"); + } + return Response.created(uriInfo.getAbsolutePathBuilder().path(model.getId()).build()).build(); } /**