use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIRevisionDeploymentByNameAndRevsionID.
/**
* Get APIRevisionDeployment details by providing deployment name and revision uuid
*
* @return APIRevisionDeployment object
* @throws APIManagementException if an error occurs while retrieving revision details
*/
public APIRevisionDeployment getAPIRevisionDeploymentByNameAndRevsionID(String name, String revisionId) throws APIManagementException {
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_REVISION_DEPLOYMENT_MAPPING_BY_NAME_AND_REVISION_UUID)) {
statement.setString(1, name);
statement.setString(2, revisionId);
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
String environmentName = rs.getString("NAME");
String vhost = rs.getString("VHOST");
apiRevisionDeployment.setDeployment(environmentName);
apiRevisionDeployment.setVhost(VHostUtils.resolveIfNullToDefaultVhost(environmentName, vhost));
apiRevisionDeployment.setRevisionUUID(rs.getString("REVISION_UUID"));
apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOYED_TIME"));
}
}
} catch (SQLException e) {
handleException("Failed to get API Revision deployment mapping details for deployment name: " + name, e);
}
return apiRevisionDeployment;
}
use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.
the class APIMgtDBUtil method mergeRevisionDeploymentDTOs.
/**
* Handle connection rollback logic. Rethrow original exception so that it can be handled centrally.
* @param rs result set
* @throws SQLException sql exception
* @throws APIManagementException api management exception
*/
public static List<APIRevisionDeployment> mergeRevisionDeploymentDTOs(ResultSet rs) throws APIManagementException, SQLException {
List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
Map<String, APIRevisionDeployment> uniqueSet = new HashMap<>();
while (rs.next()) {
APIRevisionDeployment apiRevisionDeployment;
String environmentName = rs.getString("NAME");
String vhost = VHostUtils.resolveIfNullToDefaultVhost(environmentName, rs.getString("VHOST"));
String revisionUuid = rs.getString("REVISION_UUID");
String uniqueKey = (environmentName != null ? environmentName : "") + (vhost != null ? vhost : "") + (revisionUuid != null ? revisionUuid : "");
if (!uniqueSet.containsKey(uniqueKey)) {
apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setDeployment(environmentName);
apiRevisionDeployment.setVhost(vhost);
apiRevisionDeployment.setRevisionUUID(revisionUuid);
apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOY_TIME"));
apiRevisionDeployment.setSuccessDeployedTime(rs.getString("DEPLOYED_TIME"));
apiRevisionDeploymentList.add(apiRevisionDeployment);
uniqueSet.put(uniqueKey, apiRevisionDeployment);
} else {
apiRevisionDeployment = uniqueSet.get(uniqueKey);
if (!apiRevisionDeployment.isDisplayOnDevportal()) {
apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
}
if (apiRevisionDeployment.getDeployedTime() == null) {
apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOY_TIME"));
}
if (apiRevisionDeployment.getSuccessDeployedTime() == null) {
apiRevisionDeployment.setSuccessDeployedTime(rs.getString("DEPLOYED_TIME"));
}
}
}
return apiRevisionDeploymentList;
}
use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method deployAPIProductRevision.
@Override
public Response deployAPIProductRevision(String apiProductId, String revisionId, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
Map<String, Environment> environments = APIUtil.getEnvironments(organization);
List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTO) {
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setRevisionUUID(revisionId);
String environment = apiRevisionDeploymentDTO.getName();
if (environments.get(environment) == null) {
RestApiUtil.handleBadRequest("Gateway environment not found: " + environment, log);
}
apiRevisionDeployment.setDeployment(environment);
apiRevisionDeployment.setVhost(apiRevisionDeploymentDTO.getVhost());
if (StringUtils.isEmpty(apiRevisionDeploymentDTO.getVhost())) {
// vhost is only required when deploying an revision, not required when un-deploying a revision
// since the same scheme 'APIRevisionDeployment' is used for deploy and undeploy, handle it here.
RestApiUtil.handleBadRequest("Required field 'vhost' not found in deployment", log);
}
apiRevisionDeployment.setDisplayOnDevportal(apiRevisionDeploymentDTO.isDisplayOnDevportal());
apiRevisionDeployments.add(apiRevisionDeployment);
}
apiProvider.deployAPIProductRevision(apiProductId, revisionId, apiRevisionDeployments);
List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeploymentList(revisionId);
List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsResponse) {
apiRevisionDeploymentDTOS.add(APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
}
Response.Status status = Response.Status.CREATED;
return Response.status(status).entity(apiRevisionDeploymentDTOS).build();
}
use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.
the class VHost method fromEndpointUrls.
public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementException {
VHost vhost = new VHost();
for (String endpoint : endpoints) {
if (StringUtils.isEmpty(endpoint)) {
continue;
}
String[] elem = endpoint.split(PROTOCOL_SEPARATOR);
if (elem.length != 2) {
throw new APIManagementException("Error reading gateway environment endpoint URL");
}
// URL is not parsing for ws and wss protocols
URL url;
try {
switch(elem[0]) {
case HTTPS_PROTOCOL:
url = new URL(endpoint);
vhost.setHttpsPort(url.getPort() < 0 ? DEFAULT_HTTPS_PORT : url.getPort());
vhost.setHost(url.getHost());
vhost.setHttpContext(url.getPath());
break;
case HTTP_PROTOCOL:
url = new URL(endpoint);
vhost.setHttpPort(url.getPort() < 0 ? DEFAULT_HTTP_PORT : url.getPort());
String host = StringUtils.isNotEmpty(vhost.getHost()) ? vhost.getHost() : url.getHost();
vhost.setHost(host);
String httpContext = StringUtils.isNotEmpty(vhost.getHttpContext()) ? vhost.getHttpContext() : url.getPath();
vhost.setHttpContext(httpContext);
break;
case WSS_PROTOCOL:
// URL is not parsing for wss protocols, hence change to https
url = new URL(HTTPS_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWssPort(url.getPort() < 0 ? DEFAULT_WSS_PORT : url.getPort());
break;
case WS_PROTOCOL:
// URL is not parsing for ws protocols, hence change to http
url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWsPort(url.getPort() < 0 ? DEFAULT_WS_PORT : url.getPort());
break;
case WEBSUB_HTTP_PROTOCOL:
url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWebsubHttpPort(url.getPort() < 0 ? DEFAULT_WEBSUB_HTTP_PORT : url.getPort());
break;
case WEBSUB_HTTPS_PROTOCOL:
url = new URL(HTTPS_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWebsubHttpsPort(url.getPort() < 0 ? DEFAULT_WEBSUB_HTTPS_PORT : url.getPort());
break;
}
} catch (MalformedURLException e) {
throw new APIManagementException("Error reading gateway environment endpoint URL", e);
}
}
// host of Vhost is set by HTTP or HTTPS endpoints, if host is empty, the required fields are missing.
if (StringUtils.isEmpty(vhost.getHost())) {
throw new APIManagementException("Error while building VHost, missing required HTTP or HTTPS endpoint");
}
return vhost;
}
use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByLabel.
public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByLabel(String[] labels, String tenantDomain) throws APIManagementException {
String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_LABEL;
query = query.replaceAll(SQLConstants.GATEWAY_LABEL_REGEX, String.join(",", Collections.nCopies(labels.length, "?")));
List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
int index = 1;
for (String label : labels) {
preparedStatement.setString(index, label);
index++;
}
preparedStatement.setString(index, 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 Artifact for labels : " + StringUtils.join(",", labels), e);
}
return apiRuntimeArtifactDtoList;
}
Aggregations