1
|
1 package de.mpiwg.itgroup.annotations.restlet;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.FileInputStream;
|
|
5 import java.io.FileNotFoundException;
|
|
6 import java.io.IOException;
|
|
7 import java.io.InputStream;
|
|
8 import java.util.Hashtable;
|
|
9 import java.util.Properties;
|
|
10
|
|
11 import javax.naming.NamingEnumeration;
|
|
12 import javax.naming.NamingException;
|
|
13 import javax.naming.directory.Attribute;
|
|
14 import javax.naming.directory.DirContext;
|
|
15 import javax.naming.directory.InitialDirContext;
|
|
16 import javax.naming.directory.SearchControls;
|
|
17 import javax.naming.directory.SearchResult;
|
|
18 import javax.servlet.ServletContext;
|
|
19
|
|
20 import org.apache.log4j.BasicConfigurator;
|
|
21 import org.apache.log4j.Logger;
|
|
22 import org.restlet.Application;
|
|
23 import org.restlet.Context;
|
|
24 import org.restlet.Restlet;
|
|
25 import org.restlet.routing.Router;
|
|
26 import org.restlet.security.ChallengeAuthenticator;
|
|
27
|
|
28 import scala.sys.process.ProcessBuilderImpl.Dummy;
|
|
29
|
|
30 public class RestServer extends Application {
|
|
31
|
|
32 public static Logger logger = Logger.getRootLogger();
|
|
33
|
|
34 private ChallengeAuthenticator authenticator;
|
|
35
|
|
36 /**
|
|
37 * Properties holding consumer keys and secrets
|
|
38 */
|
|
39 private Properties consumerKeys;
|
|
40 public final String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
|
|
41
|
|
42 /**
|
|
43 * constructor
|
|
44 *
|
|
45 * @param parentContext
|
|
46 */
|
|
47 public RestServer(Context parentContext) {
|
|
48 super(parentContext);
|
|
49
|
|
50 Logger rl = Logger.getRootLogger();
|
|
51 BasicConfigurator.configure();
|
|
52 // read consumerKeys from webapp
|
|
53 consumerKeys = new Properties();
|
|
54 ServletContext sc = (ServletContext) getContext().getServerDispatcher()
|
|
55 .getContext().getAttributes()
|
|
56 .get("org.restlet.ext.servlet.ServletContext");
|
|
57 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 }
|
|
70 }
|
|
71 }
|
|
72 if (ps != null) {
|
|
73 rl.debug("loading consumer keys from "+CONSUMER_KEYS_PATH);
|
|
74 try {
|
|
75 consumerKeys.load(ps);
|
|
76 } catch (IOException e) {
|
|
77 // TODO Auto-generated catch block
|
|
78 e.printStackTrace();
|
|
79 }
|
|
80 rl.debug("consumer keys: "+consumerKeys);
|
|
81 } else {
|
|
82 rl.error("Unable to get resource "+CONSUMER_KEYS_PATH);
|
|
83 }
|
|
84 } 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.
|
|
93 * @param consumerKey
|
|
94 * @return
|
|
95 */
|
|
96 public String getConsumerSecret(String consumerKey) {
|
|
97 return consumerKeys.getProperty(consumerKey);
|
|
98 }
|
|
99
|
|
100
|
|
101 /*
|
|
102 * (non-Javadoc)
|
|
103 *
|
|
104 * @see org.restlet.Application#createInboundRoot()
|
|
105 */
|
|
106 @Override
|
|
107 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);
|
|
114
|
|
115 Router router = new Router(getContext());
|
|
116 /*
|
|
117 router.attach("/annotator/annotations", AnnotatorAnnotations.class);
|
|
118 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 */
|
|
124 router.attach("/", AnnotatorInfo.class);
|
|
125 //authenticator.setNext(router);
|
|
126 //return authenticator;
|
|
127
|
|
128 return router;
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * Hole den vollen Benutzernamen aus dem LDAP
|
|
133 *
|
|
134 * @param creator
|
|
135 * @return
|
|
136 */
|
|
137 public String getUserNameFromLdap(String creator) {
|
|
138 String retString = creator; // falls nichts gefunden wird einfach den
|
|
139 // creator zurueckgeben
|
|
140 Hashtable<String, String> env = new Hashtable<String, String>();
|
|
141 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
|
|
142 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
|
|
143
|
|
144 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; // TODO should go into config file
|
|
145 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
|
|
146
|
|
147 DirContext dctx;
|
|
148 try {
|
|
149 dctx = new InitialDirContext(env);
|
|
150 } catch (NamingException e1) {
|
|
151 // TODO Auto-generated catch block
|
|
152 e1.printStackTrace();
|
|
153 return retString;
|
|
154 }
|
|
155
|
|
156 String base = "ou=People";
|
|
157
|
|
158 SearchControls sc = new SearchControls();
|
|
159 String[] attributeFilter = { "cn", "mail" };
|
|
160 sc.setReturningAttributes(attributeFilter);
|
|
161 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
162
|
|
163 String filter = "(uid=" + creator + ")";
|
|
164
|
|
165 try {
|
|
166 NamingEnumeration<SearchResult> results = dctx.search(base, filter,
|
|
167 sc);
|
|
168 while (results.hasMore()) {
|
|
169 SearchResult sr = (SearchResult) results.next();
|
|
170 javax.naming.directory.Attributes attrs = sr.getAttributes();
|
|
171
|
|
172 Attribute attr = attrs.get("cn");
|
|
173 retString = (String) attr.get();
|
|
174 }
|
|
175 } catch (NamingException e) {
|
|
176 // TODO Auto-generated catch block
|
|
177 e.printStackTrace();
|
|
178 }
|
|
179
|
|
180 try {
|
|
181 dctx.close();
|
|
182 } catch (NamingException e) {
|
|
183 // TODO Auto-generated catch block
|
|
184 e.printStackTrace();
|
|
185 }
|
|
186 return retString;
|
|
187 }
|
|
188
|
|
189 }
|