Search in sources :

Example 1 with CertificatesDTO

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

the class ApisApiServiceImpl method getAPIClientCertificates.

@Override
public Response getAPIClientCertificates(String apiId, Integer limit, Integer offset, String alias, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    List<ClientCertificateDTO> certificates = new ArrayList<>();
    String query = CertificateRestApiUtils.buildQueryString("alias", alias, "apiId", apiId);
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        int tenantId = APIUtil.getInternalOrganizationId(organization);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        int totalCount = apiProvider.getClientCertificateCount(tenantId);
        if (totalCount > 0) {
            APIIdentifier apiIdentifier = null;
            if (StringUtils.isNotEmpty(apiId)) {
                API api = apiProvider.getAPIbyUUID(apiId, organization);
                apiIdentifier = api.getId();
            }
            certificates = apiProvider.searchClientCertificates(tenantId, alias, apiIdentifier, organization);
        }
        ClientCertificatesDTO certificatesDTO = CertificateRestApiUtils.getPaginatedClientCertificates(certificates, limit, offset, query);
        PaginationDTO paginationDTO = new PaginationDTO();
        paginationDTO.setLimit(limit);
        paginationDTO.setOffset(offset);
        paginationDTO.setTotal(totalCount);
        certificatesDTO.setPagination(paginationDTO);
        return Response.status(Response.Status.OK).entity(certificatesDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the client certificates.", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ClientCertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 2 with CertificatesDTO

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

the class EndpointCertificatesApiServiceImpl method getEndpointCertificates.

public Response getEndpointCertificates(Integer limit, Integer offset, String alias, String endpoint, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    List<CertificateMetadataDTO> certificates;
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    String query = CertificateRestApiUtils.buildQueryString("alias", alias, "endpoint", endpoint);
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Call the search certificate api to get the filtered certificates for " + "tenant id : %d, alias : %s, and endpoint : %s", tenantId, alias, endpoint));
            }
            certificates = apiProvider.searchCertificates(tenantId, alias, endpoint);
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("There is no query parameters provided. So, retrieve all the certificates" + " belongs to the tenantId : %d", tenantId));
            }
            certificates = apiProvider.getCertificates(userName);
        }
        CertificatesDTO certificatesDTO = CertificateRestApiUtils.getPaginatedCertificates(certificates, limit, offset, query);
        return Response.status(Response.Status.OK).entity(certificatesDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the certificates.", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificatesDTO)

Example 3 with CertificatesDTO

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

the class CertificateRestApiUtils method getPaginatedClientCertificates.

/**
 * To get the paginated list of client certificates.
 *
 * @param clientCertificateDTOList Client certificate list.
 * @param limit                    Limit
 * @param offset                   Offset
 * @param query                    query
 * @return paginated list of client certificates.
 */
public static ClientCertificatesDTO getPaginatedClientCertificates(List<ClientCertificateDTO> clientCertificateDTOList, int limit, int offset, String query) {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Filter the client certificates based on the pagination parameters, limit = %d and" + "offset = %d", limit, offset));
    }
    int certCount = clientCertificateDTOList.size();
    List<ClientCertMetadataDTO> clientCertificateList = new ArrayList<>();
    ClientCertificatesDTO certificatesDTO = new ClientCertificatesDTO();
    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++) {
        ClientCertMetadataDTO clientCertMetadataDTO = new ClientCertMetadataDTO();
        ClientCertificateDTO clientCertificateDTO = clientCertificateDTOList.get(i);
        clientCertMetadataDTO.setAlias(clientCertificateDTO.getAlias());
        clientCertMetadataDTO.setApiId(clientCertificateDTO.getApiIdentifier().toString());
        clientCertMetadataDTO.setTier(clientCertificateDTO.getTierName());
        clientCertificateList.add(clientCertMetadataDTO);
    }
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, certCount);
    String paginatedPrevious = "";
    String paginatedNext = "";
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = getCertificatesPaginatedURL(RestApiConstants.CLIENT_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.CLIENT_CERTS_GET_PAGINATED_URL, paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), query);
    }
    certificatesDTO.setCount(clientCertificateList.size());
    certificatesDTO.setCertificates(clientCertificateList);
    return certificatesDTO;
}
Also used : ClientCertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertMetadataDTO) ArrayList(java.util.ArrayList) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) ClientCertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO)

Example 4 with CertificatesDTO

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

ArrayList (java.util.ArrayList)3 ClientCertificatesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO)3 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 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)2 CertificatesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificatesDTO)2 ClientCertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertMetadataDTO)2 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)2 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1 CertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO)1