use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class GatewayStartupListener method deployArtifactsAtStartup.
private boolean deployArtifactsAtStartup(String tenantDomain) throws ArtifactSynchronizerException {
GatewayArtifactSynchronizerProperties gatewayArtifactSynchronizerProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration().getGatewayArtifactSynchronizerProperties();
boolean flag = false;
long waitTime = System.currentTimeMillis() + 60 * 1000;
long retryDuration = 5000;
if (gatewayArtifactSynchronizerProperties.isRetrieveFromStorageEnabled()) {
InMemoryAPIDeployer inMemoryAPIDeployer = new InMemoryAPIDeployer();
while (waitTime > System.currentTimeMillis() && !flag) {
flag = inMemoryAPIDeployer.deployAllAPIsAtGatewayStartup(gatewayArtifactSynchronizerProperties.getGatewayLabels(), tenantDomain);
if (!flag) {
log.error("Unable to deploy synapse artifacts at gateway. Next retry in " + (retryDuration / 1000) + " seconds");
try {
Thread.sleep(retryDuration);
} catch (InterruptedException ignore) {
}
}
}
}
return flag;
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class APIArtifactGeneratorUtil method generateAPIArtifact.
public static RuntimeArtifactDto generateAPIArtifact(List<String> apiUuids, String name, String version, String gatewayLabel, String type, String tenantDomain) throws APIManagementException {
GatewayArtifactGenerator gatewayArtifactGenerator = ServiceReferenceHolder.getInstance().getGatewayArtifactGenerator(type);
if (gatewayArtifactGenerator != null) {
List<APIRuntimeArtifactDto> gatewayArtifacts;
if (StringUtils.isNotEmpty(gatewayLabel)) {
byte[] decodedValue = Base64.decodeBase64(gatewayLabel.getBytes());
String[] gatewayLabels = new String(decodedValue).split("\\|");
if (!apiUuids.isEmpty()) {
gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDs(apiUuids, gatewayLabels, tenantDomain);
} else {
gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByLabel(gatewayLabels, tenantDomain);
}
} else {
gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifacts(tenantDomain);
}
if (gatewayArtifacts != null) {
if (gatewayArtifacts.isEmpty()) {
throw new APIManagementException("No API Artifacts", ExceptionCodes.NO_API_ARTIFACT_FOUND);
}
for (APIRuntimeArtifactDto apiRuntimeArtifactDto : gatewayArtifacts) {
String organizationId = gatewayArtifactsMgtDAO.retrieveOrganization(apiRuntimeArtifactDto.getApiId());
if (organizationId != null) {
apiRuntimeArtifactDto.setOrganization(organizationId);
}
}
}
if (gatewayArtifacts == null || gatewayArtifacts.isEmpty()) {
return null;
}
return gatewayArtifactGenerator.generateGatewayArtifact(gatewayArtifacts);
} else {
Set<String> gatewayArtifactGeneratorTypes = ServiceReferenceHolder.getInstance().getGatewayArtifactGeneratorTypes();
throw new APIManagementException("Couldn't find gateway Type", ExceptionCodes.from(ExceptionCodes.GATEWAY_TYPE_NOT_FOUND, String.join(",", gatewayArtifactGeneratorTypes)));
}
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts 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;
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByAPIIDAndLabel.
public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByAPIIDAndLabel(String apiId, String[] labels, String tenantDomain) throws APIManagementException {
String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_APIID_AND_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)) {
preparedStatement.setString(1, apiId);
int index = 2;
for (String label : labels) {
preparedStatement.setString(index, label);
index++;
}
preparedStatement.setString(index, tenantDomain);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
apiRuntimeArtifactDto.setApiId(apiId);
String label = resultSet.getString("LABEL");
// Do not handle the exception here since runtime artifacts are retrieved by API UUID
// throw the exception here.
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);
} catch (IOException e) {
// Do not handle the exception here since runtime artifacts are retrieved by API UUID
// throw the exception here.
handleException("Error occurred retrieving input stream from byte array.", e);
}
}
apiRuntimeArtifactDto.setFile(true);
apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
}
}
} catch (SQLException e) {
handleException("Failed to retrieve Gateway Artifact for Api : " + apiId + " and labels: " + StringUtils.join(",", labels), e);
}
return apiRuntimeArtifactDtoList;
}
use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByAPIIDs.
public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByAPIIDs(List<String> apiIds, String[] labels, String tenantDomain) throws APIManagementException {
// Split apiId list into smaller list of size 25
List<List<String>> apiIdsChunk = new ArrayList<>();
int apiIdListSize = apiIds.size();
int apiIdArrayIndex = 0;
int apiIdsChunkSize = SQLConstants.API_ID_CHUNK_SIZE;
while (apiIdArrayIndex < apiIdListSize) {
apiIdsChunk.add(apiIds.subList(apiIdArrayIndex, Math.min(apiIdArrayIndex + apiIdsChunkSize, apiIdListSize)));
apiIdArrayIndex += apiIdsChunkSize;
}
List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
for (List<String> apiIdList : apiIdsChunk) {
String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_MULTIPLE_APIIDs_AND_LABEL;
query = query.replaceAll(SQLConstants.GATEWAY_LABEL_REGEX, String.join(",", Collections.nCopies(labels.length, "?")));
query = query.replaceAll(SQLConstants.API_ID_REGEX, String.join(",", Collections.nCopies(apiIdList.size(), "?")));
try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
int index = 1;
for (String apiId : apiIdList) {
preparedStatement.setString(index, apiId);
index++;
}
for (String label : labels) {
preparedStatement.setString(index, label);
index++;
}
preparedStatement.setString(index, tenantDomain);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
String apiId = resultSet.getString("API_ID");
apiRuntimeArtifactDto.setApiId(apiId);
String label = resultSet.getString("LABEL");
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);
} catch (IOException e) {
// Do not handle the exception here since runtime artifacts are retrieved by API UUID
// throw the exception here.
handleException("Error occurred retrieving input stream from byte array.", e);
}
}
apiRuntimeArtifactDto.setFile(true);
apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
}
}
} catch (SQLException e) {
handleException("Failed to retrieve Gateway Artifact for Apis : " + apiIdList + " and labels: " + StringUtils.join(",", labels), e);
}
}
return apiRuntimeArtifactDtoList;
}
Aggregations