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