Search in sources :

Example 1 with RuntimeArtifactDto

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);
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) InputStream(java.io.InputStream) EnvironmentDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.EnvironmentDto) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) IOException(java.io.IOException) ApiProjectDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.ApiProjectDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DeploymentDescriptorDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.DeploymentDescriptorDto)

Example 2 with RuntimeArtifactDto

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();
    }
}
Also used : SynapseArtifactListDTO(org.wso2.carbon.apimgt.internal.service.dto.SynapseArtifactListDTO) Files(java.nio.file.Files) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) RuntimeArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.RuntimeArtifactGeneratorUtil) APIArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.APIArtifactGeneratorUtil) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) APIConstants(org.wso2.carbon.apimgt.impl.APIConstants) ArrayList(java.util.ArrayList) SubscriptionValidationDataUtil(org.wso2.carbon.apimgt.internal.service.utils.SubscriptionValidationDataUtil) List(java.util.List) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) Response(javax.ws.rs.core.Response) RestApiConstants(org.wso2.carbon.apimgt.rest.api.common.RestApiConstants) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UUIDListDTO(org.wso2.carbon.apimgt.internal.service.dto.UUIDListDTO) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) RetrieveApiArtifactsApiService(org.wso2.carbon.apimgt.internal.service.RetrieveApiArtifactsApiService) SynapseArtifactListDTO(org.wso2.carbon.apimgt.internal.service.dto.SynapseArtifactListDTO) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) StreamingOutput(javax.ws.rs.core.StreamingOutput) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 3 with RuntimeArtifactDto

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();
    }
}
Also used : Files(java.nio.file.Files) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) RuntimeArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.RuntimeArtifactGeneratorUtil) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) APIConstants(org.wso2.carbon.apimgt.impl.APIConstants) SubscriptionValidationDataUtil(org.wso2.carbon.apimgt.internal.service.utils.SubscriptionValidationDataUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Response(javax.ws.rs.core.Response) RestApiConstants(org.wso2.carbon.apimgt.rest.api.common.RestApiConstants) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) RuntimeMetadataApiService(org.wso2.carbon.apimgt.internal.service.RuntimeMetadataApiService) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File)

Example 4 with RuntimeArtifactDto

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());
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) HashMap(java.util.HashMap) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 5 with RuntimeArtifactDto

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);
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) HashMap(java.util.HashMap) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

File (java.io.File)8 RuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)7 HashMap (java.util.HashMap)4 Files (java.nio.file.Files)3 Response (javax.ws.rs.core.Response)3 StreamingOutput (javax.ws.rs.core.StreamingOutput)3 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)3 ExceptionCodes (org.wso2.carbon.apimgt.api.ExceptionCodes)3 API (org.wso2.carbon.apimgt.api.model.API)3 APIConstants (org.wso2.carbon.apimgt.impl.APIConstants)3 RuntimeArtifactGeneratorUtil (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.RuntimeArtifactGeneratorUtil)3 SubscriptionValidationDataUtil (org.wso2.carbon.apimgt.internal.service.utils.SubscriptionValidationDataUtil)3 RestApiConstants (org.wso2.carbon.apimgt.rest.api.common.RestApiConstants)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2