annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java @ 1:6556943c4fb9

include neo4j and restlet
author casties
date Wed, 27 Jun 2012 17:52:02 +0200
parents
children 47b53ae385d1
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;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
22 import org.restlet.Application;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
23 import org.restlet.Context;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
24 import org.restlet.Restlet;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
25 import org.restlet.routing.Router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
26 import org.restlet.security.ChallengeAuthenticator;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
27
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
28 import scala.sys.process.ProcessBuilderImpl.Dummy;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
29
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
30 public class RestServer extends Application {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
31
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
32 public static Logger logger = Logger.getRootLogger();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
33
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
34 private ChallengeAuthenticator authenticator;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
35
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
36 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
37 * Properties holding consumer keys and secrets
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
38 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
39 private Properties consumerKeys;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
40 public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
41
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
42 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
43 * constructor
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
44 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
45 * @param parentContext
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
46 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
47 public RestServer(Context parentContext) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
48 super(parentContext);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
49
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
50 Logger rl = Logger.getRootLogger();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
51 BasicConfigurator.configure();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
52 // read consumerKeys from webapp
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
53 consumerKeys = new Properties();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
54 ServletContext sc = (ServletContext) getContext().getServerDispatcher()
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
55 .getContext().getAttributes()
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
56 .get("org.restlet.ext.servlet.ServletContext");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
57 if (sc != null) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
58 InputStream ps = sc.getResourceAsStream(CONSUMER_KEYS_PATH);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
59 if (ps == null) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
60 // try as file
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
61 File pf = new File(sc.getRealPath(CONSUMER_KEYS_PATH));
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
62 if (pf != null) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
63 rl.debug("trying file for consumer keys: "+pf);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
64 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
65 ps = new FileInputStream(pf);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
66 } catch (FileNotFoundException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
67 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
68 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
69 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
70 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
71 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
72 if (ps != null) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
73 rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
74 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
75 consumerKeys.load(ps);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
76 } catch (IOException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
77 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
78 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
79 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
80 rl.debug("consumer keys: "+consumerKeys);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
81 } else {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
82 rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
83 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
84 } else {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
85 rl.error("Unable to get ServletContext!");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
86 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
87
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
88 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
89
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
90 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
91 * returns consumer secret for consumer key.
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
92 * returns null if consumer key doesn't exist.
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
93 * @param consumerKey
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
94 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
95 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
96 public String getConsumerSecret(String consumerKey) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
97 return consumerKeys.getProperty(consumerKey);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
98 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
99
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
100
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
101 /*
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
102 * (non-Javadoc)
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
103 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
104 * @see org.restlet.Application#createInboundRoot()
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
105 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
106 @Override
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
107 public Restlet createInboundRoot() {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
108 //this.authenticator = createAuthenticator();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
109
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
110 // String target = "{rh}/{rf}/XX";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
111 // Redirector redirector = new
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
112 // Redirector(getContext().createChildContext(), target,
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
113 // Redirector.MODE_CLIENT_SEE_OTHER);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
114
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
115 Router router = new Router(getContext());
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
116 /*
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
117 router.attach("/annotator/annotations", AnnotatorAnnotations.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
118 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
119 router.attach("/annotator/search", AnnotatorSearch.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
120
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
121 // router.attach("",redirector);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
122 router.attach("/annotator", ExtendedAnnotationInput.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
123 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
124 router.attach("/", AnnotatorInfo.class);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
125 //authenticator.setNext(router);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
126 //return authenticator;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
127
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
128 return router;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
129 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
130
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
131 /**
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
132 * Hole den vollen Benutzernamen aus dem LDAP
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
133 *
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
134 * @param creator
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
135 * @return
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
136 */
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
137 public String getUserNameFromLdap(String creator) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
138 String retString = creator; // falls nichts gefunden wird einfach den
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
139 // creator zurueckgeben
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
140 Hashtable<String, String> env = new Hashtable<String, String>();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
141 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
142 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
143
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
144 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; // TODO should go into config file
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
145 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
146
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
147 DirContext dctx;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
148 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
149 dctx = new InitialDirContext(env);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
150 } catch (NamingException e1) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
151 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
152 e1.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
153 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
154 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
155
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
156 String base = "ou=People";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
157
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
158 SearchControls sc = new SearchControls();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
159 String[] attributeFilter = { "cn", "mail" };
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
160 sc.setReturningAttributes(attributeFilter);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
161 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
162
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
163 String filter = "(uid=" + creator + ")";
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
164
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
165 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
166 NamingEnumeration<SearchResult> results = dctx.search(base, filter,
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
167 sc);
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
168 while (results.hasMore()) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
169 SearchResult sr = (SearchResult) results.next();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
170 javax.naming.directory.Attributes attrs = sr.getAttributes();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
171
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
172 Attribute attr = attrs.get("cn");
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
173 retString = (String) attr.get();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
174 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
175 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
176 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
177 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
178 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
179
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
180 try {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
181 dctx.close();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
182 } catch (NamingException e) {
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
183 // TODO Auto-generated catch block
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
184 e.printStackTrace();
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
185 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
186 return retString;
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
187 }
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
188
6556943c4fb9 include neo4j and restlet
casties
parents:
diff changeset
189 }