Search in sources :

Example 1 with BytesResource

use of nl.nn.adapterframework.core.BytesResource in project iaf by ibissource.

the class LiquibaseMigrator method getChangeLog.

private Resource getChangeLog() {
    AppConstants appConstants = AppConstants.getInstance(getApplicationContext().getClassLoader());
    String changeLogFile = appConstants.getString("liquibase.changeLogFile", "DatabaseChangelog.xml");
    URL resource = getResource(changeLogFile);
    if (resource == null) {
        String msg = "unable to find database changelog file [" + changeLogFile + "]";
        msg += " classLoader [" + getConfigurationClassLoader() + "]";
        log.debug(msg);
        return null;
    }
    try {
        return new BytesResource(resource.openStream(), changeLogFile);
    } catch (IOException e) {
        log.debug("unable to open or read changelog [" + changeLogFile + "]", e);
    }
    return null;
}
Also used : BytesResource(nl.nn.adapterframework.core.BytesResource) IOException(java.io.IOException) URL(java.net.URL) AppConstants(nl.nn.adapterframework.util.AppConstants)

Example 2 with BytesResource

use of nl.nn.adapterframework.core.BytesResource 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)

Aggregations

IOException (java.io.IOException)2 BytesResource (nl.nn.adapterframework.core.BytesResource)2 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 Configuration (nl.nn.adapterframework.configuration.Configuration)1 DatabaseMigratorBase (nl.nn.adapterframework.jdbc.migration.DatabaseMigratorBase)1 AppConstants (nl.nn.adapterframework.util.AppConstants)1 StringBuilderWriter (org.apache.commons.io.output.StringBuilderWriter)1