blob: cb1cde598a0c51b3ac15e4c82343ac83132a156c [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.config;
2
margaretha1d1c73e2019-12-03 18:07:28 +01003import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
margarethaf68daa62017-09-21 02:11:24 +02006import java.io.IOException;
7import java.io.InputStream;
margaretha1d1c73e2019-12-03 18:07:28 +01008import java.io.InputStreamReader;
margarethaf68daa62017-09-21 02:11:24 +02009import java.util.ArrayList;
margarethaee0cbfe2018-08-28 17:47:14 +020010import java.util.Arrays;
margarethaf68daa62017-09-21 02:11:24 +020011import java.util.HashMap;
margaretha2df06602018-11-14 19:10:30 +010012import java.util.HashSet;
margarethaf68daa62017-09-21 02:11:24 +020013import java.util.List;
14import java.util.Map;
15import java.util.Properties;
margarethaee0cbfe2018-08-28 17:47:14 +020016import java.util.Set;
margaretha34954472018-10-24 20:05:17 +020017import java.util.regex.Pattern;
margarethacdd26e62024-07-12 12:40:26 +020018import java.util.stream.Collectors;
margarethaf68daa62017-09-21 02:11:24 +020019
margaretha46e2c952024-05-23 09:09:54 +020020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
margarethad7b7d532018-08-13 17:10:31 +020023import de.ids_mannheim.korap.util.KrillProperties;
Michael Hanl482f30d2015-09-25 12:39:46 +020024import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanlca740d72015-06-16 10:04:58 +020025import lombok.Getter;
margaretha339fd2e2018-11-13 12:14:53 +010026import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020027
28/**
margaretha35e1ca22023-11-16 22:00:01 +010029 * Describes configuration for Kustvakt by importing properties
30 * from kustvakt.conf file and setting default values if they are
31 * not configured.
margaretha398f4722019-01-09 19:07:20 +010032 *
margaretha35e1ca22023-11-16 22:00:01 +010033 * MH: if configuration class is extended, loadSubTypes method should
34 * be
Michael Hanl8abaf9e2016-05-23 16:46:35 +020035 * overriden
36 *
margaretha6ef00dd2018-09-12 14:06:38 +020037 * @author hanl
margaretha6ef00dd2018-09-12 14:06:38 +020038 * @author margaretha
Michael Hanlca740d72015-06-16 10:04:58 +020039 */
40
margaretha339fd2e2018-11-13 12:14:53 +010041@Setter
Michael Hanlca740d72015-06-16 10:04:58 +020042@Getter
43public class KustvaktConfiguration {
44
Michael Hanle17eaa52016-01-22 20:55:05 +010045 public static final Map<String, Object> KUSTVAKT_USER = new HashMap<>();
margarethac9d29412023-04-28 12:40:34 +020046 public static final String DATA_FOLDER = "data";
Michael Hanl19390652016-01-16 11:01:24 +010047
margaretha46e2c952024-05-23 09:09:54 +020048 public final static Logger log = LoggerFactory
49 .getLogger(KustvaktConfiguration.class);
50
margaretha52ee9e32019-12-11 16:36:14 +010051 private String vcInCaching;
margaretha35e1ca22023-11-16 22:00:01 +010052
Michael Hanlbadd79c2015-06-19 07:41:03 +020053 private String indexDir;
54 private int port;
margaretha19295962018-06-26 16:00:47 +020055 // todo: make exclusive so that the containg languages can really
56 // only be used then
Michael Hanlbadd79c2015-06-19 07:41:03 +020057 private List<String> queryLanguages;
Michael Hanlca740d72015-06-16 10:04:58 +020058
Michael Hanlca740d72015-06-16 10:04:58 +020059 private String serverHost;
Michael Hanlca740d72015-06-16 10:04:58 +020060
Michael Hanl482f30d2015-09-25 12:39:46 +020061 private int maxhits;
62 private int returnhits;
63 private String keystoreLocation;
64 private String keystorePassword;
Michael Hanl482f30d2015-09-25 12:39:46 +020065 private String host;
66 private String shibUserMapping;
67 private String userConfig;
68 private int inactiveTime;
69 private int loginAttemptTTL;
70 private long loginAttemptNum;
71 private boolean allowMultiLogIn;
Michael Hanl482f30d2015-09-25 12:39:46 +020072 private int loadFactor;
margaretha7cb68a12024-07-11 12:28:22 +020073
74 // EM: determine if search and match info services restricted
75 // to logged in users. This replaces @SearchResourceFilters
76 private boolean isLoginRequired;
margaretha19295962018-06-26 16:00:47 +020077
Michael Hanl482f30d2015-09-25 12:39:46 +020078 private byte[] sharedSecret;
Michael Hanl482f30d2015-09-25 12:39:46 +020079 private int longTokenTTL;
80 private int tokenTTL;
81 private int shortTokenTTL;
82 private String[] rewrite_strategies;
Michael Hanl482f30d2015-09-25 12:39:46 +020083
Michael Hanlbadd79c2015-06-19 07:41:03 +020084 private String default_pos;
margaretha4fa4b062019-01-28 19:43:30 +010085 private String default_morphology;
Michael Hanlbadd79c2015-06-19 07:41:03 +020086 private String default_lemma;
margaretha798e8bd2019-02-06 15:48:58 +010087 private String default_orthography;
Michael Hanlbadd79c2015-06-19 07:41:03 +020088 private String default_dep;
89 private String default_const;
Marc Kupietz805afde2020-03-24 09:08:50 +010090 private String apiWelcomeMessage;
margaretha798e8bd2019-02-06 15:48:58 +010091 private String defaultStructureFoundry;
margarethaa76ed242017-05-24 17:48:22 +020092 private ArrayList<String> foundries;
93 private ArrayList<String> layers;
margaretha19295962018-06-26 16:00:47 +020094
margarethaea68a0a2017-09-21 03:09:49 +020095 private String baseURL;
margarethad7b7d532018-08-13 17:10:31 +020096 private Properties properties;
margaretha19295962018-06-26 16:00:47 +020097
margaretha7926adc2018-08-30 13:45:33 +020098 private Set<String> supportedVersions;
99 private String currentVersion;
100
Michael Hanlbadd79c2015-06-19 07:41:03 +0200101 // deprec?!
102 private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
margaretha4dee07a2022-05-27 11:45:28 +0200103 private String networkEndpointURL;
margaretha35e1ca22023-11-16 22:00:01 +0100104
margaretha34954472018-10-24 20:05:17 +0200105 // license patterns
106 protected Pattern publicLicensePattern;
107 protected Pattern freeLicensePattern;
108 protected Pattern allLicensePattern;
Michael Hanlbadd79c2015-06-19 07:41:03 +0200109
margaretha339fd2e2018-11-13 12:14:53 +0100110 // random code generator
111 private String secureRandomAlgorithm;
112 private String messageDigestAlgorithm;
margaretha4fa4b062019-01-28 19:43:30 +0100113
margaretha852a0f62019-02-19 12:14:30 +0100114 // EM: metadata restriction
115 // another variable might be needed to define which metadata fields are restricted
116 private boolean isMetadataRestricted = false;
margaretha35e1ca22023-11-16 22:00:01 +0100117
margaretha14686b52021-07-26 15:24:16 +0200118 // EM: Maybe needed when we support pipe registration
margaretha137d4ee2019-12-13 14:49:15 +0100119 @Deprecated
margaretha1d1c73e2019-12-03 18:07:28 +0100120 public static Map<String, String> pipes = new HashMap<>();
margaretha35e1ca22023-11-16 22:00:01 +0100121
margaretha7926adc2018-08-30 13:45:33 +0200122 public KustvaktConfiguration (Properties properties) throws Exception {
margarethaf68daa62017-09-21 02:11:24 +0200123 load(properties);
margaretha35e1ca22023-11-16 22:00:01 +0100124 // readPipesFile("pipes");
margarethad7b7d532018-08-13 17:10:31 +0200125 KrillProperties.setProp(properties);
margarethad151c302024-06-10 10:50:42 +0200126 KrillProperties.updateConfigurations(properties);
margarethaf68daa62017-09-21 02:11:24 +0200127 }
margaretha19295962018-06-26 16:00:47 +0200128
margaretha3d55b002019-03-19 12:00:44 +0100129 public KustvaktConfiguration () {}
margaretha35e1ca22023-11-16 22:00:01 +0100130
margaretha3d55b002019-03-19 12:00:44 +0100131 public void loadBasicProperties (Properties properties) {
margaretha7cb419c2024-09-18 09:31:18 +0200132 port = Integer.valueOf(properties.getProperty("server.port", "8089"));
margaretha3d55b002019-03-19 12:00:44 +0100133 baseURL = properties.getProperty("kustvakt.base.url", "/api/*");
margaretha35e1ca22023-11-16 22:00:01 +0100134 setSecureRandomAlgorithm(
135 properties.getProperty("security.secure.random.algorithm", ""));
margaretha3d55b002019-03-19 12:00:44 +0100136 setMessageDigestAlgorithm(
137 properties.getProperty("security.md.algorithm", "MD5"));
138 }
margaretha35e1ca22023-11-16 22:00:01 +0100139
Michael Hanlca740d72015-06-16 10:04:58 +0200140 /**
141 * loading of the properties and mapping to parameter variables
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200142 *
Michael Hanl482f30d2015-09-25 12:39:46 +0200143 * @param properties
Michael Hanlca740d72015-06-16 10:04:58 +0200144 * @return
margaretha19295962018-06-26 16:00:47 +0200145 * @throws Exception
Michael Hanlca740d72015-06-16 10:04:58 +0200146 */
margaretha19295962018-06-26 16:00:47 +0200147 protected void load (Properties properties) throws Exception {
margaretha3d55b002019-03-19 12:00:44 +0100148 loadBasicProperties(properties);
Marc Kupietz805afde2020-03-24 09:08:50 +0100149
margaretha35e1ca22023-11-16 22:00:01 +0100150 apiWelcomeMessage = properties.getProperty("api.welcome.message",
151 "Welcome to KorAP API!");
margaretha7926adc2018-08-30 13:45:33 +0200152 currentVersion = properties.getProperty("current.api.version", "v1.0");
margarethaa8c364b2021-02-19 13:00:31 +0100153
margaretha35e1ca22023-11-16 22:00:01 +0100154 String supportedVersions = properties
155 .getProperty("supported.api.versions", "");
156
margarethaa8c364b2021-02-19 13:00:31 +0100157 this.supportedVersions = new HashSet<>();
margaretha35e1ca22023-11-16 22:00:01 +0100158 if (!supportedVersions.isEmpty()) {
159 List<String> versionArray = Arrays
margarethacdd26e62024-07-12 12:40:26 +0200160 .stream(supportedVersions.split(",")).map(String::trim)
161 .collect(Collectors.toList());
margarethaa8c364b2021-02-19 13:00:31 +0100162 this.supportedVersions.addAll(versionArray);
163 }
margarethaf7abb362018-09-18 20:09:37 +0200164 this.supportedVersions.add(currentVersion);
margaretha7926adc2018-08-30 13:45:33 +0200165
margarethac9d29412023-04-28 12:40:34 +0200166 maxhits = Integer.valueOf(properties.getProperty("maxhits", "50000"));
margaretha35e1ca22023-11-16 22:00:01 +0100167 returnhits = Integer
168 .valueOf(properties.getProperty("returnhits", "50000"));
Michael Hanla0f3f942016-02-17 15:15:51 +0100169 indexDir = properties.getProperty("krill.indexDir", "");
margaretha35e1ca22023-11-16 22:00:01 +0100170
Michael Hanlca740d72015-06-16 10:04:58 +0200171 // server options
margaretha19295962018-06-26 16:00:47 +0200172 serverHost = String
173 .valueOf(properties.getProperty("server.host", "localhost"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200174 String queries = properties.getProperty("korap.ql", "");
Michael Hanlca740d72015-06-16 10:04:58 +0200175 String[] qls = queries.split(",");
176 queryLanguages = new ArrayList<>();
177 for (String querylang : qls)
178 queryLanguages.add(querylang.trim().toUpperCase());
Michael Hanlbadd79c2015-06-19 07:41:03 +0200179
margaretha35e1ca22023-11-16 22:00:01 +0100180 default_const = properties.getProperty("default.foundry.constituent",
181 "corenlp");
182 default_dep = properties.getProperty("default.foundry.dependency",
183 "malt");
margaretha4fa4b062019-01-28 19:43:30 +0100184 default_lemma = properties.getProperty("default.foundry.lemma", "tt");
margaretha35e1ca22023-11-16 22:00:01 +0100185 default_morphology = properties
186 .getProperty("default.foundry.morphology", "marmot");
187 default_pos = properties.getProperty("default.foundry.partOfSpeech",
188 "tt");
189 default_orthography = properties
190 .getProperty("default.foundry.orthography", "opennlp");
191 defaultStructureFoundry = properties
192 .getProperty("default.foundry.structure", "base");
Michael Hanlbadd79c2015-06-19 07:41:03 +0200193
Michael Hanl482f30d2015-09-25 12:39:46 +0200194 // security configuration
margaretha7cb68a12024-07-11 12:28:22 +0200195 isLoginRequired = Boolean
196 .valueOf(properties.getProperty("login.required", "false"));
197
margaretha19295962018-06-26 16:00:47 +0200198 inactiveTime = TimeUtils.convertTimeToSeconds(
199 properties.getProperty("security.idleTimeoutDuration", "10M"));
200 allowMultiLogIn = Boolean
201 .valueOf(properties.getProperty("security.multipleLogIn"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200202
margaretha19295962018-06-26 16:00:47 +0200203 loginAttemptNum = Long.parseLong(
204 properties.getProperty("security.loginAttemptNum", "3"));
205 loginAttemptTTL = TimeUtils.convertTimeToSeconds(
206 properties.getProperty("security.authAttemptTTL", "30M"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200207
margaretha19295962018-06-26 16:00:47 +0200208 loadFactor = Integer.valueOf(
209 properties.getProperty("security.encryption.loadFactor", "15"));
margaretha19295962018-06-26 16:00:47 +0200210
margaretha35e1ca22023-11-16 22:00:01 +0100211 sharedSecret = properties.getProperty("security.sharedSecret", "")
212 .getBytes();
Michael Hanl482f30d2015-09-25 12:39:46 +0200213
margaretha19295962018-06-26 16:00:47 +0200214 longTokenTTL = TimeUtils.convertTimeToSeconds(
215 properties.getProperty("security.longTokenTTL", "100D"));
216 tokenTTL = TimeUtils.convertTimeToSeconds(
217 properties.getProperty("security.tokenTTL", "72H"));
218 shortTokenTTL = TimeUtils.convertTimeToSeconds(
219 properties.getProperty("security.shortTokenTTL", "3H"));
margaretha35e1ca22023-11-16 22:00:01 +0100220
margaretha4dee07a2022-05-27 11:45:28 +0200221 // network endpoint
margaretha35e1ca22023-11-16 22:00:01 +0100222 networkEndpointURL = properties.getProperty("network.endpoint.url", "");
Michael Hanlca740d72015-06-16 10:04:58 +0200223 }
margaretha35e1ca22023-11-16 22:00:01 +0100224
margaretha137d4ee2019-12-13 14:49:15 +0100225 @Deprecated
margaretha1d1c73e2019-12-03 18:07:28 +0100226 public void readPipesFile (String filename) throws IOException {
227 File file = new File(filename);
228 if (file.exists()) {
229 BufferedReader br = new BufferedReader(
230 new InputStreamReader(new FileInputStream(file)));
231
232 String line = null;
margaretha35e1ca22023-11-16 22:00:01 +0100233 while ((line = br.readLine()) != null) {
margaretha1d1c73e2019-12-03 18:07:28 +0100234 String[] parts = line.split("\t");
margaretha35e1ca22023-11-16 22:00:01 +0100235 if (parts.length != 2) {
margaretha1d1c73e2019-12-03 18:07:28 +0100236 continue;
237 }
margaretha35e1ca22023-11-16 22:00:01 +0100238 else {
margaretha1d1c73e2019-12-03 18:07:28 +0100239 pipes.put(parts[0], parts[1]);
240 }
241 }
242 br.close();
243 }
244 }
Michael Hanlca740d72015-06-16 10:04:58 +0200245
Michael Hanlbadd79c2015-06-19 07:41:03 +0200246 /**
247 * set properties
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200248 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200249 * @param props
margaretha19295962018-06-26 16:00:47 +0200250 * @throws IOException
Michael Hanlbadd79c2015-06-19 07:41:03 +0200251 */
margaretha19295962018-06-26 16:00:47 +0200252 // public void setProperties (Properties props) throws IOException
253 // {
254 // this.load(props);
255 // }
Michael Hanlca740d72015-06-16 10:04:58 +0200256
Michael Hanlbadd79c2015-06-19 07:41:03 +0200257 /**
258 * properties can be overloaded after spring init
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200259 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200260 * @param stream
margaretha7926adc2018-08-30 13:45:33 +0200261 * @throws Exception
Michael Hanlbadd79c2015-06-19 07:41:03 +0200262 */
margaretha7926adc2018-08-30 13:45:33 +0200263 public void setPropertiesAsStream (InputStream stream) throws Exception {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200264 try {
Michael Hanld6eadd52015-11-11 18:30:33 +0100265
Michael Hanlbadd79c2015-06-19 07:41:03 +0200266 Properties p = new Properties();
267 p.load(stream);
268 this.load(p);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200269 }
270 catch (IOException e) {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200271 e.printStackTrace();
272 }
273
274 }
275
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200276 public BACKENDS chooseBackend (String value) {
Michael Hanlca740d72015-06-16 10:04:58 +0200277 if (value == null || value.equals("null"))
278 return DEFAULT_ENGINE;
279 else
280 return Enum.valueOf(BACKENDS.class, value.toUpperCase());
281 }
282
Michael Hanlca740d72015-06-16 10:04:58 +0200283 public enum BACKENDS {
margaretha4dee07a2022-05-27 11:45:28 +0200284 NEO4J, LUCENE, NETWORK
Michael Hanlca740d72015-06-16 10:04:58 +0200285 }
286
287}