use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetSubscriptionPolicy.
@Test(description = "Test getting Subscription policy")
public void testGetSubscriptionPolicy() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
Mockito.when(policyDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName())).thenReturn(subscriptionPolicy);
adminService.getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
Mockito.verify(policyDAO, Mockito.times(1)).getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
// Error path
Mockito.when(policyDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
try {
adminService.getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't retrieve Subscription policy with name: " + subscriptionPolicy.getPolicyName());
}
}
use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdDelete.
/**
* Delete a subscription
*
* @param subscriptionId Id of the subscription
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK response if the deletion was successful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsSubscriptionIdDelete(String subscriptionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = subscriptionsSubscriptionIdGetFingerprint(subscriptionId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
apiStore.deleteAPISubscription(subscriptionId);
} catch (GatewayException e) {
String errorMessage = "Failed to remove subscription :" + subscriptionId + " from gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while deleting subscription";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().build();
}
use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Get all subscriptions.
* {@code <p/>}
* If apiId is specified this will return the subscribed applications of that api
* If application id is specified this will return the api subscriptions of that application
*
* @param apiId ID of the API
* @param applicationId ID of the Application
* @param offset offset value
* @param limit limit value
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return Subscription List
* @throws NotFoundException If failed to get the subscription
*/
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
List<Subscription> subscribedApiList = null;
SubscriptionListDTO subscriptionListDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!StringUtils.isEmpty(apiId)) {
subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else if (!StringUtils.isEmpty(applicationId)) {
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null) {
if (!StringUtils.isEmpty(apiType)) {
ApiType apiTypeEnum = ApiType.fromString(apiType);
if (apiTypeEnum == null) {
throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
}
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
} else {
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
}
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else {
String errorMessage = "Application not found: " + applicationId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} else {
// mandatory parameters not provided
String errorMessage = "Either applicationId or apiId should be provided";
ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(subscriptionListDTO).build();
}
use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method updateAPISubscriptionStatus.
@Override
public void updateAPISubscriptionStatus(List<SubscriptionValidationData> subscriptionValidationDataList) throws GatewayException {
SubscriptionEvent subscriptionBlockEvent = new SubscriptionEvent(APIMgtConstants.GatewayEventTypes.SUBSCRIPTION_STATUS_CHANGE);
subscriptionBlockEvent.setSubscriptionsList(subscriptionValidationDataList);
publishToStoreTopic(subscriptionBlockEvent);
if (log.isDebugEnabled()) {
log.debug("Subscription updated event has been successfully published to broker");
}
}
use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method deleteAPISubscription.
/**
* {@inheritDoc}
*/
@Override
public void deleteAPISubscription(List<SubscriptionValidationData> subscriptionValidationDataList) throws GatewayException {
SubscriptionEvent subscriptionDeleteEvent = new SubscriptionEvent(APIMgtConstants.GatewayEventTypes.SUBSCRIPTION_DELETE);
subscriptionDeleteEvent.setSubscriptionsList(subscriptionValidationDataList);
publishToStoreTopic(subscriptionDeleteEvent);
if (log.isDebugEnabled()) {
log.debug("Subscription deleted event has been successfully published to broker");
}
}
Aggregations