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