Search in sources :

Example 76 with APIDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.

the class ExportUtils method addDependentAPIsToArchive.

/**
 * Retrieve dependent APIs by checking the resources of the API Product and store those in the archive directory.
 *
 * @param archivePath           Temp location to save the API artifacts
 * @param apiProductDtoToReturn API Product DTO which the resources should be considered
 * @param userName              User name of the requester
 * @param provider              API Product Provider
 * @param exportFormat          Export format of the API meta data, could be yaml or json
 * @param isStatusPreserved     Whether API status is preserved while export
 * @param organization          Organization
 * @throws APIImportExportException If an error occurs while creating the directory or extracting the archive
 * @throws APIManagementException   If an error occurs while retrieving API related resources
 */
public static void addDependentAPIsToArchive(String archivePath, APIProductDTO apiProductDtoToReturn, ExportFormat exportFormat, APIProvider provider, String userName, Boolean isStatusPreserved, boolean preserveDocs, boolean preserveCredentials, String organization) throws APIImportExportException, APIManagementException {
    String apisDirectoryPath = archivePath + File.separator + ImportExportConstants.APIS_DIRECTORY;
    CommonUtil.createDirectory(apisDirectoryPath);
    List<ProductAPIDTO> apisList = apiProductDtoToReturn.getApis();
    for (ProductAPIDTO productAPIDTO : apisList) {
        String apiProductRequesterDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        API api = provider.getAPIbyUUID(productAPIDTO.getApiId(), apiProductRequesterDomain);
        APIDTO apiDtoToReturn = APIMappingUtil.fromAPItoDTO(api, preserveCredentials, null);
        File dependentAPI = exportApi(provider, api.getId(), apiDtoToReturn, api, userName, exportFormat, isStatusPreserved, preserveDocs, StringUtils.EMPTY, organization);
        CommonUtil.extractArchive(dependentAPI, apisDirectoryPath);
    }
}
Also used : ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) API(org.wso2.carbon.apimgt.api.model.API) ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 77 with APIDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.

the class ExportUtils method addEndpointCertificatesToArchive.

/**
 * Retrieve the endpoint certificates and store those in the archive directory.
 *
 * @param archivePath  File path to export the endpoint certificates
 * @param apiDto       API DTO to be exported
 * @param tenantId     Tenant id of the user
 * @param exportFormat Export format of file
 * @throws APIImportExportException If an error occurs while exporting endpoint certificates
 */
