Search in sources :

Example 61 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

/**
 * Retrieves APIs qualifying under given search condition
 *
 * @param limit       maximum number of APIs returns
 * @param offset      starting index
 * @param labels      Labels of the store for which the apis need to be retrieved
 * @param query       search condition
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return matched APIs for the given search condition
 */
@Override
public Response apisGet(Integer limit, Integer offset, String labels, String query, String ifNoneMatch, Request request) throws NotFoundException {
    List<API> apisResult = null;
    APIListDTO apiListDTO = null;
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<String> labelList = new ArrayList<>();
        if (labels != null) {
            labelList = Arrays.asList(labels.split(","));
        }
        apisResult = apiStore.searchAPIsByStoreLabels(query, offset, limit, labelList);
        // convert API
        apiListDTO = APIMappingUtil.toAPIListDTO(apisResult);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving APIs ";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, query);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(apiListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 62 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ApiImportExportManager method getAPIDetails.

/**
 * Retrieves all API details of the APIs for the given search query. API details consist of:
 *      1. API {@link org.wso2.carbon.apimgt.core.models.API}
 *      2. Document Info {@link org.wso2.carbon.apimgt.core.models.DocumentInfo}
 *      3. Document Content {@link org.wso2.carbon.apimgt.core.models.DocumentContent}
 *      4. Swagger Definition
 *      5. Gateway Definition
 *      6. Thumbnail content
 *
 * @param limit number of max results
 * @param offset starting location when returning a limited set of results
 * @param query searchQuery
 * @return {@link APIDetails} instance
 * @throws APIManagementException if an error occurs while retrieving API details
 */
public Set<APIDetails> getAPIDetails(Integer limit, Integer offset, String query) throws APIManagementException {
    Set<APIDetails> apiDetailSet = new HashSet<>();
    // search for APIs
    List<API> apis = apiPublisher.searchAPIs(limit, offset, query);
    if (apis == null || apis.isEmpty()) {
        // no APIs found, return
        return apiDetailSet;
    }
    // iterate and collect all information
    for (API api : apis) {
        api = apiPublisher.getAPIbyUUID(api.getId());
        // get endpoints at API Level
        Map<String, Endpoint> endpoints = api.getEndpoint();
        if (endpoints.isEmpty()) {
            log.error("No Endpoints found for api: " + api.getName() + ", version: " + api.getVersion());
        // skip this API
        // continue;
        }
        Set<Endpoint> endpointSet = new HashSet<>();
        for (Map.Entry<String, Endpoint> endpointEntry : endpoints.entrySet()) {
            if (APIMgtConstants.GLOBAL_ENDPOINT.equals(endpointEntry.getValue().getApplicableLevel())) {
                Endpoint endpoint = new Endpoint.Builder(apiPublisher.getEndpoint(endpointEntry.getValue().getId())).id("").build();
                endpoints.replace(endpointEntry.getKey(), endpoint);
                endpointSet.add(endpoint);
            }
        }
        // get Endpoints at Resource Level
        Map<String, UriTemplate> uriTemplateMap = api.getUriTemplates();
        uriTemplateMap.forEach((k, v) -> {
            UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v);
            Map<String, Endpoint> resourceEndpoints = uriTemplateBuilder.getEndpoint();
            resourceEndpoints.forEach((type, value) -> {
                Endpoint endpoint = null;
                if (APIMgtConstants.GLOBAL_ENDPOINT.equals(value.getApplicableLevel())) {
                    try {
                        endpoint = new Endpoint.Builder(apiPublisher.getEndpoint(value.getId())).id("").build();
                        endpointSet.add(endpoint);
                    } catch (APIManagementException e) {
                        log.error("Error in getting endpoints for Resource: " + v.getTemplateId(), e);
                    }
                } else {
                    endpoint = new Endpoint.Builder(value).id("").build();
                }
                resourceEndpoints.replace(type, endpoint);
            });
            uriTemplateMap.replace(k, uriTemplateBuilder.endpoint(resourceEndpoints).build());
        });
        api = new API.APIBuilder(api).endpoint(endpoints).uriTemplates(uriTemplateMap).build();
        // get swagger definition
        String swaggerDefinition;
        try {
            swaggerDefinition = apiPublisher.getApiSwaggerDefinition(api.getId());
        } catch (APIManagementException e) {
            log.error("Error in getting Swagger configuration for api: " + api.getName() + ", version: " + api.getVersion(), e);
            // skip this API
            continue;
        }
        // get gateway configuration
        String gatewayConfig;
        try {
            gatewayConfig = apiPublisher.getApiGatewayConfig(api.getId());
        } catch (APIManagementException e) {
            log.error("Error in getting gateway configuration for api: " + api.getName() + ", version: " + api.getVersion(), e);
            // skip this API
            continue;
        }
        // get doc information
        List<DocumentInfo> documentInfo = null;
        try {
            documentInfo = apiPublisher.getAllDocumentation(api.getId(), 0, Integer.MAX_VALUE);
        } catch (APIManagementException e) {
            log.error("Error in getting documentation content for api: " + api.getName() + ", version: " + api.getVersion(), e);
        // no need to skip the API as docs don't affect API functionality
        }
        Set<DocumentContent> documentContents = new HashSet<>();
        if (documentInfo != null && !documentInfo.isEmpty()) {
            // iterate and collect document content
            for (DocumentInfo aDocumentInfo : documentInfo) {
                try {
                    documentContents.add(apiPublisher.getDocumentationContent(aDocumentInfo.getId()));
                } catch (APIManagementException e) {
                    log.error("Error in getting documentation content for api: " + api.getName() + ", version: " + api.getVersion() + ", doc id: " + aDocumentInfo.getId(), e);
                // no need to skip the API as docs don't affect API functionality
                }
            }
        }
        // get thumbnail
        InputStream thumbnailStream = null;
        try {
            thumbnailStream = apiPublisher.getThumbnailImage(api.getId());
        } catch (APIManagementException e) {
            log.error("Error in getting thumbnail for api: " + api.getName() + ", version: " + api.getVersion(), e);
        // no need to skip the API as thumbnail don't affect API functionality
        }
        // search operation returns a summary of APIs, need to get all details of APIs
        APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
        apiDetails.setGatewayConfiguration(gatewayConfig);
        apiDetails.setEndpoints(endpointSet);
        if (documentInfo != null && !documentInfo.isEmpty()) {
            apiDetails.addDocumentInformation(documentInfo);
        }
        if (!documentContents.isEmpty()) {
            apiDetails.addDocumentContents(documentContents);
        }
        if (thumbnailStream != null) {
            apiDetails.setThumbnailStream(thumbnailStream);
        }
        apiDetailSet.add(apiDetails);
    }
    return apiDetailSet;
}
Also used : InputStream(java.io.InputStream) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 63 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class MappingUtil method fromSubscriptionListToDTO.

/**
 * Converts Subscription model into SubscriptionListDTO object
 *
 * @param subscriptionList list of subscriptions
 * @param limit            no of items to return
 * @param offset value to offset
 * @return SubscriptionListDTO containing subscriptions
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptionList, Integer limit, Integer offset) {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    for (Subscription subscription : subscriptionList) {
        subscriptionListDTO.addListItem(fromSubscription(subscription));
    }
    // TODO need to change when pagination implementation goes on
    subscriptionListDTO.count(subscriptionList.size());
    return subscriptionListDTO;
}
Also used : Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)

Aggregations

HashMap (java.util.HashMap)24 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 PreparedStatement (java.sql.PreparedStatement)14 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)11 Map (java.util.Map)10 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 SQLException (java.sql.SQLException)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)8 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)8 ArrayList (java.util.ArrayList)7 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)7 Test (org.testng.annotations.Test)6 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)4 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)4 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)4 Connection (java.sql.Connection)3 Response (javax.ws.rs.core.Response)3