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