Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 3:47b53ae385d1
merging old code
author | casties |
---|---|
date | Fri, 29 Jun 2012 20:38:27 +0200 |
parents | 6556943c4fb9 |
children | 3599b29c393f |
comparison
equal
deleted
inserted
replaced
2:f2d44c41eedf | 3:47b53ae385d1 |
---|---|
17 import javax.naming.directory.SearchResult; | 17 import javax.naming.directory.SearchResult; |
18 import javax.servlet.ServletContext; | 18 import javax.servlet.ServletContext; |
19 | 19 |
20 import org.apache.log4j.BasicConfigurator; | 20 import org.apache.log4j.BasicConfigurator; |
21 import org.apache.log4j.Logger; | 21 import org.apache.log4j.Logger; |
22 import org.neo4j.graphdb.GraphDatabaseService; | |
23 import org.neo4j.graphdb.factory.GraphDatabaseFactory; | |
24 import org.neo4j.kernel.AbstractGraphDatabase; | |
25 import org.neo4j.server.WrappingNeoServerBootstrapper; | |
22 import org.restlet.Application; | 26 import org.restlet.Application; |
23 import org.restlet.Context; | 27 import org.restlet.Context; |
24 import org.restlet.Restlet; | 28 import org.restlet.Restlet; |
25 import org.restlet.routing.Router; | 29 import org.restlet.routing.Router; |
26 import org.restlet.security.ChallengeAuthenticator; | 30 import org.restlet.security.ChallengeAuthenticator; |
28 import scala.sys.process.ProcessBuilderImpl.Dummy; | 32 import scala.sys.process.ProcessBuilderImpl.Dummy; |
29 | 33 |
30 public class RestServer extends Application { | 34 public class RestServer extends Application { |
31 | 35 |
32 public static Logger logger = Logger.getRootLogger(); | 36 public static Logger logger = Logger.getRootLogger(); |
33 | 37 |
34 private ChallengeAuthenticator authenticator; | 38 private ChallengeAuthenticator authenticator; |
35 | 39 |
36 /** | 40 /** |
37 * Properties holding consumer keys and secrets | 41 * Properties holding consumer keys and secrets |
38 */ | 42 */ |
39 private Properties consumerKeys; | 43 private Properties consumerKeys; |
40 public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property"; | 44 public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property"; |
45 | |
46 private GraphDatabaseService graphDb; | |
47 public static final String GRAPHDB_KEY = "annotationmanager.graphdb"; | |
48 public String DB_PATH = "WEB-INF/neo4j-annotation-db"; | |
41 | 49 |
42 /** | 50 /** |
43 * constructor | 51 * constructor |
44 * | 52 * |
45 * @param parentContext | 53 * @param parentContext |
46 */ | 54 */ |
47 public RestServer(Context parentContext) { | 55 public RestServer(Context parentContext) { |
48 super(parentContext); | 56 super(parentContext); |
49 | 57 // TODO: is this the right place to run the log4j configurator? |
50 Logger rl = Logger.getRootLogger(); | |
51 BasicConfigurator.configure(); | 58 BasicConfigurator.configure(); |
52 // read consumerKeys from webapp | 59 |
53 consumerKeys = new Properties(); | 60 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes() |
54 ServletContext sc = (ServletContext) getContext().getServerDispatcher() | |
55 .getContext().getAttributes() | |
56 .get("org.restlet.ext.servlet.ServletContext"); | 61 .get("org.restlet.ext.servlet.ServletContext"); |
57 if (sc != null) { | 62 if (sc != null) { |
58 InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH); | 63 // look for database service in context |
59 if (ps == null) { | 64 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); |
60 // try as file | 65 if (graphDb == null) { |
61 File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH)); | 66 /* |
62 if (pf != null) { | 67 * open database |
63 rl.debug("trying file for consumer keys: "+pf); | 68 */ |
64 try { | 69 String dbFn = getResourcePath(sc, DB_PATH); |
65 ps = new FileInputStream(pf); | 70 if (dbFn != null) { |
66 } catch (FileNotFoundException e) { | 71 logger.debug("opening DB " + dbFn); |
67 // TODO Auto-generated catch block | 72 graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn); |
68 e.printStackTrace(); | 73 // store in context |
69 } | 74 sc.setAttribute(GRAPHDB_KEY, graphDb); |
75 WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper((AbstractGraphDatabase) graphDb); | |
76 logger.debug("Starting DB admin server..."); | |
77 srv.start(); | |
78 } else { | |
79 logger.error("Unable to get resource " + DB_PATH); | |
70 } | 80 } |
71 } | 81 } |
82 /* | |
83 * read consumerKeys from webapp | |
84 */ | |
85 consumerKeys = new Properties(); | |
86 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH); | |
72 if (ps != null) { | 87 if (ps != null) { |
73 rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH); | 88 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH); |
74 try { | 89 try { |
75 consumerKeys.load(ps); | 90 consumerKeys.load(ps); |
76 } catch (IOException e) { | 91 } catch (IOException e) { |
77 // TODO Auto-generated catch block | 92 // TODO Auto-generated catch block |
78 e.printStackTrace(); | 93 e.printStackTrace(); |
79 } | 94 } |
80 rl.debug("consumer keys: "+consumerKeys); | 95 logger.debug("consumer keys: " + consumerKeys); |
81 } else { | 96 } else { |
82 rl.error("Unable to get resource "+CONSUMER_KEYS_PATH); | 97 logger.error("Unable to get resource " + CONSUMER_KEYS_PATH); |
83 } | 98 } |
84 } else { | 99 } else { |
85 rl.error("Unable to get ServletContext!"); | 100 logger.error("Unable to get ServletContext!"); |
86 } | 101 } |
87 | 102 |
88 } | 103 } |
89 | 104 |
90 /** | 105 /** |
91 * returns consumer secret for consumer key. | 106 * returns consumer secret for consumer key. returns null if consumer key |
92 * returns null if consumer key doesn't exist. | 107 * doesn't exist. |
108 * | |
93 * @param consumerKey | 109 * @param consumerKey |
94 * @return | 110 * @return |
95 */ | 111 */ |
96 public String getConsumerSecret(String consumerKey) { | 112 public String getConsumerSecret(String consumerKey) { |
97 return consumerKeys.getProperty(consumerKey); | 113 return consumerKeys.getProperty(consumerKey); |
98 } | 114 } |
99 | 115 |
100 | |
101 /* | 116 /* |
102 * (non-Javadoc) | 117 * (non-Javadoc) |
103 * | 118 * |
104 * @see org.restlet.Application#createInboundRoot() | 119 * @see org.restlet.Application#createInboundRoot() |
105 */ | 120 */ |
106 @Override | 121 @Override |
107 public Restlet createInboundRoot() { | 122 public Restlet createInboundRoot() { |
108 //this.authenticator = createAuthenticator(); | 123 // this.authenticator = createAuthenticator(); |
109 | |
110 // String target = "{rh}/{rf}/XX"; | |
111 // Redirector redirector = new | |
112 // Redirector(getContext().createChildContext(), target, | |
113 // Redirector.MODE_CLIENT_SEE_OTHER); | |
114 | 124 |
115 Router router = new Router(getContext()); | 125 Router router = new Router(getContext()); |
116 /* | 126 |
117 router.attach("/annotator/annotations", AnnotatorAnnotations.class); | 127 router.attach("/annotator/annotations", AnnotatorAnnotations.class); |
118 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class); | 128 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class); |
119 router.attach("/annotator/search", AnnotatorSearch.class); | 129 // router.attach("/annotator/search", AnnotatorSearch.class); |
120 | 130 |
121 // router.attach("",redirector); | 131 // router.attach("",redirector); router.attach("/annotator", |
122 router.attach("/annotator", ExtendedAnnotationInput.class); | 132 // ExtendedAnnotationInput.class); |
123 */ | 133 |
124 router.attach("/", AnnotatorInfo.class); | 134 router.attach("/", AnnotatorInfo.class); |
125 //authenticator.setNext(router); | 135 // authenticator.setNext(router); |
126 //return authenticator; | 136 // return authenticator; |
127 | 137 |
128 return router; | 138 return router; |
129 } | 139 } |
130 | 140 |
131 /** | 141 /** |
132 * Hole den vollen Benutzernamen aus dem LDAP | 142 * Hole den vollen Benutzernamen aus dem LDAP |
139 // creator zurueckgeben | 149 // creator zurueckgeben |
140 Hashtable<String, String> env = new Hashtable<String, String>(); | 150 Hashtable<String, String> env = new Hashtable<String, String>(); |
141 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; | 151 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; |
142 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); | 152 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); |
143 | 153 |
144 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; // TODO should go into config file | 154 // TODO: should go into config file |
155 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; | |
145 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); | 156 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); |
146 | 157 |
147 DirContext dctx; | 158 DirContext dctx; |
148 try { | 159 try { |
149 dctx = new InitialDirContext(env); | 160 dctx = new InitialDirContext(env); |
161 sc.setSearchScope(SearchControls.SUBTREE_SCOPE); | 172 sc.setSearchScope(SearchControls.SUBTREE_SCOPE); |
162 | 173 |
163 String filter = "(uid=" + creator + ")"; | 174 String filter = "(uid=" + creator + ")"; |
164 | 175 |
165 try { | 176 try { |
166 NamingEnumeration<SearchResult> results = dctx.search(base, filter, | 177 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc); |
167 sc); | |
168 while (results.hasMore()) { | 178 while (results.hasMore()) { |
169 SearchResult sr = (SearchResult) results.next(); | 179 SearchResult sr = (SearchResult) results.next(); |
170 javax.naming.directory.Attributes attrs = sr.getAttributes(); | 180 javax.naming.directory.Attributes attrs = sr.getAttributes(); |
171 | 181 |
172 Attribute attr = attrs.get("cn"); | 182 Attribute attr = attrs.get("cn"); |
184 e.printStackTrace(); | 194 e.printStackTrace(); |
185 } | 195 } |
186 return retString; | 196 return retString; |
187 } | 197 } |
188 | 198 |
199 /** | |
200 * returns resource from path (in webapp) as InputStream. | |
201 * | |
202 * @param sc | |
203 * @param path | |
204 * @return | |
205 */ | |
206 protected InputStream getResourceAsStream(ServletContext sc, String path) { | |
207 InputStream ps = sc.getResourceAsStream(path); | |
208 if (ps == null) { | |
209 // try as file | |
210 File pf = new File(sc.getRealPath(path)); | |
211 if (pf != null) { | |
212 logger.debug("trying file for: " + pf); | |
213 try { | |
214 ps = new FileInputStream(pf); | |
215 } catch (FileNotFoundException e) { | |
216 logger.error(e); | |
217 } | |
218 } | |
219 } | |
220 return ps; | |
221 } | |
222 | |
223 /** | |
224 * get a real file name for a web app file pathname. | |
225 * | |
226 * If filename starts with "/" its treated as absolute else the path is | |
227 * appended to the base directory of the web-app. | |
228 * | |
229 * @param filename | |
230 * @param sc | |
231 * @return | |
232 */ | |
233 public static String getResourcePath(ServletContext sc, String filename) { | |
234 File f = new File(filename); | |
235 // is the filename absolute? | |
236 if (!f.isAbsolute()) { | |
237 // relative path -> use getRealPath to resolve in webapp | |
238 filename = sc.getRealPath(filename); | |
239 } | |
240 return filename; | |
241 } | |
242 | |
189 } | 243 } |