use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class ModulesRestController method uploadUpgrades.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Upload upgrade zip bundle, to be installed on restart", notes = "The bundle can be downloaded from the Mango Store")
@RequestMapping(method = RequestMethod.POST, value = "/upload-upgrades")
public void uploadUpgrades(@ApiParam(value = "Restart after upload completes", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart, MultipartHttpServletRequest multipartRequest) throws IOException {
synchronized (UPLOAD_UPGRADE_LOCK) {
if (UPLOAD_UPGRADE_IN_PROGRESS == null) {
UPLOAD_UPGRADE_IN_PROGRESS = new Object();
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.upgradeUploadInProgress"));
}
}
try {
List<MultipartFile> files = new ArrayList<>();
MultiValueMap<String, MultipartFile> filemap = multipartRequest.getMultiFileMap();
for (String nameField : filemap.keySet()) {
files.addAll(filemap.get(nameField));
}
// Validate the zip
if (files.size() == 0)
throw new BadRequestException(new TranslatableMessage("rest.error.noFileProvided"));
// Create the temp directory into which to download, if necessary.
File tempDir = new File(Common.MA_HOME, ModuleUtils.DOWNLOAD_DIR);
if (!tempDir.exists())
tempDir.mkdirs();
// Delete anything that is currently the temp directory.
FileUtils.cleanDirectory(tempDir);
try {
// Save the upload(s) to the temp dir
for (MultipartFile file : files) {
File newFile = new File(tempDir, file.getOriginalFilename());
try (FileOutputStream fos = new FileOutputStream(newFile)) {
org.springframework.util.StreamUtils.copy(file.getInputStream(), fos);
}
}
String[] potentialUpgrades = tempDir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith(".zip"))
return true;
else
return false;
}
});
boolean didUpgrade = false;
for (String potentialUpgrade : potentialUpgrades) {
File file = new File(tempDir, potentialUpgrade);
boolean core = false;
boolean hasWebModules = false;
try (FileInputStream fis = new FileInputStream(file)) {
// Test to see if it is a core or a bundle of only zips or many zip files
try (ZipInputStream is = new ZipInputStream(fis)) {
ZipEntry entry = is.getNextEntry();
if (entry == null) {
// Not a zip file or empty, either way we don't care
throw new BadRequestException(new TranslatableMessage("rest.error.badUpgradeFile"));
} else {
do {
if ("release.signed".equals(entry.getName())) {
core = true;
break;
} else if (entry.getName().startsWith(WEB_MODULE_PREFIX)) {
hasWebModules = true;
}
} while ((entry = is.getNextEntry()) != null);
}
}
}
if (core) {
// move file to core directory
Files.move(file, new File(coreDir, "m2m2-core-upgrade.zip"));
didUpgrade = true;
} else if (hasWebModules) {
// This is a zip with modules in web/modules move them all out into the MA_HOME/web/modules dir
try (FileInputStream fis = new FileInputStream(file)) {
try (ZipInputStream is = new ZipInputStream(fis)) {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
if (entry.getName().startsWith(WEB_MODULE_PREFIX)) {
File newModule = new File(coreDir, entry.getName());
try (FileOutputStream fos = new FileOutputStream(newModule)) {
org.springframework.util.StreamUtils.copy(is, fos);
}
didUpgrade = true;
}
}
}
}
} else {
// if its a module move it to the modules folder
if (potentialUpgrade.startsWith(ModuleUtils.Constants.MODULE_PREFIX)) {
// Its extra work but we better check that it is a module from the store:
didUpgrade = maybeCopyModule(file);
} else {
// Is this a zip of modules?
try (FileInputStream fis = new FileInputStream(file)) {
try (ZipInputStream is = new ZipInputStream(fis)) {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
if (entry.getName().startsWith(ModuleUtils.Constants.MODULE_PREFIX)) {
// Extract it and confirm it is a module
File newModule = new File(tempDir, entry.getName());
try (FileOutputStream fos = new FileOutputStream(newModule)) {
org.springframework.util.StreamUtils.copy(is, fos);
}
didUpgrade = maybeCopyModule(newModule);
}
}
}
}
}
}
}
// Ensure we have some upgrades
if (!didUpgrade)
throw new BadRequestException(new TranslatableMessage("rest.error.invalidUpgradeFile"));
} finally {
FileUtils.deleteDirectory(tempDir);
}
} finally {
// Release the lock
synchronized (UPLOAD_UPGRADE_LOCK) {
UPLOAD_UPGRADE_IN_PROGRESS = null;
}
}
if (restart)
ModulesDwr.scheduleRestart();
}
use of com.infiniteautomation.mango.rest.v2.exception.BadRequestException in project ma-modules-public by infiniteautomation.
the class JsonDataRestController method mergeNode.
JsonNode mergeNode(final JsonNode existingData, final String[] dataPath, final JsonNode newData) {
JsonNode destination = getNode(existingData, dataPath);
JsonNode mergedNode;
if (destination.isObject()) {
// object merge
if (!newData.isObject()) {
throw new BadRequestException(new TranslatableMessage("rest.error.cantMergeNodeTypeIntoObject", newData.getNodeType()));
}
ObjectNode destinationObject = (ObjectNode) destination;
Iterator<Entry<String, JsonNode>> it = newData.fields();
while (it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
destinationObject.set(entry.getKey(), entry.getValue());
}
mergedNode = destinationObject;
} else if (destination.isArray()) {
// append operation
ArrayNode destinationArray = (ArrayNode) destination;
destinationArray.add(newData);
mergedNode = destinationArray;
} else {
throw new BadRequestException(new TranslatableMessage("rest.error.cantMergeIntoX", destination.getNodeType()));
}
return mergedNode;
}
Aggregations