use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method validateSubscription.
/**
* Validates a subscription
*
* @param apiContext Context of the API
* @param apiVersion Version of the API
* @param clientId Client id of the application
* @return Subscription Validation Information
* @throws APIMgtDAOException If failed to get subscription validation results.
*/
public SubscriptionValidationResult validateSubscription(String apiContext, String apiVersion, String clientId) throws APIMgtDAOException {
final String validateSubscriptionSql = "SELECT SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, " + "SUBS.SUB_STATUS AS SUB_STATUS, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "APP.NAME AS APP_NAME, APP.CREATED_BY AS APP_OWNER, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_APPLICATION APP, AM_SUBSCRIPTION_POLICY POLICY, " + "AM_APP_KEY_MAPPING KEYS " + "WHERE API.CONTEXT = ? AND API.VERSION = ? AND KEYS.CLIENT_ID= ? " + "AND APP.AND SUBS.API_ID = API.UUID AND SUBS.APPLICATION_ID = APP.UUID " + "AND SUBS.TIER_ID = POLICY.UUID AND KEYS.APPLICATION_ID = APP.UUID";
SubscriptionValidationResult validationInfo = new SubscriptionValidationResult(false);
try (Connection conn = DAOUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(validateSubscriptionSql)) {
ps.setString(1, apiContext);
ps.setString(2, apiVersion);
ps.setString(3, clientId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
validationInfo.setValid(true);
validationInfo.setApiId(rs.getString("API_ID"));
validationInfo.setApplicationId(rs.getString("APP_ID"));
validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
validationInfo.setApiProvider(rs.getString("API_PROVIDER"));
validationInfo.setApiName(rs.getString("API_NAME"));
validationInfo.setApplicationName(rs.getString("APP_NAME"));
validationInfo.setApplicationOwner(rs.getString("APP_OWNER"));
validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUBS_POLICY")));
}
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "validating subscription(apiContext: " + apiContext + ", apiVersion: " + apiVersion + ", clientId: " + clientId + ")", e);
}
return validationInfo;
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class ApiDAOImpl method getSubscripitonPolciesByAPIId.
private Set<Policy> getSubscripitonPolciesByAPIId(Connection connection, String apiId) throws SQLException {
final String query = "SELECT amPolcySub.NAME FROM AM_API_SUBS_POLICY_MAPPING apimsubmapping," + "AM_SUBSCRIPTION_POLICY amPolcySub where apimsubmapping.SUBSCRIPTION_POLICY_ID=amPolcySub.UUID " + "AND apimsubmapping.API_ID = ?";
Set<Policy> policies = new HashSet<>();
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, apiId);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
while (rs.next()) {
policies.add(new SubscriptionPolicy(rs.getString("NAME")));
}
}
}
return policies;
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class ApiDAOImpl method getResourcesOfApi.
/**
* @see org.wso2.carbon.apimgt.core.dao.ApiDAO#getResourcesOfApi(String, String)
*/
@Override
public List<UriTemplate> getResourcesOfApi(String apiContext, String apiVersion) throws APIMgtDAOException {
final String query = "SELECT operationMapping.OPERATION_ID AS OPERATION_ID,operationMapping.HTTP_METHOD AS " + "HTTP_METHOD,operationMapping.URL_PATTERN AS URL_PATTERN,operationMapping.AUTH_SCHEME AS AUTH_SCHEME," + "operationMapping.API_POLICY_ID AS API_POLICY_ID FROM AM_API_OPERATION_MAPPING operationMapping," + "AM_API api WHERE operationMapping.API_ID = api.UUID AND api.CONTEXT = ? AND api.VERSION = ?";
List<UriTemplate> uriTemplates = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, apiContext);
preparedStatement.setString(2, apiVersion);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
UriTemplate uriTemplate = new UriTemplate.UriTemplateBuilder().uriTemplate(resultSet.getString("URL_PATTERN")).authType(resultSet.getString("AUTH_SCHEME")).httpVerb(resultSet.getString("HTTP_METHOD")).policy(new APIPolicy(resultSet.getString("API_POLICY_ID"), "")).templateId(resultSet.getString("OPERATION_ID")).build();
uriTemplates.add(uriTemplate);
}
}
} catch (SQLException e) {
String msg = "getting API resources for Context: " + apiContext + ", Version: " + apiVersion;
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
}
return uriTemplates;
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class ApiDAOImpl method getApiPolicyByAPIId.
private Policy getApiPolicyByAPIId(Connection connection, String apiId) throws SQLException {
final String query = "SELECT amapipolicy.NAME AS POLICY_NAME FROM AM_API_POLICY_MAPPING apimpolicymapping," + "AM_API_POLICY amapipolicy WHERE apimpolicymapping.API_POLICY_ID=amapipolicy.UUID " + "AND apimpolicymapping.API_ID = ?";
Policy apiPolicy = null;
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, apiId);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
if (rs.next()) {
apiPolicy = new APIPolicy(rs.getString("POLICY_NAME"));
}
}
}
return apiPolicy;
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addAPIPipeline.
/**
* Adding pipelines of API policy to database
*
* @param connection connection to db
* @param uuid policy id/ uuid of the policy
* @throws SQLException if error occurred while inserting pipeline to db
*/
private static void addAPIPipeline(Connection connection, List<Pipeline> pipelines, String uuid) throws SQLException, APIMgtDAOException {
final String query = "INSERT INTO AM_CONDITION_GROUP (UUID, QUOTA_TYPE, UNIT_TIME, TIME_UNIT, DESCRIPTION, QUOTA, " + "QUOTA_UNIT) VALUES (?,?,?,?,?,?,?)";
String dbProductName = connection.getMetaData().getDatabaseProductName();
try (PreparedStatement statement = connection.prepareStatement(query, new String[] { DAOUtil.getConvertedAutoGeneratedColumnName(dbProductName, APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_GROUP_ID) })) {
for (Pipeline pipeline : pipelines) {
statement.setString(1, uuid);
statement.setString(2, pipeline.getQuotaPolicy().getType());
statement.setLong(3, pipeline.getQuotaPolicy().getLimit().getUnitTime());
statement.setString(4, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
statement.setString(5, pipeline.getDescription());
Limit limit = pipeline.getQuotaPolicy().getLimit();
setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
statement.executeUpdate();
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
// get the auto increment id
int conditionId = rs.getInt(1);
List<Condition> conditionList = pipeline.getConditions();
for (Condition condition : conditionList) {
if (PolicyConstants.IP_CONDITION_TYPE.equals(condition.getType()) || PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType()) || PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
addIPCondition(connection, condition, conditionId);
} else if (PolicyConstants.HEADER_CONDITION_TYPE.equals(condition.getType())) {
addHeaderCondition(connection, condition, conditionId);
} else if (PolicyConstants.JWT_CLAIMS_CONDITION_TYPE.equals(condition.getType())) {
addJWTClaimCondition(connection, condition, conditionId);
} else if (PolicyConstants.QUERY_PARAMS_CONDITION_TYPE.equals(condition.getType())) {
addParamCondition(connection, condition, conditionId);
} else {
// unsupported Condition
throw new IllegalArgumentException("Unsupported Condition type: " + condition.getType());
}
}
} else {
String errorMsg = "Unable to retrieve auto incremented id, hence unable to add Pipeline Condition";
throw new IllegalStateException(errorMsg);
}
}
}
}
Aggregations