Search in sources :

Example 11 with ApplicationListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class GatewayUtils method generateApplicationList.

public static ApplicationListDTO generateApplicationList(List<Application> applicationList, SubscriptionDataStore subscriptionDataStore) {
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    List<ApplicationInfoDTO> applicationInfoDTOList = new ArrayList<>();
    for (Application application : applicationList) {
        ApplicationInfoDTO applicationInfoDTO = new ApplicationInfoDTO().id(application.getId()).name(application.getName()).policy(application.getPolicy()).attributes(application.getAttributes()).subName(application.getSubName()).uuid(application.getUUID()).tokenType(application.getTokenType()).keys(convertToApplicationKeyMapping(application.getId(), subscriptionDataStore));
        applicationInfoDTOList.add(applicationInfoDTO);
    }
    applicationListDTO.setList(applicationInfoDTOList);
    applicationListDTO.setCount(applicationInfoDTOList.size());
    return applicationListDTO;
}
Also used : ArrayList(java.util.ArrayList) ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.ApplicationInfoDTO) Application(org.wso2.carbon.apimgt.keymgt.model.entity.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.ApplicationListDTO)

Example 12 with ApplicationListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsGet.

/**
 * Retrieves all the applications that the user has access to
 *
 * @param groupId     group Id
 * @param query       search condition
 * @param limit       max number of objects returns
 * @param offset      starting index
 * @param ifNoneMatch If-None-Match header value
 * @return Response object containing resulted applications
 */
@Override
public Response applicationsGet(String groupId, String query, String sortBy, String sortOrder, Integer limit, Integer offset, String ifNoneMatch, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DEFAULT_SORT_ORDER;
    sortBy = sortBy != null ? ApplicationMappingUtil.getApplicationSortByField(sortBy) : APIConstants.APPLICATION_NAME;
    query = query == null ? "" : query;
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    String username = RestApiCommonUtil.getLoggedInUsername();
    // todo: Do a second level filtering for the incoming group ID.
    // todo: eg: use case is when there are lots of applications which is accessible to his group "g1", he wants to see
    // todo: what are the applications shared to group "g2" among them.
    groupId = RestApiUtil.getLoggedInUserGroupId();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        Subscriber subscriber = new Subscriber(username);
        Application[] applications;
        applications = apiConsumer.getApplicationsWithPagination(new Subscriber(username), groupId, offset, limit, query, sortBy, sortOrder, organization);
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        int applicationCount = apiMgtDAO.getAllApplicationCount(subscriber, groupId, query);
        applicationListDTO = ApplicationMappingUtil.fromApplicationsToDTO(applications);
        ApplicationMappingUtil.setPaginationParamsWithSortParams(applicationListDTO, groupId, limit, offset, applicationCount, sortOrder, sortBy.toLowerCase());
        return Response.ok().entity(applicationListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.rootCauseMessageMatches(e, "start index seems to be greater than the limit count")) {
            // this is not an error of the user as he does not know the total number of applications available.
            // Thus sends an empty response
            applicationListDTO.setCount(0);
            applicationListDTO.setPagination(new PaginationDTO());
            return Response.ok().entity(applicationListDTO).build();
        } else {
            String errorMessage = "Error while retrieving Applications";
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) 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) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationListDTO)

Example 13 with ApplicationListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsGet.

@Override
public Response applicationsGet(String user, Integer limit, Integer offset, String accept, String applicationName, String tenantDomain, String sortBy, String sortOrder, MessageContext messageContext) {
    // To store the initial value of the user (specially if it is null or empty)
    String givenUser = user;
    // if no username provided user associated with access token will be used
    if (user == null || StringUtils.isEmpty(user)) {
        user = RestApiCommonUtil.getLoggedInUsername();
        givenUser = StringUtils.EMPTY;
    }
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DEFAULT_SORT_ORDER;
    sortBy = getApplicationsSortByField(sortBy);
    applicationName = applicationName != null ? applicationName : StringUtils.EMPTY;
    ApplicationListDTO applicationListDTO;
    try {
        Application[] allMatchedApps;
        boolean migrationMode = Boolean.getBoolean(RestApiConstants.MIGRATION_MODE);
        int allApplicationsCount = 0;
        if (!migrationMode) {
            // normal non-migration flow
            if (!MultitenantUtils.getTenantDomain(user).equals(RestApiCommonUtil.getLoggedInUserTenantDomain())) {
                String errorMsg = "User " + user + " is not available for the current tenant domain";
                log.error(errorMsg);
                return Response.status(Response.Status.FORBIDDEN).entity(errorMsg).build();
            }
            APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(user);
            // If no user is passed, get the applications for the tenant (not only for the user)
            APIAdmin apiAdmin = new APIAdminImpl();
            int tenantId = APIUtil.getTenantId(user);
            allMatchedApps = apiAdmin.getApplicationsWithPagination(user, givenUser, tenantId, limit, offset, applicationName, sortBy, sortOrder);
            allApplicationsCount = apiAdmin.getApplicationsCount(tenantId, givenUser, applicationName);
        } else {
            // flow at migration process
            if (StringUtils.isEmpty(tenantDomain)) {
                tenantDomain = MultitenantUtils.getTenantDomain(user);
            }
            RestApiUtil.handleMigrationSpecificPermissionViolations(tenantDomain, RestApiCommonUtil.getLoggedInUsername());
            APIAdmin apiAdmin = new APIAdminImpl();
            allMatchedApps = apiAdmin.getAllApplicationsOfTenantForMigration(tenantDomain);
        }
        applicationListDTO = ApplicationMappingUtil.fromApplicationsToDTO(allMatchedApps);
        ApplicationMappingUtil.setPaginationParams(applicationListDTO, limit, offset, allApplicationsCount);
        return Response.ok().entity(applicationListDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving applications of the user " + user, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) Application(org.wso2.carbon.apimgt.api.model.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationListDTO)

Example 14 with ApplicationListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.ApplicationListDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method fromApplicationsToDTO.

public static ApplicationListDTO fromApplicationsToDTO(Application[] applications) {
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
    if (applicationInfoDTOs == null) {
        applicationInfoDTOs = new ArrayList<>();
        applicationListDTO.setList(applicationInfoDTOs);
    }
    for (Application application : applications) {
        applicationInfoDTOs.add(fromApplicationToInfoDTO(application));
    }
    applicationListDTO.setCount(applicationInfoDTOs.size());
    return applicationListDTO;
}
Also used : ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationInfoDTO) Application(org.wso2.carbon.apimgt.api.model.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationListDTO)

Aggregations

ArrayList (java.util.ArrayList)5 Application (org.wso2.carbon.apimgt.api.model.Application)4 Application (org.wso2.carbon.apimgt.core.models.Application)4 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)2 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationListDTO)2 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationListDTO)2 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.gateway.dto.ApplicationListDTO)2 ApplicationListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationListDTO)2 PaginationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)2 Response (javax.ws.rs.core.Response)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1 Application (org.wso2.carbon.apimgt.api.model.subscription.Application)1