use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class APIProviderImplTest method getLCBean.
private LifecycleBean getLCBean() {
LifecycleBean bean = new LifecycleBean();
List<Property> lifecycleProps = new ArrayList<Property>();
Property property1 = new Property();
property1.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.1.item");
String[] values1 = { "status:Created", "name:Requires re-subscription when publishing the API", "value:false", "order:1" };
property1.setValues(values1);
Property property2 = new Property();
property2.setKey("registry.lifecycle.APILifeCycle.state");
String[] values2 = { "Created" };
property2.setValues(values2);
Property property3 = new Property();
property3.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.0.item.permission");
String[] values3 = { "registry.custom_lifecycle.checklist.option.APILifeCycle.0.item.permission" };
property3.setValues(values3);
Property property4 = new Property();
property4.setKey("registry.lifecycle.APILifeCycle.lastStateUpdatedTime");
String[] values4 = { "2017-08-31 13:36:54.501" };
property4.setValues(values4);
Property property5 = new Property();
property5.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.1.item.permission");
String[] values5 = { "registry.custom_lifecycle.checklist.option.APILifeCycle.1.item.permission" };
property5.setValues(values5);
Property property6 = new Property();
property6.setKey("registry.custom_lifecycle.checklist.option.APILifeCycle.0.item");
String[] values6 = { "status:Created", "name:Deprecate old versions after publishing the API", "value:false", "order:0" };
property6.setValues(values6);
Property property7 = new Property();
property7.setKey("registry.LC.name");
String[] values7 = { "APILifeCycle" };
property7.setValues(values7);
lifecycleProps.add(property1);
lifecycleProps.add(property2);
lifecycleProps.add(property3);
lifecycleProps.add(property4);
lifecycleProps.add(property5);
lifecycleProps.add(property6);
Property[] propertyArr = new Property[lifecycleProps.size()];
bean.setLifecycleProperties(lifecycleProps.toArray(propertyArr));
String[] userRoles = { "publisher" };
bean.setRolesOfUser(userRoles);
return bean;
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class APILoggerManager method invokeService.
private String invokeService(String path, String tenantDomain) throws IOException, APIManagementException {
String serviceURLStr = eventHubConfigurationDto.getServiceUrl().concat(APIConstants.INTERNAL_WEB_APP_EP);
HttpGet method = new HttpGet(serviceURLStr + path);
URL serviceURL = new URL(serviceURLStr + path);
byte[] credentials = getServiceCredentials(eventHubConfigurationDto);
int servicePort = serviceURL.getPort();
String serviceProtocol = serviceURL.getProtocol();
method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, StandardCharsets.UTF_8));
if (tenantDomain != null) {
method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
}
HttpClient httpClient = APIUtil.getHttpClient(servicePort, serviceProtocol);
HttpResponse httpResponse = null;
int retryCount = 0;
boolean retry;
do {
try {
httpResponse = httpClient.execute(method);
retry = false;
} catch (IOException ex) {
retryCount++;
if (retryCount < RETRIEVAL_RETRIES) {
retry = true;
log.warn("Failed retrieving " + path + " from remote endpoint: " + ex.getMessage() + ". Retrying after " + RETRIEVAL_TIMEOUT_IN_SECONDS + " seconds.");
try {
Thread.sleep(RETRIEVAL_TIMEOUT_IN_SECONDS * 1000L);
} catch (InterruptedException e) {
// Ignore
}
} else {
throw new APIManagementException("Error while calling internal service", ex);
}
}
} while (retry);
if (HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()) {
log.error("Could not retrieve subscriptions for tenantDomain : " + tenantDomain);
throw new APIManagementException("Error while retrieving subscription from " + path);
}
return EntityUtils.toString(httpResponse.getEntity(), UTF8);
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class ApiMgtDAO method getSubscriptionBlockCondition.
/**
* Get details of the subscription block condition by condition value and tenant domain
*
* @param conditionValue condition value of the block condition
* @param tenantDomain tenant domain of the block condition
* @return Block condition
* @throws APIManagementException
*/
public BlockConditionsDTO getSubscriptionBlockCondition(String conditionValue, String tenantDomain) throws APIManagementException {
Connection connection = null;
PreparedStatement selectPreparedStatement = null;
ResultSet resultSet = null;
BlockConditionsDTO blockCondition = null;
try {
String query = SQLConstants.ThrottleSQLConstants.GET_SUBSCRIPTION_BLOCK_CONDITION_BY_VALUE_AND_DOMAIN_SQL;
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(true);
selectPreparedStatement = connection.prepareStatement(query);
selectPreparedStatement.setString(1, conditionValue);
selectPreparedStatement.setString(2, tenantDomain);
resultSet = selectPreparedStatement.executeQuery();
if (resultSet.next()) {
blockCondition = new BlockConditionsDTO();
blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
blockCondition.setConditionType(resultSet.getString("TYPE"));
blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
blockCondition.setUUID(resultSet.getString("UUID"));
}
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
handleException("Failed to rollback getting Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, ex);
}
}
handleException("Failed to get Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, e);
} finally {
APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
}
return blockCondition;
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class ApiMgtDAO method addSubscription.
private int addSubscription(Connection connection, ApiTypeWrapper apiTypeWrapper, Application application, String subscriptionStatus, String subscriber) throws APIManagementException, SQLException {
final boolean isProduct = apiTypeWrapper.isAPIProduct();
int subscriptionId = -1;
int id = -1;
String apiUUID;
Identifier identifier;
String tier;
// Query to check if this subscription already exists
String checkDuplicateQuery = SQLConstants.CHECK_EXISTING_SUBSCRIPTION_API_SQL;
if (!isProduct) {
identifier = apiTypeWrapper.getApi().getId();
apiUUID = apiTypeWrapper.getApi().getUuid();
if (apiUUID != null) {
id = getAPIID(apiUUID);
}
if (id == -1) {
id = identifier.getId();
}
} else {
identifier = apiTypeWrapper.getApiProduct().getId();
id = apiTypeWrapper.getApiProduct().getProductId();
apiUUID = apiTypeWrapper.getApiProduct().getUuid();
}
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
try (PreparedStatement ps = connection.prepareStatement(checkDuplicateQuery)) {
ps.setInt(1, id);
ps.setInt(2, application.getId());
try (ResultSet resultSet = ps.executeQuery()) {
// If the subscription already exists
if (resultSet.next()) {
String subStatus = resultSet.getString("SUB_STATUS");
String subCreationStatus = resultSet.getString("SUBS_CREATE_STATE");
if ((APIConstants.SubscriptionStatus.UNBLOCKED.equals(subStatus) || APIConstants.SubscriptionStatus.ON_HOLD.equals(subStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subStatus)) && APIConstants.SubscriptionCreatedStatus.SUBSCRIBE.equals(subCreationStatus)) {
// Throw error saying subscription already exists.
log.error(String.format("Subscription already exists for API/API Prouct %s in Application %s", apiTypeWrapper.getName(), application.getName()));
throw new SubscriptionAlreadyExistingException(String.format("Subscription already exists for" + " API/API Prouct %s in Application %s", apiTypeWrapper.getName(), application.getName()));
} else if (APIConstants.SubscriptionStatus.UNBLOCKED.equals(subStatus) && APIConstants.SubscriptionCreatedStatus.UN_SUBSCRIBE.equals(subCreationStatus)) {
deleteSubscriptionByApiIDAndAppID(id, application.getId(), connection);
} else if (APIConstants.SubscriptionStatus.BLOCKED.equals(subStatus) || APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subStatus)) {
log.error(String.format(String.format("Subscription to API/API Prouct %%s through application" + " %%s was blocked"), apiTypeWrapper.getName(), application.getName()));
throw new SubscriptionBlockedException(String.format("Subscription to API/API Product %s " + "through application %s was blocked", apiTypeWrapper.getName(), application.getName()));
} else if (APIConstants.SubscriptionStatus.REJECTED.equals(subStatus)) {
throw new SubscriptionBlockedException("Subscription to API " + apiTypeWrapper.getName() + " through application " + application.getName() + " was rejected");
}
}
}
}
// This query to update the AM_SUBSCRIPTION table
String sqlQuery = SQLConstants.ADD_SUBSCRIPTION_SQL;
// Adding data to the AM_SUBSCRIPTION table
// ps = conn.prepareStatement(sqlQuery, Statement.RETURN_GENERATED_KEYS);
String subscriptionIDColumn = "SUBSCRIPTION_ID";
String subscriptionUUID = UUID.randomUUID().toString();
if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
subscriptionIDColumn = "subscription_id";
}
try (PreparedStatement preparedStForInsert = connection.prepareStatement(sqlQuery, new String[] { subscriptionIDColumn })) {
if (!isProduct) {
tier = apiTypeWrapper.getApi().getId().getTier();
preparedStForInsert.setString(1, tier);
preparedStForInsert.setString(10, tier);
} else {
tier = apiTypeWrapper.getApiProduct().getId().getTier();
preparedStForInsert.setString(1, tier);
preparedStForInsert.setString(10, tier);
}
preparedStForInsert.setInt(2, id);
preparedStForInsert.setInt(3, application.getId());
preparedStForInsert.setString(4, subscriptionStatus != null ? subscriptionStatus : APIConstants.SubscriptionStatus.UNBLOCKED);
preparedStForInsert.setString(5, APIConstants.SubscriptionCreatedStatus.SUBSCRIBE);
preparedStForInsert.setString(6, subscriber);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
preparedStForInsert.setTimestamp(7, timestamp);
preparedStForInsert.setTimestamp(8, timestamp);
preparedStForInsert.setString(9, subscriptionUUID);
preparedStForInsert.executeUpdate();
try (ResultSet rs = preparedStForInsert.getGeneratedKeys()) {
while (rs.next()) {
// subscriptionId = rs.getInt(1);
subscriptionId = Integer.parseInt(rs.getString(1));
}
}
}
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), tenantId, tenantDomain, subscriptionId, subscriptionUUID, id, apiUUID, application.getId(), application.getUUID(), tier, (subscriptionStatus != null ? subscriptionStatus : APIConstants.SubscriptionStatus.UNBLOCKED));
return subscriptionId;
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class ApiMgtDAO method getTopicSubscriptions.
/**
* Retrieves set of web hook topic subscriptions for a application.
*
* @param applicationId application UUID
* @return set of web hook subscriptions.
* @throws APIManagementException if failed to retrieve web hook topc subscriptions
*/
public Set<Subscription> getTopicSubscriptions(String applicationId) throws APIManagementException {
Connection conn = null;
ResultSet resultSet = null;
PreparedStatement ps = null;
String getTopicSubscriptionsQuery = SQLConstants.GET_WH_TOPIC_SUBSCRIPTIONS;
Set<Subscription> subscriptionSet = new HashSet();
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(getTopicSubscriptionsQuery);
ps.setString(1, applicationId);
resultSet = ps.executeQuery();
while (resultSet.next()) {
Subscription subscription = new Subscription();
subscription.setApiUuid(resultSet.getString("API_UUID"));
subscription.setCallback(resultSet.getString("HUB_CALLBACK_URL"));
Timestamp deliveryTime = resultSet.getTimestamp("DELIVERED_AT");
if (deliveryTime != null) {
subscription.setLastDelivery(new Date(deliveryTime.getTime()));
}
subscription.setLastDeliveryState(resultSet.getInt("DELIVERY_STATE"));
subscription.setTopic(resultSet.getString("HUB_TOPIC"));
subscription.setAppID(resultSet.getString("APPLICATION_ID"));
subscriptionSet.add(subscription);
}
} catch (SQLException e) {
handleException("Failed to retrieve topic subscriptions for application " + applicationId, e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
}
return null;
}
Aggregations