Search in sources :

Example 1 with DatabaseMigratorBase

use of nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase in project iaf by ibissource.

the class ShowLiquibaseScript method getConfigurations.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/jdbc/liquibase")
@Produces(MediaType.APPLICATION_JSON)
public Response getConfigurations() throws ApiException {
    List<String> configNames = new ArrayList<String>();
    for (Configuration config : getIbisManager().getConfigurations()) {
        DatabaseMigratorBase databaseMigrator = config.getBean("jdbcMigrator", DatabaseMigratorBase.class);
        if (databaseMigrator.hasMigrationScript()) {
            configNames.add(config.getName());
        }
    }
    HashMap<String, Object> resultMap = new HashMap<>();
    resultMap.put("configurationsWithLiquibaseScript", configNames);
    return Response.status(Response.Status.OK).entity(resultMap).build();
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DatabaseMigratorBase(nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with DatabaseMigratorBase

use of nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase in project iaf by ibissource.

the class ShowLiquibaseScript method generateSQL.

@POST
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/jdbc/liquibase")
@Produces(MediaType.APPLICATION_JSON)
public Response generateSQL(MultipartBody inputDataMap) throws ApiException {
    Response.ResponseBuilder response = Response.noContent();
    InputStream file = null;
    if (inputDataMap.getAttachment("file") != null) {
        file = resolveTypeFromMap(inputDataMap, "file", InputStream.class, null);
    }
    String configuration = resolveStringFromMap(inputDataMap, "configuration", null);
    if (configuration == null && file == null) {
        return response.status(Response.Status.BAD_REQUEST).build();
    }
    Writer writer = new StringBuilderWriter();
    Configuration config = getIbisManager().getConfiguration(configuration);
    try {
        DatabaseMigratorBase databaseMigrator = config.getBean("jdbcMigrator", DatabaseMigratorBase.class);
        if (file != null) {
            String filename = inputDataMap.getAttachment("file").getContentDisposition().getParameter("filename");
            if (filename.endsWith(".xml")) {
                databaseMigrator.update(writer, new BytesResource(file, filename));
            } else {
                try (ZipInputStream stream = new ZipInputStream(file)) {
                    ZipEntry entry;
                    while ((entry = stream.getNextEntry()) != null) {
                        databaseMigrator.update(writer, new BytesResource(StreamUtil.dontClose(stream), entry.getName()));
                    }
                }
            }
        } else {
            databaseMigrator.update(writer);
        }
    } catch (Exception e) {
        throw new ApiException("Error generating SQL script", e);
    }
    String result = writer.toString();
    if (StringUtils.isEmpty(result)) {
        throw new ApiException("Make sure liquibase xml script exists for configuration [" + configuration + "]");
    }
    HashMap<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", result);
    return Response.status(Response.Status.CREATED).entity(resultMap).build();
}
Also used : StringBuilderWriter(org.apache.commons.io.output.StringBuilderWriter) Configuration(nl.nn.adapterframework.configuration.Configuration) BytesResource(nl.nn.adapterframework.core.BytesResource) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) DatabaseMigratorBase(nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Response(javax.ws.rs.core.Response) ZipInputStream(java.util.zip.ZipInputStream) StringBuilderWriter(org.apache.commons.io.output.StringBuilderWriter) Writer(java.io.Writer) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with DatabaseMigratorBase

use of nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase in project iaf by ibissource.

the class ShowLiquibaseScript method downloadScript.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/jdbc/liquibase/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadScript() throws ApiException {
    List<Configuration> configurations = new ArrayList<Configuration>();
    for (Configuration config : getIbisManager().getConfigurations()) {
        DatabaseMigratorBase databaseMigrator = config.getBean("jdbcMigrator", DatabaseMigratorBase.class);
        if (databaseMigrator.hasMigrationScript()) {
            configurations.add(config);
        }
    }
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(OutputStream out) throws IOException, WebApplicationException {
            try (ZipOutputStream zos = new ZipOutputStream(out)) {
                for (Configuration configuration : configurations) {
                    AppConstants appConstants = AppConstants.getInstance(configuration.getClassLoader());
                    String changeLogFile = appConstants.getString("liquibase.changeLogFile", "DatabaseChangelog.xml");
                    try (InputStream file = configuration.getClassLoader().getResourceAsStream(changeLogFile)) {
                        if (file != null) {
                            ZipEntry entry = new ZipEntry(changeLogFile);
                            zos.putNextEntry(entry);
                            zos.write(StreamUtil.streamToByteArray(file, false));
                            zos.closeEntry();
                        }
                    }
                }
            } catch (IOException e) {
                throw new ApiException("Failed to create zip file with scripts.", e);
            }
        }
    };
    return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"DatabaseChangelog.zip\"").build();
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) DatabaseMigratorBase(nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase) ZipOutputStream(java.util.zip.ZipOutputStream) AppConstants(nl.nn.adapterframework.util.AppConstants) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RolesAllowed (javax.annotation.security.RolesAllowed)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Configuration (nl.nn.adapterframework.configuration.Configuration)3 DatabaseMigratorBase (nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 GET (javax.ws.rs.GET)2 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 BytesResource (nl.nn.adapterframework.core.BytesResource)1