use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsPost.
/**
* Adds a new application
*
* @param body Application details to be added
* @param request msf4j request object
* @return 201 Response if successful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsPost(ApplicationDTO body, Request request) throws NotFoundException {
URI location = null;
ApplicationDTO createdApplicationDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiConsumer = RestApiUtil.getConsumer(username);
Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
ApplicationCreationResponse applicationResponse = apiConsumer.addApplication(application);
String applicationUUID = applicationResponse.getApplicationUUID();
Application createdApplication = apiConsumer.getApplication(applicationUUID, username);
createdApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(createdApplication);
location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + createdApplicationDTO.getApplicationId());
// be in either pending or approved state) send back the workflow response
if (ApplicationStatus.APPLICATION_ONHOLD.equals(createdApplication.getStatus())) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(applicationResponse.getWorkflowResponse());
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding new application : " + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for application : " + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
// .build()
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdApplicationDTO).build();
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPost.
/**
* Import an Application which has been exported to a zip file
*
* @param fileInputStream content stream of the zip file which contains exported Application
* @param fileDetail meta information of the zip file
* @param request msf4j request object
* @return Application that was imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
return Response.status(Response.Status.OK).entity(response).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse in project carbon-apimgt by wso2.
the class APIStoreImpl method addApplication.
@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
ApplicationCreationResponse applicationResponse = null;
try {
if (getApplicationDAO().isApplicationNameExists(application.getName())) {
String message = "An application already exists with a duplicate name - " + application.getName();
log.error(message);
throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
}
// Tier validation
Policy tier = application.getPolicy();
if (tier == null) {
String message = "Tier name cannot be null - " + application.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
} else {
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
if (policy == null) {
String message = "Specified tier " + tier.getPolicyName() + " is invalid";
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
}
application.setPolicy(policy);
}
// Generate UUID for application
String generatedUuid = UUID.randomUUID().toString();
application.setId(generatedUuid);
String permissionString = application.getPermissionString();
if (permissionString != null && !("").equals(permissionString)) {
HashMap roleNamePermissionList;
roleNamePermissionList = getAPIPermissionArray(permissionString);
application.setPermissionMap(roleNamePermissionList);
}
application.setCreatedTime(LocalDateTime.now());
getApplicationDAO().addApplication(application);
WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
workflow.setApplication(application);
workflow.setCreatedBy(getUsername());
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = appCreationWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(appCreationWFExecutor, workflow);
} else {
getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
addWorkflowEntries(workflow);
}
APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
applicationResponse = new ApplicationCreationResponse(application.getId(), response);
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while creating the application - " + application.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (ParseException e) {
String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
} catch (WorkflowException e) {
String errorMsg = "Error occurred in workflow";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
}
return applicationResponse;
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddApplication.
@Test(description = "Add an application")
public void testAddApplication() throws APIManagementException {
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
Policy policy = Mockito.mock(Policy.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
Application application = new Application(APP_NAME, USER_NAME);
application.setPolicy(new ApplicationPolicy(TIER));
application.setPermissionString("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\",\"SUBSCRIPTION\"]}]");
Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
ApplicationCreationResponse response = apiStore.addApplication(application);
Assert.assertNotNull(response.getApplicationUUID());
Mockito.verify(applicationDAO, Mockito.times(1)).addApplication(application);
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddApplicationPermissionStringEmpty.
@Test(description = "Add an application with empty permission String")
public void testAddApplicationPermissionStringEmpty() throws APIManagementException {
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
Policy policy = Mockito.mock(Policy.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
Application application = new Application(APP_NAME, USER_NAME);
application.setPolicy(new ApplicationPolicy(TIER));
application.setPermissionString("");
Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
ApplicationCreationResponse applicationResponse = apiStore.addApplication(application);
String applicationUuid = applicationResponse.getApplicationUUID();
Assert.assertNotNull(applicationUuid);
Mockito.verify(applicationDAO, Mockito.times(1)).addApplication(application);
}
Aggregations