Search in sources :

Example 26 with ApplicationDTO

use of org.wso2.carbon.apimgt.internal.service.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)

Example 27 with ApplicationDTO

use of org.wso2.carbon.apimgt.internal.service.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)

Example 28 with ApplicationDTO

use of org.wso2.carbon.apimgt.internal.service.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 29 with ApplicationDTO

use of org.wso2.carbon.apimgt.internal.service.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 30 with ApplicationDTO

use of org.wso2.carbon.apimgt.internal.service.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)

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