Search in sources :

Example 31 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO in project carbon-apimgt by wso2.

the class ExportUtils method createApplicationDTOToExport.

/**
 * Create an aggregated Application DTO to be exported.
 *
 * @param application Application{@link Application} to be exported
 * @param apiConsumer API Consumer
 * @param withKeys    Export the Application with keys or not
 * @return Exported application
 * @throws APIManagementException If an error occurs while retrieving subscribed APIs
 */
private static ExportedApplication createApplicationDTOToExport(Application application, APIConsumer apiConsumer, Boolean withKeys) throws APIManagementException {
    ApplicationDTO applicationDto = ApplicationMappingUtil.fromApplicationtoDTO(application);
    // Set keys if withKeys is true
    if (withKeys == null || !withKeys) {
        application.clearOAuthApps();
    } else {
        List<ApplicationKeyDTO> applicationKeyDTOs = new ArrayList<>();
        for (APIKey apiKey : application.getKeys()) {
            // Encode the consumer secret and set it
            apiKey.setConsumerSecret(new String(Base64.encodeBase64(apiKey.getConsumerSecret().getBytes(Charset.defaultCharset()))));
            ApplicationKeyDTO applicationKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(apiKey);
            applicationKeyDTOs.add(applicationKeyDTO);
        }
        applicationDto.setKeys(applicationKeyDTOs);
    }
    // Get the subscribed API details and add it to a set
    Set<SubscribedAPI> subscribedAPIs = apiConsumer.getSubscribedAPIs(application.getSubscriber(), application.getName(), application.getGroupId());
    Set<ExportedSubscribedAPI> exportedSubscribedAPIs = new HashSet<>();
    for (SubscribedAPI subscribedAPI : subscribedAPIs) {
        ExportedSubscribedAPI exportedSubscribedAPI = new ExportedSubscribedAPI(subscribedAPI.getApiId(), subscribedAPI.getSubscriber(), subscribedAPI.getTier().getName());
        exportedSubscribedAPIs.add(exportedSubscribedAPI);
    }
    // Set the subscription count by counting the number of subscribed APIs
    applicationDto.setSubscriptionCount(exportedSubscribedAPIs.size());
    // Set the application
    ExportedApplication exportedApplication = new ExportedApplication(applicationDto);
    // Set the subscribed APIs
    exportedApplication.setSubscribedAPIs(exportedSubscribedAPIs);
    return exportedApplication;
}
Also used : ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO) APIKey(org.wso2.carbon.apimgt.api.model.APIKey) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) HashSet(java.util.HashSet)

Aggregations

Application (org.wso2.carbon.apimgt.core.models.Application)11 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO)11 ArrayList (java.util.ArrayList)10 Application (org.wso2.carbon.apimgt.api.model.Application)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)7 Test (org.junit.Test)6 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)6 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)6 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)6 HashMap (java.util.HashMap)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5 ApplicationTokenDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationTokenDTO)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Response (javax.ws.rs.core.Response)4 JSONObject (org.json.simple.JSONObject)4