public static void addEndpointCertificatesToArchive(String archivePath, APIDTO apiDto, int tenantId, ExportFormat exportFormat) throws APIImportExportException {
    List<String> productionEndpoints;
    List<String> sandboxEndpoints;
    List<String> productionFailovers;
    List<String> sandboxFailovers;
    Set<String> uniqueEndpointURLs = new HashSet<>();
    JsonArray endpointCertificatesDetails = new JsonArray();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String endpointConfigString = gson.toJson(apiDto.getEndpointConfig());
    String endpointCertsDirectoryPath = archivePath + File.separator + ImportExportConstants.ENDPOINT_CERTIFICATES_DIRECTORY;
    CommonUtil.createDirectory(endpointCertsDirectoryPath);
    if (StringUtils.isEmpty(endpointConfigString) || "null".equals(endpointConfigString)) {
        if (log.isDebugEnabled()) {
            log.debug("Endpoint Details are empty for API: " + apiDto.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDto.getVersion());
        }
        return;
    }
    try {
        JSONTokener tokener = new JSONTokener(endpointConfigString);
        JSONObject endpointConfig = new JSONObject(tokener);
        productionEndpoints = getEndpointURLs(endpointConfig, API_DATA_PRODUCTION_ENDPOINTS, apiDto.getName());
        sandboxEndpoints = getEndpointURLs(endpointConfig, API_DATA_SANDBOX_ENDPOINTS, apiDto.getName());
        productionFailovers = getEndpointURLs(endpointConfig, APIConstants.ENDPOINT_PRODUCTION_FAILOVERS, apiDto.getName());
        sandboxFailovers = getEndpointURLs(endpointConfig, APIConstants.ENDPOINT_SANDBOX_FAILOVERS, apiDto.getName());
        // Remove duplicate and append result
        uniqueEndpointURLs.addAll(productionEndpoints);
        uniqueEndpointURLs.addAll(sandboxEndpoints);
        uniqueEndpointURLs.addAll(productionFailovers);
        uniqueEndpointURLs.addAll(sandboxFailovers);
        for (String url : uniqueEndpointURLs) {
            JsonArray certificateListOfUrl = getEndpointCertificateContentAndMetaData(tenantId, url, endpointCertsDirectoryPath);
            endpointCertificatesDetails.addAll(certificateListOfUrl);
        }
        if (endpointCertificatesDetails.size() > 0) {
            CommonUtil.writeDtoToFile(endpointCertsDirectoryPath + ImportExportConstants.ENDPOINTS_CERTIFICATE_FILE, exportFormat, ImportExportConstants.TYPE_ENDPOINT_CERTIFICATES, endpointCertificatesDetails);
        } else if (log.isDebugEnabled()) {
            log.debug("No endpoint certificates available for API: " + apiDto.getName() + StringUtils.SPACE + APIConstants.API_DATA_VERSION + ": " + apiDto.getVersion() + ". Skipping certificate export.");
        }
    } catch (JSONException e) {
        throw new APIImportExportException("Error in converting Endpoint config to JSON object in API: " + apiDto.getName(), e);
    } catch (IOException e) {
        throw new APIImportExportException("Error while retrieving saving endpoint certificate details for API: " + apiDto.getName() + " as YAML", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 78 with APIDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addWebsocketTopicMappings.

private static void addWebsocketTopicMappings(API api, APIDTO apidto) {
    org.json.JSONObject endpointConfiguration = new org.json.JSONObject(api.getEndpointConfig());
    String sandboxEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_SANDBOX_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_SANDBOX_ENDPOINTS).getString("url") : null;
    String productionEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_PRODUCTION_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_PRODUCTION_ENDPOINTS).getString("url") : null;
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    for (APIOperationsDTO operation : apidto.getOperations()) {
        String key = operation.getTarget();
        String mapping = operation.getUriMapping() == null ? "" : Paths.get("/", operation.getUriMapping()).toString();
        Map<String, String> endpoints = new HashMap<>();
        if (sandboxEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, sandboxEndpointUrl + mapping);
        }
        if (productionEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, productionEndpointUrl + mapping);
        }
        perTopicMappings.put(key, endpoints);
    }
    api.setWebSocketTopicMappingConfiguration(new WebSocketTopicMappingConfiguration(perTopicMappings));
    addWebsocketTopicResourceKeys(api);
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) Map(java.util.Map) HashMap(java.util.HashMap)

Example 79 with APIDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method setAPIFaultSequencesToBeAdded.

private static void setAPIFaultSequencesToBeAdded(API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
    String faultSeqExt = APIUtil.getSequenceExtensionName(api) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
    if (!APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
        gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
        List<MediationPolicyDTO> mediationPolicies = apidto.getMediationPolicies();
        GatewayContentDTO faultSequenceContent = retrieveSequence(extractedPath, mediationPolicies, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, api);
        if (faultSequenceContent != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(faultSequenceContent, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    }
    gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) MediationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.MediationPolicyDTO)

Example 80 with APIDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDTO in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method setCustomSequencesToBeAdded.

private static void setCustomSequencesToBeAdded(APIProduct apiProduct, API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
    if (APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
        GatewayContentDTO gatewayInContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE);
        if (gatewayOutContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayOutContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayFaultContentDTO = retrieveOperationPolicySequenceForProducts(apiProduct, api, extractedPath, APIConstants.OPERATION_SEQUENCE_TYPE_FAULT);
        if (gatewayFaultContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayFaultContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    } else {
        GatewayContentDTO gatewayInContentDTO = retrieveSequence(apiProduct, extractedPath, apidto.getMediationPolicies(), APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, api);
        if (gatewayInContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayInContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
        GatewayContentDTO gatewayOutContentDTO = retrieveSequence(apiProduct, extractedPath, apidto.getMediationPolicies(), APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, api);
        if (gatewayOutContentDTO != null) {
            gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(gatewayOutContentDTO, gatewayAPIDTO.getSequenceToBeAdd()));
        }
    }
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Aggregations

APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO)29 HashMap (java.util.HashMap)28 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)27 ArrayList (java.util.ArrayList)25 API (org.wso2.carbon.apimgt.api.model.API)25 IOException (java.io.IOException)18 API (org.wso2.carbon.apimgt.core.models.API)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)15 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDTO)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)14 File (java.io.File)12 Response (javax.ws.rs.core.Response)12 Map (java.util.Map)11 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)11 Test (org.junit.Test)10 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)10 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)10 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)10 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)10