]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/AbstractCmsEntryPoint.java
Prepare next development cycle
[lgpl/argeo-commons.git] / ui / AbstractCmsEntryPoint.java
1 package org.argeo.cms.ui;
2
3 import static org.argeo.naming.SharedSecret.X_SHARED_SECRET;
4
5 import java.security.PrivilegedAction;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.jcr.Node;
10 import javax.jcr.PathNotFoundException;
11 import javax.jcr.Property;
12 import javax.jcr.Repository;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15 import javax.jcr.nodetype.NodeType;
16 import javax.security.auth.Subject;
17 import javax.security.auth.login.LoginContext;
18 import javax.security.auth.login.LoginException;
19 import javax.servlet.http.HttpServletRequest;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.cms.CmsException;
24 import org.argeo.cms.auth.CurrentUser;
25 import org.argeo.cms.auth.HttpRequestCallbackHandler;
26 import org.argeo.eclipse.ui.specific.UiContext;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.naming.AuthPassword;
29 import org.argeo.naming.SharedSecret;
30 import org.argeo.node.NodeConstants;
31 import org.eclipse.rap.rwt.RWT;
32 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
33 import org.eclipse.rap.rwt.client.WebClient;
34 import org.eclipse.rap.rwt.client.service.BrowserNavigation;
35 import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
36 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
37 import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Shell;
41
42 /** Manages history and navigation */
43 public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint implements CmsView {
44 private static final long serialVersionUID = 906558779562569784L;
45
46 private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
47
48 // private final Subject subject;
49 private LoginContext loginContext;
50
51 private final Repository repository;
52 private final String workspace;
53 private final String defaultPath;
54 private final Map<String, String> factoryProperties;
55
56 // Current state
57 private Session session;
58 private Node node;
59 private String nodePath;// useful when changing auth
60 private String state;
61 private Throwable exception;
62
63 // Client services
64 private final JavaScriptExecutor jsExecutor;
65 private final BrowserNavigation browserNavigation;
66
67 public AbstractCmsEntryPoint(Repository repository, String workspace, String defaultPath,
68 Map<String, String> factoryProperties) {
69 this.repository = repository;
70 this.workspace = workspace;
71 this.defaultPath = defaultPath;
72 this.factoryProperties = new HashMap<String, String>(factoryProperties);
73 // subject = new Subject();
74
75 // Initial login
76 LoginContext lc;
77 try {
78 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
79 new HttpRequestCallbackHandler(UiContext.getHttpRequest(), UiContext.getHttpResponse()));
80 lc.login();
81 } catch (LoginException e) {
82 try {
83 lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS);
84 lc.login();
85 } catch (LoginException e1) {
86 throw new CmsException("Cannot log in as anonymous", e1);
87 }
88 }
89 authChange(lc);
90
91 jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
92 browserNavigation = RWT.getClient().getService(BrowserNavigation.class);
93 if (browserNavigation != null)
94 browserNavigation.addBrowserNavigationListener(new CmsNavigationListener());
95 }
96
97 @Override
98 protected Shell createShell(Display display) {
99 Shell shell = super.createShell(display);
100 shell.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_SHELL);
101 display.disposeExec(new Runnable() {
102
103 @Override
104 public void run() {
105 if (log.isTraceEnabled())
106 log.trace("Logging out " + session);
107 JcrUtils.logoutQuietly(session);
108 }
109 });
110 return shell;
111 }
112
113 @Override
114 protected final void createContents(final Composite parent) {
115 UiContext.setData(CmsView.KEY, this);
116 Subject.doAs(getSubject(), new PrivilegedAction<Void>() {
117 @Override
118 public Void run() {
119 try {
120 initUi(parent);
121 } catch (Exception e) {
122 throw new CmsException("Cannot create entrypoint contents", e);
123 }
124 return null;
125 }
126 });
127 }
128
129 /** Create UI */
130 protected abstract void initUi(Composite parent);
131
132 /** Recreate UI after navigation or auth change */
133 protected abstract void refresh();
134
135 /**
136 * The node to return when no node was found (for authenticated users and
137 * anonymous)
138 */
139 private Node getDefaultNode(Session session) throws RepositoryException {
140 if (!session.hasPermission(defaultPath, "read")) {
141 String userId = session.getUserID();
142 if (userId.equals(NodeConstants.ROLE_ANONYMOUS))
143 // TODO throw a special exception
144 throw new CmsException("Login required");
145 else
146 throw new CmsException("Unauthorized");
147 }
148 return session.getNode(defaultPath);
149 }
150
151 protected String getBaseTitle() {
152 return factoryProperties.get(WebClient.PAGE_TITLE);
153 }
154
155 public void navigateTo(String state) {
156 exception = null;
157 String title = setState(state);
158 doRefresh();
159 if (browserNavigation != null)
160 browserNavigation.pushState(state, title);
161 }
162
163 // @Override
164 // public synchronized Subject getSubject() {
165 // return subject;
166 // }
167
168 // @Override
169 // public LoginContext getLoginContext() {
170 // return loginContext;
171 // }
172 protected Subject getSubject() {
173 return loginContext.getSubject();
174 }
175
176 @Override
177 public boolean isAnonymous() {
178 return CurrentUser.isAnonymous(getSubject());
179 }
180
181 @Override
182 public synchronized void logout() {
183 if (loginContext == null)
184 throw new CmsException("Login context should not be null");
185 try {
186 CurrentUser.logoutCmsSession(loginContext.getSubject());
187 loginContext.logout();
188 LoginContext anonymousLc = new LoginContext(NodeConstants.LOGIN_CONTEXT_ANONYMOUS);
189 anonymousLc.login();
190 authChange(anonymousLc);
191 } catch (LoginException e) {
192 log.error("Cannot logout", e);
193 }
194 }
195
196 @Override
197 public synchronized void authChange(LoginContext lc) {
198 if (lc == null)
199 throw new CmsException("Login context cannot be null");
200 // logout previous login context
201 if (this.loginContext != null)
202 try {
203 this.loginContext.logout();
204 } catch (LoginException e1) {
205 log.warn("Could not log out: " + e1);
206 }
207 this.loginContext = lc;
208 Subject.doAs(getSubject(), new PrivilegedAction<Void>() {
209
210 @Override
211 public Void run() {
212 try {
213 JcrUtils.logoutQuietly(session);
214 session = repository.login(workspace);
215 if (nodePath != null)
216 try {
217 node = session.getNode(nodePath);
218 } catch (PathNotFoundException e) {
219 navigateTo("~");
220 }
221
222 // refresh UI
223 doRefresh();
224 } catch (RepositoryException e) {
225 throw new CmsException("Cannot perform auth change", e);
226 }
227 return null;
228 }
229
230 });
231 }
232
233 @Override
234 public void exception(final Throwable e) {
235 AbstractCmsEntryPoint.this.exception = e;
236 log.error("Unexpected exception in CMS", e);
237 doRefresh();
238 }
239
240 protected synchronized void doRefresh() {
241 Subject.doAs(getSubject(), new PrivilegedAction<Void>() {
242 @Override
243 public Void run() {
244 refresh();
245 return null;
246 }
247 });
248 }
249
250 /** Sets the state of the entry point and retrieve the related JCR node. */
251 protected synchronized String setState(String newState) {
252 String previousState = this.state;
253
254 String newNodePath = null;
255 String prefix = null;
256 this.state = newState;
257 if (newState.equals("~"))
258 this.state = "";
259
260 try {
261 int firstSlash = state.indexOf('/');
262 if (firstSlash == 0) {
263 newNodePath = state;
264 prefix = "";
265 } else if (firstSlash > 0) {
266 prefix = state.substring(0, firstSlash);
267 newNodePath = state.substring(firstSlash);
268 } else {
269 newNodePath = defaultPath;
270 prefix = state;
271
272 }
273
274 // auth
275 int colonIndex = prefix.indexOf('$');
276 if (colonIndex > 0) {
277 // String user = prefix.substring(0, colonIndex);
278 // // if (isAnonymous()) {
279 // String token = prefix.substring(colonIndex + 1);
280 // LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, new
281 // CallbackHandler() {
282 //
283 // @Override
284 // public void handle(Callback[] callbacks) throws IOException,
285 // UnsupportedCallbackException {
286 // for (Callback callback : callbacks) {
287 // if (callback instanceof NameCallback)
288 // ((NameCallback) callback).setName(user);
289 // else if (callback instanceof PasswordCallback)
290 // ((PasswordCallback) callback).setPassword(token.toCharArray());
291 // }
292 //
293 // }
294 // });
295 SharedSecret token = new SharedSecret(new AuthPassword(X_SHARED_SECRET + '$' + prefix));
296 LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, token);
297 lc.login();
298 authChange(lc);// sets the node as well
299 // } else {
300 // // TODO check consistency
301 // }
302 } else {
303 Node newNode = null;
304 if (session.nodeExists(newNodePath))
305 newNode = session.getNode(newNodePath);
306 else
307 throw new CmsException("Data " + newNodePath + " does not exist");
308 setNode(newNode);
309 }
310 String title = publishMetaData(getNode());
311
312 if (log.isTraceEnabled())
313 log.trace("node=" + newNodePath + ", state=" + state + " (prefix=" + prefix + ")");
314
315 return title;
316 } catch (Exception e) {
317 log.error("Cannot set state '" + state + "'", e);
318 if (state.equals("") || newState.equals("~") || newState.equals(previousState))
319 return "Unrecoverable exception : " + e.getClass().getSimpleName();
320 if (previousState.equals(""))
321 previousState = "~";
322 navigateTo(previousState);
323 throw new CmsException("Unexpected issue when accessing #" + newState, e);
324 }
325 }
326
327 private String publishMetaData(Node node) throws RepositoryException {
328 // Title
329 String title;
330 if (node.isNodeType(NodeType.MIX_TITLE) && node.hasProperty(Property.JCR_TITLE))
331 title = node.getProperty(Property.JCR_TITLE).getString() + " - " + getBaseTitle();
332 else
333 title = getBaseTitle();
334
335 HttpServletRequest request = UiContext.getHttpRequest();
336 if (request == null)
337 return null;
338
339 StringBuilder js = new StringBuilder();
340 title = title.replace("'", "\\'");// sanitize
341 js.append("document.title = '" + title + "';");
342 jsExecutor.execute(js.toString());
343 return title;
344 }
345
346 // Simply remove some illegal character
347 // private String clean(String stringToClean) {
348 // return stringToClean.replaceAll("'", "").replaceAll("\\n", "")
349 // .replaceAll("\\t", "");
350 // }
351
352 protected synchronized Node getNode() {
353 return node;
354 }
355
356 private synchronized void setNode(Node node) throws RepositoryException {
357 this.node = node;
358 this.nodePath = node == null ? null : node.getPath();
359 }
360
361 protected String getState() {
362 return state;
363 }
364
365 protected Throwable getException() {
366 return exception;
367 }
368
369 protected void resetException() {
370 exception = null;
371 }
372
373 protected Session getSession() {
374 return session;
375 }
376
377 private class CmsNavigationListener implements BrowserNavigationListener {
378 private static final long serialVersionUID = -3591018803430389270L;
379
380 @Override
381 public void navigated(BrowserNavigationEvent event) {
382 setState(event.getState());
383 doRefresh();
384 }
385 }
386 }