[DEV] update with new interface of archidata and karso

This commit is contained in:
Edouard DUPIN 2023-05-28 23:54:00 +02:00
parent f4d2b550a2
commit f7979dcbed
8 changed files with 205 additions and 57 deletions

View File

@ -4,11 +4,9 @@
<artifactId>karanage</artifactId> <artifactId>karanage</artifactId>
<version>0.2.3</version> <version>0.2.3</version>
<properties> <properties>
<maven.compiler.version>3.1</maven.compiler.version> <maven.compiler.version>3.1</maven.compiler.version>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target> <maven.compiler.target>17</maven.compiler.target>
<maven.dependency.version>3.1.1</maven.dependency.version> <maven.dependency.version>3.1.1</maven.dependency.version>
</properties> </properties>
<repositories> <repositories>
@ -22,7 +20,13 @@
<dependency> <dependency>
<groupId>kangaroo-and-rabbit</groupId> <groupId>kangaroo-and-rabbit</groupId>
<artifactId>archidata</artifactId> <artifactId>archidata</artifactId>
<version>0.3.4</version> <version>0.3.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
<!--<scope>test</scope>-->
</dependency> </dependency>
</dependencies> </dependencies>
@ -30,6 +34,11 @@
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test/src</testSourceDirectory> <testSourceDirectory>test/src</testSourceDirectory>
<directory>${project.basedir}/out/maven/</directory> <directory>${project.basedir}/out/maven/</directory>
<resources>
<resource>
<directory>src/resources</directory>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>

View File

