use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApplicationInfoDTO in project carbon-apimgt by wso2.
the class ApplicationMappingUtil method fromApplicationsToDTO.
/**
* Converts an Application[] array into a corresponding ApplicationListDTO
*
* @param applications array of Application objects
* @param limit limit parameter
* @param offset starting index
* @return ApplicationListDTO object corresponding to Application[] array
*/
public static ApplicationListDTO fromApplicationsToDTO(List<Application> applications, int limit, int offset) {
ApplicationListDTO applicationListDTO = new ApplicationListDTO();
List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
if (applicationInfoDTOs == null) {
applicationInfoDTOs = new ArrayList<>();
applicationListDTO.setList(applicationInfoDTOs);
}
// identifying the proper start and end indexes
int start = offset < applications.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= applications.size() - 1 ? offset + limit - 1 : applications.size() - 1;
for (int i = start; i <= end; i++) {
applicationInfoDTOs.add(fromApplicationToInfoDTO(applications.get(i)));
}
applicationListDTO.setCount(applicationInfoDTOs.size());
return applicationListDTO;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApplicationInfoDTO 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;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApplicationInfoDTO 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);
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApplicationInfoDTO in project carbon-apimgt by wso2.
the class ApplicationMappingUtil method fromApplicationsToDTO.
/**
* Converts an Application[] array into a corresponding ApplicationListDTO
*
* @param applications array of Application objects
* @return ApplicationListDTO object corresponding to Application[] array
*/
public static ApplicationListDTO fromApplicationsToDTO(Application[] applications) throws APIManagementException {
ApplicationListDTO applicationListDTO = new ApplicationListDTO();
List<ApplicationInfoDTO> applicationInfoDTOs = applicationListDTO.getList();
if (applicationInfoDTOs == null) {
applicationInfoDTOs = new ArrayList<>();
applicationListDTO.setList(applicationInfoDTOs);
}
for (Application application : applications) {
ApplicationInfoDTO applicationInfoDTO = fromApplicationToInfoDTO(application);
applicationInfoDTOs.add(applicationInfoDTO);
}
applicationListDTO.setCount(applicationInfoDTOs.size());
return applicationListDTO;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApplicationInfoDTO in project carbon-apimgt by wso2.
the class GatewayUtils method convertToApplicationDto.
private static ApplicationInfoDTO convertToApplicationDto(int applicationId, SubscriptionDataStore subscriptionDataStore) {
ApplicationInfoDTO applicationInfoDTO = new ApplicationInfoDTO();
Application application = subscriptionDataStore.getApplicationById(applicationId);
if (application != null) {
applicationInfoDTO.setId(application.getId());
applicationInfoDTO.setName(application.getName());
applicationInfoDTO.setPolicy(application.getPolicy());
applicationInfoDTO.setAttributes(application.getAttributes());
applicationInfoDTO.setSubName(application.getSubName());
applicationInfoDTO.setUuid(application.getUUID());
applicationInfoDTO.setTokenType(application.getTokenType());
applicationInfoDTO.setKeys(convertToApplicationKeyMapping(applicationId, subscriptionDataStore));
}
return applicationInfoDTO;
}
Aggregations