Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java @ 86:e3f0613b2f2d
renamed getFullname to make it configurable.
fixed Restlet at 2.2.3.
author | casties |
---|---|
date | Mon, 02 Feb 2015 11:32:53 +0100 |
parents | 4724fc7b4733 |
children | 247cbbb385de |
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% | |
86 | 23 * Author: Robert Casties (casties@mpiwg-berlin.mpg.de), |
24 * Dirk Wintergruen (dwinter@mpiwg-berlin.mpg.de) | |
70 | 25 */ |
26 | |
18 | 27 import java.io.File; |
28 import java.io.FileInputStream; | |
29 import java.io.FileNotFoundException; | |
30 import java.io.IOException; | |
31 import java.io.InputStream; | |
32 import java.util.Hashtable; | |
33 import java.util.Properties; | |
71 | 34 import java.util.concurrent.ConcurrentMap; |
75 | 35 import java.util.logging.Logger; |
18 | 36 |
37 import javax.naming.NamingEnumeration; | |
38 import javax.naming.NamingException; | |
39 import javax.naming.directory.Attribute; | |
40 import javax.naming.directory.DirContext; | |
41 import javax.naming.directory.InitialDirContext; | |
42 import javax.naming.directory.SearchControls; | |
43 import javax.naming.directory.SearchResult; | |
44 import javax.servlet.ServletContext; | |
45 | |
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 | |
75 | 59 public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.BaseRestlet"); |
18 | 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 | |
86 | 101 /** |
102 * web frontend admin user name | |
103 */ | |
50 | 104 public static final String ADMIN_USER_KEY = "annotationmanager.admin.user"; |
86 | 105 /** |
106 * web frontend admin user password | |
107 */ | |
50 | 108 public static final String ADMIN_PASSWORD_KEY = "annotationmanager.admin.password"; |
109 | |
18 | 110 /** |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
111 * 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
|
112 */ |
58 | 113 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
|
114 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
|
115 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
116 /** |
58 | 117 * prefix to create uris for tags in store. |
118 */ | |
119 public static String TAGS_URI_PREFIX = ""; | |
120 public static final String TAGS_URI_KEY = "annotationmanager.uris.tags"; | |
121 | |
122 /** | |
123 * prefix to create uris for persons in store. | |
124 */ | |
125 public static String PERSONS_URI_PREFIX = ""; | |
126 public static final String PERSONS_URI_KEY = "annotationmanager.uris.persons"; | |
127 | |
128 /** | |
129 * prefix to create uris for groups in store. | |
130 */ | |
131 public static String GROUPS_URI_PREFIX = ""; | |
132 public static final String GROUPS_URI_KEY = "annotationmanager.uris.groups"; | |
133 | |
59
e2f86ef9b871
make annotation uri in store configurable. fix npe with no tags.
casties
parents:
58
diff
changeset
|
134 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
|
135 |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
136 |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
137 /* (non-Javadoc) |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
138 * @see org.restlet.Application#start() |
18 | 139 */ |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
140 @Override |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
141 public synchronized void start() throws Exception { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
142 configure(getContext()); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
143 super.start(); |
18 | 144 } |
145 | |
146 /** | |
147 * Configures the restlet. | |
148 * Reads serverConfig, consumerKeys and graphDb config from config files and starts graphDb. | |
149 * Uses config from webapp context if already initialized. | |
71 | 150 * @param context |
18 | 151 */ |
71 | 152 protected void configure(Context context) { |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
153 ConcurrentMap<String, Object> attrs = context.getAttributes(); |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
154 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
|
155 if (sc != null) { |
75 | 156 logger = context.getLogger(); |
18 | 157 logger.info(getVersion() + " starting..."); |
158 | |
159 /* | |
160 * read config from webapp | |
161 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
162 serverConfig = (Properties) sc.getAttribute(SERVERCONFIG_KEY); |
18 | 163 if (serverConfig == null) { |
164 serverConfig = new Properties(); | |
165 InputStream ps = getResourceAsStream(sc, CONFIG_PROPS_PATH); | |
166 if (ps != null) { | |
75 | 167 logger.fine("loading config from " + CONFIG_PROPS_PATH); |
18 | 168 try { |
169 serverConfig.load(ps); | |
170 /* | |
171 * read serverconfig options | |
172 */ | |
173 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath); | |
174 ldapServerUrl = serverConfig.getProperty(LDAP_SERVER_KEY, null); | |
60
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 * uri prefixes |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
177 */ |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
178 if (serverConfig.containsKey(PERSONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
179 BaseRestlet.PERSONS_URI_PREFIX = serverConfig.getProperty(PERSONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
180 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
181 if (serverConfig.containsKey(GROUPS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
182 BaseRestlet.GROUPS_URI_PREFIX = serverConfig.getProperty(GROUPS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
183 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
184 if (serverConfig.containsKey(TAGS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
185 BaseRestlet.TAGS_URI_PREFIX = serverConfig.getProperty(TAGS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
186 } |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
187 if (serverConfig.containsKey(ANNOTATIONS_URI_KEY)) { |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
188 AnnotationStore.ANNOTATION_URI_PREFIX = serverConfig.getProperty(ANNOTATIONS_URI_KEY); |
99d9afcfd04d
configuration of uri prefixes was completely broken.
casties
parents:
59
diff
changeset
|
189 } |
18 | 190 } catch (IOException e) { |
75 | 191 logger.warning("Error loading server config: "+e.toString()); |
18 | 192 } |
75 | 193 logger.fine("config: " + serverConfig); |
18 | 194 } else { |
75 | 195 logger.severe("Unable to get resource " + CONFIG_PROPS_PATH); |
18 | 196 } |
197 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
198 sc.setAttribute(SERVERCONFIG_KEY, serverConfig); |
18 | 199 } |
200 // look for database service in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
201 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); |
18 | 202 if (graphDb == null) { |
203 /* | |
204 * open database | |
205 */ | |
206 String dbFn = getResourcePath(sc, graphdbPath); | |
207 if (dbFn != null) { | |
75 | 208 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
|
209 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
|
210 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
|
211 graphDb = graphDbBuilder.newGraphDatabase(); |
18 | 212 registerShutdownHook(graphDb); |
213 // store in context | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
214 sc.setAttribute(GRAPHDB_KEY, graphDb); |
18 | 215 // AnnotationStore |
216 store = new AnnotationStore(graphDb); | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
217 sc.setAttribute(ANNSTORE_KEY, store); |
18 | 218 // 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
|
219 srv = new WrappingNeoServerBootstrapper((GraphDatabaseAPI) graphDb); |
75 | 220 logger.fine("Starting DB admin server..."); |
18 | 221 // store in context |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
222 sc.setAttribute(GRAPHDBSRV_KEY, srv); |
18 | 223 srv.start(); |
224 } else { | |
75 | 225 logger.severe("Unable to get resource " + dbFn); |
18 | 226 } |
22 | 227 } else { |
228 // get existing AnnotationStore | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
229 store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY); |
18 | 230 } |
231 /* | |
232 * read consumerKeys from webapp | |
233 */ | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
234 consumerKeys = (Properties) sc.getAttribute(CONSUMERKEYS_KEY); |
18 | 235 if (consumerKeys == null) { |
236 consumerKeys = new Properties(); | |
237 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); | |
238 if (ps != null) { | |
75 | 239 logger.fine("loading consumer keys from " + CONSUMER_KEYS_PATH); |
18 | 240 try { |
241 consumerKeys.load(ps); | |
242 } catch (IOException e) { | |
243 // TODO Auto-generated catch block | |
244 e.printStackTrace(); | |
245 } | |
75 | 246 logger.fine("consumer keys: " + consumerKeys); |
18 | 247 } else { |
75 | 248 logger.severe("Unable to get resource " + CONSUMER_KEYS_PATH); |
18 | 249 } |
250 // store config | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
251 sc.setAttribute(CONSUMERKEYS_KEY, consumerKeys); |
18 | 252 } |
253 } else { | |
75 | 254 logger.severe("Unable to get ServletContext!"); |
18 | 255 } |
256 } | |
257 | |
258 public abstract String getVersion(); | |
259 | |
260 /** | |
57
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
261 * @return the authorizationMode |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
262 */ |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
263 public boolean isAuthorizationMode() { |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
264 return authorizationMode; |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
265 } |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
266 |
4efb21cf0ce0
new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents:
50
diff
changeset
|
267 /** |
19 | 268 * @return the store |
269 */ | |
270 public AnnotationStore getAnnotationStore() { | |
271 return store; | |
272 } | |
273 | |
274 /** | |
18 | 275 * returns consumer secret for consumer key. returns null if consumer key |
276 * doesn't exist. | |
277 * | |
278 * @param consumerKey | |
279 * @return | |
280 */ | |
281 public String getConsumerSecret(String consumerKey) { | |
282 return consumerKeys.getProperty(consumerKey); | |
283 } | |
284 | |
285 /** | |
86 | 286 * Return the full name (String) of the person with the given user-id. |
287 * | |
288 * Contacts a naming service (currently LDAP). | |
289 * | |
290 * @param userId | |
291 * @return full-name | |
292 */ | |
293 public String getFullNameForId(String userId) { | |
294 return getFullNameFromLdap(userId); | |
295 } | |
296 | |
297 /** | |
18 | 298 * Hole den vollen Benutzernamen aus dem LDAP |
299 * | |
300 * @param creator | |
301 * @return | |
302 */ | |
303 public String getFullNameFromLdap(String creator) { | |
304 String retString = creator; // falls nichts gefunden wird einfach den | |
305 // creator zurueckgeben | |
306 if (ldapServerUrl == null) { | |
307 return retString; | |
308 } | |
309 Hashtable<String, String> env = new Hashtable<String, String>(); | |
310 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; | |
311 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); | |
312 env.put(javax.naming.Context.PROVIDER_URL, ldapServerUrl); | |
313 | |
314 DirContext dctx; | |
315 try { | |
316 dctx = new InitialDirContext(env); | |
317 } catch (NamingException e) { | |
75 | 318 logger.warning("Error in getFullNameFromLDAP! "+e); |
18 | 319 return retString; |
320 } | |
321 | |
322 String base = "ou=people"; | |
323 | |
324 SearchControls sc = new SearchControls(); | |
325 String[] attributeFilter = { "cn", "mail" }; | |
326 sc.setReturningAttributes(attributeFilter); | |
327 sc.setSearchScope(SearchControls.SUBTREE_SCOPE); | |
328 | |
329 String filter = "(uid=" + creator + ")"; | |
330 | |
331 try { | |
332 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc); | |
333 while (results.hasMore()) { | |
334 SearchResult sr = (SearchResult) results.next(); | |
335 javax.naming.directory.Attributes attrs = sr.getAttributes(); | |
336 | |
337 Attribute attr = attrs.get("cn"); | |
338 retString = (String) attr.get(); | |
339 } | |
340 } catch (NamingException e) { | |
75 | 341 logger.warning("Error in getFullNameFromLDAP!"+e); |
18 | 342 } |
343 | |
344 try { | |
345 dctx.close(); | |
346 } catch (NamingException e) { | |
75 | 347 logger.warning("Error in getFullNameFromLDAP!"+e); |
18 | 348 } |
349 return retString; | |
350 } | |
351 | |
352 /** | |
74 | 353 * returns resource from path as InputStream. |
354 * | |
355 * Tries path in webapp first, then uses classpath loader. | |
356 * Relative paths in webapp start in /WEB-INF/. | |
18 | 357 * |
358 * @param sc | |
359 * @param path | |
360 * @return | |
361 */ | |
362 protected InputStream getResourceAsStream(ServletContext sc, String path) { | |
71 | 363 InputStream ps = null; |
364 if (sc == null) { | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
365 // no servlet context -> use class loader |
71 | 366 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); |
367 } else { | |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
368 // try path in webapp first |
74 | 369 String webPath = path; |
370 if (!webPath.startsWith("/")) { | |
371 // 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
|
372 webPath = "/WEB-INF/" + webPath; |
74 | 373 } |
374 ps = sc.getResourceAsStream(webPath); | |
71 | 375 if (ps == null) { |
376 // try as file | |
74 | 377 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
|
378 if (pf.canRead()) { |
75 | 379 logger.fine("trying file for: " + pf); |
71 | 380 try { |
381 ps = new FileInputStream(pf); | |
382 } catch (FileNotFoundException e) { | |
75 | 383 logger.severe(e.toString()); |
71 | 384 } |
72
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
385 } else { |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
386 // use class loader |
4c2cea836bc0
restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents:
71
diff
changeset
|
387 ps = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); |
18 | 388 } |
389 } | |
390 } | |
391 return ps; | |
392 } | |
393 | |
394 /** | |
395 * get a real file name for a web app file pathname. | |
396 * | |
397 * If filename starts with "/" its treated as absolute else the path is | |
74 | 398 * appended to the /WEB-INF/ directory in the web-app. |
18 | 399 * |
400 * @param filename | |
401 * @param sc | |
402 * @return | |
403 */ | |
404 public static String getResourcePath(ServletContext sc, String filename) { | |
405 File f = new File(filename); | |
406 // is the filename absolute? | |
71 | 407 if (!f.isAbsolute() && sc != null) { |
18 | 408 // 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
|
409 filename = sc.getRealPath("/WEB-INF/" + filename); |
18 | 410 } |
411 return filename; | |
412 } | |
413 | |
414 /* | |
415 * (non-Javadoc) | |
416 * | |
417 * @see org.restlet.Application#stop() | |
418 */ | |
419 @Override | |
420 public synchronized void stop() throws Exception { | |
421 /* | |
422 * trying to clean up databases, not sure if this is the right way... | |
423 */ | |
424 if (srv != null) { | |
75 | 425 logger.info("Stopping DB admin server..."); |
18 | 426 srv.stop(); |
427 srv = null; | |
428 } | |
429 if (graphDb != null) { | |
75 | 430 logger.info("Stopping DB..."); |
18 | 431 graphDb.shutdown(); |
432 graphDb = null; | |
433 } | |
434 super.stop(); | |
435 } | |
436 | |
437 private static void registerShutdownHook(final GraphDatabaseService graphDb) { | |
438 // Registers a shutdown hook for the Neo4j instance so that it | |
439 // shuts down nicely when the VM exits (even if you "Ctrl-C" the | |
440 // running example before it's completed) | |
441 Runtime.getRuntime().addShutdownHook(new Thread() { | |
442 @Override | |
443 public void run() { | |
444 graphDb.shutdown(); | |
445 } | |
446 }); | |
447 } | |
448 | |
449 } |