@ -15,59 +15,72 @@ import org.kar.karanage.api.LogResource;
import org.kar.karanage.api.StateHistoryResource; import org.kar.karanage.api.StateHistoryResource;
import org.kar.karanage.api.StateResource; import org.kar.karanage.api.StateResource;
import org.kar.karanage.api.UserResource; 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.GlobalConfiguration;
import org.kar.archidata.SqlWrapper;
import org.kar.archidata.UpdateJwtPublicKey; import org.kar.archidata.UpdateJwtPublicKey;
import org.kar.archidata.catcher.ExceptionCatcher; import org.kar.archidata.catcher.ExceptionCatcher;
import org.kar.archidata.catcher.FailExceptionCatcher; import org.kar.archidata.catcher.FailExceptionCatcher;
import org.kar.archidata.catcher.InputExceptionCatcher; import org.kar.archidata.catcher.InputExceptionCatcher;
import org.kar.archidata.catcher.SystemExceptionCatcher; import org.kar.archidata.catcher.SystemExceptionCatcher;
import org.kar.archidata.filter.AuthenticationFilter;
import org.kar.archidata.filter.CORSFilter; import org.kar.archidata.filter.CORSFilter;
import org.kar.archidata.filter.OptionFilter; import org.kar.archidata.filter.OptionFilter;
import org.kar.archidata.migration.MigrationEngine;
import org.kar.archidata.util.ConfigBaseVariable; import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.karanage.model.StateHistory; import org.slf4j.LoggerFactory;
import org.kar.karanage.model.StateInstant; import org.slf4j.Logger;
import org.kar.karanage.model.DataLog;
import org.kar.karanage.model.Group;
public class WebLauncher { 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() { private static URI getBaseURI() {
return UriBuilder.fromUri(ConfigBaseVariable.getlocalAddress()).build(); return UriBuilder.fromUri(ConfigBaseVariable.getlocalAddress()).build();
} }
public static void main(String[] args) { public void migrateDB() throws Exception {
ConfigBaseVariable.bdDatabase = "karanage"; WebLauncher.LOGGER.info("Create migration engine");
MigrationEngine migrationEngine = new MigrationEngine();
// generate the BDD: WebLauncher.LOGGER.info("Add initialization");
try { migrationEngine.setInit(new Initialization());
String out = ""; WebLauncher.LOGGER.info("Add migration since last version");
out += SqlWrapper.createTable(StateInstant.class); // NOTHING for now
out += SqlWrapper.createTable(StateHistory.class); WebLauncher.LOGGER.info("Migrate the DB [START]");
out += SqlWrapper.createTable(Group.class); migrationEngine.migrate(GlobalConfiguration.dbConfig);
out += SqlWrapper.createTable(DataLog.class); WebLauncher.LOGGER.info("Migrate the DB [STOP]");
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 // Configure resources
// =================================================================== // ===================================================================
ResourceConfig rc = new ResourceConfig(); ResourceConfig rc = new ResourceConfig();
// add multipart models .. // add multipart models ..
rc.register(new MultiPartFeature()); rc.register(MultiPartFeature.class);
// global authentication system // global authentication system
rc.register(new OptionFilter()); rc.register(OptionFilter.class);
// remove cors ==> all time called by an other system... // remove cors ==> all time called by an other system...
rc.register(new CORSFilter()); rc.register(CORSFilter.class);
// global authentication system // global authentication system
rc.registerClasses(AuthenticationFilter.class); rc.register(KaranageAuthenticationFilter.class);
// register exception catcher // register exception catcher
rc.register(InputExceptionCatcher.class); rc.register(InputExceptionCatcher.class);
rc.register(SystemExceptionCatcher.class); rc.register(SystemExceptionCatcher.class);
@ -75,14 +88,14 @@ public class WebLauncher {
rc.register(ExceptionCatcher.class); rc.register(ExceptionCatcher.class);
// add default resource: // add default resource:
rc.registerClasses(UserResource.class); rc.register(UserResource.class);
rc.registerClasses(StateResource.class); rc.register(StateResource.class);
rc.registerClasses(StateHistoryResource.class); rc.register(StateHistoryResource.class);
rc.registerClasses(LogResource.class); rc.register(LogResource.class);
rc.registerClasses(HealthCheck.class); rc.register(HealthCheck.class);
rc.registerClasses(Front.class); rc.register(Front.class);
// add jackson to be discovenr when we are ins standalone server // add jackson to be discovenr when we are ins standalone server
rc.register(JacksonFeature.class); rc.register(JacksonFeature.class);
@ -109,8 +122,8 @@ public class WebLauncher {
// =================================================================== // ===================================================================
// start periodic update of the token ... // start periodic update of the token ...
// =================================================================== // ===================================================================
UpdateJwtPublicKey keyUpdater = new UpdateJwtPublicKey(); this.keyUpdater = new UpdateJwtPublicKey();
keyUpdater.start(); this.keyUpdater.start();
// =================================================================== // ===================================================================
// run JERSEY // run JERSEY
@ -118,12 +131,13 @@ public class WebLauncher {
try { try {
server.start(); server.start();
System.out.println("Jersey app started at " + getBaseURI()); System.out.println("Jersey app started at " + getBaseURI());
System.out.println("Press CTRL^C to exit..");
Thread.currentThread().join();
} catch (Exception e) { } catch (Exception e) {
System.out.println("There was an error while starting Grizzly HTTP server."); System.out.println("There was an error while starting Grizzly HTTP server.");
e.printStackTrace(); e.printStackTrace();
} }
}
public void stopOther() {
keyUpdater.kill(); keyUpdater.kill();
try { try {
keyUpdater.join(4000, 0); keyUpdater.join(4000, 0);

View File

@ -1,16 +1,38 @@
package org.kar.karanage; package org.kar.karanage;
import org.kar.archidata.util.ConfigBaseVariable; 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() {} private WebLauncherLocal() {}
public static void main(String[] args) throws InterruptedException { 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) { if (true) {
// for local test: // for local test:
ConfigBaseVariable.apiAdress = "http://0.0.0.0:20080/karanage/api/"; 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();
} }
} }

View File

@ -211,7 +211,7 @@ public class LogResource {
throw new InputException("group", "url: /log/{group}/... ==> Unknown group name"); throw new InputException("group", "url: /log/{group}/... ==> Unknown group name");
} }
//System.out.println("receive to insert : " + listDataToInsert.size()); //System.out.println("receive to insert : " + listDataToInsert.size());
DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig); DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connection.setAutoCommit(false); entry.connection.setAutoCommit(false);
try { try {
String query = "INSERT INTO log (`create_date`, `group`, `system`, `clientUuid`, `clientId`, `data`) VALUES (?, ?, ?, ?, ?, ?)"; String query = "INSERT INTO log (`create_date`, `group`, `system`, `clientUuid`, `clientId`, `data`) VALUES (?, ?, ?, ?, ?, ?)";

View File

@ -1,8 +1,13 @@
package org.kar.karanage.api; package org.kar.karanage.api;
import org.kar.archidata.filter.GenericContext; import org.kar.archidata.filter.GenericContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kar.archidata.model.User; import org.kar.archidata.model.User;
import org.kar.karanage.model.UserKaranage; import org.kar.karanage.model.UserKaranage;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.kar.archidata.SqlWrapper; import org.kar.archidata.SqlWrapper;
import org.kar.archidata.annotation.security.RolesAllowed; import org.kar.archidata.annotation.security.RolesAllowed;
@ -16,7 +21,19 @@ import java.util.List;
@Path("/users") @Path("/users")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class UserResource { 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() { public UserResource() {
} }
@ -42,7 +59,7 @@ public class UserResource {
System.out.println("getUser " + userId); System.out.println("getUser " + userId);
GenericContext gc = (GenericContext) sc.getUserPrincipal(); GenericContext gc = (GenericContext) sc.getUserPrincipal();
System.out.println("==================================================="); System.out.println("===================================================");
System.out.println("== USER ? " + gc.user); System.out.println("== USER ? " + gc.userByToken.name);
System.out.println("==================================================="); System.out.println("===================================================");
try { try {
return SqlWrapper.get(UserKaranage.class, userId); return SqlWrapper.get(UserKaranage.class, userId);
@ -53,17 +70,14 @@ public class UserResource {
return null; return null;
} }
// curl http://localhost:9993/api/users/3
@GET @GET
@Path("me") @Path("me")
@RolesAllowed("USER") @RolesAllowed("USER")
public User getMe(@Context SecurityContext sc) { public UserOut getMe(@Context SecurityContext sc) {
System.out.println("getMe()"); logger.debug("getMe()");
GenericContext gc = (GenericContext) sc.getUserPrincipal(); GenericContext gc = (GenericContext) sc.getUserPrincipal();
System.out.println("==================================================="); logger.debug("== USER ? {}", gc.userByToken);
System.out.println("== USER ? " + gc.user); return new UserOut(gc.userByToken.id, gc.userByToken.name);
System.out.println("===================================================");
return gc.user;
} }
} }

View File

@ -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");
}
}

View File

@ -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');
""");
}
}

View File

@ -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