From f7979dcbed49cc64cfc7e38e8b5674fe79fd1844 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 28 May 2023 23:54:00 +0200 Subject: [PATCH] [DEV] update with new interface of archidata and karso --- back/pom.xml | 25 ++++-- back/src/org/kar/karanage/WebLauncher.java | 88 +++++++++++-------- .../org/kar/karanage/WebLauncherLocal.java | 28 +++++- .../src/org/kar/karanage/api/LogResource.java | 2 +- .../org/kar/karanage/api/UserResource.java | 30 +++++-- .../filter/KaranageAuthenticationFilter.java | 23 +++++ .../karanage/migration/Initialization.java | 31 +++++++ back/src/resources/simplelogger.properties | 35 ++++++++ 8 files changed, 205 insertions(+), 57 deletions(-) create mode 100644 back/src/org/kar/karanage/filter/KaranageAuthenticationFilter.java create mode 100644 back/src/org/kar/karanage/migration/Initialization.java create mode 100644 back/src/resources/simplelogger.properties diff --git a/back/pom.xml b/back/pom.xml index 85eb629..b6babad 100644 --- a/back/pom.xml +++ b/back/pom.xml @@ -1,14 +1,12 @@ - 4.0.0 - org.kar - karanage - 0.2.3 - - + 4.0.0 + org.kar + karanage + 0.2.3 + 3.1 17 17 - 3.1.1 @@ -22,7 +20,13 @@ kangaroo-and-rabbit archidata - 0.3.4 + 0.3.7 + + + org.slf4j + slf4j-simple + 2.0.7 + @@ -30,6 +34,11 @@ src test/src ${project.basedir}/out/maven/ + + + src/resources + + org.apache.maven.plugins diff --git a/back/src/org/kar/karanage/WebLauncher.java b/back/src/org/kar/karanage/WebLauncher.java index cf6ff6c..d0c46fb 100755 --- a/back/src/org/kar/karanage/WebLauncher.java +++ b/back/src/org/kar/karanage/WebLauncher.java @@ -15,59 +15,72 @@ import org.kar.karanage.api.LogResource; import org.kar.karanage.api.StateHistoryResource; import org.kar.karanage.api.StateResource; import org.kar.karanage.api.UserResource; +import org.kar.karanage.filter.KaranageAuthenticationFilter; +import org.kar.karanage.migration.Initialization; import org.kar.archidata.GlobalConfiguration; -import org.kar.archidata.SqlWrapper; import org.kar.archidata.UpdateJwtPublicKey; import org.kar.archidata.catcher.ExceptionCatcher; import org.kar.archidata.catcher.FailExceptionCatcher; import org.kar.archidata.catcher.InputExceptionCatcher; import org.kar.archidata.catcher.SystemExceptionCatcher; -import org.kar.archidata.filter.AuthenticationFilter; import org.kar.archidata.filter.CORSFilter; import org.kar.archidata.filter.OptionFilter; +import org.kar.archidata.migration.MigrationEngine; import org.kar.archidata.util.ConfigBaseVariable; -import org.kar.karanage.model.StateHistory; -import org.kar.karanage.model.StateInstant; -import org.kar.karanage.model.DataLog; -import org.kar.karanage.model.Group; +import org.slf4j.LoggerFactory; +import org.slf4j.Logger; public class WebLauncher { - private WebLauncher() { - } - + final static Logger LOGGER = LoggerFactory.getLogger(WebLauncher.class); + protected UpdateJwtPublicKey keyUpdater = null; + public WebLauncher() { + ConfigBaseVariable.bdDatabase = "karanage"; + } + private static URI getBaseURI() { return UriBuilder.fromUri(ConfigBaseVariable.getlocalAddress()).build(); } - public static void main(String[] args) { - ConfigBaseVariable.bdDatabase = "karanage"; + public void migrateDB() throws Exception { + WebLauncher.LOGGER.info("Create migration engine"); + MigrationEngine migrationEngine = new MigrationEngine(); + WebLauncher.LOGGER.info("Add initialization"); + migrationEngine.setInit(new Initialization()); + WebLauncher.LOGGER.info("Add migration since last version"); + // NOTHING for now + WebLauncher.LOGGER.info("Migrate the DB [START]"); + migrationEngine.migrate(GlobalConfiguration.dbConfig); + WebLauncher.LOGGER.info("Migrate the DB [STOP]"); + } - // generate the BDD: - try { - String out = ""; - out += SqlWrapper.createTable(StateInstant.class); - out += SqlWrapper.createTable(StateHistory.class); - out += SqlWrapper.createTable(Group.class); - out += SqlWrapper.createTable(DataLog.class); - System.out.println(out); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + public static void main(String[] args) throws Exception { + WebLauncher.LOGGER.info("[START] application wake UP"); + WebLauncher launcher = new WebLauncher(); + launcher.migrateDB(); + launcher.process(); + WebLauncher.LOGGER.info("end-configure the server & wait finish process:"); + Thread.currentThread().join(); + WebLauncher.LOGGER.info("STOP Key updater"); + launcher.stopOther(); + WebLauncher.LOGGER.info("STOP the REST server:"); + } + + + public void process() throws InterruptedException { // =================================================================== // Configure resources // =================================================================== ResourceConfig rc = new ResourceConfig(); // add multipart models .. - rc.register(new MultiPartFeature()); + rc.register(MultiPartFeature.class); // global authentication system - rc.register(new OptionFilter()); + rc.register(OptionFilter.class); // remove cors ==> all time called by an other system... - rc.register(new CORSFilter()); + rc.register(CORSFilter.class); // global authentication system - rc.registerClasses(AuthenticationFilter.class); + rc.register(KaranageAuthenticationFilter.class); // register exception catcher rc.register(InputExceptionCatcher.class); rc.register(SystemExceptionCatcher.class); @@ -75,14 +88,14 @@ public class WebLauncher { rc.register(ExceptionCatcher.class); // add default resource: - rc.registerClasses(UserResource.class); - rc.registerClasses(StateResource.class); - rc.registerClasses(StateHistoryResource.class); - rc.registerClasses(LogResource.class); + rc.register(UserResource.class); + rc.register(StateResource.class); + rc.register(StateHistoryResource.class); + rc.register(LogResource.class); - rc.registerClasses(HealthCheck.class); - rc.registerClasses(Front.class); + rc.register(HealthCheck.class); + rc.register(Front.class); // add jackson to be discovenr when we are ins standalone server rc.register(JacksonFeature.class); @@ -109,8 +122,8 @@ public class WebLauncher { // =================================================================== // start periodic update of the token ... // =================================================================== - UpdateJwtPublicKey keyUpdater = new UpdateJwtPublicKey(); - keyUpdater.start(); + this.keyUpdater = new UpdateJwtPublicKey(); + this.keyUpdater.start(); // =================================================================== // run JERSEY @@ -118,12 +131,13 @@ public class WebLauncher { try { server.start(); System.out.println("Jersey app started at " + getBaseURI()); - System.out.println("Press CTRL^C to exit.."); - Thread.currentThread().join(); } catch (Exception e) { System.out.println("There was an error while starting Grizzly HTTP server."); e.printStackTrace(); } + } + + public void stopOther() { keyUpdater.kill(); try { keyUpdater.join(4000, 0); diff --git a/back/src/org/kar/karanage/WebLauncherLocal.java b/back/src/org/kar/karanage/WebLauncherLocal.java index 659f9ad..3a2bbca 100755 --- a/back/src/org/kar/karanage/WebLauncherLocal.java +++ b/back/src/org/kar/karanage/WebLauncherLocal.java @@ -1,16 +1,38 @@ package org.kar.karanage; import org.kar.archidata.util.ConfigBaseVariable; +import org.slf4j.LoggerFactory; +import org.slf4j.Logger; -public class WebLauncherLocal { +public class WebLauncherLocal extends WebLauncher { + final Logger logger = LoggerFactory.getLogger(WebLauncherLocal.class); private WebLauncherLocal() {} public static void main(String[] args) throws InterruptedException { + WebLauncherLocal launcher = new WebLauncherLocal(); + launcher.process(); + launcher.logger.info("end-configure the server & wait finish process:"); + Thread.currentThread().join(); + launcher.logger.info("STOP the REST server:"); + } + + @Override + public void process() throws InterruptedException { if (true) { // for local test: ConfigBaseVariable.apiAdress = "http://0.0.0.0:20080/karanage/api/"; - ConfigBaseVariable.ssoAdress = "https://atria-soft.org/karso/api/"; + //ConfigBaseVariable.ssoAdress = "https://atria-soft.org/karso/api/"; } - WebLauncher.main(args); + try { + super.migrateDB(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + while (true) { + logger.error("Migration fail ==> waiting intervention of administrator..."); + Thread.sleep(60*60*1000); + } + } + super.process(); } } diff --git a/back/src/org/kar/karanage/api/LogResource.java b/back/src/org/kar/karanage/api/LogResource.java index 3a30548..29bfdff 100644 --- a/back/src/org/kar/karanage/api/LogResource.java +++ b/back/src/org/kar/karanage/api/LogResource.java @@ -211,7 +211,7 @@ public class LogResource { throw new InputException("group", "url: /log/{group}/... ==> Unknown group name"); } //System.out.println("receive to insert : " + listDataToInsert.size()); - DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig); + DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); entry.connection.setAutoCommit(false); try { String query = "INSERT INTO log (`create_date`, `group`, `system`, `clientUuid`, `clientId`, `data`) VALUES (?, ?, ?, ?, ?, ?)"; diff --git a/back/src/org/kar/karanage/api/UserResource.java b/back/src/org/kar/karanage/api/UserResource.java index 10319da..a8fae92 100755 --- a/back/src/org/kar/karanage/api/UserResource.java +++ b/back/src/org/kar/karanage/api/UserResource.java @@ -1,8 +1,13 @@ package org.kar.karanage.api; import org.kar.archidata.filter.GenericContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.kar.archidata.model.User; import org.kar.karanage.model.UserKaranage; + +import com.fasterxml.jackson.annotation.JsonInclude; + import org.kar.archidata.SqlWrapper; import org.kar.archidata.annotation.security.RolesAllowed; @@ -16,7 +21,19 @@ import java.util.List; @Path("/users") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public class UserResource { + final Logger logger = LoggerFactory.getLogger(UserResource.class); + @JsonInclude(JsonInclude.Include.NON_NULL) + public class UserOut { + public long id; + public String login; + public UserOut(long id, String login) { + super(); + this.id = id; + this.login = login; + } + + } public UserResource() { } @@ -42,7 +59,7 @@ public class UserResource { System.out.println("getUser " + userId); GenericContext gc = (GenericContext) sc.getUserPrincipal(); System.out.println("==================================================="); - System.out.println("== USER ? " + gc.user); + System.out.println("== USER ? " + gc.userByToken.name); System.out.println("==================================================="); try { return SqlWrapper.get(UserKaranage.class, userId); @@ -53,17 +70,14 @@ public class UserResource { return null; } - // curl http://localhost:9993/api/users/3 @GET @Path("me") @RolesAllowed("USER") - public User getMe(@Context SecurityContext sc) { - System.out.println("getMe()"); + public UserOut getMe(@Context SecurityContext sc) { + logger.debug("getMe()"); GenericContext gc = (GenericContext) sc.getUserPrincipal(); - System.out.println("==================================================="); - System.out.println("== USER ? " + gc.user); - System.out.println("==================================================="); - return gc.user; + logger.debug("== USER ? {}", gc.userByToken); + return new UserOut(gc.userByToken.id, gc.userByToken.name); } } diff --git a/back/src/org/kar/karanage/filter/KaranageAuthenticationFilter.java b/back/src/org/kar/karanage/filter/KaranageAuthenticationFilter.java new file mode 100644 index 0000000..9a54074 --- /dev/null +++ b/back/src/org/kar/karanage/filter/KaranageAuthenticationFilter.java @@ -0,0 +1,23 @@ +package org.kar.karanage.filter; + +import org.kar.archidata.filter.AuthenticationFilter; + +import jakarta.ws.rs.Priorities; +import jakarta.ws.rs.ext.Provider; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.annotation.Priority; + +//@PreMatching +@Provider +@Priority(Priorities.AUTHENTICATION) +public class KaranageAuthenticationFilter extends AuthenticationFilter { + final Logger logger = LoggerFactory.getLogger(KaranageAuthenticationFilter.class); + + public KaranageAuthenticationFilter() { + super("karanage"); + } + +} diff --git a/back/src/org/kar/karanage/migration/Initialization.java b/back/src/org/kar/karanage/migration/Initialization.java new file mode 100644 index 0000000..88fea81 --- /dev/null +++ b/back/src/org/kar/karanage/migration/Initialization.java @@ -0,0 +1,31 @@ +package org.kar.karanage.migration; + +import org.kar.archidata.migration.MigrationSqlStep; +import org.kar.karanage.model.DataLog; +import org.kar.karanage.model.Group; +import org.kar.karanage.model.StateHistory; +import org.kar.karanage.model.StateInstant; + +public class Initialization extends MigrationSqlStep { + + public static final int KARSO_INITIALISATION_ID = 1; + + @Override + public String getName() { + return "Initialization"; + } + + public Initialization() throws Exception { + addClass(StateInstant.class); + addClass(StateHistory.class); + addClass(Group.class); + addClass(DataLog.class); + + addAction(""" + INSERT INTO `group` (`id`, `name`, `description`) VALUES + (1, 'test', 'Basic test interface'); + """); + + } + +} diff --git a/back/src/resources/simplelogger.properties b/back/src/resources/simplelogger.properties new file mode 100644 index 0000000..4314b58 --- /dev/null +++ b/back/src/resources/simplelogger.properties @@ -0,0 +1,35 @@ +# SLF4J's SimpleLogger configuration file +# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err. +# Default logging detail level for all instances of SimpleLogger. +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, defaults to "info". +org.slf4j.simpleLogger.defaultLogLevel=trace + +# Logging detail level for a SimpleLogger instance named "xxxxx". +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, the default logging detail level is used. +#org.slf4j.simpleLogger.log.xxxxx= + +# Set to true if you want the current date and time to be included in output messages. +# Default is false, and will output the number of milliseconds elapsed since startup. +#org.slf4j.simpleLogger.showDateTime=false + +# The date and time format to be used in the output messages. +# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. +# If the format is not specified or is invalid, the default format is used. +# The default format is yyyy-MM-dd HH:mm:ss:SSS Z. +#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z + +# Set to true if you want to output the current thread name. +# Defaults to true. +org.slf4j.simpleLogger.showThreadName=true + +# Set to true if you want the Logger instance name to be included in output messages. +# Defaults to true. +#org.slf4j.simpleLogger.showLogName=true + +# Set to true if you want the last component of the name to be included in output messages. +# Defaults to false. +#org.slf4j.simpleLogger.showShortLogName=false + +