Search in sources :

Example 16 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class APIConsumerImpl method searchPaginatedAPIs.

/**
 * Pagination API search based on solr indexing
 *
 * @param registry
 * @param searchTerm
 * @param searchType
 * @return
 * @throws APIManagementException
 */
public Map<String, Object> searchPaginatedAPIs(Registry registry, String searchTerm, String searchType, int start, int end, boolean limitAttributes) throws APIManagementException {
    SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
    List<API> apiList = new ArrayList<API>();
    searchTerm = searchTerm.trim();
    Map<String, Object> result = new HashMap<String, Object>();
    int totalLength = 0;
    boolean isMore = false;
    String criteria = APIConstants.API_OVERVIEW_NAME;
    try {
        String paginationLimit = getAPIManagerConfiguration().getFirstProperty(APIConstants.API_STORE_APIS_PER_PAGE);
        // If the Config exists use it to set the pagination limit
        final int maxPaginationLimit;
        if (paginationLimit != null) {
            // The additional 1 added to the maxPaginationLimit is to help us determine if more
            // APIs may exist so that we know that we are unable to determine the actual total
            // API count. We will subtract this 1 later on so that it does not interfere with
            // the logic of the rest of the application
            int pagination = Integer.parseInt(paginationLimit);
            // leading to some of the APIs not being displayed
            if (pagination < 11) {
                pagination = 11;
                log.warn("Value of '" + APIConstants.API_STORE_APIS_PER_PAGE + "' is too low, defaulting to 11");
            }
            maxPaginationLimit = start + pagination + 1;
        } else // Else if the config is not specified we go with default functionality and load all
        {
            maxPaginationLimit = Integer.MAX_VALUE;
        }
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        if (artifactManager != null) {
            if (APIConstants.API_PROVIDER.equalsIgnoreCase(searchType)) {
                criteria = APIConstants.API_OVERVIEW_PROVIDER;
                searchTerm = searchTerm.replaceAll("@", "-AT-");
            } else if (APIConstants.API_VERSION_LABEL.equalsIgnoreCase(searchType)) {
                criteria = APIConstants.API_OVERVIEW_VERSION;
            } else if (APIConstants.API_CONTEXT.equalsIgnoreCase(searchType)) {
                criteria = APIConstants.API_OVERVIEW_CONTEXT;
            } else if (APIConstants.API_DESCRIPTION.equalsIgnoreCase(searchType)) {
                criteria = APIConstants.API_OVERVIEW_DESCRIPTION;
            } else if (APIConstants.API_TAG.equalsIgnoreCase(searchType)) {
                criteria = APIConstants.API_OVERVIEW_TAG;
            }
            // Create the search attribute map for PUBLISHED APIs
            final String searchValue = searchTerm;
            Map<String, List<String>> listMap = new HashMap<String, List<String>>();
            listMap.put(criteria, new ArrayList<String>() {

                {
                    add(searchValue);
                }
            });
            boolean displayAPIsWithMultipleStatus = APIUtil.isAllowDisplayAPIsWithMultipleStatus();
            // multiple status. This is because pagination is breaking when we do a another filtering with the API Status
            if (!displayAPIsWithMultipleStatus) {
                listMap.put(APIConstants.API_OVERVIEW_STATUS, new ArrayList<String>() {

                    {
                        add(APIConstants.PUBLISHED);
                    }
                });
            }
            GenericArtifact[] genericArtifacts = artifactManager.findGenericArtifacts(listMap);
            totalLength = PaginationContext.getInstance().getLength();
            boolean isFound = true;
            if (genericArtifacts == null || genericArtifacts.length == 0) {
                if (APIConstants.API_OVERVIEW_PROVIDER.equals(criteria)) {
                    genericArtifacts = searchAPIsByOwner(artifactManager, searchValue);
                    if (genericArtifacts == null || genericArtifacts.length == 0) {
                        isFound = false;
                    }
                } else {
                    isFound = false;
                }
            }
            if (!isFound) {
                result.put("apis", apiSet);
                result.put("length", 0);
                result.put("isMore", isMore);
                return result;
            }
            // Check to see if we can speculate that there are more APIs to be loaded
            if (maxPaginationLimit == totalLength) {
                // More APIs exist, cannot determine total API count without incurring perf hit
                isMore = true;
                // Remove the additional 1 added earlier when setting max pagination limit
                --totalLength;
            }
            int tempLength = 0;
            for (GenericArtifact artifact : genericArtifacts) {
                String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
                if (APIUtil.isAllowDisplayAPIsWithMultipleStatus()) {
                    if (APIConstants.PROTOTYPED.equals(status) || APIConstants.PUBLISHED.equals(status) || APIConstants.DEPRECATED.equals(status)) {
                        API resultAPI;
                        if (limitAttributes) {
                            resultAPI = APIUtil.getAPI(artifact);
                        } else {
                            resultAPI = APIUtil.getAPI(artifact, registry);
                        }
                        if (resultAPI != null) {
                            apiList.add(resultAPI);
                        }
                    }
                } else {
                    if (APIConstants.PROTOTYPED.equals(status) || APIConstants.PUBLISHED.equals(status)) {
                        API resultAPI;
                        if (limitAttributes) {
                            resultAPI = APIUtil.getAPI(artifact);
                        } else {
                            resultAPI = APIUtil.getAPI(artifact, registry);
                        }
                        if (resultAPI != null) {
                            apiList.add(resultAPI);
                        }
                    }
                }
                // Ensure the APIs returned matches the length, there could be an additional API
                // returned due incrementing the pagination limit when getting from registry
                tempLength++;
                if (tempLength >= totalLength) {
                    break;
                }
            }
            apiSet.addAll(apiList);
        }
    } catch (RegistryException e) {
        handleException("Failed to search APIs with type", e);
    }
    result.put("apis", apiSet);
    result.put("length", totalLength);
    result.put("isMore", isMore);
    return result;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) TreeSet(java.util.TreeSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) JSONObject(org.json.simple.JSONObject) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 17 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class APIConsumerImpl method getPaginatedAPIsWithTag.

