Changeset 3:47b53ae385d1 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Jun 29, 2012, 6:38:27 PM (12 years ago)
Author:
casties
Branch:
default
Message:

merging old code

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  
    2020import org.apache.log4j.BasicConfigurator;
    2121import org.apache.log4j.Logger;
     22import org.neo4j.graphdb.GraphDatabaseService;
     23import org.neo4j.graphdb.factory.GraphDatabaseFactory;
     24import org.neo4j.kernel.AbstractGraphDatabase;
     25import org.neo4j.server.WrappingNeoServerBootstrapper;
    2226import org.restlet.Application;
    2327import org.restlet.Context;
     
    3135
    3236    public static Logger logger = Logger.getRootLogger();
    33    
     37
    3438    private ChallengeAuthenticator authenticator;
    3539
     
    3842     */
    3943    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";
    4149
    4250    /**
     
    4755    public RestServer(Context parentContext) {
    4856        super(parentContext);
    49 
    50         Logger rl = Logger.getRootLogger();
     57        // TODO: is this the right place to run the log4j configurator?
    5158        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()
    5661                .get("org.restlet.ext.servlet.ServletContext");
    5762        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);
    7080                }
    7181            }
     82            /*
     83             * read consumerKeys from webapp
     84             */
     85            consumerKeys = new Properties();
     86            InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
    7287            if (ps != null) {
    73                 rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
     88                logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
    7489                try {
    7590                    consumerKeys.load(ps);
     
    7893                    e.printStackTrace();
    7994                }
    80                 rl.debug("consumer keys: "+consumerKeys);
     95                logger.debug("consumer keys: " + consumerKeys);
    8196            } else {
    82                 rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
     97                logger.error("Unable to get resource " + CONSUMER_KEYS_PATH);
    8398            }
    8499        } 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     *
    93109     * @param consumerKey
    94110     * @return
     
    98114    }
    99115
    100 
    101116    /*
    102117     * (non-Javadoc)
     
    106121    @Override
    107122    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();
    114124
    115125        Router router = new Router(getContext());
    116         /*
     126
    117127        router.attach("/annotator/annotations", AnnotatorAnnotations.class);
    118128        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
    124134        router.attach("/", AnnotatorInfo.class);
    125         //authenticator.setNext(router);
    126         //return authenticator;
    127        
     135        // authenticator.setNext(router);
     136        // return authenticator;
     137
    128138        return router;
    129139    }
     
    142152        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
    143153
    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";
    145156        env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
    146157
     
    164175
    165176        try {
    166             NamingEnumeration<SearchResult> results = dctx.search(base, filter,
    167                     sc);
     177            NamingEnumeration<SearchResult> results = dctx.search(base, filter, sc);
    168178            while (results.hasMore()) {
    169179                SearchResult sr = (SearchResult) results.next();
     
    187197    }
    188198
     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
    189243}
  • src/main/webapp/WEB-INF/web.xml

    r1 r3  
    55xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    66http://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>
    88    <!-- Application classname -->
    99    <context-param>
Note: See TracChangeset for help on using the changeset viewer.