Search in sources :

Example 11 with ApplicationDTO

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

the class MappingUtil method convertToApplicationDtoList.

/**
 * convert {@link ApplicationDTO} to {@link Application}
 *
 * @param applicationList List of {@link Application}
 * @return ApplicationEvent list
 */
public static List<ApplicationDTO> convertToApplicationDtoList(List<Application> applicationList) {
    List<ApplicationDTO> applicationDTOList = new ArrayList<>();
    for (Application application : applicationList) {
        ApplicationDTO applicationDTO = new ApplicationDTO();
        applicationDTO.setName(application.getName());
        applicationDTO.setApplicationId(application.getId());
        applicationDTO.setThrottlingTier(application.getPolicy().getUuid());
        applicationDTO.setSubscriber(application.getCreatedUser());
        applicationDTOList.add(applicationDTO);
    }
    return applicationDTOList;
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ApplicationDTO) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 12 with ApplicationDTO

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

the class ApplicationMappingUtil method fromApplicationToInfoDTO.

/**
 * Maps the attribute from Application to ApplicationDto
 * @param application
 * @return
 */
public static ApplicationInfoDTO fromApplicationToInfoDTO(Application application) {
    ApplicationInfoDTO applicationInfoDTO = new ApplicationInfoDTO();
    applicationInfoDTO.setApplicationId(application.getUUID());
    applicationInfoDTO.setStatus(application.getStatus());
    applicationInfoDTO.setName(application.getName());
    applicationInfoDTO.setGroupId(application.getGroupId());
    applicationInfoDTO.setOwner(application.getOwner());
    return applicationInfoDTO;
}
Also used : ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationInfoDTO)

Example 13 with ApplicationDTO

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

the class APIConsumerImpl method generateApiKey.

@Override
public String generateApiKey(Application application, String userName, long validityPeriod, String permittedIP, String permittedReferer) throws APIManagementException {
    JwtTokenInfoDTO jwtTokenInfoDTO = APIUtil.getJwtTokenInfoDTO(application, userName, MultitenantUtils.getTenantDomain(userName));
    ApplicationDTO applicationDTO = new ApplicationDTO();
    applicationDTO.setId(application.getId());
    applicationDTO.setName(application.getName());
    applicationDTO.setOwner(application.getOwner());
    applicationDTO.setTier(application.getTier());
    applicationDTO.setUuid(application.getUUID());
    jwtTokenInfoDTO.setApplication(applicationDTO);
    jwtTokenInfoDTO.setSubscriber(userName);
    jwtTokenInfoDTO.setExpirationTime(validityPeriod);
    jwtTokenInfoDTO.setKeyType(application.getKeyType());
    jwtTokenInfoDTO.setPermittedIP(permittedIP);
    jwtTokenInfoDTO.setPermittedReferer(permittedReferer);
    ApiKeyGenerator apiKeyGenerator = loadApiKeyGenerator();
    return apiKeyGenerator.generateToken(jwtTokenInfoDTO);
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationDTO) JwtTokenInfoDTO(org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO) ApiKeyGenerator(org.wso2.carbon.apimgt.impl.token.ApiKeyGenerator)

Example 14 with ApplicationDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO 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 15 with ApplicationDTO

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

the class ApplicationsApiServiceImpl method preProcessAndAddApplication.

/**
 * Preprocess and add the application
 *
 * @param username       Username
 * @param applicationDto Application DTO
 * @param organization   Identifier of an organization
 * @return Created application
 */
private Application preProcessAndAddApplication(String username, ApplicationDTO applicationDto, String organization) throws APIManagementException {
    APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
    // validate the tier specified for the application
    String tierName = applicationDto.getThrottlingPolicy();
    if (tierName == null) {
        RestApiUtil.handleBadRequest("Throttling tier cannot be null", log);
    }
    Object applicationAttributesFromUser = applicationDto.getAttributes();
    Map<String, String> applicationAttributes = new ObjectMapper().convertValue(applicationAttributesFromUser, Map.class);
    if (applicationAttributes != null) {
        applicationDto.setAttributes(applicationAttributes);
    }
    // we do not honor tokenType sent in the body and
    // all the applications created will of 'JWT' token type
    applicationDto.setTokenType(ApplicationDTO.TokenTypeEnum.JWT);
    // subscriber field of the body is not honored. It is taken from the context
    Application application = ApplicationMappingUtil.fromDTOtoApplication(applicationDto, username);
    int applicationId = apiConsumer.addApplication(application, username, organization);
    // retrieves the created application and send as the response
    return apiConsumer.getApplicationById(applicationId);
}
Also used : 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

BeforeClass (org.testng.annotations.BeforeClass)17 ApplicationDTO (org.wso2.micro.gateway.tests.common.model.ApplicationDTO)17 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 JSONObject (org.json.JSONObject)10 HashMap (java.util.HashMap)9 Application (org.wso2.carbon.apimgt.api.model.Application)8 API (org.wso2.micro.gateway.tests.common.model.API)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)7 KeyValidationInfo (org.wso2.micro.gateway.tests.common.KeyValidationInfo)7 MockAPIPublisher (org.wso2.micro.gateway.tests.common.MockAPIPublisher)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 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5