Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 73:4b8c909cabf3
small cleanup. trying restlet 2.2-snapshot now -- feeling lucky.
author | casties |
---|---|
date | Wed, 12 Mar 2014 11:54:07 +0100 |
parents | 4c2cea836bc0 |
children | e5ff09208c28 |
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; |
18 | 34 |
35 import javax.naming.NamingEnumeration; | |
36 import javax.naming.NamingException; | |
37 import javax.naming.directory.Attribute; | |
38 import javax.naming.directory.DirContext; | |
39 import javax.naming.directory.InitialDirContext; | |
40 import javax.naming.directory.SearchControls; | |
41 import javax.naming.directory.SearchResult; | |
42 import javax.servlet.ServletContext; | |
43 | |
44 import org.apache.log4j.BasicConfigurator; | |
45 import org.apache.log4j.Logger; | |
46 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
|
47 import org.neo4j.graphdb.factory.GraphDatabaseBuilder; |
18 | 48 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
|
49 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
|
50 import org.neo4j.kernel.GraphDatabaseAPI; |
18 | 51 import org.neo4j.server.WrappingNeoServerBootstrapper; |
52 import org.restlet.Application; | |
53 import org.restlet.Context; | |
54 | |
55 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
56 | |
57 public abstract class BaseRestlet extends Application { | |
58 | |
59 public static Logger logger = Logger.getLogger(BaseRestlet.class); | |
60 | |
61 /** | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
62 * Properties holding consumer keys and secrets. |
18 | 63 */ |
64 protected Properties consumerKeys; | |
71 | 65 public String CONSUMER_KEYS_PATH = "consumerkeys.property"; |
18 | 66 public static final String CONSUMERKEYS_KEY = "annotationmanager.consumerkeys"; |
67 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
68 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
69 * 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
|
70 */ |
18 | 71 protected Properties serverConfig; |
71 | 72 public String CONFIG_PROPS_PATH = "serverconfig.property"; |
18 | 73 public static final String SERVERCONFIG_KEY = "annotationmanager.serverconfig"; |
74 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
75 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
76 * database instance; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
77 */ |
18 | 78 protected GraphDatabaseService graphDb; |
79 public static final String GRAPHDB_KEY = "annotationmanager.graphdb"; | |
80 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path"; | |
71 | 81 public String graphdbPath = "neo4j-annotation-db"; |
18 | 82 |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
83 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
84 * 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
|
85 */ |
18 | 86 protected WrappingNeoServerBootstrapper srv; |
87 public static final String GRAPHDBSRV_KEY = "annotationmanager.graphdb.srv"; | |
88 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
89 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
90 * annotation store instance. |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
91 */ |
18 | 92 protected AnnotationStore store; |
93 public static final String ANNSTORE_KEY = "annotationmanager.store"; | |
94 | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
95 /** |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
96 * 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
|
97 */ |
18 | 98 protected String ldapServerUrl; |
99 public static final String LDAP_SERVER_KEY = "annotationmanager.ldapserver.url"; | |
100 | |
50 | 101 public static final String ADMIN_USER_KEY = "annotationmanager.admin.user"; |
102 public static final String ADMIN_PASSWORD_KEY = "annotationmanager.admin.password"; | |
103 | |
18 | 104 /** |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
105 * 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
|
106 */ |
58 | 107 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
|
108 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
|
109 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
110 /** |
58 | 111 * prefix to create uris for tags in store. |
112 */ | |
113 public static String TAGS_URI_PREFIX = ""; | |
114 public static final String TAGS_URI_KEY = "annotationmanager.uris.tags"; | |
115 | |
116 /** | |
117 * prefix to create uris for persons in store. | |
118 */ | |
119 public static String PERSONS_URI_PREFIX = ""; | |
120 public static final String PERSONS_URI_KEY = "annotationmanager.uris.persons"; | |
121 | |
122 /** | |
123 * prefix to create uris for groups in store. | |
124 */ | |
125 public static String GROUPS_URI_PREFIX = ""; | |
126 public static final String GROUPS_URI_KEY = "annotationmanager.uris.groups"; | |
127 | |
59
e2f86ef9b871
make annotation uri in store configurable. fix npe with no tags.
casties
parents:
58
diff
changeset
|
128 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
|
129 |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
130 |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
131 /* (non-Javadoc) |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
132 * @see org.restlet.Application#start() |
18 | 133 */ |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
134 @Override |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
135 public synchronized void start() throws Exception { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
136 configure(getContext()); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
137 super.start(); |
18 | 138 } |
139 | |
140 /** | |
141 * Configures the restlet. | |
142 * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb. | |
143 * Uses config from webapp context if already initialized. | |
71 | 144 * @param context |
18 | 145 */ |
71 | 146 protected void configure(Context context) { |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
147 ConcurrentMap<String, Object> attrs = context.getAttributes(); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
148 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
|
149 if (sc != null) { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
150 if (sc.getAttribute("annotationserver.log4j.configured") == null) { |
18 | 151 // TODO: is this the right place to run the log4j configurator? |
152 BasicConfigurator.configure(); | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
153 sc.setAttribute("annotationserver.log4j.configured", "done"); |
18 | 154 } |
155 logger.info(getVersion() + " starting..."); | |
156 | |
157 /* | |
158 * read config from webapp | |
159 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
160 serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY); |
18 | 161 if (serverConfig == null) { |
162 serverConfig = new Properties(); | |
163 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH); | |
164 if (ps != null) { | |
165 logger.debug("loading config from " + CONFIG_PROPS_PATH); | |
166 try { | |
167 serverConfig.load(ps); | |
168 /* | |
169 * read serverconfig options | |
170 */ | |
171 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath); | |
172 ldapServerUrl = serverConfig.getProperty(LDAP_SERVER_KEY, null); | |
60
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 * uri prefixes |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
175 */ |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
176 if (serverConfig.containsKey(PERSONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
177 BaseRestlet.PERSONS_URI_PREFIX = serverConfig.getProperty(PERSONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
178 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
179 if (serverConfig.containsKey(GROUPS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
180 BaseRestlet.GROUPS_URI_PREFIX = serverConfig.getProperty(GROUPS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
181 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
182 if (serverConfig.containsKey(TAGS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
183 BaseRestlet.TAGS_URI_PREFIX = serverConfig.getProperty(TAGS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
184 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
185 if (serverConfig.containsKey(ANNOTATIONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
186 AnnotationStore.ANNOTATION_URI_PREFIX = serverConfig.getProperty(ANNOTATIONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
187 } |
18 | 188 } catch (IOException e) { |
189 logger.warn("Error loading server config: ", e); | |
190 } | |
191 logger.debug("config: " + serverConfig); | |
192 } else { | |
193 logger.error("Unable to get resource " + CONFIG_PROPS_PATH); | |
194 } | |
195 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
196 sc.setAttribute(SERVERCONFIG_KEY, serverConfig); |
18 | 197 } |
198 // look for database service in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
199 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); |
18 | 200 if (graphDb == null) { |
201 /* | |
202 * open database | |
203 */ | |
204 String dbFn = getResourcePath(sc, graphdbPath); | |
205 if (dbFn != null) { | |
206 logger.debug("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
|
207 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
|
208 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
|
209 graphDb = graphDbBuilder.newGraphDatabase(); |
18 | 210 registerShutdownHook(graphDb); |
211 // store in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
212 sc.setAttribute(GRAPHDB_KEY, graphDb); |
18 | 213 // AnnotationStore |
214 store = new AnnotationStore(graphDb); | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
215 sc.setAttribute(ANNSTORE_KEY, store); |
18 | 216 // 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
|
217 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb); |
18 | 218 logger.debug("Starting DB admin server..."); |
219 // store in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
220 sc.setAttribute(GRAPHDBSRV_KEY, srv); |
18 | 221 srv.start(); |
222 } else { | |
223 logger.error("Unable to get resource " + dbFn); | |
224 } | |
22 | 225 } else { |
226 // get existing AnnotationStore | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
227 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY); |
18 | 228 } |
229 /* | |
230 * read consumerKeys from webapp | |
231 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
232 consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY); |
18 | 233 if (consumerKeys == null) { |
234 consumerKeys = new Properties(); | |
235 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); | |
236 if (ps != null) { | |
237 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH); | |
238 try { | |
239 consumerKeys.load(ps); | |
240 } catch (IOException e) { | |
241 // TODO Auto-generated catch block | |
242 e.printStackTrace(); | |
243 } | |
244 logger.debug("consumer keys: " + consumerKeys); | |
245 } else { | |
246 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH); | |
247 } | |
248 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
249 sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys); |
18 | 250 } |
251 } else { | |
252 logger.error("Unable to get ServletContext!"); | |
253 } | |
254 } | |
255 | |
256 public abstract String getVersion(); | |
257 | |
258 /** | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
259 * @return the authorizationMode |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
260 */ |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
261 public boolean isAuthorizationMode() { |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
262 return authorizationMode; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
263 } |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
264 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
265 /** |
19 | 266 * @return the store |
267 */ | |
268 public AnnotationStore getAnnotationStore() { | |
269 return store; | |
270 } | |
271 | |
272 /** | |
18 | 273 * returns consumer secret for consumer key. returns null if consumer key |
274 * doesn't exist. | |
275 * | |
276 * @param consumerKey | |
277 * @return | |
278 */ | |
279 public String getConsumerSecret(String consumerKey) { | |
280 return consumerKeys.getProperty(consumerKey); | |
281 } | |
282 | |
283 /** | |
284 * Hole den vollen Benutzernamen aus dem LDAP | |
285 * | |
286 * @param creator | |
287 * @return | |
288 */ | |
289 public String getFullNameFromLdap(String creator) { | |
290 String retString = creator; // falls nichts gefunden wird einfach den | |
291 // creator zurueckgeben | |
292 if (ldapServerUrl == null) { | |
293 return retString; | |
294 } | |
295 Hashtable<String, String> env = new Hashtable<String, String>(); | |
296 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; | |
297 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); | |
298 env.put(javax.naming.Context.PROVIDER_URL, ldapServerUrl); | |
299 | |
300 DirContext dctx; | |
301 try { | |
302 dctx = new InitialDirContext(env); | |
303 } catch (NamingException e) { | |
304 logger.error("Error in getFullNameFromLDAP!", e); | |
305 return retString; | |
306 } | |
307 | |
308 String base = "ou=people"; | |
309 | |
310 SearchControls sc = new SearchControls(); | |
311 String[] attributeFilter = { "cn", "mail" }; | |
312 sc.setReturningAttributes(attributeFilter); | |
313 sc.setSearchScope(SearchControls.SUBTREE_SCOPE); | |
314 | |
315 String filter = "(uid=" + creator + ")"; | |
316 | |
317 try { | |
318 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc); | |
319 while (results.hasMore()) { | |
320 SearchResult sr = (SearchResult) results.next(); | |
321 javax.naming.directory.Attributes attrs = sr.getAttributes(); | |
322 | |
323 Attribute attr = attrs.get("cn"); | |
324 retString = (String) attr.get(); | |
325 } | |
326 } catch (NamingException e) { | |
327 logger.error("Error in getFullNameFromLDAP!", e); | |
328 } | |
329 | |
330 try { | |
331 dctx.close(); | |
332 } catch (NamingException e) { | |
333 logger.error("Error in getFullNameFromLDAP!", e); | |
334 } | |
335 return retString; | |
336 } | |
337 | |
338 /** | |
339 * returns resource from path (in webapp) as InputStream. | |
340 * | |
341 * @param sc | |
342 * @param path | |
343 * @return | |
344 */ | |
345 protected InputStream getResourceAsStream(ServletContext sc, String path) { | |
71 | 346 InputStream ps = null; |
347 if (sc == null) { | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
348 // no servlet context -> use class loader |
71 | 349 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
350 /* Request request = new Request(Method.GET, "riap://component/ping"); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
351 Response response = getContext().getClientDispatcher().handle(request); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
352 Representation repr = response.getEntity(); */ |
71 | 353 } else { |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
354 // try path in webapp first |
71 | 355 ps = sc.getResourceAsStream(path); |
356 if (ps == null) { | |
357 // try as file | |
358 File pf = new File(sc.getRealPath(path)); | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
359 if (pf.canRead()) { |
71 | 360 logger.debug("trying file for: " + pf); |
361 try { | |
362 ps = new FileInputStream(pf); | |
363 } catch (FileNotFoundException e) { | |
364 logger.error(e); | |
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 | |
379 * appended to the base directory of the web-app. | |
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 |
390 filename = sc.getRealPath(filename); | |
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) { | |
406 logger.debug("Stopping DB admin server..."); | |
407 srv.stop(); | |
408 srv = null; | |
409 } | |
410 if (graphDb != null) { | |
411 logger.debug("Stopping DB..."); | |
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 } |