use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method addThreatProtectionPolicy.
/**
* {@inheritDoc}
*/
@Override
public void addThreatProtectionPolicy(ThreatProtectionPolicy policy) throws GatewayException {
ThreatProtectionEvent event = new ThreatProtectionEvent(APIMgtConstants.GatewayEventTypes.THREAT_PROTECTION_POLICY_ADD);
event.setPolicy(policy);
publishToThreatProtectionTopic(event);
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIStateChangeWorkflow method updateAPIStatusForWorkflowComplete.
private void updateAPIStatusForWorkflowComplete(String apiId, String status, String updatedBy, LocalDateTime time) throws APIManagementException {
boolean requireReSubscriptions = false;
boolean deprecateOlderVersion = false;
try {
API api = apiDAO.getAPI(apiId);
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lastUpdatedTime(time);
apiBuilder.updatedBy(updatedBy);
LifecycleState currentState = apiLifecycleManager.getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
apiBuilder.lifecycleState(currentState);
if (APIMgtConstants.APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
apiBuilder.workflowStatus(APIMgtConstants.APILCWorkflowStatus.APPROVED.toString());
apiDAO.updateAPIWorkflowStatus(apiId, APIMgtConstants.APILCWorkflowStatus.APPROVED);
}
List<CheckItemBean> list = currentState.getCheckItemBeanList();
for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
CheckItemBean checkItemBean = (CheckItemBean) iterator.next();
if (APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS.equals(checkItemBean.getName())) {
deprecateOlderVersion = checkItemBean.isValue();
} else if (APIMgtConstants.REQUIRE_RE_SUBSCRIPTIONS.equals(checkItemBean.getName())) {
requireReSubscriptions = checkItemBean.isValue();
}
}
API originalAPI = apiBuilder.build();
apiLifecycleManager.executeLifecycleEvent(api.getLifeCycleStatus(), status, apiBuilder.getLifecycleInstanceId(), updatedBy, originalAPI);
if (deprecateOlderVersion) {
if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
API oldAPI = apiDAO.getAPI(api.getCopiedFromApiId());
if (oldAPI != null) {
API.APIBuilder previousAPI = new API.APIBuilder(oldAPI);
previousAPI.setLifecycleStateInfo(apiLifecycleManager.getLifecycleDataForState(previousAPI.getLifecycleInstanceId(), previousAPI.getLifeCycleStatus()));
if (APIUtils.validateTargetState(previousAPI.getLifecycleState(), APIStatus.DEPRECATED.getStatus())) {
apiLifecycleManager.executeLifecycleEvent(previousAPI.getLifeCycleStatus(), APIStatus.DEPRECATED.getStatus(), previousAPI.getLifecycleInstanceId(), updatedBy, previousAPI.build());
}
}
}
}
if (!requireReSubscriptions) {
if (StringUtils.isNotEmpty(api.getCopiedFromApiId())) {
List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(api.getCopiedFromApiId());
List<Subscription> subscriptionList = new ArrayList<>();
for (Subscription subscription : subscriptions) {
if (api.getPolicies().contains(subscription.getPolicy())) {
if (!APIMgtConstants.SubscriptionStatus.ON_HOLD.equals(subscription.getStatus())) {
subscriptionList.add(new Subscription(UUID.randomUUID().toString(), subscription.getApplication(), subscription.getApi(), subscription.getPolicy()));
}
}
apiSubscriptionDAO.copySubscriptions(subscriptionList);
}
}
}
// publish API state change to gateway
apiGateway.changeAPIState(originalAPI, status);
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
} catch (GatewayException e) {
String message = "Error occurred while changing the state of api ID: " + apiId + " to " + status + "in gateway";
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptionsException.
@Test(description = "Error occurred while deleting API with zero subscriptions")
public void testDeleteApiWithZeroSubscriptionsException() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
API api = SampleTestObjectCreator.createDefaultAPI().build();
String uuid = api.getId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, workflowDAO, null, null, null, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
// LifeCycleException
Mockito.doThrow(LifecycleException.class).when(apiLifecycleManager).removeLifecycle(api.getLifecycleInstanceId());
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while Disassociating the API with Lifecycle id " + uuid);
}
// ApiDAOException
Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).deleteAPI(uuid);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting the API with id " + uuid);
}
// GatewayException
Mockito.doThrow(GatewayException.class).when(gateway).deleteAPI(api);
try {
apiPublisher.deleteAPI(uuid);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while deleting API with id - " + uuid + " from gateway");
}
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class APIStoreImpl method addCompositeApi.
/**
* {@inheritDoc}
*/
@Override
public String addCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
apiBuilder.provider(getUsername());
if (StringUtils.isEmpty(apiBuilder.getId())) {
apiBuilder.id(UUID.randomUUID().toString());
}
LocalDateTime localDateTime = LocalDateTime.now();
apiBuilder.createdTime(localDateTime);
apiBuilder.lastUpdatedTime(localDateTime);
apiBuilder.createdBy(getUsername());
apiBuilder.updatedBy(getUsername());
if (!isApiNameExist(apiBuilder.getName()) && !isContextExist(apiBuilder.getContext())) {
setUriTemplates(apiBuilder);
setGatewayDefinitionSource(apiBuilder);
if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
try {
CompositeAPI createdAPI = apiBuilder.build();
APIUtils.validate(createdAPI);
// publishing config to gateway
gateway.addCompositeAPI(createdAPI);
getApiDAO().addApplicationAssociatedAPI(createdAPI);
if (log.isDebugEnabled()) {
log.debug("API " + createdAPI.getName() + "-" + createdAPI.getVersion() + " was created " + "successfully.", log);
}
} catch (GatewayException e) {
String message = "Error publishing service configuration to Gateway " + apiBuilder.getName();
log.error(message, e);
throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (APIMgtDAOException e) {
String message = "Error when adding composite API " + apiBuilder.getName();
throw new APIManagementException(message, e, e.getErrorHandler());
}
} else {
String message = "Duplicate API already Exist with name/Context " + apiBuilder.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.API_ALREADY_EXISTS);
}
return apiBuilder.getId();
}
use of org.wso2.carbon.apimgt.core.exception.GatewayException in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsUnblockSubscriptionPost.
/**
* @param subscriptionId ID of the subscription
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Modified-Since value
* @param request ms4j request object
* @return ms4j request object
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsUnblockSubscriptionPost(String subscriptionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
Subscription subscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
if (subscription == null) {
String errorMessage = "Subscription not found : " + subscriptionId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
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();
} else if (subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.REJECTED) || subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.ON_HOLD)) {
String errorMessage = "Cannot update subcription from " + subscription.getStatus() + "to " + APIMgtConstants.SubscriptionStatus.ACTIVE;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_STATE_INVALID);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
apiPublisher.updateSubscriptionStatus(subscriptionId, APIMgtConstants.SubscriptionStatus.ACTIVE);
Subscription newSubscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(newSubscription);
return Response.ok().entity(subscriptionDTO).build();
} catch (GatewayException e) {
String errorMessage = "Failed to unblock subscription :" + subscriptionId + " in gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while unblocking the subscription " + subscriptionId;
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();
}
}
Aggregations