Search in sources :

Example 1 with APIInfoListDTO

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

the class AnalyticsMappingUtil method fromAPIInfoListToDTO.

/**
 * Converts and APIInfoList to APIInfoListDTO.
 *
 * @param apiInfoList list of ApiInfo objects
 * @return corresponding APIInfoListDTO object
 */
public static APIInfoListDTO fromAPIInfoListToDTO(List<APIInfo> apiInfoList, ZoneId zoneId) {
    APIInfoListDTO apiInfoListDTO = new APIInfoListDTO();
    List<APIInfoDTO> apiInfoDTOList = new ArrayList<>();
    apiInfoListDTO.setCount(apiInfoList.size());
    for (APIInfo apiInfo : apiInfoList) {
        apiInfoDTOList.add(fromAPIInfoToDTO(apiInfo, zoneId));
    }
    apiInfoListDTO.setList(apiInfoDTOList);
    return apiInfoListDTO;
}
Also used : ArrayList(java.util.ArrayList) APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoDTO) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO)

Example 2 with APIInfoListDTO

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

the class APIInfoMappingUtil method fromAPIInfoListToDTO.

/**
 * Converts a List object of APIIdentifiers into a DTO
 *
 * @param apiIds a list of APIIdentifier objects
 * @return APIInfoListDTO object containing APIInfoDTOs
 */
public static APIInfoListDTO fromAPIInfoListToDTO(List<APIIdentifier> apiIds) throws UnsupportedEncodingException {
    APIInfoListDTO apiInfoListDTO = new APIInfoListDTO();
    List<APIInfoDTO> apiInfoDTOs = apiInfoListDTO.getList();
    if (apiInfoDTOs == null) {
        apiInfoDTOs = new ArrayList<>();
        apiInfoListDTO.setList(apiInfoDTOs);
    }
    for (APIIdentifier apiId : apiIds) {
        apiInfoDTOs.add(fromAPIInfoToDTO(apiId));
    }
    apiInfoListDTO.setCount(apiInfoDTOs.size());
    return apiInfoListDTO;
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APIInfoDTO) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APIInfoListDTO)

Example 3 with APIInfoListDTO

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

the class ApplicationsApiServiceImpl method applicationsImportPost.

/**
 * 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 preserveOwner       If true, preserve the original owner of the application
 * @param skipSubscriptions   If true, skip subscriptions of the application
 * @param appOwner            Target owner of the application
 * @param skipApplicationKeys Skip application keys while importing
 * @param update              Update if existing application found or import
 * @param messageContext      Message Context
 * @return imported Application
 */
@Override
public Response applicationsImportPost(InputStream fileInputStream, Attachment fileDetail, Boolean preserveOwner, Boolean skipSubscriptions, String appOwner, Boolean skipApplicationKeys, Boolean update, MessageContext messageContext) throws APIManagementException {
    String ownerId;
    Application application;
    try {
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        String extractedFolderPath = CommonUtil.getArchivePathOfExtractedDirectory(fileInputStream, ImportExportConstants.UPLOAD_APPLICATION_FILE_NAME);
        String jsonContent = ImportUtils.getApplicationDefinitionAsJson(extractedFolderPath);
        // Retrieving the field "data" in api.yaml/json and convert it to a JSON object for further processing
        JsonElement configElement = new JsonParser().parse(jsonContent).getAsJsonObject().get(APIConstants.DATA);
        ExportedApplication exportedApplication = new Gson().fromJson(configElement, ExportedApplication.class);
        // Retrieve the application DTO object from the aggregated exported application
        ApplicationDTO applicationDTO = exportedApplication.getApplicationInfo();
        if (!StringUtils.isBlank(appOwner)) {
            ownerId = appOwner;
        } else if (preserveOwner != null && preserveOwner) {
            ownerId = applicationDTO.getOwner();
        } else {
            ownerId = username;
        }
        if (!MultitenantUtils.getTenantDomain(ownerId).equals(MultitenantUtils.getTenantDomain(username))) {
            throw new APIManagementException("Cross Tenant Imports are not allowed", ExceptionCodes.TENANT_MISMATCH);
        }
        String applicationGroupId = String.join(",", applicationDTO.getGroups());
        if (applicationDTO.getGroups() != null && applicationDTO.getGroups().size() > 0) {
            ImportUtils.validateOwner(username, applicationGroupId, apiConsumer);
        }
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        if (APIUtil.isApplicationExist(ownerId, applicationDTO.getName(), applicationGroupId, organization) && update != null && update) {
            int appId = APIUtil.getApplicationId(applicationDTO.getName(), ownerId);
            Application oldApplication = apiConsumer.getApplicationById(appId);
            application = preProcessAndUpdateApplication(ownerId, applicationDTO, oldApplication, oldApplication.getUUID());
        } else {
            application = preProcessAndAddApplication(ownerId, applicationDTO, organization);
            update = Boolean.FALSE;
        }
        List<APIIdentifier> skippedAPIs = new ArrayList<>();
        if (skipSubscriptions == null || !skipSubscriptions) {
            skippedAPIs = ImportUtils.importSubscriptions(exportedApplication.getSubscribedAPIs(), ownerId, application, update, apiConsumer, organization);
        }
        Application importedApplication = apiConsumer.getApplicationById(application.getId());
        importedApplication.setOwner(ownerId);
        ApplicationInfoDTO importedApplicationDTO = ApplicationMappingUtil.fromApplicationToInfoDTO(importedApplication);
        URI location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + importedApplicationDTO.getApplicationId());
        // check whether keys need to be skipped while import
        if (skipApplicationKeys == null || !skipApplicationKeys) {
            // if this is an update, old keys will be removed and the OAuth app will be overridden with new values
            if (update) {
                if (applicationDTO.getKeys().size() > 0 && importedApplication.getKeys().size() > 0) {
                    importedApplication.getKeys().clear();
                }
            }
            // Add application keys if present and keys does not exists in the current application
            if (applicationDTO.getKeys().size() > 0 && importedApplication.getKeys().size() == 0) {
                for (ApplicationKeyDTO applicationKeyDTO : applicationDTO.getKeys()) {
                    ImportUtils.addApplicationKey(ownerId, importedApplication, applicationKeyDTO, apiConsumer, update);
                }
            }
        }
        if (skippedAPIs.isEmpty()) {
            return Response.created(location).entity(importedApplicationDTO).build();
        } else {
            APIInfoListDTO skippedAPIListDTO = APIInfoMappingUtil.fromAPIInfoListToDTO(skippedAPIs);
            return Response.created(location).status(207).entity(skippedAPIListDTO).build();
        }
    } catch (URISyntaxException | UserStoreException | APIImportExportException e) {
        throw new APIManagementException("Error while importing Application", e);
    } catch (UnsupportedEncodingException e) {
        throw new APIManagementException("Error while Decoding apiId", e);
    } catch (IOException e) {
        throw new APIManagementException("Error while reading the application definition", e);
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationInfoDTO) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) JsonElement(com.google.gson.JsonElement) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoListDTO) JsonParser(com.google.gson.JsonParser)

