Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#isCertificatePresent()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#isCertificatePresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIImportUtil.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Update API with the certificate.
 * If certificate alias already exists for tenant in database, certificate content will be
 * updated in trust store. If cert alias does not exits in database for that tenant, add the certificate to
 * publisher and gateway nodes. In such case if alias already exits in the trust store, update the certificate
 * content for that alias.
 *
 * @param certificate Certificate JSON element
 * @param apiProvider API Provider
 * @param importedApi API to import
 * @param tenantId    Tenant Id
 */
private static void updateAPIWithCertificate(JsonElement certificate, APIProvider apiProvider, API importedApi,
                                             int tenantId) {

    String certificateContent = certificate.getAsJsonObject()
            .get(APIImportExportConstants.CERTIFICATE_CONTENT_JSON_KEY).getAsString();
    String alias = certificate.getAsJsonObject().get(APIImportExportConstants.ALIAS_JSON_KEY).getAsString();
    String endpoint = certificate.getAsJsonObject().get(APIImportExportConstants.HOSTNAME_JSON_KEY)
            .getAsString();
    try {
        if (apiProvider.isCertificatePresent(tenantId, alias)
                || (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() ==
                (apiProvider.addCertificate(APIUtil.replaceEmailDomainBack(importedApi.getId().getProviderName()),
                        certificateContent, alias, endpoint)))) {
            apiProvider.updateCertificate(certificateContent, alias);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while importing certificate endpoint [" + endpoint + " ]" + "alias ["
                + alias + " ] tenant user ["
                + APIUtil.replaceEmailDomainBack(importedApi.getId().getProviderName()) + "]";
        log.error(errorMessage, e);
    }
}
 
Example 2
Source File: EndpointCertificatesApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public Response endpointCertificatesAliasContentGet(String alias, MessageContext messageContext) {
    String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    String certFileName = alias + ".crt";

    if (!StringUtils.isNotEmpty(alias)) {
        RestApiUtil.handleBadRequest("The alias cannot be empty", log);
    }

    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        if (!apiProvider.isCertificatePresent(tenantId, alias)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Could not find a certificate in truststore which belongs to tenant : %d " +
                        "and with alias : %s. Hence the operation is terminated.", tenantId, alias));
            }
            String message = "Certificate for Alias '" + alias + "' is not found.";
            RestApiUtil.handleResourceNotFoundError(message, log);
        }

        Object certificate = apiProvider.getCertificateContent(alias);
        if (certificate != null) {
            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 certificate status.", e, log);
    }
    return null;
}
 
Example 3
Source File: EndpointCertificatesApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public Response endpointCertificatesAliasDelete(String alias, MessageContext messageContext) {
    String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    String userName = RestApiUtil.getLoggedInUsername();

    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        if (!apiProvider.isCertificatePresent(tenantId, alias)) {
            String message = "Certificate for alias '" + alias + "' is not found.";
            RestApiUtil.handleResourceNotFoundError(message, log);
        }

        int responseCode = apiProvider.deleteCertificate(userName, alias, null);

        if (responseCode == ResponseCode.SUCCESS.getResponseCode()) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("The certificate which belongs to tenant : %d represented by the alias : " +
                        "%s is deleted successfully", tenantId, alias));
            }
            return Response.ok().build();
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Failed to delete the certificate which belongs to tenant : %d " +
                        "represented by the alias : %s.", tenantId, alias));
            }
            RestApiUtil.handleInternalServerError("Error while deleting the certificate for alias '" +
                    alias + "'.", log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while deleting the certificate for alias '" +
                alias + "'.", e, log);
    }
    return null;
}
 
Example 4
Source File: EndpointCertificatesApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public Response endpointCertificatesAliasGet(String alias, MessageContext messageContext) {
    String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);

    if (!StringUtils.isNotEmpty(alias)) {
        RestApiUtil.handleBadRequest("The alias cannot be empty", log);
    }

    if (log.isDebugEnabled()) {
        log.debug(String.format("Retrieving the common information of the certificate which is represented by the" +
                " alias : %s", alias));
    }

    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        if (!apiProvider.isCertificatePresent(tenantId, alias)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Could not find a certificate in truststore which belongs to tenant %d " +
                        "and with alias %s. Hence the operation is terminated.", tenantId, alias));
            }
            String message = "Certificate for Alias '" + alias + "' is not found.";
            RestApiUtil.handleResourceNotFoundError(message, log);
        }

        CertificateInformationDTO certificateInformationDTO = apiProvider.getCertificateStatus(alias);

        CertificateValidityDTO certificateValidityDTO = new CertificateValidityDTO();
        certificateValidityDTO.setFrom(certificateInformationDTO.getFrom());
        certificateValidityDTO.setTo(certificateInformationDTO.getTo());

        CertificateInfoDTO certificateInfoDTO = new CertificateInfoDTO();
        certificateInfoDTO.setValidity(certificateValidityDTO);
        certificateInfoDTO.setStatus(certificateInformationDTO.getStatus());
        certificateInfoDTO.setSubject(certificateInformationDTO.getSubject());
        certificateInfoDTO.setVersion(certificateInformationDTO.getVersion());

        return Response.ok().entity(certificateInfoDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the certificate status.", e, log);
    }
    return null;
}