use of io.syndesis.server.jsondb.CloseableJsonDB in project syndesis by syndesisio.
the class IntegrationSupportHandler method export.
@GET
@Path("/export.zip")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput export(@NotNull @QueryParam("id") @ApiParam(required = true) List<String> requestedIds) throws IOException {
List<String> ids = requestedIds;
if (ids == null || ids.isEmpty()) {
throw new ClientErrorException("No 'integration' query parameter specified.", Response.Status.BAD_REQUEST);
}
LinkedHashSet<String> extensions = new LinkedHashSet<>();
CloseableJsonDB memJsonDB = MemorySqlJsonDB.create(jsonDB.getIndexes());
if (ids.contains("all")) {
ids = new ArrayList<>();
for (Integration integration : dataManager.fetchAll(Integration.class)) {
ids.add(integration.getId().get());
}
}
for (String id : ids) {
Integration integration = integrationHandler.getIntegration(id);
addToExport(memJsonDB, integration);
resourceManager.collectDependencies(integration.getSteps(), true).stream().filter(Dependency::isExtension).map(Dependency::getId).forEach(extensions::add);
}
LOG.debug("Extensions: {}", extensions);
return out -> {
try (ZipOutputStream tos = new ZipOutputStream(out)) {
ModelExport exportObject = ModelExport.of(Schema.VERSION);
addEntry(tos, EXPORT_MODEL_INFO_FILE_NAME, Json.writer().writeValueAsBytes(exportObject));
addEntry(tos, EXPORT_MODEL_FILE_NAME, memJsonDB.getAsByteArray("/"));
memJsonDB.close();
for (String extensionId : extensions) {
addEntry(tos, "extensions/" + Names.sanitize(extensionId) + ".jar", IOUtils.toByteArray(extensionDataManager.getExtensionBinaryFile(extensionId)));
}
}
};
}
use of io.syndesis.server.jsondb.CloseableJsonDB in project syndesis by syndesisio.
the class IntegrationSupportHandler method importIntegration.
@POST
@Path("/import")
@Produces(MediaType.APPLICATION_JSON)
public List<ChangeEvent> importIntegration(@Context SecurityContext sec, InputStream is) {
try (ZipInputStream zis = new ZipInputStream(is)) {
HashSet<String> extensionIds = new HashSet<>();
ArrayList<ChangeEvent> changeEvents = new ArrayList<>();
ModelExport modelExport = null;
while (true) {
ZipEntry entry = zis.getNextEntry();
if (entry == null) {
break;
}
if (EXPORT_MODEL_INFO_FILE_NAME.equals(entry.getName())) {
modelExport = Json.reader().forType(ModelExport.class).readValue(zis);
}
if (EXPORT_MODEL_FILE_NAME.equals(entry.getName())) {
CloseableJsonDB memJsonDB = MemorySqlJsonDB.create(jsonDB.getIndexes());
memJsonDB.set("/", nonClosing(zis));
changeEvents.addAll(importModels(sec, modelExport, memJsonDB));
memJsonDB.close();
for (ChangeEvent changeEvent : changeEvents) {
if (changeEvent.getKind().get().equals("extension")) {
extensionIds.add(changeEvent.getId().get());
}
}
}
// import missing extensions that were in the model.
if (entry.getName().startsWith("extensions/")) {
for (String extensionId : extensionIds) {
if (entry.getName().equals("extensions/" + Names.sanitize(extensionId) + ".jar")) {
String path = "/extensions/" + extensionId;
InputStream existing = extensionDataManager.getExtensionDataAccess().read(path);
if (existing == null) {
extensionDataManager.getExtensionDataAccess().write(path, zis);
} else {
existing.close();
}
}
}
}
zis.closeEntry();
}
if (changeEvents.isEmpty()) {
LOG.info("Could not import integration: No integration data model found.");
throw new ClientErrorException("Does not look like an integration export.", Response.Status.BAD_REQUEST);
}
return changeEvents;
} catch (IOException e) {
if (LOG.isInfoEnabled()) {
LOG.info("Could not import integration: " + e, e);
}
throw new ClientErrorException("Could not import integration: " + e, Response.Status.BAD_REQUEST, e);
}
}
Aggregations