blob: 87f08380627af6116c8d97238e82d673ac7a971d [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.config;
2
margarethaf68daa62017-09-21 02:11:24 +02003import java.io.IOException;
4import java.io.InputStream;
margarethaf68daa62017-09-21 02:11:24 +02005import java.util.ArrayList;
margarethaee0cbfe2018-08-28 17:47:14 +02006import java.util.Arrays;
margarethaf68daa62017-09-21 02:11:24 +02007import java.util.HashMap;
margaretha2df06602018-11-14 19:10:30 +01008import java.util.HashSet;
margarethaf68daa62017-09-21 02:11:24 +02009import java.util.List;
10import java.util.Map;
11import java.util.Properties;
margarethaee0cbfe2018-08-28 17:47:14 +020012import java.util.Set;
margaretha34954472018-10-24 20:05:17 +020013import java.util.regex.Pattern;
margarethaf68daa62017-09-21 02:11:24 +020014
margarethad7b7d532018-08-13 17:10:31 +020015import de.ids_mannheim.korap.util.KrillProperties;
Michael Hanl482f30d2015-09-25 12:39:46 +020016import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanlca740d72015-06-16 10:04:58 +020017import lombok.Getter;
margaretha339fd2e2018-11-13 12:14:53 +010018import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020019
20/**
margaretha398f4722019-01-09 19:07:20 +010021 * Describes configuration for Kustvakt by importing properties
22 * from kustvakt.conf file and setting default values if they are
23 * not configured.
24 *
25 * MH: if configuration class is extended, loadSubTypes method should be
Michael Hanl8abaf9e2016-05-23 16:46:35 +020026 * overriden
27 *
margaretha6ef00dd2018-09-12 14:06:38 +020028 * @author hanl
margaretha6ef00dd2018-09-12 14:06:38 +020029 * @author margaretha
Michael Hanlca740d72015-06-16 10:04:58 +020030 */
31
margaretha339fd2e2018-11-13 12:14:53 +010032@Setter
Michael Hanlca740d72015-06-16 10:04:58 +020033@Getter
34public class KustvaktConfiguration {
35
Michael Hanle17eaa52016-01-22 20:55:05 +010036 public static final Map<String, Object> KUSTVAKT_USER = new HashMap<>();
Michael Hanl19390652016-01-16 11:01:24 +010037
Michael Hanlbadd79c2015-06-19 07:41:03 +020038 private String indexDir;
39 private int port;
margaretha19295962018-06-26 16:00:47 +020040 // todo: make exclusive so that the containg languages can really
41 // only be used then
Michael Hanlbadd79c2015-06-19 07:41:03 +020042 private List<String> queryLanguages;
Michael Hanlca740d72015-06-16 10:04:58 +020043
Michael Hanlca740d72015-06-16 10:04:58 +020044 private String serverHost;
Michael Hanlca740d72015-06-16 10:04:58 +020045
Michael Hanl482f30d2015-09-25 12:39:46 +020046 private int maxhits;
47 private int returnhits;
48 private String keystoreLocation;
49 private String keystorePassword;
50 private Properties mailProperties;
51 private String host;
52 private String shibUserMapping;
53 private String userConfig;
54 private int inactiveTime;
55 private int loginAttemptTTL;
56 private long loginAttemptNum;
57 private boolean allowMultiLogIn;
Michael Hanl482f30d2015-09-25 12:39:46 +020058 private int loadFactor;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020059 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020060 private int validationStringLength;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020061 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020062 private int validationEmaillength;
margaretha19295962018-06-26 16:00:47 +020063
Michael Hanl482f30d2015-09-25 12:39:46 +020064 private byte[] sharedSecret;
Michael Hanlcb2d3f92016-06-02 17:34:06 +020065 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020066 private String adminToken;
67 private int longTokenTTL;
68 private int tokenTTL;
69 private int shortTokenTTL;
70 private String[] rewrite_strategies;
71 private String passcodeSaltField;
72
Michael Hanlbadd79c2015-06-19 07:41:03 +020073 private String default_pos;
margaretha4fa4b062019-01-28 19:43:30 +010074 private String default_morphology;
Michael Hanlbadd79c2015-06-19 07:41:03 +020075 private String default_lemma;
margaretha798e8bd2019-02-06 15:48:58 +010076 private String default_orthography;
Michael Hanlbadd79c2015-06-19 07:41:03 +020077 private String default_dep;
78 private String default_const;
margaretha798e8bd2019-02-06 15:48:58 +010079 private String defaultStructureFoundry;
margarethaa76ed242017-05-24 17:48:22 +020080 private ArrayList<String> foundries;
81 private ArrayList<String> layers;
margaretha19295962018-06-26 16:00:47 +020082
margarethaea68a0a2017-09-21 03:09:49 +020083 private String baseURL;
margarethad7b7d532018-08-13 17:10:31 +020084 private Properties properties;
margaretha19295962018-06-26 16:00:47 +020085
margaretha7926adc2018-08-30 13:45:33 +020086 private Set<String> supportedVersions;
87 private String currentVersion;
88
Michael Hanlbadd79c2015-06-19 07:41:03 +020089 // deprec?!
90 private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
margaretha34954472018-10-24 20:05:17 +020091
92 // license patterns
93 protected Pattern publicLicensePattern;
94 protected Pattern freeLicensePattern;
95 protected Pattern allLicensePattern;
Michael Hanlbadd79c2015-06-19 07:41:03 +020096
margaretha339fd2e2018-11-13 12:14:53 +010097 // random code generator
98 private String secureRandomAlgorithm;
99 private String messageDigestAlgorithm;
margaretha4fa4b062019-01-28 19:43:30 +0100100
margaretha339fd2e2018-11-13 12:14:53 +0100101
margaretha7926adc2018-08-30 13:45:33 +0200102 public KustvaktConfiguration (Properties properties) throws Exception {
margarethaf68daa62017-09-21 02:11:24 +0200103 load(properties);
margarethad7b7d532018-08-13 17:10:31 +0200104 KrillProperties.setProp(properties);
margarethaf68daa62017-09-21 02:11:24 +0200105 }
margaretha19295962018-06-26 16:00:47 +0200106
Michael Hanlca740d72015-06-16 10:04:58 +0200107 /**
108 * loading of the properties and mapping to parameter variables
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109 *
Michael Hanl482f30d2015-09-25 12:39:46 +0200110 * @param properties
Michael Hanlca740d72015-06-16 10:04:58 +0200111 * @return
margaretha19295962018-06-26 16:00:47 +0200112 * @throws Exception
Michael Hanlca740d72015-06-16 10:04:58 +0200113 */
margaretha19295962018-06-26 16:00:47 +0200114 protected void load (Properties properties) throws Exception {
margaretha7926adc2018-08-30 13:45:33 +0200115 currentVersion = properties.getProperty("current.api.version", "v1.0");
116 String supportedVersions =
117 properties.getProperty("supported.api.version", "");
margaretha2df06602018-11-14 19:10:30 +0100118
119 this.supportedVersions = new HashSet<>(Arrays.asList(supportedVersions.split(" ")));
margarethaf7abb362018-09-18 20:09:37 +0200120 this.supportedVersions.add(currentVersion);
margaretha7926adc2018-08-30 13:45:33 +0200121
margarethaea68a0a2017-09-21 03:09:49 +0200122 baseURL = properties.getProperty("kustvakt.base.url", "/api/*");
Michael Hanl482f30d2015-09-25 12:39:46 +0200123 maxhits = new Integer(properties.getProperty("maxhits", "50000"));
124 returnhits = new Integer(properties.getProperty("returnhits", "50000"));
Michael Hanla0f3f942016-02-17 15:15:51 +0100125 indexDir = properties.getProperty("krill.indexDir", "");
Michael Hanl2760cc42015-11-16 19:30:01 +0100126 port = new Integer(properties.getProperty("server.port", "8095"));
Michael Hanlca740d72015-06-16 10:04:58 +0200127 // server options
margaretha19295962018-06-26 16:00:47 +0200128 serverHost = String
129 .valueOf(properties.getProperty("server.host", "localhost"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200130 String queries = properties.getProperty("korap.ql", "");
Michael Hanlca740d72015-06-16 10:04:58 +0200131 String[] qls = queries.split(",");
132 queryLanguages = new ArrayList<>();
133 for (String querylang : qls)
134 queryLanguages.add(querylang.trim().toUpperCase());
Michael Hanlbadd79c2015-06-19 07:41:03 +0200135
margaretha19295962018-06-26 16:00:47 +0200136 default_const =
margaretha4fa4b062019-01-28 19:43:30 +0100137 properties.getProperty("default.foundry.constituent", "corenlp");
margaretha19295962018-06-26 16:00:47 +0200138 default_dep =
margaretha4fa4b062019-01-28 19:43:30 +0100139 properties.getProperty("default.foundry.dependency", "malt");
140 default_lemma = properties.getProperty("default.foundry.lemma", "tt");
141 default_morphology = properties.getProperty("default.foundry.morphology", "marmot");
margaretha19295962018-06-26 16:00:47 +0200142 default_pos =
margaretha4fa4b062019-01-28 19:43:30 +0100143 properties.getProperty("default.foundry.partOfSpeech", "tt");
margaretha798e8bd2019-02-06 15:48:58 +0100144 default_orthography =
margaretha4fa4b062019-01-28 19:43:30 +0100145 properties.getProperty("default.foundry.orthography", "opennlp");
margaretha798e8bd2019-02-06 15:48:58 +0100146 defaultStructureFoundry =
147 properties.getProperty("default.foundry.structure", "base");
Michael Hanlbadd79c2015-06-19 07:41:03 +0200148
Michael Hanl482f30d2015-09-25 12:39:46 +0200149 // security configuration
margaretha19295962018-06-26 16:00:47 +0200150 inactiveTime = TimeUtils.convertTimeToSeconds(
151 properties.getProperty("security.idleTimeoutDuration", "10M"));
152 allowMultiLogIn = Boolean
153 .valueOf(properties.getProperty("security.multipleLogIn"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200154
margaretha19295962018-06-26 16:00:47 +0200155 loginAttemptNum = Long.parseLong(
156 properties.getProperty("security.loginAttemptNum", "3"));
157 loginAttemptTTL = TimeUtils.convertTimeToSeconds(
158 properties.getProperty("security.authAttemptTTL", "30M"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200159
margaretha19295962018-06-26 16:00:47 +0200160 loadFactor = Integer.valueOf(
161 properties.getProperty("security.encryption.loadFactor", "15"));
162 validationStringLength = Integer.valueOf(properties
163 .getProperty("security.validation.stringLength", "150"));
164 validationEmaillength = Integer.valueOf(properties
165 .getProperty("security.validation.emailLength", "40"));
166
167 sharedSecret =
168 properties.getProperty("security.sharedSecret", "").getBytes();
Michael Hanl482f30d2015-09-25 12:39:46 +0200169 adminToken = properties.getProperty("security.adminToken");
170
margaretha19295962018-06-26 16:00:47 +0200171 longTokenTTL = TimeUtils.convertTimeToSeconds(
172 properties.getProperty("security.longTokenTTL", "100D"));
173 tokenTTL = TimeUtils.convertTimeToSeconds(
174 properties.getProperty("security.tokenTTL", "72H"));
175 shortTokenTTL = TimeUtils.convertTimeToSeconds(
176 properties.getProperty("security.shortTokenTTL", "3H"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200177
margaretha19295962018-06-26 16:00:47 +0200178 // passcodeSaltField =
179 // properties.getProperty("security.passcode.salt",
180 // "accountCreation");
margaretha339fd2e2018-11-13 12:14:53 +0100181
182 setSecureRandomAlgorithm(properties
183 .getProperty("security.secure.random.algorithm", "SHA1PRNG"));
184
185 setMessageDigestAlgorithm(
186 properties.getProperty("security.md.algorithm", "MD5"));
187
Michael Hanlca740d72015-06-16 10:04:58 +0200188 }
189
Michael Hanlbadd79c2015-06-19 07:41:03 +0200190 /**
191 * set properties
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200192 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200193 * @param props
margaretha19295962018-06-26 16:00:47 +0200194 * @throws IOException
Michael Hanlbadd79c2015-06-19 07:41:03 +0200195 */
margaretha19295962018-06-26 16:00:47 +0200196 // public void setProperties (Properties props) throws IOException
197 // {
198 // this.load(props);
199 // }
Michael Hanlca740d72015-06-16 10:04:58 +0200200
Michael Hanlbadd79c2015-06-19 07:41:03 +0200201 /**
202 * properties can be overloaded after spring init
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200203 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200204 * @param stream
margaretha7926adc2018-08-30 13:45:33 +0200205 * @throws Exception
Michael Hanlbadd79c2015-06-19 07:41:03 +0200206 */
margaretha7926adc2018-08-30 13:45:33 +0200207 public void setPropertiesAsStream (InputStream stream) throws Exception {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200208 try {
Michael Hanld6eadd52015-11-11 18:30:33 +0100209
Michael Hanlbadd79c2015-06-19 07:41:03 +0200210 Properties p = new Properties();
211 p.load(stream);
212 this.load(p);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200213 }
214 catch (IOException e) {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200215 e.printStackTrace();
216 }
217
218 }
219
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200220 public BACKENDS chooseBackend (String value) {
Michael Hanlca740d72015-06-16 10:04:58 +0200221 if (value == null || value.equals("null"))
222 return DEFAULT_ENGINE;
223 else
224 return Enum.valueOf(BACKENDS.class, value.toUpperCase());
225 }
226
Michael Hanlca740d72015-06-16 10:04:58 +0200227 public enum BACKENDS {
228 NEO4J, LUCENE
229 }
230
231}