use of org.wso2.carbon.apimgt.api.model.Provider in project carbon-apimgt by wso2.
the class ApiDAOImpl method constructAPIFromResultSet.
private API constructAPIFromResultSet(Connection connection, PreparedStatement statement) throws SQLException, IOException, APIMgtDAOException {
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setTechnicalOwner(rs.getString("TECHNICAL_OWNER"));
businessInformation.setTechnicalOwnerEmail(rs.getString("TECHNICAL_EMAIL"));
businessInformation.setBusinessOwner(rs.getString("BUSINESS_OWNER"));
businessInformation.setBusinessOwnerEmail(rs.getString("BUSINESS_EMAIL"));
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(rs.getBoolean("CORS_ENABLED"));
String allowOrigins = rs.getString("CORS_ALLOW_ORIGINS");
corsConfiguration.setAllowOrigins(DAOUtil.commaSeperatedStringToList(allowOrigins));
corsConfiguration.setAllowCredentials(rs.getBoolean("CORS_ALLOW_CREDENTIALS"));
String allowHeaders = rs.getString("CORS_ALLOW_HEADERS");
corsConfiguration.setAllowHeaders(DAOUtil.commaSeperatedStringToList(allowHeaders));
String allowMethods = rs.getString("CORS_ALLOW_METHODS");
corsConfiguration.setAllowMethods(DAOUtil.commaSeperatedStringToList(allowMethods));
String apiPrimaryKey = rs.getString("UUID");
return new API.APIBuilder(rs.getString("PROVIDER"), rs.getString("NAME"), rs.getString("VERSION")).id(apiPrimaryKey).context(rs.getString("CONTEXT")).isDefaultVersion(rs.getBoolean("IS_DEFAULT_VERSION")).description(rs.getString("DESCRIPTION")).visibility(API.Visibility.valueOf(rs.getString("VISIBILITY"))).visibleRoles(getVisibleRoles(connection, apiPrimaryKey)).isResponseCachingEnabled(rs.getBoolean("IS_RESPONSE_CACHED")).cacheTimeout(rs.getInt("CACHE_TIMEOUT")).hasOwnGateway(rs.getBoolean("HAS_OWN_GATEWAY")).tags(getTags(connection, apiPrimaryKey)).labels(getLabelIdsForAPI(connection, apiPrimaryKey)).wsdlUri(ApiResourceDAO.getTextValueForCategory(connection, apiPrimaryKey, ResourceCategory.WSDL_TEXT)).transport(getTransports(connection, apiPrimaryKey)).endpoint(getEndPointsForApi(connection, apiPrimaryKey)).apiPermission(getPermissionsStringForApi(connection, apiPrimaryKey)).permissionMap(getPermissionMapForApi(connection, apiPrimaryKey)).businessInformation(businessInformation).lifecycleInstanceId(rs.getString("LIFECYCLE_INSTANCE_ID")).lifeCycleStatus(rs.getString("CURRENT_LC_STATUS")).corsConfiguration(corsConfiguration).createdBy(rs.getString("CREATED_BY")).updatedBy(rs.getString("UPDATED_BY")).createdTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime()).lastUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime()).uriTemplates(getUriTemplates(connection, apiPrimaryKey)).policies(getSubscripitonPolciesByAPIId(connection, apiPrimaryKey)).copiedFromApiId(rs.getString("COPIED_FROM_API")).workflowStatus(rs.getString("LC_WORKFLOW_STATUS")).securityScheme(rs.getInt("SECURITY_SCHEME")).apiPolicy(getApiPolicyByAPIId(connection, apiPrimaryKey)).threatProtectionPolicies(getThreatProtectionPolicies(connection, apiPrimaryKey)).build();
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.Provider in project carbon-apimgt by wso2.
the class ApiDAOImpl method getAPIsByStatus.
/**
* @see ApiDAO#getAPIsByStatus(Set, List, List)
*/
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<API> getAPIsByStatus(Set<String> roles, List<String> statuses, List<String> labels) throws APIMgtDAOException {
// check for null at the beginning before constructing the query to retrieve APIs from database
if (roles == null || statuses == null) {
String errorMessage = "Role list or API status list should not be null to retrieve APIs.";
log.error(errorMessage);
throw new APIMgtDAOException(errorMessage);
}
// the below query will be used to retrieve the union of,
// published/prototyped APIs (statuses) with public visibility and
// published/prototyped APIs with restricted visibility where APIs are restricted based on roles of the user
String labelQuery = null;
if (labels.isEmpty()) {
labelQuery = "SELECT LABEL_ID FROM AM_LABELS WHERE TYPE_NAME='STORE'";
} else {
labelQuery = "SELECT LABEL_ID FROM AM_LABELS WHERE NAME IN ( " + DAOUtil.getParameterString(labels.size()) + ") AND TYPE_NAME='STORE'";
}
final String query = "Select UUID, PROVIDER, NAME, CONTEXT, VERSION, DESCRIPTION, CURRENT_LC_STATUS, " + "LIFECYCLE_INSTANCE_ID, LC_WORKFLOW_STATUS, SECURITY_SCHEME FROM (" + API_SUMMARY_SELECT + " WHERE " + "VISIBILITY = '" + API.Visibility.PUBLIC + "' " + "AND " + "CURRENT_LC_STATUS IN (" + DAOUtil.getParameterString(statuses.size()) + ") AND " + "API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)" + "UNION " + API_SUMMARY_SELECT + " WHERE " + "VISIBILITY = '" + API.Visibility.RESTRICTED + "' " + "AND " + "UUID IN (SELECT API_ID FROM AM_API_VISIBLE_ROLES WHERE ROLE IN " + "(" + DAOUtil.getParameterString(roles.size()) + ")) " + " AND CURRENT_LC_STATUS IN (" + DAOUtil.getParameterString(statuses.size()) + ") AND " + " API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)) A" + " JOIN AM_API_LABEL_MAPPING LM ON A.UUID=LM.API_ID WHERE LM.LABEL_ID IN (" + labelQuery + ")";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
int i = 0;
// put desired API status into the query (to get APIs with public visibility)
for (String status : statuses) {
statement.setString(++i, status);
}
statement.setString(++i, ApiType.STANDARD.toString());
// put desired roles into the query
for (String role : roles) {
statement.setString(++i, role);
}
// put desired API status into the query (to get APIs with restricted visibility)
for (String status : statuses) {
statement.setString(++i, status);
}
statement.setString(++i, ApiType.STANDARD.toString());
// Set the label names in the query
for (String label : labels) {
statement.setString(++i, label);
}
return constructAPISummaryList(connection, statement);
} catch (SQLException e) {
String errorMessage = "Error while retrieving API list in store.";
throw new APIMgtDAOException(errorMessage, e);
}
}
use of org.wso2.carbon.apimgt.api.model.Provider in project carbon-apimgt by wso2.
the class ApiFileDAOImpl method updateAPI.
/**
* @see ApiDAO#updateAPI(String apiID, API substituteAPI)
*/
@Override
public void updateAPI(String apiID, API substituteAPI) throws APIMgtDAOException {
API oldAPI = getAPI(apiID);
if (oldAPI == null) {
String errorMsg = "Error while updating API. Unable to find API with Id: " + apiID;
log.error(errorMsg);
throw new APIMgtDAOException(errorMsg, ExceptionCodes.API_NOT_FOUND);
}
// set immutable properties from old API
API updatedAPI = new API.APIBuilder(substituteAPI).id(apiID).provider(oldAPI.getProvider()).name(oldAPI.getName()).version(oldAPI.getVersion()).context(oldAPI.getContext()).createdTime(oldAPI.getCreatedTime()).createdBy(oldAPI.getCreatedBy()).lifecycleInstanceId(oldAPI.getLifecycleInstanceId()).lifeCycleStatus(oldAPI.getLifeCycleStatus()).copiedFromApiId(oldAPI.getCopiedFromApiId()).build();
// Adding the API override the existing files.
addAPI(updatedAPI);
}
use of org.wso2.carbon.apimgt.api.model.Provider in project carbon-apimgt by wso2.
the class APIPublisherImpl method searchAPIs.
/**
* @param limit Limit
* @param offset Offset
* @param query Search query
* @return List of APIS.
* @throws APIManagementException If failed to formatApiSearch APIs.
*/
@Override
public List<API> searchAPIs(Integer limit, Integer offset, String query) throws APIManagementException {
List<API> apiResults;
String user = getUsername();
Set<String> roles = new HashSet<>();
try {
// TODO: Need to validate users roles against results returned
if (!"admin".equals(user)) {
// Whenever call identity provider should convert pseudo name to actual name
String userId = getIdentityProvider().getIdOfUser(user);
roles = new HashSet<>(getIdentityProvider().getRoleIdsOfUser(userId));
}
if (query != null && !query.isEmpty()) {
String[] attributes = query.split(ATTRIBUTE_DELIMITER);
Map<String, String> attributeMap = new HashMap<>();
boolean isFullTextSearch = false;
String searchAttribute, searchValue;
if (!query.contains(KEY_VALUE_DELIMITER)) {
isFullTextSearch = true;
} else {
log.debug("Search query: " + query);
for (String attribute : attributes) {
searchAttribute = attribute.split(KEY_VALUE_DELIMITER)[0];
searchValue = attribute.split(KEY_VALUE_DELIMITER)[1];
log.debug(searchAttribute + KEY_VALUE_DELIMITER + searchValue);
attributeMap.put(searchAttribute, searchValue);
}
}
if (isFullTextSearch) {
apiResults = getApiDAO().searchAPIs(roles, user, query, offset, limit);
} else {
log.debug("Attributes:", attributeMap.toString());
apiResults = getApiDAO().attributeSearchAPIs(roles, user, attributeMap, offset, limit);
}
} else {
apiResults = getApiDAO().getAPIs(roles, user);
}
return apiResults;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while Searching the API with query " + query;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (IdentityProviderException e) {
String errorMsg = "Error occurred while calling SCIM endpoint to retrieve user " + user + "'s information";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.api.model.Provider in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateCompositeApiFromSwaggerResource.
@Override
public CompositeAPI.Builder generateCompositeApiFromSwaggerResource(String provider, String apiDefinition) throws APIManagementException {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(apiDefinition);
if (swagger == null) {
throw new APIManagementException("Swagger could not be generated from provided API definition");
}
Info apiInfo = swagger.getInfo();
if (apiInfo == null) {
throw new APIManagementException("Provided Swagger definition doesn't contain API information");
} else {
String apiName = apiInfo.getTitle();
String apiVersion = apiInfo.getVersion();
String apiDescription = apiInfo.getDescription();
CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder().provider(provider).name(apiName).version(apiVersion).description(apiDescription).context(swagger.getBasePath());
List<APIResource> apiResourceList = parseSwaggerAPIResources(new StringBuilder(apiDefinition));
Map<String, UriTemplate> uriTemplateMap = new HashMap();
for (APIResource apiResource : apiResourceList) {
uriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
}
apiBuilder.uriTemplates(uriTemplateMap);
apiBuilder.id(UUID.randomUUID().toString());
return apiBuilder;
}
}
Aggregations