4349 |
10 Feb 17 |
nicklas |
1 |
package net.sf.basedb.relax; |
4349 |
10 Feb 17 |
nicklas |
2 |
|
4608 |
03 Oct 17 |
nicklas |
3 |
import java.io.InputStream; |
4608 |
03 Oct 17 |
nicklas |
4 |
import java.net.URL; |
4553 |
28 Aug 17 |
nicklas |
5 |
import java.util.Collections; |
4553 |
28 Aug 17 |
nicklas |
6 |
import java.util.Set; |
4553 |
28 Aug 17 |
nicklas |
7 |
|
4350 |
10 Feb 17 |
nicklas |
8 |
import javax.servlet.ServletRequest; |
4350 |
10 Feb 17 |
nicklas |
9 |
|
7036 |
10 Feb 23 |
nicklas |
10 |
import org.apache.commons.lang3.time.FastDateFormat; |
4608 |
03 Oct 17 |
nicklas |
11 |
import org.jdom2.Document; |
4349 |
10 Feb 17 |
nicklas |
12 |
import org.slf4j.Logger; |
4349 |
10 Feb 17 |
nicklas |
13 |
import org.slf4j.LoggerFactory; |
4349 |
10 Feb 17 |
nicklas |
14 |
|
4350 |
10 Feb 17 |
nicklas |
15 |
import net.sf.basedb.clients.web.Base; |
4350 |
10 Feb 17 |
nicklas |
16 |
import net.sf.basedb.core.Application; |
4608 |
03 Oct 17 |
nicklas |
17 |
import net.sf.basedb.core.ConfigurationException; |
4553 |
28 Aug 17 |
nicklas |
18 |
import net.sf.basedb.core.Include; |
4553 |
28 Aug 17 |
nicklas |
19 |
import net.sf.basedb.core.ItemQuery; |
4632 |
22 Nov 17 |
nicklas |
20 |
import net.sf.basedb.core.Permission; |
4632 |
22 Nov 17 |
nicklas |
21 |
import net.sf.basedb.core.PermissionDeniedException; |
4632 |
22 Nov 17 |
nicklas |
22 |
import net.sf.basedb.core.Role; |
4350 |
10 Feb 17 |
nicklas |
23 |
import net.sf.basedb.core.SessionControl; |
4632 |
22 Nov 17 |
nicklas |
24 |
import net.sf.basedb.core.SystemItems; |
4632 |
22 Nov 17 |
nicklas |
25 |
import net.sf.basedb.core.User; |
4577 |
19 Sep 17 |
nicklas |
26 |
import net.sf.basedb.relax.converter.DateToStringConverter; |
4577 |
19 Sep 17 |
nicklas |
27 |
import net.sf.basedb.relax.converter.StringToDateConverter; |
4608 |
03 Oct 17 |
nicklas |
28 |
import net.sf.basedb.util.FileUtil; |
4608 |
03 Oct 17 |
nicklas |
29 |
import net.sf.basedb.util.XmlUtil2; |
4349 |
10 Feb 17 |
nicklas |
30 |
|
4350 |
10 Feb 17 |
nicklas |
31 |
|
4349 |
10 Feb 17 |
nicklas |
32 |
/** |
4349 |
10 Feb 17 |
nicklas |
Global constants and utility functions for the Relax package. |
4349 |
10 Feb 17 |
nicklas |
34 |
|
4349 |
10 Feb 17 |
nicklas |
@since 1.0 |
4349 |
10 Feb 17 |
nicklas |
36 |
*/ |
4349 |
10 Feb 17 |
nicklas |
37 |
public final class Relax |
4349 |
10 Feb 17 |
nicklas |
38 |
{ |
4349 |
10 Feb 17 |
nicklas |
39 |
|
4349 |
10 Feb 17 |
nicklas |
40 |
private static final Logger logger = |
4349 |
10 Feb 17 |
nicklas |
41 |
LoggerFactory.getLogger(Relax.class); |
4349 |
10 Feb 17 |
nicklas |
42 |
|
4349 |
10 Feb 17 |
nicklas |
43 |
/** |
4349 |
10 Feb 17 |
nicklas |
The current version of this package. |
4349 |
10 Feb 17 |
nicklas |
45 |
*/ |
5979 |
24 Jun 20 |
nicklas |
46 |
public static final String VERSION = "1.7-dev"; |
4349 |
10 Feb 17 |
nicklas |
47 |
|
4350 |
10 Feb 17 |
nicklas |
48 |
/** |
4405 |
20 Mar 17 |
nicklas |
Path to the directory on the BASE file system where released data files |
4405 |
20 Mar 17 |
nicklas |
are saved. |
4405 |
20 Mar 17 |
nicklas |
51 |
*/ |
4405 |
20 Mar 17 |
nicklas |
52 |
public static final String RELEASE_DIR = "/home/Releases"; |
4405 |
20 Mar 17 |
nicklas |
53 |
|
4553 |
28 Aug 17 |
nicklas |
54 |
/** |
4553 |
28 Aug 17 |
nicklas |
Include options to make a query only return items in the currently active project. |
4553 |
28 Aug 17 |
nicklas |
Use with {@link ItemQuery#setIncludes(java.util.Collection)} |
4553 |
28 Aug 17 |
nicklas |
57 |
*/ |
4553 |
28 Aug 17 |
nicklas |
58 |
public static final Set<Include> INCLUDE_IN_CURRENT_PROJECT = Collections.singleton(Include.IN_PROJECT); |
4553 |
28 Aug 17 |
nicklas |
59 |
|
4577 |
19 Sep 17 |
nicklas |
60 |
/** |
4577 |
19 Sep 17 |
nicklas |
Default converter for date values to string values: yyyyMMdd |
4577 |
19 Sep 17 |
nicklas |
62 |
*/ |
7036 |
10 Feb 23 |
nicklas |
63 |
public static final DateToStringConverter CONVERTER_DATE_TO_STRING = new DateToStringConverter(FastDateFormat.getInstance("yyyyMMdd")); |
4405 |
20 Mar 17 |
nicklas |
64 |
|
4405 |
20 Mar 17 |
nicklas |
65 |
/** |
4577 |
19 Sep 17 |
nicklas |
Alternate converter for date values with a '-' separator: yyyy-MM-dd |
4577 |
19 Sep 17 |
nicklas |
67 |
*/ |
7036 |
10 Feb 23 |
nicklas |
68 |
public static final DateToStringConverter CONVERTER_DATE_TO_STRING_WITH_SEPARATOR = new DateToStringConverter(FastDateFormat.getInstance("yyyy-MM-dd")); |
4577 |
19 Sep 17 |
nicklas |
69 |
|
4577 |
19 Sep 17 |
nicklas |
70 |
/** |
4577 |
19 Sep 17 |
nicklas |
Default converter for string values to date values: yyyyMMdd |
4577 |
19 Sep 17 |
nicklas |
72 |
*/ |
7036 |
10 Feb 23 |
nicklas |
73 |
public static final StringToDateConverter CONVERTER_STRING_TO_DATE = new StringToDateConverter(FastDateFormat.getInstance("yyyyMMdd")); |
4577 |
19 Sep 17 |
nicklas |
74 |
|
4577 |
19 Sep 17 |
nicklas |
75 |
/** |
4577 |
19 Sep 17 |
nicklas |
Default converter for date+time values to string values: yyyyMMdd HHmm |
4577 |
19 Sep 17 |
nicklas |
77 |
*/ |
7036 |
10 Feb 23 |
nicklas |
78 |
public static final DateToStringConverter CONVERTER_DATETIME_TO_STRING = new DateToStringConverter(FastDateFormat.getInstance("yyyyMMdd HHmm")); |
4577 |
19 Sep 17 |
nicklas |
79 |
|
4577 |
19 Sep 17 |
nicklas |
80 |
/** |
4577 |
19 Sep 17 |
nicklas |
Default converter for date+time (with seconds) values to string values: yyyyMMdd HHmmss |
4577 |
19 Sep 17 |
nicklas |
82 |
*/ |
7036 |
10 Feb 23 |
nicklas |
83 |
public static final DateToStringConverter CONVERTER_TIMESTAMP_TO_STRING = new DateToStringConverter(FastDateFormat.getInstance("yyyyMMdd HHmmss")); |
4577 |
19 Sep 17 |
nicklas |
84 |
|
4577 |
19 Sep 17 |
nicklas |
85 |
/** |
4577 |
19 Sep 17 |
nicklas |
Alternate converter for date+time values with separators: yyyy-MM-dd HH:mm |
4577 |
19 Sep 17 |
nicklas |
87 |
*/ |
7036 |
10 Feb 23 |
nicklas |
88 |
public static final DateToStringConverter CONVERTER_DATETIME_TO_STRING_WITH_SEPARATOR = new DateToStringConverter(FastDateFormat.getInstance("yyyy-MM-dd HH:mm")); |
4577 |
19 Sep 17 |
nicklas |
89 |
|
4577 |
19 Sep 17 |
nicklas |
90 |
/** |
4577 |
19 Sep 17 |
nicklas |
Default converter for string values to date+time values: yyyyMMdd HHmm |
4577 |
19 Sep 17 |
nicklas |
92 |
*/ |
7036 |
10 Feb 23 |
nicklas |
93 |
public static final StringToDateConverter CONVERTER_STRING_TO_DATETIME = new StringToDateConverter(FastDateFormat.getInstance("yyyyMMdd HHmm")); |
4577 |
19 Sep 17 |
nicklas |
94 |
|
4577 |
19 Sep 17 |
nicklas |
95 |
|
4608 |
03 Oct 17 |
nicklas |
96 |
private static volatile XmlConfig rootConfig; |
4608 |
03 Oct 17 |
nicklas |
97 |
private static volatile ServerMode serverMode; |
4608 |
03 Oct 17 |
nicklas |
98 |
|
4577 |
19 Sep 17 |
nicklas |
99 |
/** |
4608 |
03 Oct 17 |
nicklas |
Get the server mode for this server. The mode affects what items that are installed, |
4608 |
03 Oct 17 |
nicklas |
various default values, etc. |
4608 |
03 Oct 17 |
nicklas |
102 |
*/ |
4608 |
03 Oct 17 |
nicklas |
103 |
public static ServerMode getServerMode() |
4608 |
03 Oct 17 |
nicklas |
104 |
{ |
4608 |
03 Oct 17 |
nicklas |
105 |
if (serverMode == null) |
4608 |
03 Oct 17 |
nicklas |
106 |
{ |
4608 |
03 Oct 17 |
nicklas |
107 |
serverMode = ServerMode.valueOf(getConfig().getConfig("server-mode", null, ServerMode.RELAX.name())); |
4608 |
03 Oct 17 |
nicklas |
108 |
} |
4608 |
03 Oct 17 |
nicklas |
109 |
return serverMode; |
4608 |
03 Oct 17 |
nicklas |
110 |
} |
4608 |
03 Oct 17 |
nicklas |
111 |
|
4608 |
03 Oct 17 |
nicklas |
112 |
/** |
4608 |
03 Oct 17 |
nicklas |
Get the Relax configuration. |
4608 |
03 Oct 17 |
nicklas |
114 |
*/ |
4608 |
03 Oct 17 |
nicklas |
115 |
public static XmlConfig getConfig() |
4608 |
03 Oct 17 |
nicklas |
116 |
{ |
4608 |
03 Oct 17 |
nicklas |
117 |
if (rootConfig == null) loadConfig(); |
4608 |
03 Oct 17 |
nicklas |
118 |
return rootConfig; |
4608 |
03 Oct 17 |
nicklas |
119 |
} |
4608 |
03 Oct 17 |
nicklas |
120 |
|
4608 |
03 Oct 17 |
nicklas |
121 |
/** |
4608 |
03 Oct 17 |
nicklas |
Force reload the Relax configuration. |
4608 |
03 Oct 17 |
nicklas |
123 |
*/ |
4608 |
03 Oct 17 |
nicklas |
124 |
public static synchronized XmlConfig reloadConfig() |
4608 |
03 Oct 17 |
nicklas |
125 |
{ |
4608 |
03 Oct 17 |
nicklas |
126 |
unloadConfig(); |
4608 |
03 Oct 17 |
nicklas |
127 |
loadConfig(); |
4608 |
03 Oct 17 |
nicklas |
128 |
return rootConfig; |
4608 |
03 Oct 17 |
nicklas |
129 |
} |
4608 |
03 Oct 17 |
nicklas |
130 |
|
4608 |
03 Oct 17 |
nicklas |
131 |
public static synchronized void unloadConfig() |
4608 |
03 Oct 17 |
nicklas |
132 |
{ |
4608 |
03 Oct 17 |
nicklas |
133 |
rootConfig = null; |
4608 |
03 Oct 17 |
nicklas |
134 |
serverMode = null; |
4608 |
03 Oct 17 |
nicklas |
135 |
} |
4608 |
03 Oct 17 |
nicklas |
136 |
|
4608 |
03 Oct 17 |
nicklas |
137 |
/** |
4350 |
10 Feb 17 |
nicklas |
Get an existing session control and check that a user is logged in. |
4350 |
10 Feb 17 |
nicklas |
139 |
*/ |
4350 |
10 Feb 17 |
nicklas |
140 |
public static SessionControl getSessionControl(ServletRequest req) |
4350 |
10 Feb 17 |
nicklas |
141 |
{ |
4350 |
10 Feb 17 |
nicklas |
142 |
return Application.getSessionControl(req.getParameter("ID"), |
4350 |
10 Feb 17 |
nicklas |
143 |
Base.WEBCLIENT_ID, |
4350 |
10 Feb 17 |
nicklas |
144 |
req.getRemoteAddr(), true); |
4350 |
10 Feb 17 |
nicklas |
145 |
} |
4608 |
03 Oct 17 |
nicklas |
146 |
|
4632 |
22 Nov 17 |
nicklas |
147 |
/** |
4632 |
22 Nov 17 |
nicklas |
Check if the logged in user is an administrator. |
4632 |
22 Nov 17 |
nicklas |
@since 1.1 |
4632 |
22 Nov 17 |
nicklas |
150 |
*/ |
4632 |
22 Nov 17 |
nicklas |
151 |
public static boolean isAdmin(SessionControl sc) |
4632 |
22 Nov 17 |
nicklas |
152 |
{ |
4632 |
22 Nov 17 |
nicklas |
153 |
boolean isAdmin = sc.getLoggedInUserId() == SystemItems.getId(User.ROOT) |
4632 |
22 Nov 17 |
nicklas |
154 |
|| sc.getActiveRoles().contains(SystemItems.getId(Role.ADMINISTRATOR)); |
4632 |
22 Nov 17 |
nicklas |
155 |
return isAdmin; |
4632 |
22 Nov 17 |
nicklas |
156 |
} |
4632 |
22 Nov 17 |
nicklas |
157 |
|
4632 |
22 Nov 17 |
nicklas |
158 |
/** |
4632 |
22 Nov 17 |
nicklas |
Throws a PermissionDeniedException if the logged in user is not an administrator |
4632 |
22 Nov 17 |
nicklas |
@since 1.1 |
4632 |
22 Nov 17 |
nicklas |
161 |
*/ |
4632 |
22 Nov 17 |
nicklas |
162 |
public static void checkIsAdmin(SessionControl sc, String message) |
4632 |
22 Nov 17 |
nicklas |
163 |
{ |
4632 |
22 Nov 17 |
nicklas |
164 |
if (!isAdmin(sc)) throw new PermissionDeniedException(Permission.USE, message); |
4632 |
22 Nov 17 |
nicklas |
165 |
} |
4632 |
22 Nov 17 |
nicklas |
166 |
|
4608 |
03 Oct 17 |
nicklas |
167 |
private static synchronized void loadConfig() |
4608 |
03 Oct 17 |
nicklas |
168 |
{ |
4608 |
03 Oct 17 |
nicklas |
169 |
URL configUrl = Relax.class.getResource("/relax-config.xml"); |
4608 |
03 Oct 17 |
nicklas |
170 |
if (rootConfig != null) return; |
4608 |
03 Oct 17 |
nicklas |
171 |
InputStream is = null; |
4608 |
03 Oct 17 |
nicklas |
172 |
try |
4608 |
03 Oct 17 |
nicklas |
173 |
{ |
4608 |
03 Oct 17 |
nicklas |
174 |
logger.debug("Loading configuration from; " + configUrl); |
4608 |
03 Oct 17 |
nicklas |
175 |
is = configUrl == null ? null : configUrl.openStream(); |
4608 |
03 Oct 17 |
nicklas |
176 |
if (is == null) |
4608 |
03 Oct 17 |
nicklas |
177 |
{ |
4608 |
03 Oct 17 |
nicklas |
178 |
throw new ConfigurationException("Can't find the configuration file. " + |
4608 |
03 Oct 17 |
nicklas |
179 |
"Make sure 'relax-config.xml' is in the CLASSPATH."); |
4608 |
03 Oct 17 |
nicklas |
180 |
} |
4608 |
03 Oct 17 |
nicklas |
181 |
|
4608 |
03 Oct 17 |
nicklas |
182 |
Document dom = XmlUtil2.getValidatedXml(configUrl, null); |
4608 |
03 Oct 17 |
nicklas |
183 |
rootConfig = new XmlConfig(dom.getRootElement(), "[relax]"); |
4608 |
03 Oct 17 |
nicklas |
184 |
} |
4608 |
03 Oct 17 |
nicklas |
185 |
catch (Exception ex) |
4608 |
03 Oct 17 |
nicklas |
186 |
{ |
4608 |
03 Oct 17 |
nicklas |
187 |
logger.error("Could not load configuration", ex); |
4608 |
03 Oct 17 |
nicklas |
188 |
throw new RuntimeException(ex); |
4608 |
03 Oct 17 |
nicklas |
189 |
} |
4608 |
03 Oct 17 |
nicklas |
190 |
finally |
4608 |
03 Oct 17 |
nicklas |
191 |
{ |
4608 |
03 Oct 17 |
nicklas |
192 |
FileUtil.close(is); |
4608 |
03 Oct 17 |
nicklas |
193 |
} |
4608 |
03 Oct 17 |
nicklas |
194 |
} |
4349 |
10 Feb 17 |
nicklas |
195 |
} |