Search in sources :

Example 1 with CertMetadataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO 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 2 with CertMetadataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO 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 3 with CertMetadataDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO in project carbon-apimgt by wso2.

the class CertificateRestApiUtils method getPaginatedCertificates.

/**
 * Get the paginated list of certificates based on the limit and offset values provided.
 *
 * @param certificateMetadataList : The list of certificate metadata.
 * @param limit                   : The number of items per page.
 * @param offset                  : Page number
 * @param query                   : The query parameters.
 * @return : CertificatesDTO Object with the parameters set.
 */
public static CertificatesDTO getPaginatedCertificates(List<CertificateMetadataDTO> certificateMetadataList, int limit, int offset, String query) {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Filter the certificates based on the pagination parameters, limit = %d and " + "offset = %d", limit, offset));
    }
    int certCount = certificateMetadataList.size();
    List<CertMetadataDTO> certificateList = new ArrayList<>();
    CertificatesDTO certificatesDTO = new CertificatesDTO();
    certificatesDTO.setCount(certCount > limit ? limit : certCount);
    // If the provided offset value exceeds the offset, reset the offset to default.
    if (offset > certCount) {
        offset = RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    }
    // Select only the set of Certificates which matches the given limit and offset values.
    int start = offset;
    int end = certCount > start + limit ? start + limit : certCount;
    for (int i = start; i < end; i++) {
        CertMetadataDTO certMetadataDTO = new CertMetadataDTO();
        CertificateMetadataDTO certificateMetadataDTO = certificateMetadataList.get(i);
        certMetadataDTO.setAlias(certificateMetadataDTO.getAlias());
        certMetadataDTO.setEndpoint(certificateMetadataDTO.getEndpoint());
        certificateList.add(certMetadataDTO);
    }
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, certCount);
    String paginatedPrevious = "";
    String paginatedNext = "";
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = getCertificatesPaginatedURL(RestApiConstants.CERTS_GET_PAGINATED_URL, paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), query);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = getCertificatesPaginatedURL(RestApiConstants.CERTS_GET_PAGINATED_URL, paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), query);
    }
    PaginationDTO paginationDTO = new PaginationDTO();
    paginationDTO.setNext(paginatedNext);
    paginationDTO.setPrevious(paginatedPrevious);
    paginationDTO.setLimit(limit);
    paginationDTO.setOffset(offset);
    paginationDTO.setTotal(certCount);
    certificatesDTO.setCount(certificateList.size());
    certificatesDTO.setCertificates(certificateList);
    certificatesDTO.setPagination(paginationDTO);
    return certificatesDTO;
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) ArrayList(java.util.ArrayList) PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO) ClientCertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertMetadataDTO) CertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO) ClientCertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO) CertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificatesDTO)

Aggregations

CertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)2 ArrayList (java.util.ArrayList)1 CertificatesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificatesDTO)1 ClientCertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertMetadataDTO)1 ClientCertificatesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO)1 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)1