annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 5:bbf0cc5bee29

version 0.2 really works now
author casties
date Tue, 03 Jul 2012 21:23:17 +0200
parents 3599b29c393f
children 6dfbe2400f64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
2
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
3 import java.io.File;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
4 import java.io.FileInputStream;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
5 import java.io.FileNotFoundException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
6 import java.io.IOException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
7 import java.io.InputStream;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
8 import java.util.Hashtable;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
9 import java.util.Properties;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
10
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
11 import javax.naming.NamingEnumeration;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
12 import javax.naming.NamingException;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
13 import javax.naming.directory.Attribute;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
14 import javax.naming.directory.DirContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
15 import javax.naming.directory.InitialDirContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
16 import javax.naming.directory.SearchControls;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
17 import javax.naming.directory.SearchResult;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
18 import javax.servlet.ServletContext;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
19
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
20 import org.apache.log4j.BasicConfigurator;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
21 import org.apache.log4j.Logger;
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
22 import org.neo4j.graphdb.GraphDatabaseService;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
23 import org.neo4j.graphdb.factory.GraphDatabaseFactory;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
24 import org.neo4j.kernel.AbstractGraphDatabase;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
25 import org.neo4j.server.WrappingNeoServerBootstrapper;
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
26 import org.restlet.Application;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
27 import org.restlet.Context;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
28 import org.restlet.Restlet;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
29 import org.restlet.routing.Router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
30
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
31 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
32
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
33 public class RestServer extends Application {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
34
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
35 public static Logger logger = Logger.getLogger(RestServer.class);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
36
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
37 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
38 * Properties holding consumer keys and secrets
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
39 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
40 private Properties consumerKeys;
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
41 public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
42
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
43 private Properties serverConfig;
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
44 public String CONFIG_PROPS_PATH = "WEB-INF/serverconfig.property";
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
45
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
46 private GraphDatabaseService graphDb;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
47 public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
48 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path";
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
49 public String graphdbPath = "WEB-INF/neo4j-annotation-db";
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
50
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
51 private WrappingNeoServerBootstrapper srv;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
52 public static final String GRAPHDBSRV_KEY = "annotationmanager.graphdb.srv";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
53
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
54 private AnnotationStore store;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
55 public static final String ANNSTORE_KEY = "annotationmanager.store";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
56
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
57 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
58 * constructor
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
59 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
60 * @param parentContext
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
61 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
62 public RestServer(Context parentContext) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
63 super(parentContext);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
64 // TODO: is this the right place to run the log4j configurator?
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
65 BasicConfigurator.configure();
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
66
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
67 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes()
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
68 .get("org.restlet.ext.servlet.ServletContext");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
69 if (sc != null) {
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
70 /*
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
71 * read config from webapp
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
72 */
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
73 serverConfig = new Properties();
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
74 InputStream ps1 = getResourceAsStream(sc, CONFIG_PROPS_PATH);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
75 if (ps1 != null) {
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
76 logger.debug("loading config from " + CONFIG_PROPS_PATH);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
77 try {
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
78 serverConfig.load(ps1);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
79 /*
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
80 * read serverconfig options
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
81 */
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
82 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
83 } catch (IOException e) {
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
84 logger.warn("Error loading server config: ", e);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
85 }
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
86 logger.debug("config: " + serverConfig);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
87 } else {
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
88 logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
89 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
90 // look for database service in context
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
91 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
92 if (graphDb == null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
93 /*
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
94 * open database
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
95 */
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
96 String dbFn = getResourcePath(sc, graphdbPath);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
97 if (dbFn != null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
98 logger.debug("opening DB " + dbFn);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
99 graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
100 registerShutdownHook(graphDb);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
101 // store in context
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
102 sc.setAttribute(GRAPHDB_KEY, graphDb);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
103 // AnnotationStore
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
104 store = new AnnotationStore(graphDb);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
105 sc.setAttribute(ANNSTORE_KEY, store);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
106 // admin server
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
107 srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
108 logger.debug("Starting DB admin server...");
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
109 // store in context
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
110 sc.setAttribute(GRAPHDBSRV_KEY, srv);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
111 srv.start();
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
112 } else {
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
113 logger.error("Unable to get resource " + dbFn);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
114 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
115 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
116 /*
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
117 * read consumerKeys from webapp
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
118 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
119 consumerKeys = new Properties();
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
120 InputStream ps2 = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
121 if (ps2 != null) {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
122 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
123 try {
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
124 consumerKeys.load(ps2);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
125 } catch (IOException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
126 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
127 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
128 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
129 logger.debug("consumer keys: " + consumerKeys);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
130 } else {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
131 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
132 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
133 } else {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
134 logger.error("Unable to get ServletContext!");
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
135 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
136
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
137 }
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
138
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
139 /**
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
140 * returns consumer secret for consumer key. returns null if consumer key
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
141 * doesn't exist.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
142 *
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
143 * @param consumerKey
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
144 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
145 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
146 public String getConsumerSecret(String consumerKey) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
147 return consumerKeys.getProperty(consumerKey);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
148 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
149
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
150 /*
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
151 * (non-Javadoc)
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
152 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
153 * @see org.restlet.Application#createInboundRoot()
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
154 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
155 @Override
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
156 public Restlet createInboundRoot() {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
157 // this.authenticator = createAuthenticator();
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
158
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
159 Router router = new Router(getContext());
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
160
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
161 router.attach("/annotator/annotations", AnnotatorAnnotations.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
162 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
163 router.attach("/annotator/search", AnnotatorSearch.class);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
164
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
165 // router.attach("",redirector); router.attach("/annotator",
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
166 // ExtendedAnnotationInput.class);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
167
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
168 router.attach("/", AnnotatorInfo.class);
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
169 // authenticator.setNext(router);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
170 // return authenticator;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
171
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
172 return router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
173 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
174
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
175 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
176 * Hole den vollen Benutzernamen aus dem LDAP
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
177 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
178 * @param creator
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
179 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
180 */
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
181 public String getFullNameFromLdap(String creator) {
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
182 String retString = creator; // falls nichts gefunden wird einfach den
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
183 // creator zurueckgeben
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
184 Hashtable<String, String> env = new Hashtable<String, String>();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
185 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
186 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
187
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
188 // TODO: should go into config file
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
189 String ldapUrl = "ldap://ldap.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
190 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
191
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
192 DirContext dctx;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
193 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
194 dctx = new InitialDirContext(env);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
195 } catch (NamingException e1) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
196 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
197 e1.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
198 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
199 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
200
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
201 String base = "ou=people";
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
202
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
203 SearchControls sc = new SearchControls();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
204 String[] attributeFilter = { "cn", "mail" };
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
205 sc.setReturningAttributes(attributeFilter);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
206 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
207
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
208 String filter = "(uid=" + creator + ")";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
209
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
210 try {
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
211 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc);
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
212 while (results.hasMore()) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
213 SearchResult sr = (SearchResult) results.next();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
214 javax.naming.directory.Attributes attrs = sr.getAttributes();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
215
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
216 Attribute attr = attrs.get("cn");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
217 retString = (String) attr.get();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
218 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
219 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
220 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
221 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
222 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
223
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
224 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
225 dctx.close();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
226 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
227 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
228 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
229 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
230 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
231 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
232
3
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
233 /**
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
234 * returns resource from path (in webapp) as InputStream.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
235 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
236 * @param sc
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
237 * @param path
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
238 * @return
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
239 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
240 protected InputStream getResourceAsStream(ServletContext sc, String path) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
241 InputStream ps = sc.getResourceAsStream(path);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
242 if (ps == null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
243 // try as file
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
244 File pf = new File(sc.getRealPath(path));
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
245 if (pf != null) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
246 logger.debug("trying file for: " + pf);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
247 try {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
248 ps = new FileInputStream(pf);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
249 } catch (FileNotFoundException e) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
250 logger.error(e);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
251 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
252 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
253 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
254 return ps;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
255 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
256
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
257 /**
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
258 * get a real file name for a web app file pathname.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
259 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
260 * If filename starts with "/" its treated as absolute else the path is
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
261 * appended to the base directory of the web-app.
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
262 *
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
263 * @param filename
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
264 * @param sc
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
265 * @return
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
266 */
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
267 public static String getResourcePath(ServletContext sc, String filename) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
268 File f = new File(filename);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
269 // is the filename absolute?
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
270 if (!f.isAbsolute()) {
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
271 // relative path -> use getRealPath to resolve in webapp
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
272 filename = sc.getRealPath(filename);
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
273 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
274 return filename;
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
275 }
47b53ae385d1 merging old code
casties
parents: 1
diff changeset
276
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
277 /* (non-Javadoc)
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
278 * @see org.restlet.Application#stop()
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
279 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
280 @Override
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
281 public synchronized void stop() throws Exception {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
282 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
283 * trying to clean up databases, nur sure if this is the right way...
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
284 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
285 if (srv != null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
286 logger.debug("Stopping DB admin server...");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
287 srv.stop();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
288 srv = null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
289 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
290 if (graphDb != null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
291 logger.debug("Stopping DB...");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
292 graphDb.shutdown();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
293 graphDb = null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
294 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
295 super.stop();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
296 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
297
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
298 private static void registerShutdownHook(final GraphDatabaseService graphDb) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
299 // Registers a shutdown hook for the Neo4j instance so that it
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
300 // shuts down nicely when the VM exits (even if you "Ctrl-C" the
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
301 // running example before it's completed)
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
302 Runtime.getRuntime().addShutdownHook(new Thread() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
303 @Override
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
304 public void run() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
305 graphDb.shutdown();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
306 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
307 });
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
308 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
309
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
310
1
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
311 }