Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 80:4724fc7b4733
add slashes to paths for getRealPath to work with Tomcat 8.
author | casties |
---|---|
date | Mon, 21 Jul 2014 17:28:15 +0200 |
parents | 25eb2e1df106 |
children | e3f0613b2f2d |
rev | line source |
---|---|
18 | 1 package de.mpiwg.itgroup.annotations.restlet; |
2 | |
70 | 3 /* |
4 * #%L | |
5 * AnnotationManager | |
6 * %% | |
7 * Copyright (C) 2012 - 2014 MPIWG Berlin | |
8 * %% | |
9 * This program is free software: you can redistribute it and/or modify | |
10 * it under the terms of the GNU Lesser General Public License as | |
11 * published by the Free Software Foundation, either version 3 of the | |
12 * License, or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Lesser Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Lesser Public | |
20 * License along with this program. If not, see | |
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>. | |
22 * #L% | |
23 * Author: Robert Casties (casties@mpiwg-berlin.mpg.de) | |
24 */ | |
25 | |
18 | 26 import java.io.File; |
27 import java.io.FileInputStream; | |
28 import java.io.FileNotFoundException; | |
29 import java.io.IOException; | |
30 import java.io.InputStream; | |
31 import java.util.Hashtable; | |
32 import java.util.Properties; | |
71 | 33 import java.util.concurrent.ConcurrentMap; |
75 | 34 import java.util.logging.Logger; |
18 | 35 |
36 import javax.naming.NamingEnumeration; | |
37 import javax.naming.NamingException; | |
38 import javax.naming.directory.Attribute; | |
39 import javax.naming.directory.DirContext; | |
40 import javax.naming.directory.InitialDirContext; | |
41 import javax.naming.directory.SearchControls; | |
42 import javax.naming.directory.SearchResult; | |
43 import javax.servlet.ServletContext; | |
44 | |
45 import org.neo4j.graphdb.GraphDatabaseService; | |
66
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
46 import org.neo4j.graphdb.factory.GraphDatabaseBuilder; |
18 | 47 import org.neo4j.graphdb.factory.GraphDatabaseFactory; |
66
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
48 import org.neo4j.graphdb.factory.GraphDatabaseSettings; |
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
49 import org.neo4j.kernel.GraphDatabaseAPI; |
18 | 50 import org.neo4j.server.WrappingNeoServerBootstrapper; |
51 import org.restlet.Application; | |
52 import org.restlet.Context; | |
53 | |
54 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
55 | |
56 public abstract class BaseRestlet extends Application { | |
57 | |
75 | 58 public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.BaseRestlet"); |
18 | 59 |
60 /** | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
61 * Properties holding consumer keys and secrets. |
18 | 62 */ |
63 protected Properties consumerKeys; | |
71 | 64 public String CONSUMER_KEYS_PATH = "consumerkeys.property"; |
18 | 65 public static final String CONSUMERKEYS_KEY = "annotationmanager.consumerkeys"; |
66 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
67 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
68 * Properties holding server config. |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
69 */ |
18 | 70 protected Properties serverConfig; |
71 | 71 public String CONFIG_PROPS_PATH = "serverconfig.property"; |
18 | 72 public static final String SERVERCONFIG_KEY = "annotationmanager.serverconfig"; |
73 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
74 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
75 * database instance; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
76 */ |
18 | 77 protected GraphDatabaseService graphDb; |
78 public static final String GRAPHDB_KEY = "annotationmanager.graphdb"; | |
79 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path"; | |
71 | 80 public String graphdbPath = "neo4j-annotation-db"; |
18 | 81 |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
82 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
83 * database interface server instance. |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
84 */ |
18 | 85 protected WrappingNeoServerBootstrapper srv; |
86 public static final String GRAPHDBSRV_KEY = "annotationmanager.graphdb.srv"; | |
87 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
88 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
89 * annotation store instance. |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
90 */ |
18 | 91 protected AnnotationStore store; |
92 public static final String ANNSTORE_KEY = "annotationmanager.store"; | |
93 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
94 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
95 * LDAP server URI (for looking up full user names). |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
96 */ |
18 | 97 protected String ldapServerUrl; |
98 public static final String LDAP_SERVER_KEY = "annotationmanager.ldapserver.url"; | |
99 | |
50 | 100 public static final String ADMIN_USER_KEY = "annotationmanager.admin.user"; |
101 public static final String ADMIN_PASSWORD_KEY = "annotationmanager.admin.password"; | |
102 | |
18 | 103 /** |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
104 * run in authorization mode i.e. with tokens. |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
105 */ |
58 | 106 protected boolean authorizationMode = false; |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
107 public static final String AUTHORIZATION_MODE_KEY = "annotationmanager.authorization"; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
108 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
109 /** |
58 | 110 * prefix to create uris for tags in store. |
111 */ | |
112 public static String TAGS_URI_PREFIX = ""; | |
113 public static final String TAGS_URI_KEY = "annotationmanager.uris.tags"; | |
114 | |
115 /** | |
116 * prefix to create uris for persons in store. | |
117 */ | |
118 public static String PERSONS_URI_PREFIX = ""; | |
119 public static final String PERSONS_URI_KEY = "annotationmanager.uris.persons"; | |
120 | |
121 /** | |
122 * prefix to create uris for groups in store. | |
123 */ | |
124 public static String GROUPS_URI_PREFIX = ""; | |
125 public static final String GROUPS_URI_KEY = "annotationmanager.uris.groups"; | |
126 | |
59
e2f86ef9b871
make annotation uri in store configurable. fix npe with no tags.
casties
parents:
58
diff
changeset
|
127 public static final String ANNOTATIONS_URI_KEY = "annotationmanager.uris.annotations"; |
e2f86ef9b871
make annotation uri in store configurable. fix npe with no tags.
casties
parents:
58
diff
changeset
|
128 |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
129 |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
130 /* (non-Javadoc) |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
131 * @see org.restlet.Application#start() |
18 | 132 */ |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
133 @Override |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
134 public synchronized void start() throws Exception { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
135 configure(getContext()); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
136 super.start(); |
18 | 137 } |
138 | |
139 /** | |
140 * Configures the restlet. | |
141 * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb. | |
142 * Uses config from webapp context if already initialized. | |
71 | 143 * @param context |
18 | 144 */ |
71 | 145 protected void configure(Context context) { |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
146 ConcurrentMap<String, Object> attrs = context.getAttributes(); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
147 ServletContext sc = (ServletContext) attrs.get("org.restlet.ext.servlet.ServletContext"); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
148 if (sc != null) { |
75 | 149 logger = context.getLogger(); |
18 | 150 logger.info(getVersion() + " starting..."); |
151 | |
152 /* | |
153 * read config from webapp | |
154 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
155 serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY); |
18 | 156 if (serverConfig == null) { |
157 serverConfig = new Properties(); | |
158 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH); | |
159 if (ps != null) { | |
75 | 160 logger.fine("loading config from " + CONFIG_PROPS_PATH); |
18 | 161 try { |
162 serverConfig.load(ps); | |
163 /* | |
164 * read serverconfig options | |
165 */ | |
166 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath); | |
167 ldapServerUrl = serverConfig.getProperty(LDAP_SERVER_KEY, null); | |
60
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
168 /* |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
169 * uri prefixes |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
170 */ |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
171 if (serverConfig.containsKey(PERSONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
172 BaseRestlet.PERSONS_URI_PREFIX = serverConfig.getProperty(PERSONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
173 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
174 if (serverConfig.containsKey(GROUPS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
175 BaseRestlet.GROUPS_URI_PREFIX = serverConfig.getProperty(GROUPS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
176 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
177 if (serverConfig.containsKey(TAGS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
178 BaseRestlet.TAGS_URI_PREFIX = serverConfig.getProperty(TAGS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
179 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
180 if (serverConfig.containsKey(ANNOTATIONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
181 AnnotationStore.ANNOTATION_URI_PREFIX = serverConfig.getProperty(ANNOTATIONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
182 } |
18 | 183 } catch (IOException e) { |
75 | 184 logger.warning("Error loading server config: "+e.toString()); |
18 | 185 } |
75 | 186 logger.fine("config: " + serverConfig); |
18 | 187 } else { |
75 | 188 logger.severe("Unable to get resource " + CONFIG_PROPS_PATH); |
18 | 189 } |
190 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
191 sc.setAttribute(SERVERCONFIG_KEY, serverConfig); |
18 | 192 } |
193 // look for database service in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
194 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); |
18 | 195 if (graphDb == null) { |
196 /* | |
197 * open database | |
198 */ | |
199 String dbFn = getResourcePath(sc, graphdbPath); | |
200 if (dbFn != null) { | |
75 | 201 logger.fine("opening DB " + dbFn); |
66
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
202 GraphDatabaseBuilder graphDbBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(dbFn); |
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
203 graphDbBuilder.setConfig(GraphDatabaseSettings.allow_store_upgrade, "true"); |
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
204 graphDb = graphDbBuilder.newGraphDatabase(); |
18 | 205 registerShutdownHook(graphDb); |
206 // store in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
207 sc.setAttribute(GRAPHDB_KEY, graphDb); |
18 | 208 // AnnotationStore |
209 store = new AnnotationStore(graphDb); | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
210 sc.setAttribute(ANNSTORE_KEY, store); |
18 | 211 // admin server |
66
5b568de5ee0d
updated to new Neo4J version 2.0. doesn't use new features. problems with neo4j admin web ui.
casties
parents:
60
diff
changeset
|
212 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb); |
75 | 213 logger.fine("Starting DB admin server..."); |
18 | 214 // store in context |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
215 sc.setAttribute(GRAPHDBSRV_KEY, srv); |
18 | 216 srv.start(); |
217 } else { | |
75 | 218 logger.severe("Unable to get resource " + dbFn); |
18 | 219 } |
22 | 220 } else { |
221 // get existing AnnotationStore | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
222 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY); |
18 | 223 } |
224 /* | |
225 * read consumerKeys from webapp | |
226 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
227 consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY); |
18 | 228 if (consumerKeys == null) { |
229 consumerKeys = new Properties(); | |
230 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); | |
231 if (ps != null) { | |
75 | 232 logger.fine("loading consumer keys from " + CONSUMER_KEYS_PATH); |
18 | 233 try { |
234 consumerKeys.load(ps); | |
235 } catch (IOException e) { | |
236 // TODO Auto-generated catch block | |
237 e.printStackTrace(); | |
238 } | |
75 | 239 logger.fine("consumer keys: " + consumerKeys); |
18 | 240 } else { |
75 | 241 logger.severe("Unable to get resource " + CONSUMER_KEYS_PATH); |
18 | 242 } |
243 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
244 sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys); |
18 | 245 } |
246 } else { | |
75 | 247 logger.severe("Unable to get ServletContext!"); |
18 | 248 } |
249 } | |
250 | |
251 public abstract String getVersion(); | |
252 | |
253 /** | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
254 * @return the authorizationMode |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
255 */ |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
256 public boolean isAuthorizationMode() { |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
257 return authorizationMode; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
258 } |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
259 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
260 /** |
19 | 261 * @return the store |
262 */ | |
263 public AnnotationStore getAnnotationStore() { | |
264 return store; | |
265 } | |
266 | |
267 /** | |
18 | 268 * returns consumer secret for consumer key. returns null if consumer key |
269 * doesn't exist. | |
270 * | |
271 * @param consumerKey | |
272 * @return | |
273 */ | |
274 public String getConsumerSecret(String consumerKey) { | |
275 return consumerKeys.getProperty(consumerKey); | |
276 } | |
277 | |
278 /** | |
279 * Hole den vollen Benutzernamen aus dem LDAP | |
280 * | |
281 * @param creator | |
282 * @return | |
283 */ | |
284 public String getFullNameFromLdap(String creator) { | |
285 String retString = creator; // falls nichts gefunden wird einfach den | |
286 // creator zurueckgeben | |
287 if (ldapServerUrl == null) { | |
288 return retString; | |
289 } | |
290 Hashtable<String, String> env = new Hashtable<String, String>(); | |
291 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; | |
292 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); | |
293 env.put(javax.naming.Context.PROVIDER_URL, ldapServerUrl); | |
294 | |
295 DirContext dctx; | |
296 try { | |
297 dctx = new InitialDirContext(env); | |
298 } catch (NamingException e) { | |
75 | 299 logger.warning("Error in getFullNameFromLDAP! "+e); |
18 | 300 return retString; |
301 } | |
302 | |
303 String base = "ou=people"; | |
304 | |
305 SearchControls sc = new SearchControls(); | |
306 String[] attributeFilter = { "cn", "mail" }; | |
307 sc.setReturningAttributes(attributeFilter); | |
308 sc.setSearchScope(SearchControls.SUBTREE_SCOPE); | |
309 | |
310 String filter = "(uid=" + creator + ")"; | |
311 | |
312 try { | |
313 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc); | |
314 while (results.hasMore()) { | |
315 SearchResult sr = (SearchResult) results.next(); | |
316 javax.naming.directory.Attributes attrs = sr.getAttributes(); | |
317 | |
318 Attribute attr = attrs.get("cn"); | |
319 retString = (String) attr.get(); | |
320 } | |
321 } catch (NamingException e) { | |
75 | 322 logger.warning("Error in getFullNameFromLDAP!"+e); |
18 | 323 } |
324 | |
325 try { | |
326 dctx.close(); | |
327 } catch (NamingException e) { | |
75 | 328 logger.warning("Error in getFullNameFromLDAP!"+e); |
18 | 329 } |
330 return retString; | |
331 } | |
332 | |
333 /** | |
74 | 334 * returns resource from path as InputStream. |
335 * | |
336 * Tries path in webapp first, then uses classpath loader. | |
337 * Relative paths in webapp start in /WEB-INF/. | |
18 | 338 * |
339 * @param sc | |
340 * @param path | |
341 * @return | |
342 */ | |
343 protected InputStream getResourceAsStream(ServletContext sc, String path) { | |
71 | 344 InputStream ps = null; |
345 if (sc == null) { | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
346 // no servlet context -> use class loader |
71 | 347 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); |
348 } else { | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
349 // try path in webapp first |
74 | 350 String webPath = path; |
351 if (!webPath.startsWith("/")) { | |
352 // relative path in webapp starts in WEB-INF | |
80
4724fc7b4733
add slashes to paths for getRealPath to work with Tomcat 8.
casties
parents:
75
diff
changeset
|
353 webPath = "/WEB-INF/" + webPath; |
74 | 354 } |
355 ps = sc.getResourceAsStream(webPath); | |
71 | 356 if (ps == null) { |
357 // try as file | |
74 | 358 File pf = new File(sc.getRealPath(webPath)); |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
359 if (pf.canRead()) { |
75 | 360 logger.fine("trying file for: " + pf); |
71 | 361 try { |
362 ps = new FileInputStream(pf); | |
363 } catch (FileNotFoundException e) { | |
75 | 364 logger.severe(e.toString()); |
71 | 365 } |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
366 } else { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
367 // use class loader |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
368 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); |
18 | 369 } |
370 } | |
371 } | |
372 return ps; | |
373 } | |
374 | |
375 /** | |
376 * get a real file name for a web app file pathname. | |
377 * | |
378 * If filename starts with "/" its treated as absolute else the path is | |
74 | 379 * appended to the /WEB-INF/ directory in the web-app. |
18 | 380 * |
381 * @param filename | |
382 * @param sc | |
383 * @return | |
384 */ | |
385 public static String getResourcePath(ServletContext sc, String filename) { | |
386 File f = new File(filename); | |
387 // is the filename absolute? | |
71 | 388 if (!f.isAbsolute() && sc != null) { |
18 | 389 // relative path -> use getRealPath to resolve in webapp |
80
4724fc7b4733
add slashes to paths for getRealPath to work with Tomcat 8.
casties
parents:
75
diff
changeset
|
390 filename = sc.getRealPath("/WEB-INF/" + filename); |
18 | 391 } |
392 return filename; | |
393 } | |
394 | |
395 /* | |
396 * (non-Javadoc) | |
397 * | |
398 * @see org.restlet.Application#stop() | |
399 */ | |
400 @Override | |
401 public synchronized void stop() throws Exception { | |
402 /* | |
403 * trying to clean up databases, not sure if this is the right way... | |
404 */ | |
405 if (srv != null) { | |
75 | 406 logger.info("Stopping DB admin server..."); |
18 | 407 srv.stop(); |
408 srv = null; | |
409 } | |
410 if (graphDb != null) { | |
75 | 411 logger.info("Stopping DB..."); |
18 | 412 graphDb.shutdown(); |
413 graphDb = null; | |
414 } | |
415 super.stop(); | |
416 } | |
417 | |
418 private static void registerShutdownHook(final GraphDatabaseService graphDb) { | |
419 // Registers a shutdown hook for the Neo4j instance so that it | |
420 // shuts down nicely when the VM exits (even if you "Ctrl-C" the | |
421 // running example before it's completed) | |
422 Runtime.getRuntime().addShutdownHook(new Thread() { | |
423 @Override | |
424 public void run() { | |
425 graphDb.shutdown(); | |
426 } | |
427 }); | |
428 } | |
429 | |
430 } |