Search in sources :

Example 71 with Application

use of org.wso2.carbon.apimgt.api.model.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;
}
Also used : ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationInfoDTO) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO)

Example 72 with Application

use of org.wso2.carbon.apimgt.api.model.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);
    }
}
Also used : OAuth2IntrospectionResponse(org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse) Response(feign.Response) DCRError(org.wso2.carbon.apimgt.core.auth.dto.DCRError) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) DCRClientInfo(org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException)

Example 73 with Application

use of org.wso2.carbon.apimgt.api.model.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);
}
Also used : Iterator(java.util.Iterator) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 74 with Application

use of org.wso2.carbon.apimgt.api.model.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());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationDeletionWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationDeletionWorkflow)

Example 75 with Application

use of org.wso2.carbon.apimgt.api.model.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());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)

Aggregations

Test (org.testng.annotations.Test)156 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)143 Application (org.wso2.carbon.apimgt.api.model.Application)130 Application (org.wso2.carbon.apimgt.core.models.Application)121 Test (org.junit.Test)102 ArrayList (java.util.ArrayList)98 SQLException (java.sql.SQLException)94 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)94 PreparedStatement (java.sql.PreparedStatement)88 Connection (java.sql.Connection)83 ResultSet (java.sql.ResultSet)73 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)71 HashMap (java.util.HashMap)70 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)63 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)63 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)61 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)60 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)59 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)58 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)57