Search in sources :

Example 1 with ClientCertificateDTO

use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method restoreAPIRevision.

/**
 * Restore API revision database records as the Current API of an API
 *
 * @param apiRevision content of the revision
 * @throws APIManagementException if an error occurs when restoring an API revision
 */
public void restoreAPIRevision(APIRevision apiRevision) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            // Retrieve API ID
            APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiRevision.getApiUUID());
            int apiId = getAPIID(apiRevision.getApiUUID(), connection);
            int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
            String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
            // Removing related Current API entries from AM_API_URL_MAPPING table
            PreparedStatement removeURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_API_URL_MAPPING_BY_API_ID);
            removeURLMappingsStatement.setInt(1, apiId);
            removeURLMappingsStatement.executeUpdate();
            // Restoring to AM_API_URL_MAPPING table
            PreparedStatement getURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_URL_MAPPINGS_WITH_SCOPE_AND_PRODUCT_ID_BY_REVISION_UUID);
            getURLMappingsStatement.setInt(1, apiId);
            getURLMappingsStatement.setString(2, apiRevision.getRevisionUUID());
            List<URITemplate> urlMappingList = new ArrayList<>();
            try (ResultSet rs = getURLMappingsStatement.executeQuery()) {
                while (rs.next()) {
                    String script = null;
                    URITemplate uriTemplate = new URITemplate();
                    uriTemplate.setHTTPVerb(rs.getString(1));
                    uriTemplate.setAuthType(rs.getString(2));
                    uriTemplate.setUriTemplate(rs.getString(3));
                    uriTemplate.setThrottlingTier(rs.getString(4));
                    InputStream mediationScriptBlob = rs.getBinaryStream(5);
                    if (mediationScriptBlob != null) {
                        script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
                    }
                    uriTemplate.setMediationScript(script);
                    if (!StringUtils.isEmpty(rs.getString(6))) {
                        Scope scope = new Scope();
                        scope.setKey(rs.getString(6));
                        uriTemplate.setScope(scope);
                    }
                    if (rs.getInt(7) != 0) {
                        // Adding product id to uri template id just to store value
                        uriTemplate.setId(rs.getInt(7));
                    }
                    urlMappingList.add(uriTemplate);
                }
            }
            Map<String, URITemplate> uriTemplateMap = new HashMap<>();
            for (URITemplate urlMapping : urlMappingList) {
                if (urlMapping.getScope() != null) {
                    URITemplate urlMappingNew = urlMapping;
                    URITemplate urlMappingExisting = uriTemplateMap.get(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb());
                    if (urlMappingExisting != null && urlMappingExisting.getScopes() != null) {
                        if (!urlMappingExisting.getScopes().contains(urlMapping.getScope())) {
                            urlMappingExisting.setScopes(urlMapping.getScope());
                            uriTemplateMap.put(urlMappingExisting.getUriTemplate() + urlMappingExisting.getHTTPVerb(), urlMappingExisting);
                        }
                    } else {
                        urlMappingNew.setScopes(urlMapping.getScope());
                        uriTemplateMap.put(urlMappingNew.getUriTemplate() + urlMappingNew.getHTTPVerb(), urlMappingNew);
                    }
                } else {
                    uriTemplateMap.put(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb(), urlMapping);
                }
            }
            setOperationPoliciesToURITemplatesMap(apiRevision.getRevisionUUID(), uriTemplateMap);
            PreparedStatement insertURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_URL_MAPPINGS_CURRENT_API);
            for (URITemplate urlMapping : uriTemplateMap.values()) {
                insertURLMappingsStatement.setInt(1, apiId);
                insertURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
                insertURLMappingsStatement.setString(3, urlMapping.getAuthType());
                insertURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
                insertURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
                insertURLMappingsStatement.addBatch();
            }
            insertURLMappingsStatement.executeBatch();
            // Add to AM_API_RESOURCE_SCOPE_MAPPING table and to AM_API_PRODUCT_MAPPING
            PreparedStatement getCurrentAPIURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_CURRENT_API_URL_MAPPINGS_ID);
            PreparedStatement insertScopeResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_SCOPE_RESOURCE_MAPPING);
            PreparedStatement insertProductResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_PRODUCT_RESOURCE_MAPPING);
            PreparedStatement insertOperationPolicyMappingStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.ADD_API_OPERATION_POLICY_MAPPING);
            PreparedStatement deleteOutdatedOperationPolicyStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.DELETE_OPERATION_POLICY_BY_POLICY_ID);
            Map<String, String> restoredPolicyMap = new HashMap<>();
            Set<String> usedClonedPolicies = new HashSet<String>();
            for (URITemplate urlMapping : uriTemplateMap.values()) {
                if (urlMapping.getScopes() != null) {
                    getCurrentAPIURLMappingsStatement.setInt(1, apiId);
                    getCurrentAPIURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
                    getCurrentAPIURLMappingsStatement.setString(3, urlMapping.getAuthType());
                    getCurrentAPIURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
                    getCurrentAPIURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
                    try (ResultSet rs = getCurrentAPIURLMappingsStatement.executeQuery()) {
                        while (rs.next()) {
                            for (Scope scope : urlMapping.getScopes()) {
                                insertScopeResourceMappingStatement.setString(1, scope.getKey());
                                insertScopeResourceMappingStatement.setInt(2, rs.getInt(1));
                                insertScopeResourceMappingStatement.setInt(3, tenantId);
                                insertScopeResourceMappingStatement.addBatch();
                            }
                        }
                    }
                }
                if (urlMapping.getId() != 0) {
                    getCurrentAPIURLMappingsStatement.setInt(1, apiId);
                    getCurrentAPIURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
                    getCurrentAPIURLMappingsStatement.setString(3, urlMapping.getAuthType());
                    getCurrentAPIURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
                    getCurrentAPIURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
                    try (ResultSet rs = getCurrentAPIURLMappingsStatement.executeQuery()) {
                        while (rs.next()) {
                            insertProductResourceMappingStatement.setInt(1, urlMapping.getId());
                            insertProductResourceMappingStatement.setInt(2, rs.getInt(1));
                            insertProductResourceMappingStatement.addBatch();
                        }
                    }
                }
                if (!urlMapping.getOperationPolicies().isEmpty()) {
                    getCurrentAPIURLMappingsStatement.setInt(1, apiId);
                    getCurrentAPIURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
                    getCurrentAPIURLMappingsStatement.setString(3, urlMapping.getAuthType());
                    getCurrentAPIURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
                    getCurrentAPIURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
                    try (ResultSet rs = getCurrentAPIURLMappingsStatement.executeQuery()) {
                        while (rs.next()) {
                            for (OperationPolicy policy : urlMapping.getOperationPolicies()) {
                                if (!restoredPolicyMap.keySet().contains(policy.getPolicyName())) {
                                    String restoredPolicyId = restoreOperationPolicyRevision(connection, apiRevision.getApiUUID(), policy.getPolicyId(), apiRevision.getId(), tenantDomain);
                                    // policy ID is stored in a map as same policy can be applied to multiple operations
                                    // and we only need to create the policy once.
                                    restoredPolicyMap.put(policy.getPolicyName(), restoredPolicyId);
                                    usedClonedPolicies.add(restoredPolicyId);
                                }
                                Gson gson = new Gson();
                                String paramJSON = gson.toJson(policy.getParameters());
                                insertOperationPolicyMappingStatement.setInt(1, rs.getInt(1));
                                insertOperationPolicyMappingStatement.setString(2, restoredPolicyMap.get(policy.getPolicyName()));
                                insertOperationPolicyMappingStatement.setString(3, policy.getDirection());
                                insertOperationPolicyMappingStatement.setString(4, paramJSON);
                                insertOperationPolicyMappingStatement.setInt(5, policy.getOrder());
                                insertOperationPolicyMappingStatement.addBatch();
                            }
                        }
                    }
                }
            }
            insertScopeResourceMappingStatement.executeBatch();
            insertProductResourceMappingStatement.executeBatch();
            insertOperationPolicyMappingStatement.executeBatch();
            deleteOutdatedOperationPolicyStatement.executeBatch();
            cleanUnusedClonedOperationPolicies(connection, usedClonedPolicies, apiRevision.getApiUUID());
            // Restoring AM_API_CLIENT_CERTIFICATE table entries
            PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_API_ID);
            removeClientCertificatesStatement.setInt(1, apiId);
            removeClientCertificatesStatement.executeUpdate();
            PreparedStatement getClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_BY_REVISION_UUID);
            getClientCertificatesStatement.setInt(1, apiId);
            getClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
            List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
            try (ResultSet rs = getClientCertificatesStatement.executeQuery()) {
                while (rs.next()) {
                    ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
                    clientCertificateDTO.setAlias(rs.getString(1));
                    clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
                    clientCertificateDTO.setTierName(rs.getString(3));
                    clientCertificateDTOS.add(clientCertificateDTO);
                }
            }
            PreparedStatement insertClientCertificateStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_CLIENT_CERTIFICATES_AS_CURRENT_API);
            for (ClientCertificateDTO clientCertificateDTO : clientCertificateDTOS) {
                insertClientCertificateStatement.setInt(1, tenantId);
                insertClientCertificateStatement.setString(2, clientCertificateDTO.getAlias());
                insertClientCertificateStatement.setInt(3, apiId);
                insertClientCertificateStatement.setBinaryStream(4, getInputStream(clientCertificateDTO.getCertificate()));
                insertClientCertificateStatement.setBoolean(5, false);
                insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
                insertClientCertificateStatement.setString(7, "Current API");
                insertClientCertificateStatement.addBatch();
            }
            insertClientCertificateStatement.executeBatch();
            // Restoring AM_GRAPHQL_COMPLEXITY table
            PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_API_ID);
            removeGraphQLComplexityStatement.setInt(1, apiId);
            removeGraphQLComplexityStatement.executeUpdate();
            PreparedStatement getGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
            List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<>();
            getGraphQLComplexityStatement.setInt(1, apiId);
            getGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
            try (ResultSet rs1 = getGraphQLComplexityStatement.executeQuery()) {
                while (rs1.next()) {
                    CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
                    customComplexityDetails.setType(rs1.getString("TYPE"));
                    customComplexityDetails.setField(rs1.getString("FIELD"));
                    customComplexityDetails.setComplexityValue(rs1.getInt("COMPLEXITY_VALUE"));
                    customComplexityDetailsList.add(customComplexityDetails);
                }
            }
            PreparedStatement insertGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_GRAPHQL_COMPLEXITY_AS_CURRENT_API);
            for (CustomComplexityDetails customComplexityDetails : customComplexityDetailsList) {
                insertGraphQLComplexityStatement.setString(1, UUID.randomUUID().toString());
                insertGraphQLComplexityStatement.setInt(2, apiId);
                insertGraphQLComplexityStatement.setString(3, customComplexityDetails.getType());
                insertGraphQLComplexityStatement.setString(4, customComplexityDetails.getField());
                insertGraphQLComplexityStatement.setInt(5, customComplexityDetails.getComplexityValue());
                insertGraphQLComplexityStatement.addBatch();
            }
            insertGraphQLComplexityStatement.executeBatch();
            restoreAPIRevisionMetaDataToWorkingCopy(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails) Scope(org.wso2.carbon.apimgt.api.model.Scope) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ResultSet(java.sql.ResultSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 2 with ClientCertificateDTO

use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.

the class CertificateMgtDAO method updateClientCertificate.

/**
 * To update an already existing client certificate.
 *
 * @param certificate : Specific certificate.
 * @param alias       : Alias of the certificate.
 * @param tier        : Name of tier related with the certificate.
 * @param tenantId    : ID of the tenant.
 * @param organization : Organization
 * @return true if the update succeeds, unless false.
 * @throws CertificateManagementException Certificate Management Exception.
 */
public boolean updateClientCertificate(String certificate, String alias, String tier, int tenantId, String organization) throws CertificateManagementException {
    List<ClientCertificateDTO> clientCertificateDTOList = getClientCertificates(tenantId, alias, null, organization);
    ClientCertificateDTO clientCertificateDTO;
    if (clientCertificateDTOList.size() == 0) {
        if (log.isDebugEnabled()) {
            log.debug("Client certificate update request is received for a non-existing alias " + alias + " of " + "tenant " + tenantId);
        }
        return false;
    }
    clientCertificateDTO = clientCertificateDTOList.get(0);
    if (StringUtils.isNotEmpty(certificate)) {
        clientCertificateDTO.setCertificate(certificate);
    }
    if (StringUtils.isNotEmpty(tier)) {
        clientCertificateDTO.setTierName(tier);
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            deleteClientCertificate(connection, null, alias, tenantId);
            addClientCertificate(connection, clientCertificateDTO.getCertificate(), clientCertificateDTO.getApiIdentifier(), alias, clientCertificateDTO.getTierName(), tenantId, organization);
            connection.commit();
        } catch (SQLException e) {
            handleConnectionRollBack(connection);
            handleException("Error while updating client certificate for the API for the alias " + alias, e);
        }
    } catch (SQLException e) {
        handleException("Error while updating client certificate for the API for the alias " + alias, e);
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)

Example 3 with ClientCertificateDTO

use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.

the class ExportUtils method addClientCertificatesToArchive.

/**
 * Retrieve Mutual SSL related certificates and store those in the archive directory.
 *
 * @param archivePath  Folder path to export client certificates
 * @param identifier   Identifier
 * @param tenantId     Tenant id of the user
 * @param provider     Api Provider
 * @param exportFormat Export format of file
 * @param organization Organization
 * @throws APIImportExportException If an error occurs when writing to file or retrieving certificate metadata
 */
public static void addClientCertificatesToArchive(String archivePath, Identifier identifier, int tenantId, APIProvider provider, ExportFormat exportFormat, String organization) throws APIImportExportException {
    List<ClientCertificateDTO> certificateMetadataDTOs;
    try {
        if (identifier instanceof APIProductIdentifier) {
            certificateMetadataDTOs = provider.searchClientCertificates(tenantId, null, (APIProductIdentifier) identifier, organization);
        } else {
            certificateMetadataDTOs = provider.searchClientCertificates(tenantId, null, (APIIdentifier) identifier, organization);
        }
        if (!certificateMetadataDTOs.isEmpty()) {
            String clientCertsDirectoryPath = archivePath + File.separator + ImportExportConstants.CLIENT_CERTIFICATES_DIRECTORY;
            CommonUtil.createDirectory(clientCertsDirectoryPath);
            JsonArray certificateList = getClientCertificateContentAndMetaData(certificateMetadataDTOs, clientCertsDirectoryPath);
            if (certificateList.size() > 0) {
                CommonUtil.writeDtoToFile(clientCertsDirectoryPath + ImportExportConstants.CLIENT_CERTIFICATE_FILE, exportFormat, ImportExportConstants.TYPE_CLIENT_CERTIFICATES, certificateList);
            }
        }
    } catch (IOException e) {
        throw new APIImportExportException("Error while saving as YAML or JSON", e);
    } catch (APIManagementException e) {
        throw new APIImportExportException("Error retrieving certificate meta data. tenantId [" + tenantId + "] api [" + tenantId + "]", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException)

Example 4 with ClientCertificateDTO

use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAPIClientCertificateContentByAlias.

@Override
public Response getAPIClientCertificateContentByAlias(String apiId, String alias, MessageContext messageContext) {
    String organization = null;
    String certFileName = alias + ".crt";
    try {
        organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        ClientCertificateDTO clientCertificateDTO = CertificateRestApiUtils.preValidateClientCertificate(alias, api.getId(), organization);
        if (clientCertificateDTO != null) {
            Object certificate = CertificateRestApiUtils.getDecodedCertificate(clientCertificateDTO.getCertificate());
            Response.ResponseBuilder responseBuilder = Response.ok().entity(certificate);
            responseBuilder.header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + certFileName + "\"");
            responseBuilder.header(RestApiConstants.HEADER_CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            return responseBuilder.build();
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the client certificate with alias " + alias + " for the tenant " + organization, e, log);
    }
    return null;
}
Also used : HttpResponse(org.apache.http.HttpResponse) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) Response(javax.ws.rs.core.Response) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) JSONObject(org.json.simple.JSONObject) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 5 with ClientCertificateDTO

use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method deleteAPIClientCertificateByAlias.

@Override
public Response deleteAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) {
    String organization = null;
    try {
        organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate if api exists
        validateAPIExistence(apiId);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        api.setOrganization(organization);
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(api.getStatus());
        ClientCertificateDTO clientCertificateDTO = CertificateRestApiUtils.preValidateClientCertificate(alias, api.getId(), organization);
        int responseCode = apiProvider.deleteClientCertificate(RestApiCommonUtil.getLoggedInUsername(), clientCertificateDTO.getApiIdentifier(), alias);
        if (responseCode == ResponseCode.SUCCESS.getResponseCode()) {
            // Handle api product case.
            if (API_PRODUCT_TYPE.equals(api.getType())) {
                APIIdentifier apiIdentifier = api.getId();
                APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(apiIdentifier.getProviderName(), apiIdentifier.getApiName(), apiIdentifier.getVersion());
                APIProduct apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
                apiProduct.setOrganization(organization);
                apiProvider.updateAPIProduct(apiProduct);
            } else {
                apiProvider.updateAPI(api);
            }
            if (log.isDebugEnabled()) {
                log.debug(String.format("The client certificate which belongs to tenant : %s represented by the " + "alias : %s is deleted successfully", organization, alias));
            }
            return Response.ok().entity("The certificate for alias '" + alias + "' deleted successfully.").build();
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Failed to delete the client certificate which belongs to tenant : %s " + "represented by the alias : %s.", organization, alias));
            }
            RestApiUtil.handleInternalServerError("Error while deleting the client certificate for alias '" + alias + "'.", log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while deleting the client certificate with alias " + alias + " for the tenant " + organization, e, log);
    } catch (FaultGatewaysException e) {
        RestApiUtil.handleInternalServerError("Error while publishing the certificate change to gateways for the alias " + alias, e, log);
    }
    return null;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 ArrayList (java.util.ArrayList)9 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)9 Gson (com.google.gson.Gson)7 HashMap (java.util.HashMap)7 API (org.wso2.carbon.apimgt.api.model.API)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)6 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)5 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)4