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