Skip to content

Commit 4c41d83

Browse files
committed
Fix * resource issue + update deployer init method
1 parent a064d66 commit 4c41d83

File tree

10 files changed

+114
-52
lines changed

10 files changed

+114
-52
lines changed

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/GatewayDeployer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface GatewayDeployer {
3232
*
3333
* @throws APIManagementException if error occurs when initializing the external gateway deployer
3434
*/
35-
void init(GatewayConfiguration configuration) throws APIManagementException;
35+
void init(Environment environment) throws APIManagementException;
3636

3737
/**
3838
* This method returns the type of Gateway

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public Environment addEnvironment(String tenantDomain, Environment environment)
204204
if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
205205
GatewayConfigurationService gatewayConfigurationService = new GatewayConfigurationServiceImpl();
206206
gatewayConfigurationService.addGatewayConfiguration(tenantDomain, environment.getName(),
207-
environment.getGatewayType(), APIUtil.extractGatewayConfiguration(environment, tenantDomain));
207+
environment.getGatewayType(), environment);
208208
}
209209
return environment;
210210
}
@@ -266,7 +266,7 @@ public Environment updateEnvironment(String tenantDomain, Environment environmen
266266
if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
267267
GatewayConfigurationService gatewayConfigurationService = new GatewayConfigurationServiceImpl();
268268
gatewayConfigurationService.updateGatewayConfiguration(tenantDomain, environment.getName(),
269-
environment.getType(), APIUtil.extractGatewayConfiguration(environment, tenantDomain));
269+
environment.getType(), environment);
270270
}
271271

272272
return updatedEnvironment;

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java

+1-37
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public API addAPI(API api) throws APIManagementException {
550550
validateAndSetAPISecurity(api);
551551

552552
//Validate API with Federated Gateway
553-
validateApiWithFederatedGateway(api);
553+
APIUtil.validateApiWithFederatedGateway(api);
554554

555555
//Set version timestamp to the API
556556
String latestTimestamp = calculateVersionTimestamp(provider, apiName,
@@ -997,9 +997,6 @@ public API updateAPI(API api, API existingAPI) throws APIManagementException {
997997
migrateMediationPoliciesOfAPI(api, tenantDomain, false);
998998
}
999999

1000-
//Validate API with Federated Gateway
1001-
validateApiWithFederatedGateway(api);
1002-
10031000
//get product resource mappings on API before updating the API. Update uri templates on api will remove all
10041001
//product mappings as well.
10051002
List<APIProductResource> productResources = apiMgtDAO.getProductMappingsForAPI(api);
@@ -2088,39 +2085,6 @@ private void validateAndSetAPISecurity(API api) {
20882085
api.setApiSecurity(apiSecurity);
20892086
}
20902087

2091-
/**
2092-
* Validate Api with the federated gateways
2093-
*
2094-
* @param api API Object
2095-
*/
2096-
private static void validateApiWithFederatedGateway(API api) throws APIManagementException {
2097-
2098-
try {
2099-
GatewayAgentConfiguration gatewayConfiguration = ServiceReferenceHolder.getInstance().
2100-
getExternalGatewayConnectorConfiguration(api.getGatewayType());
2101-
GatewayDeployer deployer = (GatewayDeployer) Class.forName(gatewayConfiguration.getImplementation())
2102-
.getDeclaredConstructor().newInstance();
2103-
if (deployer != null) {
2104-
GatewayAPIValidationResult errorList = null;
2105-
errorList = deployer.validateApi(api);
2106-
if (!errorList.getErrors().isEmpty()) {
2107-
throw new APIManagementException(
2108-
"Error occurred while validating the API with the federated gateway: "
2109-
+ api.getGatewayType(),
2110-
ExceptionCodes.from(ExceptionCodes.FEDERATED_GATEWAY_VALIDATION_FAILED,
2111-
api.getGatewayType(), errorList.toString()));
2112-
}
2113-
}
2114-
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
2115-
IllegalAccessException | InvocationTargetException e) {
2116-
throw new APIManagementException(
2117-
"Error occurred while validating the API with the federated gateway: "
2118-
+ api.getGatewayType(), e,
2119-
ExceptionCodes.from(ExceptionCodes.FEDERATED_GATEWAY_VALIDATION_FAILED,
2120-
api.getGatewayType()));
2121-
}
2122-
}
2123-
21242088
/**
21252089
* To validate the API Security options and set it.
21262090
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 Inc. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
package org.wso2.carbon.apimgt.impl;
19+
20+
import org.wso2.carbon.apimgt.api.APIManagementException;
21+
import org.wso2.carbon.apimgt.api.ErrorHandler;
22+
import org.wso2.carbon.apimgt.api.ExceptionCodes;
23+
24+
import static org.wso2.carbon.apimgt.api.ExceptionCodes.FEDERATED_GATEWAY_VALIDATION_FAILED;
25+
26+
/**
27+
*
28+
* This is the custom exception class for external gateway API validation
29+
*
30+
*/
31+
public class ExternalGatewayAPIValidationException extends APIManagementException {
32+
33+
34+
public ExternalGatewayAPIValidationException(String message) {
35+
super(message, ExceptionCodes.from(FEDERATED_GATEWAY_VALIDATION_FAILED, message));
36+
}
37+
38+
public ExternalGatewayAPIValidationException(String message, Throwable e) {
39+
super(message, e);
40+
}
41+
42+
public ExternalGatewayAPIValidationException(String message, ErrorHandler code) {
43+
super(message, code);
44+
}
45+
46+
public ExternalGatewayAPIValidationException(Throwable throwable) {
47+
super(throwable);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.wso2.carbon.apimgt.impl.deployer;
22

33
import org.wso2.carbon.apimgt.api.APIManagementException;
4+
import org.wso2.carbon.apimgt.api.model.Environment;
45
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
56

67
public interface GatewayConfigurationService {
78
void addGatewayConfiguration(String organization, String name, String type,
8-
GatewayConfiguration gatewayConfiguration) throws APIManagementException;
9+
Environment environment) throws APIManagementException;
910

1011
void updateGatewayConfiguration(String organization, String name, String type,
11-
GatewayConfiguration gatewayConfiguration) throws APIManagementException;
12+
Environment environment) throws APIManagementException;
1213

1314
void removeGatewayConfiguration(String tenantDomain, String name) throws APIManagementException;
1415
}

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/deployer/GatewayConfigurationServiceImpl.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package org.wso2.carbon.apimgt.impl.deployer;
22

33
import org.wso2.carbon.apimgt.api.APIManagementException;
4-
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
4+
import org.wso2.carbon.apimgt.api.model.Environment;
55
import org.wso2.carbon.apimgt.impl.factory.GatewayHolder;
66

77
public class GatewayConfigurationServiceImpl implements GatewayConfigurationService {
88
@Override
99
public void addGatewayConfiguration(String organization, String name, String type,
10-
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
10+
Environment environment) throws APIManagementException {
1111
String internKey = this.getClass().getName().concat(organization).concat(name);
1212
synchronized (internKey.intern()) {
13-
GatewayHolder.addGatewayConfiguration(organization, name, type, gatewayConfiguration);
13+
GatewayHolder.addGatewayConfiguration(organization, name, type, environment);
1414
}
1515
}
1616

1717
@Override
1818
public void updateGatewayConfiguration(String organization, String name, String type,
19-
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
19+
Environment environment) throws APIManagementException {
2020
String internKey = this.getClass().getName().concat(organization).concat(name);
2121
synchronized (internKey.intern()) {
22-
GatewayHolder.updateGatewayConfiguration(organization, name, type, gatewayConfiguration);
22+
GatewayHolder.updateGatewayConfiguration(organization, name, type, environment);
2323
}
2424
}
2525

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/factory/GatewayHolder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class GatewayHolder {
2727
private static final Map<String, OrganizationGatewayDto> organizationWiseMap = new HashMap<>();
2828

2929
public static void addGatewayConfiguration(String organization, String name, String type,
30-
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
30+
Environment environment) throws APIManagementException {
3131

3232
OrganizationGatewayDto organizationGatewayDto = getTenantGatewayDtoFromMap(organization);
3333
if (organizationGatewayDto == null) {
@@ -46,7 +46,7 @@ public static void addGatewayConfiguration(String organization, String name, Str
4646
try {
4747
deployer = (GatewayDeployer) Class.forName(gatewayAgentConfiguration.getImplementation())
4848
.getDeclaredConstructor().newInstance();
49-
deployer.init(gatewayConfiguration);
49+
deployer.init(environment);
5050
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException
5151
| NoSuchMethodException | InvocationTargetException e) {
5252
throw new APIManagementException("Error while loading gateway configuration", e);
@@ -62,10 +62,10 @@ public static void addGatewayConfiguration(String organization, String name, Str
6262
}
6363

6464
public static void updateGatewayConfiguration(String organization, String name, String type,
65-
GatewayConfiguration gatewayConfiguration) throws APIManagementException {
65+
Environment environment) throws APIManagementException {
6666

6767
removeGatewayConfiguration(organization, name);
68-
addGatewayConfiguration(organization, name, type, gatewayConfiguration);
68+
addGatewayConfiguration(organization, name, type, environment);
6969
}
7070

7171
public static void removeGatewayConfiguration(String tenantDomain, String name) {
@@ -105,7 +105,7 @@ private static OrganizationGatewayDto getTenantGatewayDto(String tenantDomain) {
105105
getExternalGatewayConnectorConfiguration(entry.getValue().getGatewayType());
106106
GatewayDeployer deployer = (GatewayDeployer) Class.forName(gatewayAgentConfiguration.getImplementation())
107107
.getDeclaredConstructor().newInstance();
108-
deployer.init(APIUtil.extractGatewayConfiguration(entry.getValue(), tenantDomain));
108+
deployer.init(entry.getValue());
109109
gatewayDto.setGatewayDeployer(deployer);
110110
newOrganizationGatewayDto.putGatewayDto(gatewayDto);
111111
}

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

+36
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@
111111
import org.wso2.carbon.apimgt.api.model.DocumentationType;
112112
import org.wso2.carbon.apimgt.api.model.EndpointSecurity;
113113
import org.wso2.carbon.apimgt.api.model.Environment;
114+
import org.wso2.carbon.apimgt.api.model.GatewayAPIValidationResult;
114115
import org.wso2.carbon.apimgt.api.model.GatewayAgentConfiguration;
115116
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
117+
import org.wso2.carbon.apimgt.api.model.GatewayDeployer;
116118
import org.wso2.carbon.apimgt.api.model.GatewayPortalConfiguration;
117119
import org.wso2.carbon.apimgt.api.model.GatewayFeatureCatalog;
118120
import org.wso2.carbon.apimgt.api.model.Identifier;
@@ -160,6 +162,7 @@
160162
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
161163
import org.wso2.carbon.apimgt.impl.APIType;
162164
import org.wso2.carbon.apimgt.impl.ExternalEnvironment;
165+
import org.wso2.carbon.apimgt.impl.ExternalGatewayAPIValidationException;
163166
import org.wso2.carbon.apimgt.impl.IDPConfiguration;
164167
import org.wso2.carbon.apimgt.impl.PasswordResolverFactory;
165168
import org.wso2.carbon.apimgt.impl.RESTAPICacheConfiguration;
@@ -252,6 +255,7 @@
252255
import java.io.InputStreamReader;
253256
import java.io.StringWriter;
254257
import java.io.UnsupportedEncodingException;
258+
import java.lang.reflect.InvocationTargetException;
255259
import java.lang.reflect.Type;
256260
import java.math.BigDecimal;
257261
import java.math.BigInteger;
@@ -11437,4 +11441,36 @@ public static String getOrganizationHandle(String name) {
1143711441
sanatizedName = sanatizedName.toLowerCase(Locale.ENGLISH).replaceAll("^-+|-+$", "");
1143811442
return sanatizedName;
1143911443
}
11444+
11445+
/**
11446+
* Validate Api with the federated gateways
11447+
*
11448+
* @param api API Object
11449+
*/
11450+
public static void validateApiWithFederatedGateway(API api) throws APIManagementException {
11451+
11452+
try {
11453+
GatewayAgentConfiguration gatewayConfiguration = org.wso2.carbon.apimgt.impl.internal.
11454+
ServiceReferenceHolder.getInstance().
11455+
getExternalGatewayConnectorConfiguration(api.getGatewayType());
11456+
GatewayDeployer deployer = (GatewayDeployer) Class.forName(gatewayConfiguration.getImplementation())
11457+
.getDeclaredConstructor().newInstance();
11458+
if (deployer != null) {
11459+
GatewayAPIValidationResult errorList = null;
11460+
errorList = deployer.validateApi(api);
11461+
if (!errorList.getErrors().isEmpty()) {
11462+
throw new ExternalGatewayAPIValidationException(
11463+
"Error occurred while validating the API with the federated gateway: "
11464+
+ api.getGatewayType(),
11465+
ExceptionCodes.from(ExceptionCodes.FEDERATED_GATEWAY_VALIDATION_FAILED,
11466+
api.getGatewayType(), errorList.toString()));
11467+
}
11468+
}
11469+
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
11470+
IllegalAccessException | InvocationTargetException e) {
11471+
throw new APIManagementException(
11472+
"Error occurred while validating the API with the federated gateway: "
11473+
+ api.getGatewayType(), e);
11474+
}
11475+
}
1144011476
}

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
import org.wso2.carbon.apimgt.api.model.ApiTypeWrapper;
7777
import org.wso2.carbon.apimgt.api.model.Documentation;
7878
import org.wso2.carbon.apimgt.api.model.DocumentationContent;
79+
import org.wso2.carbon.apimgt.api.model.GatewayAPIValidationResult;
80+
import org.wso2.carbon.apimgt.api.model.GatewayAgentConfiguration;
81+
import org.wso2.carbon.apimgt.api.model.GatewayDeployer;
7982
import org.wso2.carbon.apimgt.api.model.Identifier;
8083
import org.wso2.carbon.apimgt.api.model.Label;
8184
import org.wso2.carbon.apimgt.api.model.LifeCycleEvent;
@@ -98,6 +101,7 @@
98101
import org.wso2.carbon.apimgt.governance.api.model.RuleViolation;
99102
import org.wso2.carbon.apimgt.governance.api.service.APIMGovernanceService;
100103
import org.wso2.carbon.apimgt.impl.APIConstants;
104+
import org.wso2.carbon.apimgt.impl.ExternalGatewayAPIValidationException;
101105
import org.wso2.carbon.apimgt.impl.definitions.AsyncApiParser;
102106
import org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition;
103107
import org.wso2.carbon.apimgt.impl.definitions.OAS2Parser;
@@ -134,6 +138,7 @@
134138
import java.io.IOException;
135139
import java.io.InputStream;
136140
import java.lang.reflect.Field;
141+
import java.lang.reflect.InvocationTargetException;
137142
import java.lang.reflect.Type;
138143
import java.net.MalformedURLException;
139144
import java.net.URL;
@@ -2218,6 +2223,10 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio
22182223

22192224
SwaggerData swaggerData = new SwaggerData(existingAPI);
22202225
String updatedApiDefinition = oasParser.populateCustomManagementInfo(apiDefinition, swaggerData);
2226+
2227+
//Validate API with Federated Gateway before persisting to registry
2228+
APIUtil.validateApiWithFederatedGateway(existingAPI);
2229+
22212230
apiProvider.saveSwaggerDefinition(existingAPI, updatedApiDefinition, organization);
22222231
existingAPI.setSwaggerDefinition(updatedApiDefinition);
22232232
}

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.wso2.carbon.apimgt.governance.api.model.APIMGovernableState;
5151
import org.wso2.carbon.apimgt.impl.APIConstants;
5252
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
53+
import org.wso2.carbon.apimgt.impl.ExternalGatewayAPIValidationException;
5354
import org.wso2.carbon.apimgt.impl.GZIPUtils;
5455
import org.wso2.carbon.apimgt.impl.ServiceCatalogImpl;
5556
import org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode;
@@ -2883,6 +2884,8 @@ public Response updateAPISwagger(String apiId, String ifMatch, String apiDefinit
28832884
updatedSwagger = updateSwagger(apiId, apiDefinition, organization);
28842885
}
28852886
return Response.ok().entity(updatedSwagger).build();
2887+
} catch (ExternalGatewayAPIValidationException e) {
2888+
RestApiUtil.handleBadRequest(e.getMessage(), log);
28862889
} catch (APIManagementException e) {
28872890
//Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need
28882891
// to expose the existence of the resource

0 commit comments

Comments
 (0)