Search in sources :

Example 1 with ClientCertificatesDTO

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

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

Aggregations

ArrayList (java.util.ArrayList)2 ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)2 ClientCertificatesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertificatesDTO)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 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 ClientCertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ClientCertMetadataDTO)1 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)1