/**
 * Returns the set of APIs with the given tag from the taggedAPIs Map.
 *
 * @param tag   The name of the tag
 * @param start The starting index of the return result set
 * @param end   The end index of the return result set
 * @return A {@link Map} of APIs(between the given indexes) and the total number indicating all the available
 * APIs count
 * @throws APIManagementException
 */
@Override
public Map<String, Object> getPaginatedAPIsWithTag(String tag, int start, int end, String tenantDomain) throws APIManagementException {
    List<API> apiList = new ArrayList<API>();
    Set<API> resultSet = new TreeSet<API>(new APIVersionComparator());
    Map<String, Object> results = new HashMap<String, Object>();
    Set<API> taggedAPISet = this.getAPIsWithTag(tag, tenantDomain);
    if (taggedAPISet != null) {
        if (taggedAPISet.size() < end) {
            end = taggedAPISet.size();
        }
        int totalLength;
        apiList.addAll(taggedAPISet);
        totalLength = apiList.size();
        if (totalLength <= ((start + end) - 1)) {
            end = totalLength;
        } else {
            end = start + end;
        }
        for (int i = start; i < end; i++) {
            resultSet.add(apiList.get(i));
        }
        results.put("apis", resultSet);
        results.put("length", taggedAPISet.size());
    } else {
        results.put("apis", null);
        results.put("length", 0);
    }
    return results;
}
Also used : APIVersionComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionComparator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) JSONObject(org.json.simple.JSONObject)

Example 18 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class APIProviderImpl method searchAPIs.

/**
 * Search APIs based on given search term
 *
 * @param searchTerm
 * @param searchType
 * @param providerId
 * @throws APIManagementException
 */
