use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importSOAPAPI.
/**
* Import an API from WSDL as a SOAP API
*
* @param fileInputStream file data as input stream
* @param fileDetail file details
* @param url URL of the WSDL
* @param apiToAdd API object to be added to the system (which is not added yet)
* @param organization Organization
* @param service service
* @return API added api
*/
private API importSOAPAPI(InputStream fileInputStream, Attachment fileDetail, String url, API apiToAdd, String organization, ServiceEntry service) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// adding the api
apiProvider.addAPI(apiToAdd);
if (StringUtils.isNotBlank(url)) {
apiToAdd.setWsdlUrl(url);
apiProvider.addWSDLResource(apiToAdd.getUuid(), null, url, organization);
} else if (fileDetail != null && fileInputStream != null) {
PublisherCommonUtils.addWsdl(fileDetail.getContentType().toString(), fileInputStream, apiToAdd, apiProvider, organization);
} else if (service != null && fileInputStream == null) {
RestApiUtil.handleBadRequest("Error while importing WSDL to create a SOAP API", log);
} else if (service != null) {
PublisherCommonUtils.addWsdl(RestApiConstants.APPLICATION_OCTET_STREAM, fileInputStream, apiToAdd, apiProvider, organization);
}
// add the generated swagger definition to SOAP
APIDefinition oasParser = new OAS2Parser();
SwaggerData swaggerData = new SwaggerData(apiToAdd);
String apiDefinition = generateSOAPAPIDefinition(oasParser.generateAPIDefinition(swaggerData));
apiProvider.saveSwaggerDefinition(apiToAdd, apiDefinition, organization);
APIIdentifier createdApiId = apiToAdd.getId();
// Retrieve the newly added API to send in the response payload
API createdApi = apiProvider.getAPIbyUUID(apiToAdd.getUuid(), organization);
return createdApi;
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while importing WSDL to create a SOAP API", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ServiceEntry 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.ServiceEntry in project carbon-apimgt by wso2.
the class ServiceCatalogDAO method importServices.
public List<ServiceEntry> importServices(List<ServiceEntry> services, int tenantId, String username, boolean overwrite) throws APIManagementException {
List<ServiceEntry> serviceListToAdd = new ArrayList<>();
List<ServiceEntry> serviceListToUpdate = new ArrayList<>();
boolean isValid = true;
for (int i = 0; i < services.size(); i++) {
ServiceEntry service = services.get(i);
ServiceEntry existingService = getServiceByKey(service.getKey(), tenantId);
if (existingService != null && StringUtils.isNotEmpty(existingService.getMd5())) {
if (!existingService.getVersion().equals(service.getVersion())) {
isValid = false;
break;
}
if (!existingService.getDefinitionType().equals(service.getDefinitionType())) {
isValid = false;
break;
}
if (!existingService.getKey().equals(service.getKey())) {
isValid = false;
break;
}
if (!existingService.getName().equals(service.getName())) {
isValid = false;
break;
}
if (!existingService.getMd5().equals(service.getMd5())) {
serviceListToUpdate.add(service);
}
} else {
serviceListToAdd.add(service);
}
}
if (isValid && !overwrite && serviceListToUpdate.size() > 0) {
throw new APIManagementException("Cannot update the existing services", ExceptionCodes.from(ExceptionCodes.SERVICE_IMPORT_FAILED_WITHOUT_OVERWRITE));
}
if (isValid) {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
addServices(serviceListToAdd, tenantId, username, connection);
updateServices(serviceListToUpdate, tenantId, username, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to import services to service catalog of tenant " + tenantId, e);
}
} catch (SQLException e) {
handleException("Failed to import services to service catalog of tenant " + APIUtil.getTenantDomainFromTenantId(tenantId), e);
}
List<ServiceEntry> importedServiceList = new ArrayList<>();
importedServiceList.addAll(serviceListToAdd);
importedServiceList.addAll(serviceListToUpdate);
return importedServiceList;
} else {
return null;
}
}
use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.
the class ServiceCatalogDAO method getServiceByUUID.
public ServiceEntry getServiceByUUID(String serviceId, int tenantId) throws APIManagementException {
String query = SQLConstants.ServiceCatalogConstants.GET_SERVICE_BY_SERVICE_ID;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement ps = connection.prepareStatement(query)) {
ps.setString(1, serviceId);
ps.setInt(2, tenantId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
ServiceEntry service = getServiceParams(rs, false);
int usage = getServiceUsage(serviceId, tenantId, connection) != null ? getServiceUsage(serviceId, tenantId, connection).size() : 0;
service.setUsage(usage);
return service;
}
}
} catch (SQLException e) {
handleException("Error while retrieving details of Service with Id: " + serviceId, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.
the class ServiceCatalogDAO method updateServices.
/**
* Update list of services available in Service Catalog
* @param services List of Services that needs to be updated
* @param tenantId Tenant ID of the logged-in user
* @param username Logged-in username
* @param connection DB Connection
*/
private void updateServices(List<ServiceEntry> services, int tenantId, String username, Connection connection) throws SQLException {
try (PreparedStatement ps = connection.prepareStatement(SQLConstants.ServiceCatalogConstants.UPDATE_SERVICE_BY_KEY)) {
for (ServiceEntry service : services) {
setUpdateServiceParams(ps, service, tenantId, username);
ps.addBatch();
}
ps.executeBatch();
}
}
Aggregations