Search in sources :

Example 31 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class EndpointCertificatesApiServiceImpl method addEndpointCertificate.

public Response addEndpointCertificate(InputStream certificateInputStream, Attachment certificateDetail, String alias, String endpoint, MessageContext messageContext) {
    try {
        if (StringUtils.isEmpty(alias) || StringUtils.isEmpty(endpoint)) {
            RestApiUtil.handleBadRequest("The alias and/ or endpoint should not be empty", log);
        }
        ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
        String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
        if (StringUtils.isBlank(fileName)) {
            RestApiUtil.handleBadRequest("Certificate update failed. Proper Certificate file should be provided", log);
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
        int responseCode = apiProvider.addCertificate(userName, base64EncodedCert, alias, endpoint);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Add certificate operation response code : %d", responseCode));
        }
        if (ResponseCode.SUCCESS.getResponseCode() == responseCode) {
            CertMetadataDTO certificateDTO = new CertMetadataDTO();
            certificateDTO.setEndpoint(endpoint);
            certificateDTO.setAlias(alias);
            URI createdCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
            return Response.created(createdCertUri).entity(certificateDTO).build();
        } else if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
            RestApiUtil.handleInternalServerError("Error while adding the certificate due to an" + " internal server error", log);
        } else if (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() == responseCode) {
            RestApiUtil.handleResourceAlreadyExistsError("The alias '" + alias + "' already exists in the trust store.", log);
        } else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
            RestApiUtil.handleBadRequest("Error while adding the certificate. Certificate Expired.", log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
    } catch (IOException e) {
        RestApiUtil.handleInternalServerError("Error while generating the encoded certificate", log);
    } catch (URISyntaxException e) {
        RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
    }
    return null;
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO) URI(java.net.URI)

Example 32 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class EndpointCertificatesApiServiceImpl method updateEndpointCertificateByAlias.

public Response updateEndpointCertificateByAlias(String alias, InputStream certificateInputStream, Attachment certificateDetail, MessageContext messageContext) {
    try {
        if (StringUtils.isEmpty(alias)) {
            RestApiUtil.handleBadRequest("The alias should not be empty", log);
        }
        ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
        String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
        if (StringUtils.isBlank(fileName)) {
            RestApiUtil.handleBadRequest("Certificate update failed. The Certificate should not be empty", log);
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        int tenantId = APIUtil.getTenantId(userName);
        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));
            }
            RestApiUtil.handleResourceNotFoundError("Could not update the certificate. " + "The alias '" + alias + "' not found.", log);
        }
        String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
        int responseCode = apiProvider.updateCertificate(base64EncodedCert, alias);
        List<CertificateMetadataDTO> updatedCertificate = apiProvider.searchCertificates(tenantId, alias, null);
        if (ResponseCode.SUCCESS.getResponseCode() == responseCode && updatedCertificate.size() > 0) {
            CertificateMetadataDTO certificateMetadata = updatedCertificate.get(0);
            CertMetadataDTO certificateDTO = new CertMetadataDTO();
            certificateDTO.setAlias(certificateMetadata.getAlias());
            certificateDTO.setEndpoint(certificateMetadata.getEndpoint());
            URI updatedCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
            return Response.ok(updatedCertUri).entity(certificateDTO).build();
        }
        if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
            RestApiUtil.handleInternalServerError("Error while updating the certificate due to an internal " + "server error", log);
        } else if (ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode() == responseCode) {
            RestApiUtil.handleResourceNotFoundError("", log);
        } else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
            RestApiUtil.handleBadRequest("Error while updating the certificate. Certificate Expired.", log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
    } catch (IOException e) {
        RestApiUtil.handleInternalServerError("Error while encoding certificate", log);
    } catch (URISyntaxException e) {
        RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
    }
    return null;
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO) URI(java.net.URI)

Example 33 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteCertificate.

@Override
public int deleteCertificate(String userName, String alias, String endpoint) throws APIManagementException {
    ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    String tenantDomain = MultitenantUtils.getTenantDomain(userName);
    try {
        int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        responseCode = certificateManager.deleteCertificateFromParentNode(alias, endpoint, tenantId);
        CertificateEvent certificateEvent = new CertificateEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.ENDPOINT_CERTIFICATE_REMOVE.toString(), tenantDomain, alias, endpoint);
        APIUtil.sendNotification(certificateEvent, APIConstants.NotifierType.CERTIFICATE.name());
    } catch (UserStoreException e) {
        handleException("Error while reading tenant information", e);
    }
    return responseCode.getResponseCode();
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) CertificateEvent(org.wso2.carbon.apimgt.impl.notifier.events.CertificateEvent) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 34 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class APIProviderImpl method addClientCertificate.

@Override
public int addClientCertificate(String userName, APIIdentifier apiIdentifier, String certificate, String alias, String tierName, String organization) throws APIManagementException {
    ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    String tenantDomain = MultitenantUtils.getTenantDomain(userName);
    try {
        int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        responseCode = certificateManager.addClientCertificate(apiIdentifier, certificate, alias, tierName, tenantId, organization);
    } catch (UserStoreException e) {
        handleException("Error while reading tenant information, client certificate addition failed for the API " + apiIdentifier.toString(), e);
    }
    return responseCode.getResponseCode();
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 35 with ResponseCode

use of org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteClientCertificate.

@Override
public int deleteClientCertificate(String userName, APIIdentifier apiIdentifier, String alias) throws APIManagementException {
    ResponseCode responseCode = ResponseCode.INTERNAL_SERVER_ERROR;
    String tenantDomain = MultitenantUtils.getTenantDomain(userName);
    try {
        int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        responseCode = certificateManager.deleteClientCertificateFromParentNode(apiIdentifier, alias, tenantId);
    } catch (UserStoreException e) {
        handleException("Error while reading tenant information while trying to delete client certificate with alias " + alias + " for the API " + apiIdentifier.toString(), e);
    }
    return responseCode.getResponseCode();
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Aggregations

ResponseCode (org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode)18 Test (org.junit.Test)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 IOException (java.io.IOException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 ArrayList (java.util.ArrayList)5 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 UserStoreException (org.wso2.carbon.user.api.UserStoreException)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)3 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)3 API (org.wso2.carbon.apimgt.api.model.API)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)3 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)3