Skip to content

Commit

Permalink
[Spring Cleanup] Remove Spring dependencies in the CORS management API (
Browse files Browse the repository at this point in the history
#693)

* Remove Spring dependencies in the CORS management API

* Resolve PR comments
  • Loading branch information
NipuniBhagya authored Dec 9, 2024
1 parent 10e454c commit 9fdcf77
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~ Copyright (c) 2020-2024, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 Inc. licenses this file to you under the Apache 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
Expand Down Expand Up @@ -34,11 +34,6 @@
<artifactId>org.wso2.carbon.identity.cors.mgt.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,45 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*                                                                         
* Licensed 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.
* Copyright (c) 2020-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.cors.common;

import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.cors.mgt.core.CORSManagementService;

/**
* Service holder class for server configuration related services.
*/
public class CORSServiceHolder {

private static CORSServiceHolder instance = new CORSServiceHolder();

private CORSManagementService corsManagementService;

private CORSServiceHolder() {}

public static CORSServiceHolder getInstance() {

return instance;
private static class CORSManagementServiceHolder {
static final CORSManagementService SERVICE = (CORSManagementService) PrivilegedCarbonContext
.getThreadLocalCarbonContext().getOSGiService(CORSManagementService.class, null);
}

/**
* Get CORSManagementService osgi service.
*
* @return CORSManagementService
*/
public CORSManagementService getCorsManagementService() {

return CORSServiceHolder.getInstance().corsManagementService;
}

/**
* Set CORSManagementService osgi service.
*
* @param corsManagementService CORSManagementService.
*/
public void setCorsManagementService(CORSManagementService corsManagementService) {
public static CORSManagementService getCorsManagementService() {

CORSServiceHolder.getInstance().corsManagementService = corsManagementService;
return CORSManagementServiceHolder.SERVICE;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~ Copyright (c) 2020-2024, WSO2 LLC. (http://www.wso2.com).
~
~ WSO2 Inc. licenses this file to you under the Apache 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
Expand Down Expand Up @@ -39,11 +39,6 @@
<artifactId>cxf-rt-rs-service-description</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
Expand Down Expand Up @@ -103,6 +98,11 @@
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.server.api</groupId>
<artifactId>org.wso2.carbon.identity.api.server.cors.common</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.
*/
* Copyright (c) 2020-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.cors.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.cors.v1.factories.CorsApiServiceFactory;
import org.wso2.carbon.identity.api.server.cors.v1.model.CORSApplicationObject;
import org.wso2.carbon.identity.api.server.cors.v1.model.CORSOriginObject;
import org.wso2.carbon.identity.api.server.cors.v1.model.Error;
Expand All @@ -39,8 +41,12 @@

public class CorsApi {

@Autowired
private CorsApiService delegate;
private final CorsApiService delegate;

public CorsApi() {

this.delegate = CorsApiServiceFactory.getCorsApi();
}

@Valid
@GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.
*/
* Copyright (c) 2020-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.cors.v1.factories;

import org.wso2.carbon.identity.api.server.cors.v1.CorsApiService;
import org.wso2.carbon.identity.api.server.cors.v1.impl.CorsApiServiceImpl;

/**
* Factory class for Cors API.
*/
public class CorsApiServiceFactory {

private final static CorsApiService service = new CorsApiServiceImpl();
private final static CorsApiService SERVICE = new CorsApiServiceImpl();

/**
* Get Cors API Service.
*
* @return CorsApiService
*/
public static CorsApiService getCorsApi()
{
return service;
return SERVICE;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2020-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache 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
Expand All @@ -11,7 +11,7 @@
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
Expand All @@ -24,12 +24,12 @@
import org.wso2.carbon.identity.api.server.common.ContextLoader;
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.cors.common.CORSServiceHolder;
import org.wso2.carbon.identity.api.server.cors.common.Constants;
import org.wso2.carbon.identity.api.server.cors.v1.function.CORSApplicationToCORSApplicationObject;
import org.wso2.carbon.identity.api.server.cors.v1.function.CORSOriginToCORSOriginObject;
import org.wso2.carbon.identity.api.server.cors.v1.model.CORSApplicationObject;
import org.wso2.carbon.identity.api.server.cors.v1.model.CORSOriginObject;
import org.wso2.carbon.identity.cors.mgt.core.CORSManagementService;
import org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceClientException;
import org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException;
import org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException;
Expand All @@ -48,8 +48,14 @@
*/
public class CORSService {

private final CORSManagementService corsManagementService;
private static final Log log = LogFactory.getLog(CORSService.class);

public CORSService(CORSManagementService corsManagementService) {

this.corsManagementService = corsManagementService;
}

/**
* Get a list of associated applications of a CORS origin.
*
Expand All @@ -59,16 +65,16 @@ public List<CORSApplicationObject> getAssociatedAppsByCORSOrigin(String corsOrig

try {
String tenantDomain = ContextLoader.getTenantDomainFromContext();
List<String> corsOriginIdList = CORSServiceHolder.getInstance().getCorsManagementService()
.getTenantCORSOrigins(tenantDomain).stream().map(CORSOrigin::getId).collect(Collectors.toList());
List<String> corsOriginIdList = corsManagementService.getTenantCORSOrigins(tenantDomain)
.stream().map(CORSOrigin::getId).collect(Collectors.toList());

// Throw an exception if corsOriginId is not valid.
if (!corsOriginIdList.contains(corsOriginId)) {
throw new CORSManagementServiceClientException(String.format(ERROR_CODE_INVALID_CORS_ORIGIN_ID
.description(), corsOriginId), ERROR_CODE_INVALID_CORS_ORIGIN_ID.code());
}

List<CORSApplication> applicationList = CORSServiceHolder.getInstance().getCorsManagementService()
List<CORSApplication> applicationList = corsManagementService
.getCORSApplicationsByCORSOriginId(corsOriginId, tenantDomain);
return applicationList.stream().map(new CORSApplicationToCORSApplicationObject())
.collect(Collectors.toList());
Expand All @@ -86,8 +92,7 @@ public List<CORSOriginObject> getCORSOrigins() {

try {
String tenantDomain = ContextLoader.getTenantDomainFromContext();
List<CORSOrigin> corsOriginList = CORSServiceHolder.getInstance().getCorsManagementService()
.getTenantCORSOrigins(tenantDomain);
List<CORSOrigin> corsOriginList = corsManagementService.getTenantCORSOrigins(tenantDomain);
return corsOriginList.stream().map(new CORSOriginToCORSOriginObject()).collect(Collectors.toList());
} catch (CORSManagementServiceException e) {
throw handleCORSException(e, Constants.ErrorMessage.ERROR_CODE_CORS_RETRIEVE, null);
Expand Down
Loading

0 comments on commit 9fdcf77

Please sign in to comment.