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