use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class EnvironmentSpecificAPIPropertyDAO method getDefaultEnvironmentSpecificAPIPropertiesOfAPIs.
/**
* Getting the api configs related to default environments.
*
* @param apiUuidS
* @param envIds
* @param apiEnvironmentMap
* @return
* @throws APIManagementException
*/
private Map<String, Map<String, Environment>> getDefaultEnvironmentSpecificAPIPropertiesOfAPIs(List<String> apiUuidS, List<String> envIds, Map<String, Map<String, Environment>> apiEnvironmentMap) throws APIManagementException {
final String query = EnvironmentSpecificAPIPropertyConstants.GET_ENVIRONMENT_SPECIFIC_API_PROPERTIES_BY_APIS_ENVS_SQL.replaceAll("_ENV_ID_LIST_", String.join(",", Collections.nCopies(envIds.size(), "?"))).replaceAll("_API_ID_LIST_", String.join(",", Collections.nCopies(apiUuidS.size(), "?")));
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(query)) {
int index = 1;
for (String envId : envIds) {
preparedStatement.setString(index++, envId);
}
for (String apiId : apiUuidS) {
preparedStatement.setString(index++, apiId);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String envId = resultSet.getString(1);
// for default envs envId and envName is same
String envName = envId;
String apiId = resultSet.getString(2);
JsonObject jsonConfig = null;
try (InputStream propertyConfigBlob = resultSet.getBinaryStream(3)) {
if (propertyConfigBlob != null) {
String apiJsonConfig = APIMgtDBUtil.getStringFromInputStream(propertyConfigBlob);
jsonConfig = new Gson().fromJson(apiJsonConfig, JsonObject.class);
}
}
Map<String, Environment> environmentMap;
Environment environment;
if (apiEnvironmentMap.containsKey(apiId)) {
environmentMap = apiEnvironmentMap.get(apiId);
} else {
environmentMap = new HashMap<>();
apiEnvironmentMap.put(apiId, environmentMap);
}
environment = new Environment();
environment.setEnvId(envId);
environment.setEnvName(envName);
environment.setConfigs(jsonConfig);
environmentMap.put(envName, environment);
}
}
} catch (SQLException | IOException e) {
handleException("Error occurred when getting default environment specific api properties", e);
}
return apiEnvironmentMap;
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APIManagerConfigurationTest method testEnvironmentsConfigWithAdditionalProperties.
@Test
public void testEnvironmentsConfigWithAdditionalProperties() throws XMLStreamException, APIManagementException {
String envConfig = "<Environment type=\"hybrid\" api-console=\"true\" isDefault=\"true\">\n" + " <Name>Default</Name>\n" + " <DisplayName></DisplayName>\n" + " <Description>This is a hybrid gateway that handles both production and sandbox token traffic.</Description>\n" + " <!-- Server URL of the API gateway -->\n" + " <ServerURL>https://localhost:9440/services/</ServerURL>\n" + " <!-- Admin username for the API gateway. -->\n" + " <Username>${admin.username}</Username>\n" + " <!-- Admin password for the API gateway.-->\n" + " <Password>${admin.password}</Password>\n" + " <!-- Provider Vendor of the API gateway.-->\n" + " <Provider>wso2</Provider>\n" + " <!-- Endpoint URLs for the APIs hosted in this API gateway.-->\n" + " <GatewayEndpoint>https://localhost:9440,http://localhost:9440</GatewayEndpoint>\n" + " <!-- Additional properties for External Gateways -->\n" + " <!-- Endpoint URLs of the WebSocket APIs hosted in this API Gateway -->\n" + " <GatewayWSEndpoint>ws://localhost:9099,wss://localhost:8099</GatewayWSEndpoint>\n" + " <!-- Endpoint URLs of the WebSub APIs hosted in this API Gateway -->\n" + " <GatewayWebSubEndpoint>http://localhost:9021,https://localhost:8021</GatewayWebSubEndpoint>\n" + " <Properties>\n" + " <Property name=\"Organization\">WSO2</Property>\n" + " <Property name=\"DisplayName\">Development Environment</Property>\n" + " <Property name=\"DevAccountName\">dev-1</Property>\n" + " </Properties>\n" + " <VirtualHosts>\n" + " </VirtualHosts>\n" + " </Environment>";
OMElement element = AXIOMUtil.stringToOM(envConfig);
APIManagerConfiguration config = new APIManagerConfiguration();
config.setEnvironmentConfig(element);
Map<String, Environment> environmentsList = config.getGatewayEnvironments();
Assert.assertFalse(environmentsList.isEmpty());
Environment defaultEnv = environmentsList.get("Default");
Assert.assertFalse(defaultEnv.getAdditionalProperties().isEmpty());
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class TestUtils method mockAPIMConfiguration.
public static void mockAPIMConfiguration() throws RegistryException, UserStoreException, XMLStreamException {
ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
Map<String, Environment> apiGatewayEnvironments = new HashMap<String, Environment>();
Environment env1 = new Environment();
env1.setApiGatewayEndpoint("https://abc.com, http://abc.com");
apiGatewayEnvironments.put("PROD", env1);
// Mocking some commonly used configs
PowerMockito.when(amConfig.getApiGatewayEnvironments()).thenReturn(apiGatewayEnvironments);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_API_DOC_VISIBILITY_LEVELS)).thenReturn("true", "false");
ThrottleProperties throttleProperties = new ThrottleProperties();
PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APIProviderImpl method getLightweightAPIByUUID.
/**
* Get minimal details of API by registry artifact id
*
* @param uuid Registry artifact id
* @param organization identifier of the organization
* @return API of the provided artifact id
* @throws APIManagementException
*/
@Override
public API getLightweightAPIByUUID(String uuid, String organization) throws APIManagementException {
try {
Organization org = new Organization(organization);
PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
if (publisherAPI != null) {
API api = APIMapper.INSTANCE.toApi(publisherAPI);
checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
// / populate relavant external info
// environment
String environmentString = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
// CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
}
api.setOrganization(organization);
return api;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException e) {
String msg = "Failed to get API with uuid " + uuid;
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APIManagerConfiguration method setEnvironmentConfig.
/**
* Set property values for each gateway environments defined in the api-manager.xml config file
*
* @param environmentElem OMElement of a single environment in the gateway environments list
*/
void setEnvironmentConfig(OMElement environmentElem) throws APIManagementException {
Environment environment = new Environment();
environment.setType(environmentElem.getAttributeValue(new QName("type")));
String showInConsole = environmentElem.getAttributeValue(new QName("api-console"));
if (showInConsole != null) {
environment.setShowInConsole(Boolean.parseBoolean(showInConsole));
} else {
environment.setShowInConsole(true);
}
String isDefault = environmentElem.getAttributeValue(new QName("isDefault"));
if (isDefault != null) {
environment.setDefault(Boolean.parseBoolean(isDefault));
} else {
environment.setDefault(false);
}
environment.setName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_NAME)).getText()));
environment.setDisplayName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_DISPLAY_NAME)).getText()));
if (StringUtils.isEmpty(environment.getDisplayName())) {
environment.setDisplayName(environment.getName());
}
environment.setServerURL(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_SERVER_URL)).getText()));
environment.setUserName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_USERNAME)).getText()));
OMElement passwordElement = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_PASSWORD));
String resolvedPassword = MiscellaneousUtil.resolve(passwordElement, secretResolver);
environment.setPassword(APIUtil.replaceSystemProperty(resolvedPassword));
String provider = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_PROVIDER)).getText();
if (StringUtils.isNotEmpty(provider)) {
environment.setProvider(APIUtil.replaceSystemProperty(provider));
} else {
environment.setProvider(APIUtil.replaceSystemProperty(DEFAULT_PROVIDER));
}
environment.setApiGatewayEndpoint(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_ENDPOINT)).getText()));
OMElement websocketGatewayEndpoint = environmentElem.getFirstChildWithName(new QName(APIConstants.API_WEBSOCKET_GATEWAY_ENDPOINT));
if (websocketGatewayEndpoint != null) {
environment.setWebsocketGatewayEndpoint(APIUtil.replaceSystemProperty(websocketGatewayEndpoint.getText()));
} else {
environment.setWebsocketGatewayEndpoint(WEBSOCKET_DEFAULT_GATEWAY_URL);
}
OMElement webSubGatewayEndpoint = environmentElem.getFirstChildWithName(new QName(APIConstants.API_WEBSUB_GATEWAY_ENDPOINT));
if (webSubGatewayEndpoint != null) {
environment.setWebSubGatewayEndpoint(APIUtil.replaceSystemProperty(webSubGatewayEndpoint.getText()));
} else {
environment.setWebSubGatewayEndpoint(WEBSUB_DEFAULT_GATEWAY_URL);
}
OMElement description = environmentElem.getFirstChildWithName(new QName("Description"));
if (description != null) {
environment.setDescription(description.getText());
} else {
environment.setDescription("");
}
environment.setReadOnly(true);
List<VHost> vhosts = new LinkedList<>();
environment.setVhosts(vhosts);
environment.setEndpointsAsVhost();
Iterator vhostIterator = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOSTS)).getChildrenWithLocalName(APIConstants.API_GATEWAY_VIRTUAL_HOST);
while (vhostIterator.hasNext()) {
OMElement vhostElem = (OMElement) vhostIterator.next();
String httpEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_HTTP_ENDPOINT)).getText());
String httpsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_HTTPS_ENDPOINT)).getText());
String wsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WS_ENDPOINT)).getText());
String wssEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WSS_ENDPOINT)).getText());
String webSubHttpEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WEBSUB_HTTP_ENDPOINT)).getText());
String webSubHttpsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WEBSUB_HTTPS_ENDPOINT)).getText());
// Prefix websub endpoints with 'websub_' so that the endpoint URL
// would begin with: 'websub_http://', since API type is identified by the URL protocol below.
webSubHttpEp = "websub_" + webSubHttpEp;
webSubHttpsEp = "websub_" + webSubHttpsEp;
VHost vhost = VHost.fromEndpointUrls(new String[] { httpEp, httpsEp, wsEp, wssEp, webSubHttpEp, webSubHttpsEp });
vhosts.add(vhost);
}
OMElement properties = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_ADDITIONAL_PROPERTIES));
Map<String, String> additionalProperties = new HashMap<>();
if (properties != null) {
Iterator gatewayAdditionalProperties = properties.getChildrenWithLocalName(APIConstants.API_GATEWAY_ADDITIONAL_PROPERTY);
while (gatewayAdditionalProperties.hasNext()) {
OMElement propertyElem = (OMElement) gatewayAdditionalProperties.next();
String propName = propertyElem.getAttributeValue(new QName("name"));
String resolvedValue = MiscellaneousUtil.resolve(propertyElem, secretResolver);
additionalProperties.put(propName, resolvedValue);
}
}
environment.setAdditionalProperties(additionalProperties);
if (!apiGatewayEnvironments.containsKey(environment.getName())) {
apiGatewayEnvironments.put(environment.getName(), environment);
} else {
// This will happen only on server startup therefore we log and continue the startup
log.error("Duplicate environment name found in api-manager.xml " + environment.getName());
}
}
Aggregations