Search in sources :

Example 26 with ApplicationDTO

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

the class SubscriptionValidationDataUtil method fromApplicationToApplicationListDTO.

public static ApplicationListDTO fromApplicationToApplicationListDTO(List<Application> model) {
    ApplicationListDTO applicationListDTO = new ApplicationListDTO();
    if (model != null) {
        for (Application appModel : model) {
            ApplicationDTO applicationDTO = new ApplicationDTO();
            applicationDTO.setUuid(appModel.getUuid());
            applicationDTO.setId(appModel.getId());
            applicationDTO.setName(appModel.getName());
            applicationDTO.setPolicy(appModel.getPolicy());
            applicationDTO.setSubName(appModel.getSubName());
            applicationDTO.setTokenType(appModel.getTokenType());
            applicationDTO.setOrganization(appModel.getOrganization());
            Set<String> groupIds = appModel.getGroupIds();
            for (String grp : groupIds) {
                GroupIdDTO groupIdDTO = new GroupIdDTO();
                groupIdDTO.setApplicationId(appModel.getId());
                groupIdDTO.setGroupId(grp);
                applicationDTO.getGroupIds().add(groupIdDTO);
            }
            Map<String, String> attributes = appModel.getAttributes();
            applicationDTO.setAttributes(attributes);
            applicationListDTO.getList().add(applicationDTO);
        }
        applicationListDTO.setCount(model.size());
    } else {
        applicationListDTO.setCount(0);
    }
    return applicationListDTO;
}
Also used : GroupIdDTO(org.wso2.carbon.apimgt.internal.service.dto.GroupIdDTO) ApplicationDTO(org.wso2.carbon.apimgt.internal.service.dto.ApplicationDTO) Application(org.wso2.carbon.apimgt.api.model.subscription.Application) ApplicationListDTO(org.wso2.carbon.apimgt.internal.service.dto.ApplicationListDTO)

Example 27 with ApplicationDTO

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdGet.

/**
 * Get an application by Id
 *
 * @param applicationId   application identifier
 * @param ifNoneMatch     If-None-Match header value
 * @return response containing the required application object
 */
@Override
public Response applicationsApplicationIdGet(String applicationId, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getApplicationByUUID(applicationId, organization);
        if (application != null) {
            // Remove hidden attributes and set the rest of the attributes from config
            JSONArray applicationAttributesFromConfig = apiConsumer.getAppAttributesFromConfig(username);
            Map<String, String> existingApplicationAttributes = application.getApplicationAttributes();
            Map<String, String> applicationAttributes = new HashMap<>();
            if (existingApplicationAttributes != null && applicationAttributesFromConfig != null) {
                for (Object object : applicationAttributesFromConfig) {
                    JSONObject attribute = (JSONObject) object;
                    Boolean hidden = (Boolean) attribute.get(APIConstants.ApplicationAttributes.HIDDEN);
                    String attributeName = (String) attribute.get(APIConstants.ApplicationAttributes.ATTRIBUTE);
                    if (!BooleanUtils.isTrue(hidden)) {
                        String attributeVal = existingApplicationAttributes.get(attributeName);
                        if (attributeVal != null) {
                            applicationAttributes.put(attributeName, attributeVal);
                        } else {
                            applicationAttributes.put(attributeName, "");
                        }
                    }
                }
            }
            application.setApplicationAttributes(applicationAttributes);
            if (RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                ApplicationDTO applicationDTO = ApplicationMappingUtil.fromApplicationtoDTO(application);
                applicationDTO.setHashEnabled(OAuthServerConfiguration.getInstance().isClientSecretHashEnabled());
                Set<Scope> scopes = apiConsumer.getScopesForApplicationSubscription(username, application.getId(), organization);
                List<ScopeInfoDTO> scopeInfoList = ApplicationMappingUtil.getScopeInfoDTO(scopes);
                applicationDTO.setSubscriptionScopes(scopeInfoList);
                return Response.ok().entity(applicationDTO).build();
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving application " + applicationId, e, log);
    }
    return null;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ScopeInfoDTO) JSONObject(org.json.simple.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) 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)

Example 28 with ApplicationDTO

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdGet.

