use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ApiMgtDAO method getEnvironment.
/**
* Returns the Environment for the uuid in the tenant domain.
*
* @param tenantDomain the tenant domain to look environment
* @param uuid UUID of the environment
* @return Gateway environment with given UUID
*/
public Environment getEnvironment(String tenantDomain, String uuid) throws APIManagementException {
Environment env = null;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_ENVIRONMENT_BY_ORGANIZATION_AND_UUID_SQL)) {
prepStmt.setString(1, tenantDomain);
prepStmt.setString(2, uuid);
try (ResultSet rs = prepStmt.executeQuery()) {
if (rs.next()) {
Integer id = rs.getInt("ID");
String name = rs.getString("NAME");
String displayName = rs.getString("DISPLAY_NAME");
String description = rs.getString("DESCRIPTION");
String provider = rs.getString("PROVIDER");
env = new Environment();
env.setId(id);
env.setUuid(uuid);
env.setName(name);
env.setDisplayName(displayName);
env.setDescription(description);
env.setProvider(provider);
env.setVhosts(getVhostGatewayEnvironments(connection, id));
}
}
} catch (SQLException e) {
handleException("Failed to get Environment in tenant domain:" + tenantDomain, e);
}
return env;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.
public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
String query = SQLConstants.RETRIEVE_ARTIFACTS;
List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, tenantDomain);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String apiId = resultSet.getString("API_ID");
String label = resultSet.getString("LABEL");
try {
APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
apiRuntimeArtifactDto.setApiId(apiId);
String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
apiRuntimeArtifactDto.setLabel(label);
apiRuntimeArtifactDto.setVhost(resolvedVhost);
apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
if (artifact != null) {
byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
apiRuntimeArtifactDto.setArtifact(newArtifact);
}
}
apiRuntimeArtifactDto.setFile(true);
apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
} catch (APIManagementException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
} catch (IOException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
} catch (SQLException e) {
// handle exception inside the loop and continue with other API artifacts
log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
}
}
}
} catch (SQLException e) {
handleException("Failed to retrieve Gateway Artifacts.", e);
}
return apiRuntimeArtifactDtoList;
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class EnvironmentSpecificAPIPropertyDAO method getMGEnvironmentSpecificAPIPropertiesOfAPIs.
/**
* Getting the api configs related MGs
*
* @param apiUuidS
* @return
* @throws APIManagementException
*/
private Map<String, Map<String, Environment>> getMGEnvironmentSpecificAPIPropertiesOfAPIs(List<String> apiUuidS) throws APIManagementException {
final String query = EnvironmentSpecificAPIPropertyConstants.GET_ENVIRONMENT_SPECIFIC_API_PROPERTIES_BY_APIS_SQL.replaceAll("_API_ID_LIST_", String.join(",", Collections.nCopies(apiUuidS.size(), "?")));
Map<String, Map<String, Environment>> apiEnvironmentMap = new HashMap<>();
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(query)) {
int index = 1;
for (String apiId : apiUuidS) {
preparedStatement.setString(index++, apiId);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String envId = resultSet.getString(1);
String envName = resultSet.getString(2);
String apiId = resultSet.getString(3);
JsonObject jsonConfig = null;
try (InputStream propertyConfigBlob = resultSet.getBinaryStream(4)) {
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 MG environment specific api properties", e);
}
return apiEnvironmentMap;
}
use of org.wso2.carbon.apimgt.api.model.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.api.model.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());
}
Aggregations