use of org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto in project carbon-apimgt by wso2.
the class MicroGatewayArtifactGenerator method generateGatewayArtifact.
@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
try {
DeploymentDescriptorDto descriptorDto = new DeploymentDescriptorDto();
Map<String, ApiProjectDto> deploymentsMap = new HashMap<>();
// "tempDirectory" is the root artifact directory
File tempDirectory = CommonUtil.createTempDirectory(null);
for (APIRuntimeArtifactDto apiRuntimeArtifactDto : apiRuntimeArtifactDtoList) {
if (apiRuntimeArtifactDto.isFile()) {
InputStream artifact = (InputStream) apiRuntimeArtifactDto.getArtifact();
String fileName = apiRuntimeArtifactDto.getApiId().concat("-").concat(apiRuntimeArtifactDto.getRevision()).concat(APIConstants.ZIP_FILE_EXTENSION);
Path path = Paths.get(tempDirectory.getAbsolutePath(), fileName);
FileUtils.copyInputStreamToFile(artifact, path.toFile());
ApiProjectDto apiProjectDto = deploymentsMap.get(fileName);
if (apiProjectDto == null) {
apiProjectDto = new ApiProjectDto();
deploymentsMap.put(fileName, apiProjectDto);
apiProjectDto.setApiFile(fileName);
apiProjectDto.setEnvironments(new HashSet<>());
apiProjectDto.setOrganizationId(apiRuntimeArtifactDto.getOrganization());
}
// environment is unique for a revision in a deployment
// create new environment
EnvironmentDto environment = new EnvironmentDto();
environment.setName(apiRuntimeArtifactDto.getLabel());
environment.setVhost(apiRuntimeArtifactDto.getVhost());
// ignored if the name of the environment is same
apiProjectDto.getEnvironments().add(environment);
}
}
descriptorDto.setDeployments(new HashSet<>(deploymentsMap.values()));
String descriptorFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE).toString();
CommonUtil.writeDtoToFile(descriptorFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE_TYPE, descriptorDto);
// adding env_properties.json
Map<String, Map<String, Environment>> environmentSpecificAPIProperties = getEnvironmentSpecificAPIProperties(apiRuntimeArtifactDtoList);
String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME, environmentSpecificAPIProperties);
CommonUtil.archiveDirectory(tempDirectory.getAbsolutePath());
FileUtils.deleteQuietly(tempDirectory);
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
runtimeArtifactDto.setArtifact(new File(tempDirectory.getAbsolutePath() + APIConstants.ZIP_FILE_EXTENSION));
runtimeArtifactDto.setFile(true);
return runtimeArtifactDto;
} catch (APIImportExportException | IOException e) {
throw new APIManagementException("Error while Generating API artifact", e);
}
}
use of org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto in project carbon-apimgt by wso2.
the class RetrieveApiArtifactsApiServiceImpl method retrieveApiArtifactsPost.
public Response retrieveApiArtifactsPost(String xWSO2Tenant, String gatewayLabel, String type, UUIDListDTO uuidList, MessageContext messageContext) throws APIManagementException {
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
RuntimeArtifactDto runtimeArtifactDto = APIArtifactGeneratorUtil.generateAPIArtifact(uuidList.getUuids(), "", "", gatewayLabel, type, xWSO2Tenant);
if (runtimeArtifactDto != null) {
if (runtimeArtifactDto.isFile()) {
File artifact = (File) runtimeArtifactDto.getArtifact();
StreamingOutput streamingOutput = (outputStream) -> {
try {
Files.copy(artifact.toPath(), outputStream);
} finally {
Files.delete(artifact.toPath());
}
};
return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=apis.zip").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_ZIP).build();
} else {
SynapseArtifactListDTO synapseArtifactListDTO = new SynapseArtifactListDTO();
if (runtimeArtifactDto.getArtifact() instanceof List) {
synapseArtifactListDTO.setList((List<String>) runtimeArtifactDto.getArtifact());
synapseArtifactListDTO.setCount(((List<String>) runtimeArtifactDto.getArtifact()).size());
}
return Response.ok().entity(synapseArtifactListDTO).header(RestApiConstants.HEADER_CONTENT_TYPE, RestApiConstants.APPLICATION_JSON).build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
}
}
use of org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto in project carbon-apimgt by wso2.
the class RuntimeMetadataApiServiceImpl method runtimeMetadataGet.
public Response runtimeMetadataGet(String xWSO2Tenant, String apiId, String gatewayLabel, MessageContext messageContext) throws APIManagementException {
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
RuntimeArtifactDto runtimeArtifactDto = RuntimeArtifactGeneratorUtil.generateMetadataArtifact(xWSO2Tenant, apiId, gatewayLabel);
if (runtimeArtifactDto != null) {
File artifact = (File) runtimeArtifactDto.getArtifact();
StreamingOutput streamingOutput = (outputStream) -> {
try {
Files.copy(artifact.toPath(), outputStream);
} finally {
Files.delete(artifact.toPath());
}
};
return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=deployment.json").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
}
}
use of org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAOTest method testGetAPIContextForMetaData.
@Test
public void testGetAPIContextForMetaData() throws APIManagementException {
String uuid = UUID.randomUUID().toString();
String name = "contextapiname";
String version = "1.0.0";
String revision = UUID.randomUUID().toString();
String context = "/context2";
URL resource = getClass().getClassLoader().getResource("admin-PizzaShackAPI-1.0.0.zip");
File file = new File(resource.getPath());
gatewayArtifactsMgtDAO.addGatewayAPIArtifactAndMetaData(uuid, name, version, revision, "carbon.super", APIConstants.HTTP_PROTOCOL, file);
API api = new API(new APIIdentifier("test-provider", name, version));
api.setContext(context);
api.setContextTemplate("/context2/{version}");
api.setUUID(uuid);
apiMgtDAO.addAPI(api, -1234, "testOrg");
Map<String, String> gatewayVhosts = new HashMap<>();
gatewayVhosts.put("label1", "dev.wso2.com");
gatewayArtifactsMgtDAO.addAndRemovePublishedGatewayLabels(uuid, revision, Collections.asSet("label1"), gatewayVhosts);
List<APIRuntimeArtifactDto> artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label1" }, "carbon.super");
Assert.assertEquals(artifacts.size(), 1);
RuntimeArtifactDto artifact = artifacts.get(0);
Assert.assertNotNull(artifact);
Assert.assertEquals(context, artifacts.get(0).getContext());
}
use of org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto in project carbon-apimgt by wso2.
the class GatewayArtifactsMgtDAOTest method testAddGatewayAPIArtifactAndMetaData.
@Test
public void testAddGatewayAPIArtifactAndMetaData() throws APIManagementException {
String uuid = UUID.randomUUID().toString();
String name = "apiname";
String version = "1.0.0";
String revision = UUID.randomUUID().toString();
URL resource = getClass().getClassLoader().getResource("admin-PizzaShackAPI-1.0.0.zip");
File file = new File(resource.getPath());
gatewayArtifactsMgtDAO.addGatewayAPIArtifactAndMetaData(uuid, name, version, revision, "carbon.super", APIConstants.HTTP_PROTOCOL, file);
API api = new API(new APIIdentifier("test-provider", name, version));
api.setContext("/context1");
api.setContextTemplate("/context1/{version}");
api.setUUID(uuid);
apiMgtDAO.addAPI(api, -1234, "testOrg");
String gatewayAPIId = gatewayArtifactsMgtDAO.getGatewayAPIId(name, version, "carbon.super");
Assert.assertEquals(gatewayAPIId, uuid);
Map<String, String> gatewayVhosts = new HashMap<>();
gatewayVhosts.put("label1", "dev.wso2.com");
gatewayArtifactsMgtDAO.addAndRemovePublishedGatewayLabels(uuid, revision, Collections.asSet("label1"), gatewayVhosts);
List<APIRuntimeArtifactDto> artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label1" }, "carbon.super");
Assert.assertEquals(artifacts.size(), 1);
RuntimeArtifactDto artifact = artifacts.get(0);
Assert.assertNotNull(artifact);
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setRevisionUUID(revision);
apiRevisionDeployment.setDeployment("label1");
gatewayVhosts = new HashMap<>();
gatewayVhosts.put("label2", "prod.wso2.com");
gatewayArtifactsMgtDAO.addAndRemovePublishedGatewayLabels(uuid, revision, Collections.asSet("label2"), gatewayVhosts, Collections.asSet(apiRevisionDeployment));
artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label1" }, "carbon.super");
Assert.assertEquals(artifacts.size(), 0);
artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label2" }, "carbon.super");
Assert.assertEquals(artifacts.size(), 1);
artifact = artifacts.get(0);
Assert.assertNotNull(artifact);
}
Aggregations