Search in sources :

Example 1 with ApiModelToServiceProvider

use of org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.ApiModelToServiceProvider in project identity-api-server by wso2.

the class ServerApplicationManagementService method createApplication.

public String createApplication(ApplicationModel applicationModel, String template) {
    if (StringUtils.isNotBlank(template)) {
        String errorCode = APPLICATION_CREATION_WITH_TEMPLATES_NOT_IMPLEMENTED.getCode();
        throw buildNotImplementedError(errorCode, "Application creation with templates not supported.");
    }
    /*
         * CORS adding step should be moved to the service layer and the validation also should happen there.
         * But for now we are handling the validation here.
         */
    if (applicationModel.getInboundProtocolConfiguration() != null && applicationModel.getInboundProtocolConfiguration().getOidc() != null) {
        validateCORSOrigins(applicationModel.getInboundProtocolConfiguration().getOidc().getAllowedOrigins());
    }
    String username = ContextLoader.getUsernameFromContext();
    String tenantDomain = ContextLoader.getTenantDomainFromContext();
    String applicationId = null;
    ServiceProvider application = new ApiModelToServiceProvider().apply(applicationModel);
    try {
        applicationId = getApplicationManagementService().createApplication(application, tenantDomain, username);
        if (applicationModel.getInboundProtocolConfiguration() != null && applicationModel.getInboundProtocolConfiguration().getOidc() != null) {
            OAuthInboundFunctions.updateCorsOrigins(applicationId, applicationModel.getInboundProtocolConfiguration().getOidc());
        }
        return applicationId;
    } catch (IdentityApplicationManagementException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error while creating application. Rolling back possibly created inbound config data.", e);
        }
        rollbackInbounds(getConfiguredInbounds(application));
        throw handleIdentityApplicationManagementException(e, "Error creating application.");
    } catch (CORSManagementServiceException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error while updating CORS origins. Rolling back created application data.", e);
        }
        if (applicationId != null) {
            deleteApplication(applicationId);
        }
        if (ErrorMessages.ERROR_CODE_DUPLICATE_ORIGINS.getCode().equals(e.getErrorCode())) {
            throw buildClientError(e, "Error creating application. Found duplicate allowed origin entries.");
        }
        throw buildClientError(e, "Error creating application. Allow CORS origins update failed.");
    } catch (Throwable e) {
        /*
             * For more information read https://github.com/wso2/product-is/issues/12579. This is to overcome the
             * above issue.
             */
        if (log.isDebugEnabled()) {
            log.debug("Server encountered unexpected error. Rolling back created application data.", e);
        }
        if (applicationId != null) {
            deleteApplication(applicationId);
        } else {
            rollbackInbounds(getConfiguredInbounds(application));
        }
        throw Utils.buildServerError(ERROR_PROCESSING_REQUEST.getCode(), ERROR_PROCESSING_REQUEST.getMessage(), ERROR_PROCESSING_REQUEST.getDescription());
    }
}
Also used : ApiModelToServiceProvider(org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.ApiModelToServiceProvider) ApiModelToServiceProvider(org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.ApiModelToServiceProvider) UpdateServiceProvider(org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.UpdateServiceProvider) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) SAML2ServiceProvider(org.wso2.carbon.identity.api.server.application.management.v1.SAML2ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) CORSManagementServiceException(org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)

Aggregations

SAML2ServiceProvider (org.wso2.carbon.identity.api.server.application.management.v1.SAML2ServiceProvider)1 ApiModelToServiceProvider (org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.ApiModelToServiceProvider)1 UpdateServiceProvider (org.wso2.carbon.identity.api.server.application.management.v1.core.functions.application.UpdateServiceProvider)1 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)1 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)1 CORSManagementServiceException (org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)1