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