use of org.wso2.carbon.identity.oauth.dcr.bean.Application in project carbon-apimgt by wso2.
the class ApplicationMappingUtil method fromApplicationsToDTO.
/**
* Converts an Application[] array into a corresponding ApplicationListDTO
*
* @param applications array of Application objects
* @param limit limit parameter
* @param offset starting index
* @return ApplicationListDTO object corresponding to Application[] array
*/
public static ApplicationListDTO fromApplicationsToDTO(List<Application> applications, int limit, int offset) {
ApplicationListDTO applicationListDTO = new ApplicationListDTO();
List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
if (applicationInfoDTOs == null) {
applicationInfoDTOs = new ArrayList<>();
applicationListDTO.setList(applicationInfoDTOs);
}
// identifying the proper start and end indexes
int start = offset < applications.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= applications.size() - 1 ? offset + limit - 1 : applications.size() - 1;
for (int i = start; i <= end; i++) {
applicationInfoDTOs.add(fromApplicationToInfoDTO(applications.get(i)));
}
applicationListDTO.setCount(applicationInfoDTOs.size());
return applicationListDTO;
}
use of org.wso2.carbon.identity.oauth.dcr.bean.Application in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method updateApplication.
@Override
public OAuthApplicationInfo updateApplication(OAuthApplicationInfo oAuthApplicationInfo) throws KeyManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating OAuth2 application with : " + oAuthApplicationInfo.toString());
}
String applicationName = oAuthApplicationInfo.getClientName();
String keyType = (String) oAuthApplicationInfo.getParameter(KeyManagerConstants.APP_KEY_TYPE);
if (keyType != null) {
// Derive oauth2 app name based on key type and user input for app name
applicationName = applicationName + '_' + keyType;
}
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName(applicationName);
dcrClientInfo.setClientId(oAuthApplicationInfo.getClientId());
dcrClientInfo.setClientSecret(oAuthApplicationInfo.getClientSecret());
dcrClientInfo.addCallbackUrl(oAuthApplicationInfo.getCallBackURL());
dcrClientInfo.setGrantTypes(oAuthApplicationInfo.getGrantTypes());
Response response = dcrmServiceStub.updateApplication(dcrClientInfo, dcrClientInfo.getClientId());
if (response == null) {
throw new KeyManagementException("Error occurred while updating DCR application. Response is null", ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
try {
OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
// setting original parameter list
oAuthApplicationInfoResponse.setParameters(oAuthApplicationInfo.getParameters());
if (log.isDebugEnabled()) {
log.debug("OAuth2 application updated: " + oAuthApplicationInfoResponse.toString());
}
return oAuthApplicationInfoResponse;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR application update response " + "message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
// 400 - Known Error
try {
DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else {
// Unknown Error
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
}
use of org.wso2.carbon.identity.oauth.dcr.bean.Application in project carbon-apimgt by wso2.
the class APIStoreImpl method cleanupPendingTaskForApplicationDeletion.
private void cleanupPendingTaskForApplicationDeletion(Application application) throws APIManagementException {
WorkflowExecutor createApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
WorkflowExecutor createSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
WorkflowExecutor updateApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
String appId = application.getId();
// get subscriptions with pending status
List<Subscription> pendingSubscriptions = getApiSubscriptionDAO().getPendingAPISubscriptionsByApplication(appId);
String applicationStatus = application.getStatus();
if (pendingSubscriptions == null || pendingSubscriptions.isEmpty()) {
// check whether application is on hold state
if (ApplicationStatus.APPLICATION_ONHOLD.equals(applicationStatus)) {
// delete pending tasks for application creation if any
cleanupPendingTask(createApplicationWFExecutor, appId, WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
}
} else {
// approvals (cannot subscribe to a pending application)
for (Iterator iterator = pendingSubscriptions.iterator(); iterator.hasNext(); ) {
Subscription pendingSubscription = (Subscription) iterator.next();
// delete pending tasks for subscripton creation if any
cleanupPendingTask(createSubscriptionWFExecutor, pendingSubscription.getId(), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
}
}
// delete pending tasks for application update if any
cleanupPendingTask(updateApplicationWFExecutor, appId, WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
}
use of org.wso2.carbon.identity.oauth.dcr.bean.Application in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteApplication.
/**
* @see APIStore#deleteApplication(String)
*/
@Override
public WorkflowResponse deleteApplication(String appId) throws APIManagementException {
try {
if (appId == null) {
String message = "Application Id is not provided";
throw new APIManagementException(message, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
// get app info
Application application = getApplicationDAO().getApplication(appId);
if (application == null) {
String message = "Application cannot be found for id :" + appId;
throw new APIManagementException(message, ExceptionCodes.APPLICATION_NOT_FOUND);
}
// delete application creation pending tasks
cleanupPendingTaskForApplicationDeletion(application);
WorkflowExecutor removeApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
ApplicationDeletionWorkflow workflow = new ApplicationDeletionWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
workflow.setApplication(application);
workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Application [ " + application.getName() + " ] deletion request from - " + application.getName();
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = removeApplicationWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(removeApplicationWFExecutor, workflow);
} else {
// add entry to workflow table if it is only in pending state
addWorkflowEntries(workflow);
}
return response;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the application - " + appId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.identity.oauth.dcr.bean.Application in project carbon-apimgt by wso2.
the class APIStoreImpl method updateGrantTypesAndCallbackURL.
@Override
public OAuthApplicationInfo updateGrantTypesAndCallbackURL(String applicationId, String keyType, List<String> grantTypes, String callbackURL) throws APIManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating " + keyType + " grant type/callback of App: " + applicationId);
}
if (StringUtils.isEmpty(applicationId) || StringUtils.isEmpty(keyType)) {
String msg = "One of input values is null or empty. Application Id: " + applicationId + " Key Type: " + keyType;
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
}
if (grantTypes == null || grantTypes.isEmpty() || StringUtils.isEmpty(callbackURL)) {
String msg = "Both Grant Types list and Callback URL can't be null or empty at once.";
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
}
try {
OAuthApplicationInfo appFromDB = getApplicationDAO().getApplicationKeys(applicationId, keyType);
OAuthApplicationInfo oAuthApp = getKeyManager().retrieveApplication(appFromDB.getClientId());
oAuthApp.setGrantTypes(grantTypes);
oAuthApp.setCallBackURL(callbackURL);
oAuthApp = getKeyManager().updateApplication(oAuthApp);
if (log.isDebugEnabled()) {
log.debug("Updated " + keyType + " grant type/callback of App: " + applicationId);
}
return oAuthApp;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating " + keyType + " grant type/callback of application: " + applicationId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations