changeset 1:6556943c4fb9

include neo4j and restlet
author casties
date Wed, 27 Jun 2012 17:52:02 +0200
parents ed1916729773
children f2d44c41eedf
files pom.xml src/main/java/de/mpiwg/itgroup/annotations/AnnotationServerDb.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorInfo.java src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java src/main/resources/annotator-info.html src/main/webapp/META-INF/MANIFEST.MF src/main/webapp/WEB-INF/consumerkeys.property.template src/main/webapp/WEB-INF/etc.jaasAuth.conf src/main/webapp/WEB-INF/web.xml src/main/webapp/WEB-INF/web.xml.template
diffstat 10 files changed, 432 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pom.xml	Tue Jun 26 17:21:42 2012 +0200
+++ b/pom.xml	Wed Jun 27 17:52:02 2012 +0200
@@ -7,6 +7,7 @@
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 		<neo4j-version>1.7.1</neo4j-version>
+		<restlet-version>2.0.14</restlet-version>
 	</properties>
 	<repositories>
 		<repository>
@@ -20,6 +21,11 @@
 				<enabled>false</enabled>
 			</snapshots>
 		</repository>
+        <repository>
+            <id>maven-restlet</id>
+            <name>Public online Restlet repository</name>
+            <url>http://maven.restlet.org</url>
+        </repository>
 	</repositories>
 	<dependencies>
 		<dependency>
@@ -38,5 +44,39 @@
 			<classifier>static-web</classifier>
 			<version>${neo4j-version}</version>
 		</dependency>
+		<dependency>
+			<groupId>org.restlet.jee</groupId>
+			<artifactId>org.restlet</artifactId>
+			<version>${restlet-version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.restlet.jee</groupId>
+			<artifactId>org.restlet.ext.servlet</artifactId>
+			<version>${restlet-version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.restlet.jee</groupId>
+			<artifactId>org.restlet.ext.json</artifactId>
+			<version>${restlet-version}</version>
+		</dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+            <version>1.2.14</version>
+        </dependency>
 	</dependencies>
+	<build>
+		<pluginManagement>
+			<plugins>
+				<plugin>
+					<groupId>org.apache.maven.plugins</groupId>
+					<artifactId>maven-compiler-plugin</artifactId>
+					<configuration>
+						<source>1.6</source>
+						<target>1.6</target>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+	</build>
 </project>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/AnnotationServerDb.java	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,67 @@
+package de.mpiwg.itgroup.annotations;
+
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.factory.GraphDatabaseFactory;
+import org.neo4j.kernel.AbstractGraphDatabase;
+import org.neo4j.server.WrappingNeoServerBootstrapper;
+
+public class AnnotationServerDb {
+
+    private static String DB_PATH = "target/neo4j-annotation-db";
+    private static GraphDatabaseService graphDb;
+
+    /**
+     * @param args
+     */
+    public static void main(String[] args) {
+        AnnotationServerDb db = new AnnotationServerDb();
+        db.createDb();
+        // do something
+        AbstractGraphDatabase graphdb = (AbstractGraphDatabase) graphDb;
+        WrappingNeoServerBootstrapper srv;
+        srv = new WrappingNeoServerBootstrapper( graphdb );
+        System.out.println("Starting server...");
+        srv.start();
+        // The server is now running
+        try {
+            Thread.sleep(300*1000);
+        } catch (InterruptedException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        // until we stop it:
+        System.out.println("Stopping server...");
+        srv.stop();        
+        db.shutDown();
+    }
+
+    /**
+     * 
+     */
+    public void createDb() {
+        System.out.println();
+        System.out.println("Starting database "+DB_PATH+" ...");
+        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
+        registerShutdownHook(graphDb);
+    }
+
+    public void shutDown() {
+        System.out.println();
+        System.out.println("Shutting down database ...");
+        // START SNIPPET: shutdownServer
+        graphDb.shutdown();
+        // END SNIPPET: shutdownServer
+    }
+
+    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
+        // Registers a shutdown hook for the Neo4j instance so that it
+        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
+        // running example before it's completed)
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                graphDb.shutdown();
+            }
+        });
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorInfo.java	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,52 @@
+package de.mpiwg.itgroup.annotations.restlet;
+
+import java.io.InputStream;
+
+import org.apache.log4j.Logger;
+import org.restlet.data.Form;
+import org.restlet.data.MediaType;
+import org.restlet.representation.InputRepresentation;
+import org.restlet.representation.Representation;
+import org.restlet.resource.Get;
+import org.restlet.resource.Options;
+import org.restlet.resource.ServerResource;
+
+
+
+
+
+public class AnnotatorInfo extends ServerResource {
+
+private Logger logger = Logger.getRootLogger();
+	
+	/**
+	 * Erlaubt cross scripting bei Aufruf aus Javascript
+	 * @param entity
+	 */
+	@Options
+	public void doOptions(Representation entity) {
+	    Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
+	    if (responseHeaders == null) {
+	        responseHeaders = new Form();
+	        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
+	    }
+	    responseHeaders.add("Access-Control-Allow-Origin", "*");
+	    responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
+	    responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
+	    responseHeaders.add("Access-Control-Allow-Credentials", "false");
+	    responseHeaders.add("Access-Control-Max-Age", "60");
+	}
+
+
+	
+	@Get("html")
+	public Representation getHTML(){
+		InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/annotationManager/staticPages/main.html");
+		
+		Representation rep = new InputRepresentation(is,MediaType.TEXT_HTML);
+		return rep;
+		
+	}
+	
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,189 @@
+package de.mpiwg.itgroup.annotations.restlet;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Hashtable;
+import java.util.Properties;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.servlet.ServletContext;
+
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Logger;
+import org.restlet.Application;
+import org.restlet.Context;
+import org.restlet.Restlet;
+import org.restlet.routing.Router;
+import org.restlet.security.ChallengeAuthenticator;
+
+import scala.sys.process.ProcessBuilderImpl.Dummy;
+
+public class RestServer extends Application {
+
+    public static Logger logger = Logger.getRootLogger();
+    
+    private ChallengeAuthenticator authenticator;
+
+    /**
+     * Properties holding consumer keys and secrets
+     */
+    private Properties consumerKeys;
+    public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
+
+    /**
+     * constructor
+     * 
+     * @param parentContext
+     */
+    public RestServer(Context parentContext) {
+        super(parentContext);
+
+        Logger rl = Logger.getRootLogger();
+        BasicConfigurator.configure();
+        // read consumerKeys from webapp
+        consumerKeys = new Properties();
+        ServletContext sc = (ServletContext) getContext().getServerDispatcher()
+                .getContext().getAttributes()
+                .get("org.restlet.ext.servlet.ServletContext");
+        if (sc != null) {
+            InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH);
+            if (ps == null) {
+                // try as file
+                File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH));
+                if (pf != null) {
+                    rl.debug("trying file for consumer keys: "+pf);
+                    try {
+                        ps = new FileInputStream(pf);
+                    } catch (FileNotFoundException e) {
+                        // TODO Auto-generated catch block
+                        e.printStackTrace();
+                    }
+                }
+            }
+            if (ps != null) {
+                rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
+                try {
+                    consumerKeys.load(ps);
+                } catch (IOException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+                rl.debug("consumer keys: "+consumerKeys);
+            } else {
+                rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
+            }
+        } else {
+            rl.error("Unable to get ServletContext!");
+        }
+        
+    }
+    
+    /**
+     * returns consumer secret for consumer key.
+     * returns null if consumer key doesn't exist.
+     * @param consumerKey
+     * @return
+     */
+    public String getConsumerSecret(String consumerKey) {
+        return consumerKeys.getProperty(consumerKey);
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.restlet.Application#createInboundRoot()
+     */
+    @Override
+    public Restlet createInboundRoot() {
+        //this.authenticator = createAuthenticator();
+
+        // String target = "{rh}/{rf}/XX";
+        // Redirector redirector = new
+        // Redirector(getContext().createChildContext(), target,
+        // Redirector.MODE_CLIENT_SEE_OTHER);
+
+        Router router = new Router(getContext());
+        /*
+        router.attach("/annotator/annotations", AnnotatorAnnotations.class);
+        router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
+        router.attach("/annotator/search", AnnotatorSearch.class);
+        
+        // router.attach("",redirector);
+        router.attach("/annotator", ExtendedAnnotationInput.class);
+        */
+        router.attach("/", AnnotatorInfo.class);
+        //authenticator.setNext(router);
+        //return authenticator;
+        
+        return router;
+    }
+
+    /**
+     * Hole den vollen Benutzernamen aus dem LDAP
+     * 
+     * @param creator
+     * @return
+     */
+    public String getUserNameFromLdap(String creator) {
+        String retString = creator; // falls nichts gefunden wird einfach den
+                                    // creator zurueckgeben
+        Hashtable<String, String> env = new Hashtable<String, String>();
+        String sp = "com.sun.jndi.ldap.LdapCtxFactory";
+        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
+
+        String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; // TODO should go into config file
+        env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
+
+        DirContext dctx;
+        try {
+            dctx = new InitialDirContext(env);
+        } catch (NamingException e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+            return retString;
+        }
+
+        String base = "ou=People";
+
+        SearchControls sc = new SearchControls();
+        String[] attributeFilter = { "cn", "mail" };
+        sc.setReturningAttributes(attributeFilter);
+        sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
+
+        String filter = "(uid=" + creator + ")";
+
+        try {
+            NamingEnumeration<SearchResult> results = dctx.search(base, filter,
+                    sc);
+            while (results.hasMore()) {
+                SearchResult sr = (SearchResult) results.next();
+                javax.naming.directory.Attributes attrs = sr.getAttributes();
+
+                Attribute attr = attrs.get("cn");
+                retString = (String) attr.get();
+            }
+        } catch (NamingException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+
+        try {
+            dctx.close();
+        } catch (NamingException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return retString;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/annotator-info.html	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,6 @@
+<html>
+<body>
+<h2>Annotation manager</h2>
+<p><a href="annotations">Annotations overview</a></p>
+</body>
+</html>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/META-INF/MANIFEST.MF	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path: 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/WEB-INF/consumerkeys.property.template	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,2 @@
+# consumer keys and secrets for annotation server clients
+# format: consumerKey = consumerSecret
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/WEB-INF/etc.jaasAuth.conf	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,10 @@
+BasicJaasAuthenticationApplication {
+    com.sun.security.auth.module.LdapLoginModule REQUIRED
+    userProvider="ldap://ldap.mpiwg-berlin.mpg.de/ou=people,dc=mpiwg-berlin,dc=mpg,dc=de"
+    userFilter="(&(uid={USERNAME})(objectClass=inetOrgPerson))"
+    debug=true
+    useSSL=false
+;
+};
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/WEB-INF/web.xml	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app id="WebApp_ID" version="2.4"
+xmlns="http://java.sun.com/xml/ns/j2ee"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
+http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+    <display-name>RESTfulJavaWebServices-Restlet</display-name>
+    <!-- Application classname -->
+    <context-param>
+        <param-name>org.restlet.application</param-name>
+        <param-value>de.mpiwg.itgroup.annotations.restlet.RestServer</param-value>
+    </context-param>
+    <!-- Restletadapter -->
+    <servlet>
+        <servlet-name>RestletServlet</servlet-name>
+        <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
+    </servlet>
+    <!-- Catchallrequests -->
+    <servlet-mapping>
+        <servlet-name>RestletServlet</servlet-name>
+        <!-- <url-pattern>/annotator/*</url-pattern> -->
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+</web-app>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/WEB-INF/web.xml.template	Wed Jun 27 17:52:02 2012 +0200
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app id="WebApp_ID" version="2.4"
+xmlns="http://java.sun.com/xml/ns/j2ee"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
+http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+    <display-name>RESTfulJavaWebServices-Restlet</display-name>
+    <!-- Application classname -->
+    <context-param>
+        <param-name>org.restlet.application</param-name>
+        <param-value>de.mpiwg.itgroup.annotationManager.restlet.RestServer</param-value>
+    </context-param>
+    <context-param>
+        <param-name>de.mpiwg.itgroup.annotationManager.jaas.configFilePath</param-name>
+        <param-value>file:///etc/jaasAuth.conf</param-value>
+    </context-param>
+    <context-param>
+        <param-name>de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser</param-name>
+        <param-value>USERNAME</param-value>
+    </context-param>
+    <context-param>
+        <param-name>de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword</param-name>
+        <param-value>PASSWORD</param-value>
+    </context-param>
+    <context-param>
+        <param-name>de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreConnectionURL</param-name>
+        <param-value>jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111</param-value>
+    </context-param>
+    <!-- Restletadapter -->
+    <servlet>
+        <servlet-name>RestletServlet</servlet-name>
+        <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
+    </servlet>
+    <!-- Catchallrequests -->
+    <servlet-mapping>
+        <servlet-name>RestletServlet</servlet-name>
+        <url-pattern>/annotator/*</url-pattern>
+    </servlet-mapping>
+</web-app>
\ No newline at end of file