@Deprecated
public List<API> searchAPIs(String searchTerm, String searchType, String providerId) throws APIManagementException {
    List<API> foundApiList = new ArrayList<API>();
    String regex = "(?i)[\\w.|-]*" + searchTerm.trim() + "[\\w.|-]*";
    Pattern pattern;
    Matcher matcher;
    String apiConstant = null;
    try {
        if (providerId != null) {
            List<API> apiList = getAPIsByProvider(providerId);
            if (apiList == null || apiList.isEmpty()) {
                return apiList;
            }
            pattern = Pattern.compile(regex);
            for (API api : apiList) {
                if ("Name".equalsIgnoreCase(searchType)) {
                    apiConstant = api.getId().getApiName();
                } else if ("Provider".equalsIgnoreCase(searchType)) {
                    apiConstant = api.getId().getProviderName();
                } else if ("Version".equalsIgnoreCase(searchType)) {
                    apiConstant = api.getId().getVersion();
                } else if ("Context".equalsIgnoreCase(searchType)) {
                    apiConstant = api.getContext();
                } else if ("Status".equalsIgnoreCase(searchType)) {
                    apiConstant = api.getStatus();
                } else if (APIConstants.THROTTLE_TIER_DESCRIPTION_ATTRIBUTE.equalsIgnoreCase(searchType)) {
                    apiConstant = api.getDescription();
                }
                if (apiConstant != null) {
                    matcher = pattern.matcher(apiConstant);
                    if (matcher.find()) {
                        foundApiList.add(api);
                    }
                }
            }
        } else {
            foundApiList = searchAPIs(searchTerm, searchType);
        }
    } catch (APIManagementException e) {
        handleException("Failed to search APIs with type", e);
    }
    Collections.sort(foundApiList, new APINameComparator());
    return foundApiList;
}
Also used : Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator)

Example 19 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getApi.

@Override
public API getApi(String context, String version) throws DataLoadingException {
    Set<String> gatewayLabels = gatewayArtifactSynchronizerProperties.getGatewayLabels();
    if (gatewayLabels != null && gatewayLabels.size() > 0) {
        for (String gatewayLabel : gatewayLabels) {
            String apisEP = APIConstants.SubscriptionValidationResources.APIS + "?context=" + context + "&version=" + version + "&gatewayLabel=" + getEncodedLabel(gatewayLabel);
            API api = null;
            String responseString;
            try {
                responseString = invokeService(apisEP, null);
            } catch (IOException e) {
                String msg = "Error while executing the http client " + apisEP;
                log.error(msg, e);
                throw new DataLoadingException(msg, e);
            }
            if (responseString != null && !responseString.isEmpty()) {
                APIList list = new Gson().fromJson(responseString, APIList.class);
                if (list.getList() != null && !list.getList().isEmpty()) {
                    api = list.getList().get(0);
                }
            }
            return api;
        }
    }
    return null;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) APIList(org.wso2.carbon.apimgt.keymgt.model.entity.APIList) Gson(com.google.gson.Gson) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) IOException(java.io.IOException)

Example 20 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method loadAllApis.

@Override
public List<API> loadAllApis(String tenantDomain) throws DataLoadingException {
    Set<String> gatewayLabels = gatewayArtifactSynchronizerProperties.getGatewayLabels();
    List<API> apis = new ArrayList<>();
    if (gatewayLabels != null && gatewayLabels.size() > 0) {
        for (String gatewayLabel : gatewayLabels) {
            String apisEP = APIConstants.SubscriptionValidationResources.APIS + "?gatewayLabel=" + getEncodedLabel(gatewayLabel);
            String responseString = null;
            try {
                responseString = invokeService(apisEP, tenantDomain);
            } catch (IOException e) {
                String msg = "Error while executing the http client " + apisEP;
                log.error(msg, e);
                throw new DataLoadingException(msg, e);
            }
            if (responseString != null && !responseString.isEmpty()) {
                APIList apiList = new Gson().fromJson(responseString, APIList.class);
                apis.addAll(apiList.getList());
            }
            if (log.isDebugEnabled()) {
                log.debug("apis :" + apis.get(0).toString());
            }
        }
    }
    return apis;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) ArrayList(java.util.ArrayList) APIList(org.wso2.carbon.apimgt.keymgt.model.entity.APIList) Gson(com.google.gson.Gson) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) IOException(java.io.IOException)

Aggregations

ArrayList (java.util.ArrayList)33 API (org.wso2.carbon.apimgt.core.models.API)21 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)15 API (org.wso2.carbon.apimgt.api.model.API)14 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)12 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 JSONObject (org.json.simple.JSONObject)9 APIComparator (org.wso2.carbon.apimgt.core.util.APIComparator)8 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)7 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)6 TreeSet (java.util.TreeSet)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)5 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4 List (java.util.List)4