blob: 5c9852064fa64b13d1ce25a1c66533525a6bc44f [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.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.io.InputStream;
margarethaf68daa62017-09-21 02:11:24 +02007import java.net.URL;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.Properties;
margarethaf68daa62017-09-21 02:11:24 +020013
14import org.apache.log4j.PropertyConfigurator;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
margarethaa76ed242017-05-24 17:48:22 +020018import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanl482f30d2015-09-25 12:39:46 +020019import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanlca740d72015-06-16 10:04:58 +020020import lombok.Getter;
Michael Hanlca740d72015-06-16 10:04:58 +020021
22/**
Michael Hanl8abaf9e2016-05-23 16:46:35 +020023 * if configuration class is extended, loadSubTypes method should be
24 * overriden
25 *
margaretha0c47c652017-04-19 18:44:40 +020026 * @author hanl, margaretha
Michael Hanlca740d72015-06-16 10:04:58 +020027 * @date 05/02/2014
28 */
29
30@Getter
31public class KustvaktConfiguration {
32
Michael Hanle17eaa52016-01-22 20:55:05 +010033 public static final Map<String, Object> KUSTVAKT_USER = new HashMap<>();
Michael Hanl19390652016-01-16 11:01:24 +010034
Michael Hanlac113e52016-01-19 15:49:20 +010035 private static final Logger jlog = LoggerFactory
Michael Hanld6eadd52015-11-11 18:30:33 +010036 .getLogger(KustvaktConfiguration.class);
Michael Hanlbadd79c2015-06-19 07:41:03 +020037 private String indexDir;
38 private int port;
39 // todo: make exclusive so that the containg languages can really only be used then
40 private List<String> queryLanguages;
Michael Hanlca740d72015-06-16 10:04:58 +020041
Michael Hanlca740d72015-06-16 10:04:58 +020042 private String serverHost;
Michael Hanlca740d72015-06-16 10:04:58 +020043 private URL issuer;
44
Michael Hanl482f30d2015-09-25 12:39:46 +020045 private int maxhits;
46 private int returnhits;
47 private String keystoreLocation;
48 private String keystorePassword;
49 private Properties mailProperties;
50 private String host;
51 private String shibUserMapping;
52 private String userConfig;
53 private int inactiveTime;
54 private int loginAttemptTTL;
55 private long loginAttemptNum;
56 private boolean allowMultiLogIn;
Michael Hanl482f30d2015-09-25 12:39:46 +020057 private int loadFactor;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020058 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020059 private int validationStringLength;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020060 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020061 private int validationEmaillength;
margaretha6d61a552018-04-10 19:26:44 +020062
Michael Hanl482f30d2015-09-25 12:39:46 +020063 private byte[] sharedSecret;
Michael Hanlcb2d3f92016-06-02 17:34:06 +020064 @Deprecated
Michael Hanl482f30d2015-09-25 12:39:46 +020065 private String adminToken;
66 private int longTokenTTL;
67 private int tokenTTL;
68 private int shortTokenTTL;
69 private String[] rewrite_strategies;
70 private String passcodeSaltField;
71
Michael Hanlbadd79c2015-06-19 07:41:03 +020072 private String default_pos;
73 private String default_lemma;
Michael Hanlf21773f2015-10-16 23:02:31 +020074 private String default_token;
Michael Hanlbadd79c2015-06-19 07:41:03 +020075 private String default_dep;
76 private String default_const;
margarethaa76ed242017-05-24 17:48:22 +020077 private ArrayList<String> foundries;
78 private ArrayList<String> layers;
79
margarethaea68a0a2017-09-21 03:09:49 +020080 private String baseURL;
81
margarethaa76ed242017-05-24 17:48:22 +020082
Michael Hanlbadd79c2015-06-19 07:41:03 +020083 // deprec?!
84 private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
85
margarethaf68daa62017-09-21 02:11:24 +020086 public KustvaktConfiguration (Properties properties) throws IOException {
87 load(properties);
88 }
89
Michael Hanlca740d72015-06-16 10:04:58 +020090 /**
91 * loading of the properties and mapping to parameter variables
Michael Hanl8abaf9e2016-05-23 16:46:35 +020092 *
Michael Hanl482f30d2015-09-25 12:39:46 +020093 * @param properties
Michael Hanlca740d72015-06-16 10:04:58 +020094 * @return
margarethaa76ed242017-05-24 17:48:22 +020095 * @throws IOException
96 * @throws KustvaktException
Michael Hanlca740d72015-06-16 10:04:58 +020097 */
margaretha56e8e552017-12-05 16:31:21 +010098 protected void load (Properties properties)
margarethaa76ed242017-05-24 17:48:22 +020099 throws IOException {
margarethaea68a0a2017-09-21 03:09:49 +0200100 baseURL = properties.getProperty("kustvakt.base.url", "/api/*");
Michael Hanl482f30d2015-09-25 12:39:46 +0200101 maxhits = new Integer(properties.getProperty("maxhits", "50000"));
102 returnhits = new Integer(properties.getProperty("returnhits", "50000"));
Michael Hanla0f3f942016-02-17 15:15:51 +0100103 indexDir = properties.getProperty("krill.indexDir", "");
Michael Hanl2760cc42015-11-16 19:30:01 +0100104 port = new Integer(properties.getProperty("server.port", "8095"));
Michael Hanlca740d72015-06-16 10:04:58 +0200105 // server options
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200106 serverHost = String.valueOf(properties.getProperty("server.host",
107 "localhost"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200108 String queries = properties.getProperty("korap.ql", "");
Michael Hanlca740d72015-06-16 10:04:58 +0200109 String[] qls = queries.split(",");
110 queryLanguages = new ArrayList<>();
111 for (String querylang : qls)
112 queryLanguages.add(querylang.trim().toUpperCase());
margaretha6b3ecdd2018-03-01 18:23:56 +0100113 String is = properties.getProperty("security.jwt.issuer", "");
Michael Hanl19390652016-01-16 11:01:24 +0100114
115 if (!is.startsWith("http"))
116 is = "http://" + is;
117 issuer = new URL(is);
Michael Hanlbadd79c2015-06-19 07:41:03 +0200118
Michael Hanl482f30d2015-09-25 12:39:46 +0200119 default_const = properties
margaretha6fca7292018-03-13 13:19:00 +0100120 .getProperty("default.layer.constituent", "mate");
121 default_dep = properties.getProperty("default.layer.dependency", "mate");
122 default_lemma = properties.getProperty("default.layer.lemma", "tt");
123 default_pos = properties.getProperty("default.layer.partOfSpeech", "tt");
124 default_token = properties.getProperty("default.layer.orthography",
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200125 "opennlp");
Michael Hanlbadd79c2015-06-19 07:41:03 +0200126
Michael Hanl482f30d2015-09-25 12:39:46 +0200127 // security configuration
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200128 inactiveTime = TimeUtils.convertTimeToSeconds(properties.getProperty(
129 "security.idleTimeoutDuration", "10M"));
130 allowMultiLogIn = Boolean.valueOf(properties
131 .getProperty("security.multipleLogIn"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200132
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200133 loginAttemptNum = Long.parseLong(properties.getProperty(
134 "security.loginAttemptNum", "3"));
135 loginAttemptTTL = TimeUtils.convertTimeToSeconds(properties
136 .getProperty("security.authAttemptTTL", "30M"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200137
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200138 loadFactor = Integer.valueOf(properties.getProperty(
139 "security.encryption.loadFactor", "15"));
140 validationStringLength = Integer.valueOf(properties.getProperty(
141 "security.validation.stringLength", "150"));
142 validationEmaillength = Integer.valueOf(properties.getProperty(
143 "security.validation.emailLength", "40"));
margaretha6d61a552018-04-10 19:26:44 +0200144
Michael Hanl482f30d2015-09-25 12:39:46 +0200145 sharedSecret = properties.getProperty("security.sharedSecret", "")
146 .getBytes();
147 adminToken = properties.getProperty("security.adminToken");
148
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200149 longTokenTTL = TimeUtils.convertTimeToSeconds(properties.getProperty(
150 "security.longTokenTTL", "100D"));
151 tokenTTL = TimeUtils.convertTimeToSeconds(properties.getProperty(
152 "security.tokenTTL", "72H"));
153 shortTokenTTL = TimeUtils.convertTimeToSeconds(properties.getProperty(
154 "security.shortTokenTTL", "3H"));
Michael Hanl482f30d2015-09-25 12:39:46 +0200155
margaretha6fca7292018-03-13 13:19:00 +0100156// passcodeSaltField = properties.getProperty("security.passcode.salt",
157// "accountCreation");
margaretha5af82372017-02-16 14:22:58 +0100158
Michael Hanlca740d72015-06-16 10:04:58 +0200159 }
160
Michael Hanlbadd79c2015-06-19 07:41:03 +0200161 /**
162 * set properties
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200163 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200164 * @param props
margarethaa76ed242017-05-24 17:48:22 +0200165 * @throws IOException
Michael Hanlbadd79c2015-06-19 07:41:03 +0200166 */
margarethaf68daa62017-09-21 02:11:24 +0200167// public void setProperties (Properties props) throws IOException {
168// this.load(props);
169// }
Michael Hanlca740d72015-06-16 10:04:58 +0200170
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200171
Michael Hanlbadd79c2015-06-19 07:41:03 +0200172 /**
173 * properties can be overloaded after spring init
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200174 *
Michael Hanlbadd79c2015-06-19 07:41:03 +0200175 * @param stream
176 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200177 public void setPropertiesAsStream (InputStream stream) {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200178 try {
Michael Hanld6eadd52015-11-11 18:30:33 +0100179
Michael Hanlbadd79c2015-06-19 07:41:03 +0200180 Properties p = new Properties();
181 p.load(stream);
182 this.load(p);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200183 }
184 catch (IOException e) {
Michael Hanlbadd79c2015-06-19 07:41:03 +0200185 e.printStackTrace();
186 }
187
188 }
189
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200190
191 public BACKENDS chooseBackend (String value) {
Michael Hanlca740d72015-06-16 10:04:58 +0200192 if (value == null || value.equals("null"))
193 return DEFAULT_ENGINE;
194 else
195 return Enum.valueOf(BACKENDS.class, value.toUpperCase());
196 }
197
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200198
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200199
200 public static void loadLogger () {
201 InputStream stream = ConfigLoader.loadConfigStream("log4j.properties");
202 PropertyConfigurator.configure(stream);
203 jlog.info("Done loading logging framework Log4j!");
204 }
205
206
207
208 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200209 public static void loadLog4jLogger () {
Michael Hanl1e18cb42015-08-06 20:57:35 +0200210 /** loadSubTypes log4j configuration file programmatically */
Michael Hanlca740d72015-06-16 10:04:58 +0200211 Properties log4j = new Properties();
212 try {
Michael Hanl19390652016-01-16 11:01:24 +0100213 File f = new File(System.getProperty("user.dir"),
214 "log4j.properties");
Michael Hanld6eadd52015-11-11 18:30:33 +0100215 if (f.exists()) {
216 log4j.load(new FileInputStream(f));
Michael Hanlca740d72015-06-16 10:04:58 +0200217 PropertyConfigurator.configure(log4j);
Michael Hanl4b42baa2015-11-17 21:46:04 +0100218 jlog.info(
Michael Hanlca740d72015-06-16 10:04:58 +0200219 "using local logging properties file ({}) to configure logging system",
Michael Hanl38191512015-11-17 16:48:59 +0100220 "./log4j.properties");
Michael Hanl482f30d2015-09-25 12:39:46 +0200221 return;
222 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200223 }
224 catch (Exception e) {
Michael Hanl482f30d2015-09-25 12:39:46 +0200225 // do nothing
Michael Hanlca740d72015-06-16 10:04:58 +0200226 }
Michael Hanl482f30d2015-09-25 12:39:46 +0200227 loadClassLogger();
Michael Hanlca740d72015-06-16 10:04:58 +0200228 }
229
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200230
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200231 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200232 private static void loadClassLogger () {
Michael Hanlca740d72015-06-16 10:04:58 +0200233 Properties log4j = new Properties();
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200234 jlog.info("using class path logging properties file to configure logging system");
Michael Hanlca740d72015-06-16 10:04:58 +0200235
236 try {
237 log4j.load(KustvaktConfiguration.class.getClassLoader()
238 .getResourceAsStream("log4j.properties"));
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200239 }
240 catch (IOException e) {
Michael Hanlca740d72015-06-16 10:04:58 +0200241 // do nothing
242 }
243
244 PropertyConfigurator.configure(log4j);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200245 jlog.warn("No logger properties detected. Using default logger properties");
Michael Hanlca740d72015-06-16 10:04:58 +0200246 }
247
248 public enum BACKENDS {
249 NEO4J, LUCENE
250 }
251
252}