@Override
public Response applicationsApplicationIdGet(String applicationId, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        Application application = apiConsumer.getApplicationByUUID(applicationId, organization);
        if (application != null) {
            String applicationTenantDomain = MultitenantUtils.getTenantDomain(application.getOwner());
            // and verify that the invoking user's tenant domain has an API subscribed by this application
            if (tenantDomain.equals(applicationTenantDomain)) {
                // Remove hidden attributes and set the rest of the attributes from config
                JSONArray applicationAttributesFromConfig = apiConsumer.getAppAttributesFromConfig(username);
                Map<String, String> existingApplicationAttributes = application.getApplicationAttributes();
                Map<String, String> applicationAttributes = new HashMap<>();
                if (existingApplicationAttributes != null && applicationAttributesFromConfig != null) {
                    for (Object object : applicationAttributesFromConfig) {
                        JSONObject attribute = (JSONObject) object;
                        Boolean hidden = (Boolean) attribute.get(APIConstants.ApplicationAttributes.HIDDEN);
                        String attributeName = (String) attribute.get(APIConstants.ApplicationAttributes.ATTRIBUTE);
                        if (!BooleanUtils.isTrue(hidden)) {
                            String attributeVal = existingApplicationAttributes.get(attributeName);
                            if (attributeVal != null) {
                                applicationAttributes.put(attributeName, attributeVal);
                            } else {
                                applicationAttributes.put(attributeName, "");
                            }
                        }
                    }
                }
                application.setApplicationAttributes(applicationAttributes);
                ApplicationDTO applicationDTO = ApplicationMappingUtil.fromApplicationtoDTO(application);
                Set<Scope> scopes = apiConsumer.getScopesForApplicationSubscription(username, application.getId(), organization);
                List<ScopeInfoDTO> scopeInfoList = ApplicationMappingUtil.getScopeInfoDTO(scopes);
                applicationDTO.setSubscriptionScopes(scopeInfoList);
                return Response.ok().entity(applicationDTO).build();
            }
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving application " + applicationId, e, log);
    }
    RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
    return null;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationDTO) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeInfoDTO) JSONObject(org.json.simple.JSONObject) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 29 with ApplicationDTO

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

the class ApplicationMappingUtil method fromApplicationtoDTO.

public static ApplicationDTO fromApplicationtoDTO(Application application) {
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setApplicationId(application.getUUID());
    applicationDTO.setThrottlingPolicy(application.getTier());
    applicationDTO.setDescription(application.getDescription());
    Map<String, String> applicationAttributes = application.getApplicationAttributes();
    applicationDTO.setAttributes(applicationAttributes);
    applicationDTO.setName(application.getName());
    applicationDTO.setStatus(application.getStatus());
    applicationDTO.setOwner(application.getOwner());
    if (StringUtils.isNotEmpty(application.getGroupId())) {
        applicationDTO.setGroups(Arrays.asList(application.getGroupId().split(",")));
    }
    applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.OAUTH);
    applicationDTO.setSubscriptionCount(application.getSubscriptionCount());
    if (StringUtils.isNotEmpty(application.getTokenType()) && !APIConstants.DEFAULT_TOKEN_TYPE.equals(application.getTokenType())) {
        applicationDTO.setTokenType(ApplicationDTO.TokenTypeEnum.valueOf(application.getTokenType()));
    }
    return applicationDTO;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationDTO)

Example 30 with ApplicationDTO

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

the class ApplicationMappingUtil method fromDTOtoApplication.

public static Application fromDTOtoApplication(ApplicationDTO applicationDTO, String username) {
    // subscriber field of the body is not honored
    Subscriber subscriber = new Subscriber(username);
    Application application = new Application(applicationDTO.getName(), subscriber);
    application.setTier(applicationDTO.getThrottlingPolicy());
    application.setDescription(applicationDTO.getDescription());
    application.setUUID(applicationDTO.getApplicationId());
    // Check if the token type is not set in the request.
    if (StringUtils.isEmpty(applicationDTO.getTokenType().toString())) {
        // Set the default to JWT.
        application.setTokenType(APIConstants.TOKEN_TYPE_JWT);
    } else {
        // Otherwise set it to the type in the request.
        application.setTokenType(applicationDTO.getTokenType().toString());
    }
    Map<String, String> appAttributes = applicationDTO.getAttributes();
    application.setApplicationAttributes(appAttributes);
    if (applicationDTO.getGroups() != null && applicationDTO.getGroups().size() > 0) {
        application.setGroupId(String.join(",", applicationDTO.getGroups()));
    }
    return application;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Application(org.wso2.carbon.apimgt.api.model.Application)

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