Changeset 3:47b53ae385d1 in AnnotationManagerN4J for src
- Timestamp:
- Jun 29, 2012, 6:38:27 PM (13 years ago)
- Branch:
- default
- Location:
- src/main
- Files:
-
- 2 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java
r1 r3 20 20 import org.apache.log4j.BasicConfigurator; 21 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 26 import org.restlet.Application; 23 27 import org.restlet.Context; … … 31 35 32 36 public static Logger logger = Logger.getRootLogger(); 33 37 34 38 private ChallengeAuthenticator authenticator; 35 39 … … 38 42 */ 39 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 /** … … 47 55 public RestServer(Context parentContext) { 48 56 super(parentContext); 49 50 Logger rl = Logger.getRootLogger(); 57 // TODO: is this the right place to run the log4j configurator? 51 58 BasicConfigurator.configure(); 52 // read consumerKeys from webapp 53 consumerKeys = new Properties(); 54 ServletContext sc = (ServletContext) getContext().getServerDispatcher() 55 .getContext().getAttributes() 59 60 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes() 56 61 .get("org.restlet.ext.servlet.ServletContext"); 57 62 if (sc != null) { 58 InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH); 59 if (ps == null) { 60 // try as file 61 File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH)); 62 if (pf != null) { 63 rl.debug("trying file for consumer keys: "+pf); 64 try { 65 ps = new FileInputStream(pf); 66 } catch (FileNotFoundException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 63 // look for database service in context 64 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); 65 if (graphDb == null) { 66 /* 67 * open database 68 */ 69 String dbFn = getResourcePath(sc, DB_PATH); 70 if (dbFn != null) { 71 logger.debug("opening DB " + dbFn); 72 graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(dbFn); 73 // store in context 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 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 89 try { 75 90 consumerKeys.load(ps); … … 78 93 e.printStackTrace(); 79 94 } 80 rl.debug("consumer keys: "+consumerKeys);95 logger.debug("consumer keys: " + consumerKeys); 81 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 99 } else { 85 rl.error("Unable to get ServletContext!"); 86 } 87 88 } 89 90 /** 91 * returns consumer secret for consumer key. 92 * returns null if consumer key doesn't exist. 100 logger.error("Unable to get ServletContext!"); 101 } 102 103 } 104 105 /** 106 * returns consumer secret for consumer key. returns null if consumer key 107 * doesn't exist. 108 * 93 109 * @param consumerKey 94 110 * @return … … 98 114 } 99 115 100 101 116 /* 102 117 * (non-Javadoc) … … 106 121 @Override 107 122 public Restlet createInboundRoot() { 108 //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); 123 // this.authenticator = createAuthenticator(); 114 124 115 125 Router router = new Router(getContext()); 116 /* 126 117 127 router.attach("/annotator/annotations", AnnotatorAnnotations.class); 118 128 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class); 119 router.attach("/annotator/search", AnnotatorSearch.class);120 121 // router.attach("",redirector); 122 router.attach("/annotator",ExtendedAnnotationInput.class);123 */ 129 // router.attach("/annotator/search", AnnotatorSearch.class); 130 131 // router.attach("",redirector); router.attach("/annotator", 132 // ExtendedAnnotationInput.class); 133 124 134 router.attach("/", AnnotatorInfo.class); 125 // authenticator.setNext(router);126 // return authenticator;127 135 // authenticator.setNext(router); 136 // return authenticator; 137 128 138 return router; 129 139 } … … 142 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 156 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); 146 157 … … 164 175 165 176 try { 166 NamingEnumeration<SearchResult> results = dctx.search(base, filter, 167 sc); 177 NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc); 168 178 while (results.hasMore()) { 169 179 SearchResult sr = (SearchResult) results.next(); … … 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 } -
src/main/webapp/WEB-INF/web.xml
r1 r3 5 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 6 6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 7 <display-name> RESTfulJavaWebServices-Restlet</display-name>7 <display-name>AnnotationManager-Restlet</display-name> 8 8 <!-- Application classname --> 9 9 <context-param>
Note: See TracChangeset
for help on using the changeset viewer.