use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.
the class ServiceEntryMappingUtil method setPaginationParams.
public static void setPaginationParams(ServiceListDTO serviceListDTO, int offset, int limit, int size, ServiceFilterParams filterParams) {
Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
String paginatedPrevious = "";
String paginatedNext = "";
if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
paginatedPrevious = getServicesPaginatedUrl(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), filterParams);
}
if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
paginatedNext = getServicesPaginatedUrl(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), filterParams);
}
PaginationDTO paginationDTO = getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
serviceListDTO.setPagination(paginationDTO);
}
use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.
the class ServiceCatalogDAO method getServices.
/**
* Get services
* @param filterParams Service Filter parameters
* @param tenantId Tenant ID of the logged in user
* @param shrink Whether to shrink the response or not
* @return List of Services
* @throws APIManagementException
*/
public List<ServiceEntry> getServices(ServiceFilterParams filterParams, int tenantId, boolean shrink) throws APIManagementException {
List<ServiceEntry> serviceEntryList = new ArrayList<>();
String query;
boolean searchByKey = false;
boolean searchByDefinitionType = false;
boolean exactNameSearch = false;
boolean exactVersionSearch = false;
StringBuilder querySb = new StringBuilder();
querySb.append("SELECT UUID, SERVICE_KEY, MD5, SERVICE_NAME, SERVICE_VERSION," + " SERVICE_URL, DEFINITION_TYPE, DEFINITION_URL, DESCRIPTION, SECURITY_TYPE, MUTUAL_SSL_ENABLED," + " CREATED_TIME, LAST_UPDATED_TIME, CREATED_BY, UPDATED_BY, SERVICE_DEFINITION FROM " + " AM_SERVICE_CATALOG WHERE TENANT_ID = ? ");
String whereClauseForExactNameSearch = "AND SERVICE_NAME = ? ";
String whereClauseForNameSearch = "AND SERVICE_NAME LIKE ? ";
String whereClauseForExactVersionSearch = "AND SERVICE_VERSION = ? ";
String whereClauseForVersionSearch = " AND SERVICE_VERSION LIKE ? ";
String whereClauseWithDefinitionType = " AND DEFINITION_TYPE = ? ";
String whereClauseWithServiceKey = " AND SERVICE_KEY = ? ";
if (filterParams.getName().startsWith("\"") && filterParams.getName().endsWith("\"")) {
exactNameSearch = true;
filterParams.setName(filterParams.getName().replace("\"", "").trim());
querySb.append(whereClauseForExactNameSearch);
} else {
querySb.append(whereClauseForNameSearch);
}
if (filterParams.getVersion().startsWith("\"") && filterParams.getVersion().endsWith("\"")) {
exactVersionSearch = true;
filterParams.setVersion(filterParams.getVersion().replace("\"", "").trim());
querySb.append(whereClauseForExactVersionSearch);
} else {
querySb.append(whereClauseForVersionSearch);
}
if (StringUtils.isNotEmpty(filterParams.getDefinitionType()) && StringUtils.isEmpty(filterParams.getKey())) {
searchByDefinitionType = true;
querySb.append(whereClauseWithDefinitionType);
} else if (StringUtils.isNotEmpty(filterParams.getKey()) && StringUtils.isEmpty(filterParams.getDefinitionType())) {
searchByKey = true;
querySb.append(whereClauseWithServiceKey);
} else if (StringUtils.isNotEmpty(filterParams.getDefinitionType()) && StringUtils.isNotEmpty(filterParams.getKey())) {
searchByKey = true;
searchByDefinitionType = true;
querySb.append(whereClauseWithDefinitionType).append(whereClauseWithServiceKey);
}
querySb.append("ORDER BY ").append(filterParams.getSortBy()).append(" " + filterParams.getSortOrder());
String[] keyArray = null;
try (Connection connection = APIMgtDBUtil.getConnection()) {
String driverName = connection.getMetaData().getDriverName();
if (driverName.contains("Oracle") || driverName.contains("MS SQL") || driverName.contains("Microsoft")) {
querySb.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
} else if (driverName.contains("PostgreSQL")) {
querySb.append(" OFFSET ? LIMIT ? ");
} else {
querySb.append(" LIMIT ?, ?");
}
query = querySb.toString();
try (PreparedStatement ps = connection.prepareStatement(query)) {
keyArray = filterParams.getKey().split(",");
for (String key : keyArray) {
ps.setInt(1, tenantId);
if (exactNameSearch) {
ps.setString(2, filterParams.getName());
} else {
ps.setString(2, "%" + filterParams.getName() + "%");
}
if (exactVersionSearch) {
ps.setString(3, filterParams.getVersion());
} else {
ps.setString(3, "%" + filterParams.getVersion() + "%");
}
if (searchByKey && searchByDefinitionType) {
ps.setString(4, filterParams.getDefinitionType());
ps.setString(5, key);
ps.setInt(6, filterParams.getOffset());
ps.setInt(7, filterParams.getLimit());
} else if (searchByKey) {
ps.setString(4, key);
ps.setInt(5, filterParams.getOffset());
ps.setInt(6, filterParams.getLimit());
} else if (searchByDefinitionType) {
ps.setString(4, filterParams.getDefinitionType());
ps.setInt(5, filterParams.getOffset());
ps.setInt(6, filterParams.getLimit());
} else {
ps.setInt(4, filterParams.getOffset());
ps.setInt(5, filterParams.getLimit());
}
try (ResultSet resultSet = ps.executeQuery()) {
while (resultSet.next()) {
ServiceEntry service = getServiceParams(resultSet, shrink);
List<API> usedAPIs = getServiceUsage(service.getUuid(), tenantId, connection);
int usage = usedAPIs != null ? usedAPIs.size() : 0;
service.setUsage(usage);
serviceEntryList.add(service);
}
}
}
} catch (SQLException e) {
handleException("Error while retrieving the Services", e);
}
} catch (SQLException e) {
handleException("Error while retrieving the Services", e);
}
return serviceEntryList;
}
use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.
the class ServicesApiServiceImpl method searchServices.
@Override
public Response searchServices(String name, String version, String definitionType, String key, Boolean shrink, String sortBy, String sortOrder, Integer limit, Integer offset, MessageContext messageContext) throws APIManagementException {
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
try {
List<ServiceDTO> serviceDTOList = new ArrayList<>();
ServiceFilterParams filterParams = ServiceEntryMappingUtil.getServiceFilterParams(name, version, definitionType, key, sortBy, sortOrder, limit, offset);
List<ServiceEntry> services = serviceCatalog.getServices(filterParams, tenantId, shrink);
int totalServices = serviceCatalog.getServicesCount(tenantId, filterParams);
for (ServiceEntry service : services) {
serviceDTOList.add(ServiceEntryMappingUtil.fromServiceToDTO(service, shrink));
}
ServiceListDTO serviceListDTO = new ServiceListDTO();
serviceListDTO.setList(serviceDTOList);
ServiceEntryMappingUtil.setPaginationParams(serviceListDTO, filterParams.getOffset(), filterParams.getLimit(), totalServices, filterParams);
return Response.ok().entity(serviceListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving Services";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.
the class ServiceEntryMappingUtil method getServiceFilterParams.
/**
* Create Service Filter Params object based on the parameters
* @param name Service name
* @param version Service version
* @param definitionType Service Definition Type
* @param key Service key
* @param sortBy Sort By
* @param sortOrder Sort Order
* @param limit
* @param offset
* @return
*/
public static ServiceFilterParams getServiceFilterParams(String name, String version, String definitionType, String key, String sortBy, String sortOrder, Integer limit, Integer offset) {
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DEFAULT_SORT_ORDER;
sortBy = getServiceSortByField(sortBy);
name = name != null ? name : StringUtils.EMPTY;
version = version != null ? version : StringUtils.EMPTY;
definitionType = definitionType != null ? definitionType : StringUtils.EMPTY;
key = key != null ? key : StringUtils.EMPTY;
ServiceFilterParams filterParams = new ServiceFilterParams();
filterParams.setName(name);
filterParams.setVersion(version);
filterParams.setDefinitionType(definitionType);
filterParams.setKey(key);
filterParams.setSortBy(sortBy);
filterParams.setSortOrder(sortOrder);
filterParams.setLimit(limit);
filterParams.setOffset(offset);
return filterParams;
}
Aggregations