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