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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations