Skip to content

Commit

Permalink
[Spring Cleanup] Remove Spring dependencies in the input validation r…
Browse files Browse the repository at this point in the history
…ules API (#701)

* Remove Spring dependencies in the input validation API

* Resolve PR comments
  • Loading branch information
NipuniBhagya authored Dec 9, 2024
1 parent 48369ef commit ebd5681
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022, WSO2 LLC. (http://www.wso2.org).
~ Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.org).
~
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -43,11 +43,6 @@
</build>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -18,14 +18,21 @@

package org.wso2.carbon.identity.api.server.input.validation.common;

import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.input.validation.mgt.services.InputValidationManagementService;

/**
* Service holder class for input validation management.
*/
public class InputValidationServiceHolder {

private static InputValidationManagementService inputValidationMgtService;
private InputValidationServiceHolder () {};

private static class InputValidationManagementServiceHolder {
static final InputValidationManagementService SERVICE = (InputValidationManagementService)
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getOSGiService(InputValidationManagementService.class, null);
}

/**
* Get InputValidationMgtService OSGi service.
Expand All @@ -34,16 +41,6 @@ public class InputValidationServiceHolder {
*/
public static InputValidationManagementService getInputValidationMgtService() {

return inputValidationMgtService;
}

/**
* Set InputValidationMgtService OSGi service.
*
* @param inputValidationMgtService Input Validation Management Service.
*/
public static void setInputValidationMgtService(InputValidationManagementService inputValidationMgtService) {

InputValidationServiceHolder.inputValidationMgtService = inputValidationMgtService;
return InputValidationManagementServiceHolder.SERVICE;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
~ Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 LLC. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -78,11 +78,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -18,18 +18,17 @@

package org.wso2.carbon.identity.api.server.input.validation.v1;

import org.springframework.beans.factory.annotation.Autowired;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import java.io.InputStream;
import java.util.List;

import org.wso2.carbon.identity.api.server.input.validation.v1.factories.ValidationRulesApiServiceFactory;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.Error;
import java.util.List;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.ValidationConfigModel;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.ValidationConfigModelForField;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.ValidatorModel;
import org.wso2.carbon.identity.api.server.input.validation.v1.ValidationRulesApiService;

import javax.validation.Valid;
import javax.ws.rs.*;
Expand All @@ -43,8 +42,12 @@

public class ValidationRulesApi {

@Autowired
private ValidationRulesApiService delegate;
private final ValidationRulesApiService delegate;

public ValidationRulesApi() {

this.delegate = ValidationRulesApiServiceFactory.getValidationRulesApi();
}

@Valid
@GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -26,7 +26,7 @@
*/
public class ValidationRulesApiServiceFactory {

private final static ValidationRulesApiService service = new ValidationRulesApiServiceImpl();
private final static ValidationRulesApiService SERVICE = new ValidationRulesApiServiceImpl();

/**
* Method to get ValidationRulesApiService.
Expand All @@ -35,6 +35,6 @@ public class ValidationRulesApiServiceFactory {
*/
public static ValidationRulesApiService getValidationRulesApi() {

return service;
return SERVICE;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright (c) 2022, WSO2 LLC. (http://www.wso2.org).
* Copyright (c) 2022-2024, WSO2 LLC. (http://www.wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* WSO2 LLC. 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.
* 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.api.server.input.validation.v1.core;
Expand All @@ -22,7 +24,6 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.server.common.error.APIError;
import org.wso2.carbon.identity.api.server.common.error.ErrorResponse;
import org.wso2.carbon.identity.api.server.input.validation.common.InputValidationServiceHolder;
import org.wso2.carbon.identity.api.server.input.validation.common.util.ValidationManagementConstants;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.MappingModel;
import org.wso2.carbon.identity.api.server.input.validation.v1.models.RuleModel;
Expand All @@ -39,6 +40,7 @@
import org.wso2.carbon.identity.input.validation.mgt.model.ValidatorConfiguration;
import org.wso2.carbon.identity.input.validation.mgt.model.validators.AbstractRegExValidator;
import org.wso2.carbon.identity.input.validation.mgt.model.validators.AbstractRulesValidator;
import org.wso2.carbon.identity.input.validation.mgt.services.InputValidationManagementService;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -66,8 +68,14 @@
*/
public class ValidationRulesManagementApiService {

private final InputValidationManagementService inputValidationManagementService;
private static final Log LOGGER = LogFactory.getLog(ValidationRulesManagementApiService.class);

public ValidationRulesManagementApiService(InputValidationManagementService inputValidationManagementService) {

this.inputValidationManagementService = inputValidationManagementService;
}

/**
* Method to get input validation configuration.
*
Expand All @@ -77,7 +85,7 @@ public class ValidationRulesManagementApiService {
public List<ValidationConfigModel> getValidationConfiguration(String tenantDomain) {

try {
List<ValidationConfiguration> configurations = InputValidationServiceHolder.getInputValidationMgtService()
List<ValidationConfiguration> configurations = inputValidationManagementService
.getInputValidationConfiguration(tenantDomain);
return buildResponse(configurations);
} catch (InputValidationMgtException e) {
Expand All @@ -95,7 +103,7 @@ public ValidationConfigModel getValidationConfigurationForField(String tenantDom

try {
isFieldSupported(field);
ValidationConfiguration configuration = InputValidationServiceHolder.getInputValidationMgtService()
ValidationConfiguration configuration = inputValidationManagementService
.getInputValidationConfigurationForField(tenantDomain, field);
return buildResponse(configuration);
} catch (InputValidationMgtException e) {
Expand All @@ -115,7 +123,7 @@ public List<ValidationConfigModel> updateInputValidationConfiguration(
try {
List<ValidationConfiguration> requestDTO = buildRequestDTOFromValidationRequest(validationConfigModel);
validateProperties(requestDTO, tenantDomain);
List<ValidationConfiguration> configurations = InputValidationServiceHolder.getInputValidationMgtService()
List<ValidationConfiguration> configurations = inputValidationManagementService
.updateInputValidationConfiguration(requestDTO, tenantDomain);
return buildResponse(configurations);
} catch (InputValidationMgtException e) {
Expand All @@ -139,7 +147,7 @@ public ValidationConfigModel updateInputValidationConfigurationForField(
configModels.add(validationConfigModel);
List<ValidationConfiguration> requestDTO = buildRequestDTOFromValidationRequest(configModels);
validateProperties(requestDTO, tenantDomain);
ValidationConfiguration configurations = InputValidationServiceHolder.getInputValidationMgtService()
ValidationConfiguration configurations = inputValidationManagementService
.updateValidationConfiguration(requestDTO.get(0), tenantDomain);
return buildResponse(configurations);
} catch (InputValidationMgtException e) {
Expand All @@ -156,8 +164,7 @@ public List<ValidatorModel> getValidators(String tenantDomain) {

List<ValidatorConfiguration> validators;
try {
validators = InputValidationServiceHolder.getInputValidationMgtService()
.getValidatorConfigurations(tenantDomain);
validators = inputValidationManagementService.getValidatorConfigurations(tenantDomain);
return buildValidatorResponse(validators);
} catch (InputValidationMgtException e) {
if (ERROR_CODE_INPUT_VALIDATION_NOT_EXISTS.getCode().contains(e.getErrorCode())) {
Expand Down Expand Up @@ -361,8 +368,7 @@ private void validateProperties(List<ValidationConfiguration> configurations, St
private void validateProperties(String field, boolean isRules, List<RulesConfiguration> rules, String tenantDomain)
throws InputValidationMgtClientException {

Map<String, Validator> allValidators = InputValidationServiceHolder.getInputValidationMgtService()
.getValidators(tenantDomain);
Map<String, Validator> allValidators = inputValidationManagementService.getValidators(tenantDomain);
ValidationContext context;
for (RulesConfiguration rule: rules) {
Validator validator = allValidators.get(rule.getValidatorName());
Expand All @@ -386,7 +392,7 @@ private void validateProperties(String field, boolean isRules, List<RulesConfigu
}

// Validate provided validation is allowed for the field.
for (FieldValidationConfigurationHandler handler: InputValidationServiceHolder.getInputValidationMgtService()
for (FieldValidationConfigurationHandler handler: inputValidationManagementService
.getFieldValidationConfigurationHandlers().values()) {
if (handler.canHandle(field)) {
handler.validateValidationConfiguration(rules);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.api.server.input.validation.v1.factories;

import org.wso2.carbon.identity.api.server.input.validation.common.InputValidationServiceHolder;
import org.wso2.carbon.identity.api.server.input.validation.v1.core.ValidationRulesManagementApiService;
import org.wso2.carbon.identity.input.validation.mgt.services.InputValidationManagementService;

/**
* Factory to return ValidationRulesManagementApiService.
*/
public class ValidationRulesManagementApiServiceFactory {

private static final ValidationRulesManagementApiService SERVICE;

static {
InputValidationManagementService inputValidationManagementService = InputValidationServiceHolder
.getInputValidationMgtService();

if (inputValidationManagementService == null) {
throw new IllegalStateException("InputValidationManagementService is not available from OSGi context.");
}

SERVICE = new ValidationRulesManagementApiService(inputValidationManagementService);
}

/**
* Get ValidationRulesManagementApiService.
*
* @return ValidationRulesManagementApiService
*/
public static ValidationRulesManagementApiService getValidationRulesManagementApiService() {

return SERVICE;
}
}
Loading

0 comments on commit ebd5681

Please sign in to comment.