1818import com .hubsante .hub .service .ClientPropertiesRegistry ;
1919import com .hubsante .model .EdxlHandler ;
2020import com .hubsante .model .Validator ;
21- import com .univocity .parsers .common .ParsingContext ;
22- import com .univocity .parsers .common .processor .ObjectRowProcessor ;
23- import com .univocity .parsers .csv .CsvParser ;
24- import com .univocity .parsers .csv .CsvParserSettings ;
2521import io .micrometer .core .aop .TimedAspect ;
2622import io .micrometer .core .instrument .MeterRegistry ;
2723import jakarta .annotation .PostConstruct ;
2824import java .io .*;
2925import java .nio .charset .StandardCharsets ;
3026import java .util .*;
31- import java .util .stream .Collectors ;
3227import lombok .extern .slf4j .Slf4j ;
3328import org .springframework .beans .factory .annotation .Autowired ;
3429import org .springframework .beans .factory .annotation .Value ;
4035@ Configuration
4136public class HubConfiguration {
4237
43- private static final int ROW_LENGTH = 11 ;
4438 private static final String DATA_DIVIDER = "," ;
4539 private static final String COLUMN_DIVIDER = ";" ;
4640
47- private static final StructuredLogger structuredLog = new StructuredLogger (log );
48-
49- @ Value ("${client.preferences.file}" )
50- private File configFile ;
51-
5241 @ Value ("${supported.messages.file}" )
5342 private File supportedMessagesFile ;
5443
@@ -62,105 +51,19 @@ public class HubConfiguration {
6251
6352 @ Autowired private ClientPropertiesRegistry clientPropertiesRegistry ;
6453
65- private Map <String , Map <String , String >> clientsPerimeterAndVersions = new HashMap <>();
6654 private List <String > supportedMessages ;
6755
6856 @ PostConstruct
6957 public void init () throws Exception {
58+ // We first get the parameterized default message TTL
59+ defaultTTL = Long .parseLong (this .ttlProperty );
7060
71- try {
72- // We first get the parameterized default message TTL
73- defaultTTL = Long .parseLong (this .ttlProperty );
74-
75- // We explicitly set the Locale to ensure cross platform consistency
76- Locale .setDefault (Locale .ENGLISH );
77-
78- // We define a custom row processor to read the config file
79- // we override the rowProcessed method on the fly to store the config in a HashMap
80- // then we define the parser settings and parse the file
81- ObjectRowProcessor clientPreferencesRowProcessor =
82- new ObjectRowProcessor () {
83- @ Override
84- public void rowProcessed (Object [] objects , ParsingContext parsingContext ) {
85- if (objects .length != ROW_LENGTH ) {
86- log .warn (
87- "There were more than {} columns in the client preferences file, extra columns are being ignored" ,
88- ROW_LENGTH );
89- }
90- String [] items = Arrays .asList (objects ).toArray (new String [ROW_LENGTH ]);
91- }
92- };
93- CsvParserSettings parserSettings = new CsvParserSettings ();
94- parserSettings .getFormat ().setLineSeparator ("\n " );
95- parserSettings .getFormat ().setDelimiter (';' );
96- parserSettings .setHeaderExtractionEnabled (true );
97- parserSettings .setNullValue ("" );
98- parserSettings .setProcessor (clientPreferencesRowProcessor );
99-
100- CsvParser parser = new CsvParser (parserSettings );
101- parser .parse (new BufferedReader (new FileReader (configFile , StandardCharsets .UTF_8 )));
102- clientsPerimeterAndVersions = loadClientsPerimetersAndVersions ();
103- supportedMessages = loadSupportedMessages (vhost );
104- } catch (Exception e ) {
105- throw new Exception ("Could not read config file " + configFile .getAbsolutePath (), e );
106- }
107- }
108-
109- public Map <String , Map <String , String >> loadClientsPerimetersAndVersions () throws IOException {
110- Map <String , Map <String , String >> clientsPerimeterAndVersions = new HashMap <>();
111- BufferedReader reader =
112- new BufferedReader (new FileReader (configFile , StandardCharsets .UTF_8 ));
113- String headerLine = reader .readLine ();
114- String [] headers = headerLine .split (COLUMN_DIVIDER );
115- int numberOfColumns = headers .length ;
116-
117- Set <String > perimeterNames =
118- Arrays .stream (Constants .Perimeter .values ())
119- .map (Constants .Perimeter ::getName )
120- .collect (Collectors .toSet ());
121-
122- Map <String , Integer > perimeterColumnIndexes = new HashMap <>();
123- for (int i = 0 ; i < numberOfColumns ; i ++) {
124- if (perimeterNames .contains (headers [i ])) {
125- perimeterColumnIndexes .put (headers [i ], i );
126- }
127- }
128- String line ;
129- while ((line = reader .readLine ()) != null ) {
130- String [] values = line .split (COLUMN_DIVIDER , -1 ); // -1 allows trailing empty strings
131-
132- if (values .length < numberOfColumns ) continue ;
133-
134- String clientId = values [0 ];
135- Map <String , String > allPerimetersVersions = new HashMap <>();
136-
137- for (Map .Entry <String , Integer > perimeterMatch : perimeterColumnIndexes .entrySet ()) {
138- String perimeterName = perimeterMatch .getKey ();
139- int columnIndex = perimeterMatch .getValue ();
140- allPerimetersVersions .put (perimeterName , values [columnIndex ]);
141- }
142-
143- clientsPerimeterAndVersions .put (clientId , allPerimetersVersions );
144- }
61+ // We explicitly set the Locale to ensure cross platform consistency
62+ Locale .setDefault (Locale .ENGLISH );
14563
146- reader .close ();
147- return clientsPerimeterAndVersions ;
64+ supportedMessages = loadSupportedMessages (vhost );
14865 }
14966
150- // public String[] getClientVersionsForPerimeter(String clientId, String perimeterName) {
151- // Map<String, String> clientPerimeterDefinition =
152- // clientsPerimeterAndVersions.getOrDefault(clientId, null);
153- // if (clientPerimeterDefinition == null) {
154- // structuredLog.warn(
155- // "ClientId was not found in clientsPerimeterAndVersions, or the variable is
156- // not initialized.",
157- // Map.of(LogConstants.RECIPIENT_ID, clientId));
158- // return null;
159- // }
160- // String versions = clientPerimeterDefinition.getOrDefault(perimeterName, null);
161- // return splitString(versions);
162- // }
163-
16467 public List <String > loadSupportedMessages (String vhost ) throws Exception {
16568 List <String > supportedMessages = new ArrayList <>();
16669 try (BufferedReader reader =
0 commit comments