Search in sources :

Example 1 with ModelExport

use of io.syndesis.common.model.ModelExport 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)));
            }
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) ClientErrorException(javax.ws.rs.ClientErrorException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Connection(io.syndesis.common.model.connection.Connection) IntegrationOverview(io.syndesis.common.model.integration.IntegrationOverview) Integration(io.syndesis.common.model.integration.Integration) ZipEntry(java.util.zip.ZipEntry) JsonDbDao(io.syndesis.server.jsondb.dao.JsonDbDao) Context(javax.ws.rs.core.Context) Connector(io.syndesis.common.model.connection.Connector) StreamingOutput(javax.ws.rs.core.StreamingOutput) ListResult(io.syndesis.common.model.ListResult) Extension(io.syndesis.common.model.extension.Extension) NotNull(javax.validation.constraints.NotNull) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) ZipOutputStream(java.util.zip.ZipOutputStream) Migrator(io.syndesis.server.jsondb.dao.Migrator) ZipInputStream(java.util.zip.ZipInputStream) GET(javax.ws.rs.GET) Step(io.syndesis.common.model.integration.Step) Kind(io.syndesis.common.model.Kind) Names(io.syndesis.common.util.Names) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StreamUtils.nonClosing(org.springframework.util.StreamUtils.nonClosing) DataManager(io.syndesis.server.dao.manager.DataManager) FileDataManager(io.syndesis.server.dao.file.FileDataManager) Api(io.swagger.annotations.Api) MemorySqlJsonDB(io.syndesis.server.jsondb.impl.MemorySqlJsonDB) LinkedHashSet(java.util.LinkedHashSet) SqlJsonDB(io.syndesis.server.jsondb.impl.SqlJsonDB) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) CloseableJsonDB(io.syndesis.server.jsondb.CloseableJsonDB) IntegrationProjectGenerator(io.syndesis.integration.api.IntegrationProjectGenerator) WithId(io.syndesis.common.model.WithId) IOException(java.io.IOException) JsonDB(io.syndesis.server.jsondb.JsonDB) ModelExport(io.syndesis.common.model.ModelExport) Dependency(io.syndesis.common.model.Dependency) Component(org.springframework.stereotype.Component) Schema(io.syndesis.common.model.Schema) IntegrationResourceManager(io.syndesis.integration.api.IntegrationResourceManager) IntegrationHandler(io.syndesis.server.endpoint.v1.handler.integration.IntegrationHandler) ChangeEvent(io.syndesis.common.model.ChangeEvent) Json(io.syndesis.common.util.Json) InputStream(java.io.InputStream) Integration(io.syndesis.common.model.integration.Integration) CloseableJsonDB(io.syndesis.server.jsondb.CloseableJsonDB) Dependency(io.syndesis.common.model.Dependency) ZipOutputStream(java.util.zip.ZipOutputStream) ClientErrorException(javax.ws.rs.ClientErrorException) ModelExport(io.syndesis.common.model.ModelExport) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with ModelExport

use of io.syndesis.common.model.ModelExport 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);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) CloseableJsonDB(io.syndesis.server.jsondb.CloseableJsonDB) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) ChangeEvent(io.syndesis.common.model.ChangeEvent) ClientErrorException(javax.ws.rs.ClientErrorException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ModelExport(io.syndesis.common.model.ModelExport) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with ModelExport

use of io.syndesis.common.model.ModelExport in project syndesis by syndesisio.

the class IntegrationSupportHandlerTest method verifyJacksonBehaviorWithSourceStreams.

@SuppressWarnings({ "PMD.UnusedLocalVariable" })
@Test
public void verifyJacksonBehaviorWithSourceStreams() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    // disabling feature inline, skipt closing source stream
    try (InputStream fis = spy(new FileInputStream(new File(classLoader.getResource("model.json").getFile())))) {
        ModelExport models = Json.reader().forType(ModelExport.class).readValue(fis);
        verify(fis, times(0)).close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) ModelExport(io.syndesis.common.model.ModelExport) Test(org.junit.Test)

Aggregations

ModelExport (io.syndesis.common.model.ModelExport)3 InputStream (java.io.InputStream)3 ChangeEvent (io.syndesis.common.model.ChangeEvent)2 CloseableJsonDB (io.syndesis.server.jsondb.CloseableJsonDB)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 ClientErrorException (javax.ws.rs.ClientErrorException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Api (io.swagger.annotations.Api)1 ApiParam (io.swagger.annotations.ApiParam)1 Dependency (io.syndesis.common.model.Dependency)1 Kind (io.syndesis.common.model.Kind)1 ListResult (io.syndesis.common.model.ListResult)1 Schema (io.syndesis.common.model.Schema)1