use of org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication 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.store.v1.models.ExportedApplication 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;
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication in project carbon-apimgt by wso2.
the class ExportUtils method exportApplication.
/**
* Export a given Application to a file system as zip archive.
* The export root location is given by {@link @path}/exported-application.
*
* @param exportApplication Application{@link Application} to be exported
* @param apiConsumer API Consumer
* @param exportFormat Format to export
* @param withKeys Export the Application with keys or not
* @return Path to the exported directory with exported artifacts
* @throws APIManagementException If an error occurs while exporting an application to a file system
*/
public static File exportApplication(Application exportApplication, APIConsumer apiConsumer, ExportFormat exportFormat, Boolean withKeys) throws APIManagementException {
String archivePath = null;
String exportApplicationBasePath;
String appName = exportApplication.getName();
String appOwner = exportApplication.getOwner();
try {
// Creates a temporary directory to store the exported application artifact
File exportFolder = createTempApplicationDirectory(appName, appOwner);
exportApplicationBasePath = exportFolder.toString();
archivePath = exportApplicationBasePath.concat(File.separator + appOwner.replace(File.separator, "#") + "-" + appName);
} catch (APIImportExportException e) {
throw new APIManagementException("Unable to create the temporary directory to export the Application", e);
}
ExportedApplication applicationDtoToExport = createApplicationDTOToExport(exportApplication, apiConsumer, withKeys);
try {
createDirectory(archivePath);
// Export application details
CommonUtil.writeDtoToFile(archivePath + File.separator + ImportExportConstants.TYPE_APPLICATION, exportFormat, ImportExportConstants.TYPE_APPLICATION, applicationDtoToExport);
CommonUtil.archiveDirectory(exportApplicationBasePath);
FileUtils.deleteQuietly(new File(exportApplicationBasePath));
return new File(exportApplicationBasePath + APIConstants.ZIP_FILE_EXTENSION);
} catch (IOException | APIImportExportException e) {
throw new APIManagementException("Error while exporting Application: " + exportApplication.getName(), e);
}
}
Aggregations