Example 4 with APIInfoListDTO

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

the class APIInfoMappingUtil method fromAPIInfoListToDTO.

/**
 * Converts a List object of APIIdentifiers into a DTO
 *
 * @param apiIds a list of APIIdentifier objects
 * @return APIInfoListDTO object containing APIInfoDTOs
 */
public static APIInfoListDTO fromAPIInfoListToDTO(List<APIIdentifier> apiIds) throws UnsupportedEncodingException {
    APIInfoListDTO apiInfoListDTO = new APIInfoListDTO();
    List<APIInfoDTO> apiInfoDTOs = apiInfoListDTO.getList();
    if (apiInfoDTOs == null) {
        apiInfoDTOs = new ArrayList<>();
        apiInfoListDTO.setList(apiInfoDTOs);
    }
    for (APIIdentifier apiId : apiIds) {
        apiInfoDTOs.add(fromAPIInfoToDTO(apiId));
    }
    apiInfoListDTO.setCount(apiInfoDTOs.size());
    return apiInfoListDTO;
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoListDTO)

Example 5 with APIInfoListDTO

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

the class AnalyticsMappingUtilTestCase method fromAPIInfoListToDTOTest.

@Test
public void fromAPIInfoListToDTOTest() {
    List<APIInfo> apiInfoList = new ArrayList<>();
    apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
    apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
    apiInfoList.add(SampleTestObjectCreator.createRandomAPIInfoObject());
    APIInfoListDTO apiInfoListDTO = AnalyticsMappingUtil.fromAPIInfoListToDTO(apiInfoList, ZoneOffset.UTC);
    Assert.assertEquals(apiInfoList.size(), apiInfoListDTO.getList().size());
    for (int i = 0; i < apiInfoList.size(); i++) {
        Assert.assertEquals(epochToISO8601DateTime(apiInfoList.get(i).getCreatedTime(), ZoneOffset.UTC), apiInfoListDTO.getList().get(i).getTime());
        Assert.assertEquals(apiInfoList.get(i).getName(), apiInfoListDTO.getList().get(i).getName());
        Assert.assertEquals(apiInfoList.get(i).getContext(), apiInfoListDTO.getList().get(i).getContext());
        Assert.assertEquals(apiInfoList.get(i).getDescription(), apiInfoListDTO.getList().get(i).getDescription());
        Assert.assertEquals(apiInfoList.get(i).getLifeCycleStatus(), apiInfoListDTO.getList().get(i).getLifeCycleStatus());
        Assert.assertEquals(apiInfoList.get(i).getProvider(), apiInfoListDTO.getList().get(i).getProvider());
        Assert.assertEquals(apiInfoList.get(i).getVersion(), apiInfoListDTO.getList().get(i).getVersion());
        Assert.assertEquals(apiInfoList.get(i).getId(), apiInfoListDTO.getList().get(i).getId());
    }
}
Also used : APIInfo(org.wso2.carbon.apimgt.core.models.analytics.APIInfo) ArrayList(java.util.ArrayList) APIInfoListDTO(org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 APIInfo (org.wso2.carbon.apimgt.core.models.analytics.APIInfo)3 APIInfoListDTO (org.wso2.carbon.apimgt.rest.api.analytics.dto.APIInfoListDTO)3 APIInfoListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoListDTO)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ZoneId (java.time.ZoneId)1 Test (org.testng.annotations.Test)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Application (org.wso2.carbon.apimgt.api.model.Application)1 Analyzer (org.wso2.carbon.apimgt.core.api.Analyzer)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APISubscriptionCount (org.wso2.carbon.apimgt.core.models.analytics.APISubscriptionCount)1