diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/IdentityEventExceptionSettings.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/IdentityEventExceptionSettings.java new file mode 100644 index 000000000..a1937a73f --- /dev/null +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/IdentityEventExceptionSettings.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.scim2.common.utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * this class is the blue print of IdentityEventException settings used in SCIMUserManager. + */ +public class IdentityEventExceptionSettings { + private boolean exposeErrorCodeInMessage; + private List badRequestErrorCodes = new ArrayList<>(); + + public boolean isExposeErrorCodeInMessage() { + return exposeErrorCodeInMessage; + } + + public void setExposeErrorCodeInMessage(boolean exposeErrorCodeInMessage) { + this.exposeErrorCodeInMessage = exposeErrorCodeInMessage; + } + + public List getBadRequestErrorCodes() { + return badRequestErrorCodes; + } + + public void setBadRequestErrorCodes(List badRequestErrorCodes) { + this.badRequestErrorCodes = badRequestErrorCodes; + } +} + + diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMCommonConstants.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMCommonConstants.java index b6ee67697..92ffc81a9 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMCommonConstants.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMCommonConstants.java @@ -52,7 +52,11 @@ public class SCIMCommonConstants { //config constants public static final String CHARON_CONFIG_NAME = "charon-config.xml"; - public static final String ELEMENT_NAME_AUTHENTICATION_SCHEMES = "authenticationSchemes";; + public static final String ELEMENT_NAME_AUTHENTICATION_SCHEMES = "authenticationSchemes"; + public static final String ELEMENT_NAME_IEE_SETTINGS = "identityEventExceptionSettings"; + public static final String ELEMENT_NAME_IEE_SETTINGS_EXPOSE_ERROR_CODE_IN_MESSAGE = "exposeErrorCodeInMessage"; + public static final String ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODES = "badRequestErrorCodes"; + public static final String ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODE = "badRequestErrorCode"; public static final String ELEMENT_NAME_PROPERTY = "Property"; public static final String ELEMENT_NAME_SCHEMA = "schema"; public static final String ATTRIBUTE_NAME_NAME = "name"; diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessor.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessor.java index e087be94f..136485369 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessor.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessor.java @@ -21,6 +21,7 @@ import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.user.core.NotImplementedException; import org.wso2.charon3.core.exceptions.CharonException; import javax.xml.namespace.QName; @@ -46,9 +47,10 @@ public class SCIMConfigProcessor { private static SCIMConfigProcessor scimConfigProcessor = new SCIMConfigProcessor(); //map to keep the properties values - Map properties = new HashMap(); + Map properties = new HashMap<>(); //list to keep the authentication schemas - List authenticationSchemas = null; + List authenticationSchemas = new ArrayList<>(); + IdentityEventExceptionSettings identityEventExceptionSettings = new IdentityEventExceptionSettings(); private static final Log logger = LogFactory.getLog(SCIMConfigProcessor.class); @@ -67,6 +69,10 @@ public List getAuthenticationSchemas() { return authenticationSchemas; } + public IdentityEventExceptionSettings getIdentityEventExceptionSettings() { + return identityEventExceptionSettings; + } + public void buildConfigFromFile(String filePath) throws CharonException { try { InputStream inputStream = null; @@ -83,19 +89,15 @@ public void buildConfigFromFile(String filePath) throws CharonException { throw new FileNotFoundException(); } } catch (FileNotFoundException e) { - throw new CharonException(SCIMCommonConstants.CHARON_CONFIG_NAME + "not found."); + throw new CharonException(filePath + "not found."); } catch (XMLStreamException e) { - throw new CharonException("Error in building the configuration file: " + - SCIMCommonConstants.CHARON_CONFIG_NAME); + throw new CharonException("Error in building the configuration file: " + filePath); } catch (IOException e) { - throw new CharonException("Error in building the configuration file: " + - SCIMCommonConstants.CHARON_CONFIG_NAME); + throw new CharonException("Error in building the configuration file: " + filePath); } } private void buildConfigFromRootElement(OMElement rootElement) { - - //read any properties defined. Iterator propertiesIterator = rootElement.getChildrenWithName( new QName(SCIMCommonConstants.ELEMENT_NAME_PROPERTY)); @@ -119,8 +121,13 @@ private void buildConfigFromRootElement(OMElement rootElement) { if (authenticationSchemasIterator != null) { authenticationSchemas = buildAuthenticationSchemasMap(authenticationSchemasIterator); } - } + OMElement identityEventExceptionSettingsElement = rootElement.getFirstChildWithName( + new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS)); + if (identityEventExceptionSettingsElement != null) { + identityEventExceptionSettings = buildIdentityEventExceptionSettings(identityEventExceptionSettingsElement); + } + } private List buildAuthenticationSchemasMap (Iterator schemasIterator) { @@ -149,6 +156,43 @@ private void buildConfigFromRootElement(OMElement rootElement) { return schemasList; } + private IdentityEventExceptionSettings buildIdentityEventExceptionSettings(OMElement ieeSettingsElement) { + IdentityEventExceptionSettings result = new IdentityEventExceptionSettings(); + + // extract exposeErrorCodeInMessage + Iterator ieesPropertyIterator = ieeSettingsElement.getChildrenWithName( + new QName(SCIMCommonConstants.ELEMENT_NAME_PROPERTY)); + if (ieesPropertyIterator != null) { + while (ieesPropertyIterator.hasNext()) { + OMElement propertyElement = ieesPropertyIterator.next(); + String propertyName = propertyElement.getAttributeValue( + new QName(SCIMCommonConstants.ATTRIBUTE_NAME_NAME)); + if (SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_EXPOSE_ERROR_CODE_IN_MESSAGE.equals(propertyName)) { + boolean exposeErrorCodeInMessage = "true".equals(propertyElement.getText()); + result.setExposeErrorCodeInMessage(exposeErrorCodeInMessage); + } + } + } + + // extract badRequestErrorCodes + Iterator ieesBadRequestErrorCodesIterator = ieeSettingsElement.getChildrenWithName( + new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODES)); + if (ieesBadRequestErrorCodesIterator != null && ieesBadRequestErrorCodesIterator.hasNext()) { + OMElement ieesBadRequestErrorCodesElement = ieesBadRequestErrorCodesIterator.next(); + Iterator ieesBadRequestErrorCodeIterator = ieesBadRequestErrorCodesElement.getChildrenWithName( + new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODE)); + if (ieesBadRequestErrorCodeIterator != null && ieesBadRequestErrorCodeIterator.hasNext()) { + while (ieesBadRequestErrorCodeIterator.hasNext()) { + OMElement ieesBadRequestErrorCodeElement =ieesBadRequestErrorCodeIterator.next(); + String errorCode = ieesBadRequestErrorCodeElement.getText(); + result.getBadRequestErrorCodes().add(errorCode.trim()); + } + } + } + + return result; + } + public static SCIMConfigProcessor getInstance() { return scimConfigProcessor; } diff --git a/components/org.wso2.carbon.identity.scim2.common/src/test/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessorTest.java b/components/org.wso2.carbon.identity.scim2.common/src/test/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessorTest.java index a69b231e9..0e565a45e 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/test/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessorTest.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/test/java/org/wso2/carbon/identity/scim2/common/utils/SCIMConfigProcessorTest.java @@ -29,12 +29,15 @@ import java.util.List; import java.util.Map; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; public class SCIMConfigProcessorTest { private SCIMConfigProcessor scimConfigProcessor; private AuthenticationSchema authenticationSchema; + private IdentityEventExceptionSettings identityEventExceptionSettings; @DataProvider(name = "propertyProvider") public static Object[][] propertyProvider() { @@ -70,6 +73,11 @@ public void setUp() throws Exception { authenticationSchema.setPrimary("true"); scimConfigProcessor.authenticationSchemas = new ArrayList<>(); scimConfigProcessor.authenticationSchemas.add(authenticationSchema); + + identityEventExceptionSettings = scimConfigProcessor.getIdentityEventExceptionSettings(); + identityEventExceptionSettings.setExposeErrorCodeInMessage(true); + identityEventExceptionSettings.getBadRequestErrorCodes().add("FOO"); + identityEventExceptionSettings.getBadRequestErrorCodes().add("BAR"); } @Test @@ -102,6 +110,15 @@ public void testGetAuthenticationSchemas() throws Exception { } } + @Test + public void testGetIdentityEventExceptionSettings() throws Exception { + IdentityEventExceptionSettings ieeSettings = scimConfigProcessor.getIdentityEventExceptionSettings(); + assertTrue(ieeSettings.isExposeErrorCodeInMessage()); + assertEquals(ieeSettings.getBadRequestErrorCodes().size(), 2); + assertEquals(ieeSettings.getBadRequestErrorCodes().get(0), identityEventExceptionSettings.getBadRequestErrorCodes().get(0)); + assertEquals(ieeSettings.getBadRequestErrorCodes().get(1), identityEventExceptionSettings.getBadRequestErrorCodes().get(1)); + } + @Test(dataProvider = "filePathProvider", expectedExceptions = CharonException.class) public void testBuildConfigFromFile(String filePath) throws Exception { scimConfigProcessor.buildConfigFromFile(filePath); @@ -113,11 +130,15 @@ public void testBuildConfigFromFileHappy() throws Exception { "charon-config-test.xml").toString(); scimConfigProcessor.buildConfigFromFile(filePath); + IdentityEventExceptionSettings ieeSettings = scimConfigProcessor.getIdentityEventExceptionSettings(); + assertFalse(ieeSettings.isExposeErrorCodeInMessage()); + assertEquals(ieeSettings.getBadRequestErrorCodes().size(), 1); + assertEquals(ieeSettings.getBadRequestErrorCodes().get(0), "22001"); } @Test public void testGetInstance() throws Exception { - SCIMConfigProcessor scimConfigProcessor1 = scimConfigProcessor.getInstance(); + SCIMConfigProcessor scimConfigProcessor1 = SCIMConfigProcessor.getInstance(); assertNotNull(scimConfigProcessor1); } } diff --git a/components/org.wso2.carbon.identity.scim2.common/src/test/resources/charon-config-test.xml b/components/org.wso2.carbon.identity.scim2.common/src/test/resources/charon-config-test.xml index 2ba3b72f4..03dbc0bd6 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/test/resources/charon-config-test.xml +++ b/components/org.wso2.carbon.identity.scim2.common/src/test/resources/charon-config-test.xml @@ -47,4 +47,13 @@ false + + false + + + + 22001 + + +