]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/AbstractCmsEntryPoint.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / AbstractCmsEntryPoint.java
1 package org.argeo.cms;
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.Property;
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.nodetype.NodeType;
13 import javax.security.auth.Subject;
14 import javax.security.auth.login.CredentialNotFoundException;
15 import javax.security.auth.login.LoginContext;
16 import javax.security.auth.login.LoginException;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.ArgeoException;
21 import org.argeo.cms.auth.AuthConstants;
22 import org.argeo.cms.auth.HttpRequestCallbackHandler;
23 import org.argeo.eclipse.ui.specific.UiContext;
24 import org.argeo.jcr.JcrUtils;
25 import org.eclipse.rap.rwt.RWT;
26 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
27 import org.eclipse.rap.rwt.client.WebClient;
28 import org.eclipse.rap.rwt.client.service.BrowserNavigation;
29 import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
30 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
31 import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Shell;
35
36 /** Manages history and navigation */
37 public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint
38 implements CmsView {
39 private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
40
41 private final Subject subject;
42 private LoginContext loginContext;
43
44 private final Repository repository;
45 private final String workspace;
46 private final String defaultPath;
47 private final Map<String, String> factoryProperties;
48
49 // Current state
50 private Session session;
51 private Node node;
52 private String state;
53 private String page;
54 private Throwable exception;
55
56 // Client services
57 private final JavaScriptExecutor jsExecutor;
58 private final BrowserNavigation browserNavigation;
59
60 public AbstractCmsEntryPoint(Repository repository, String workspace,
61 String defaultPath, Map<String, String> factoryProperties) {
62 this.repository = repository;
63 this.workspace = workspace;
64 this.defaultPath = defaultPath;
65 this.factoryProperties = new HashMap<String, String>(factoryProperties);
66 subject = new Subject();
67
68 // Initial login
69 try {
70 loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER,
71 subject, new HttpRequestCallbackHandler(
72 UiContext.getHttpRequest()));
73 loginContext.login();
74 } catch (CredentialNotFoundException e) {
75 try {
76 loginContext = new LoginContext(
77 AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject);
78 loginContext.login();
79 } catch (LoginException e1) {
80 throw new ArgeoException("Cannot log as anonymous", e);
81 }
82 } catch (LoginException e) {
83 throw new ArgeoException("Cannot initialize subject", e);
84 }
85 authChange(loginContext);
86
87 jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
88 browserNavigation = RWT.getClient().getService(BrowserNavigation.class);
89 if (browserNavigation != null)
90 browserNavigation
91 .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(subject, 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",
120 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 if (session.getUserID().equals(AuthConstants.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 Subject getSubject() {
162 return subject;
163 }
164
165 @Override
166 public void logout() {
167 if (loginContext == null)
168 throw new CmsException("Login context should not be null");
169 try {
170 loginContext.logout();
171 LoginContext anonymousLc = new LoginContext(
172 AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject);
173 anonymousLc.login();
174 authChange(anonymousLc);
175 } catch (LoginException e) {
176 throw new CmsException("Cannot logout", e);
177 }
178 }
179
180 @Override
181 public void authChange(LoginContext loginContext) {
182 if (loginContext == null)
183 throw new CmsException("Login context cannot be null");
184 this.loginContext = loginContext;
185 Subject.doAs(subject, new PrivilegedAction<Void>() {
186
187 @Override
188 public Void run() {
189 try {
190 String currentPath = null;
191 if (node != null)
192 currentPath = node.getPath();
193 JcrUtils.logoutQuietly(session);
194
195 session = repository.login(workspace);
196 if (currentPath != null)
197 try {
198 node = session.getNode(currentPath);
199 } catch (Exception e) {
200 logout();
201 session = repository.login(workspace);
202 navigateTo("~");
203 throw e;
204 }
205
206 // refresh UI
207 doRefresh();
208 } catch (RepositoryException e) {
209 throw new CmsException("Cannot perform auth change", e);
210 }
211 return null;
212 }
213
214 });
215
216 }
217
218 @Override
219 public void exception(final Throwable e) {
220 AbstractCmsEntryPoint.this.exception = e;
221 log.error("Unexpected exception in CMS", e);
222 doRefresh();
223 }
224
225 protected void doRefresh() {
226 Subject.doAs(subject, new PrivilegedAction<Void>() {
227 @Override
228 public Void run() {
229 refresh();
230 return null;
231 }
232 });
233 }
234
235 // @Override
236 // public Object local(Msg msg) {
237 // String key = msg.getId();
238 // int lastDot = key.lastIndexOf('.');
239 // String className = key.substring(0, lastDot);
240 // String fieldName = key.substring(lastDot + 1);
241 // Locale locale = RWT.getLocale();
242 // ResourceBundle rb = ResourceBundle.getBundle(className, locale,
243 // msg.getClassLoader());
244 // return rb.getString(fieldName);
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 = 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 if (session.nodeExists(state))
261 node = session.getNode(state);
262 else
263 throw new CmsException("Data " + state + " does not exist");
264 page = "";
265 } else if (firstSlash > 0) {
266 String prefix = state.substring(0, firstSlash);
267 String path = state.substring(firstSlash);
268 if (session.nodeExists(path))
269 node = session.getNode(path);
270 else
271 throw new CmsException("Data " + path + " does not exist");
272 page = prefix;
273 } else {
274 node = getDefaultNode(session);
275 page = state;
276 }
277
278 // Title
279 String title;
280 if (node.isNodeType(NodeType.MIX_TITLE)
281 && node.hasProperty(Property.JCR_TITLE))
282 title = node.getProperty(Property.JCR_TITLE).getString()
283 + " - " + getBaseTitle();
284 else
285 title = getBaseTitle();
286 jsExecutor.execute("document.title = \"" + title + "\"");
287
288 if (log.isTraceEnabled())
289 log.trace("node=" + node + ", state=" + state + " (page="
290 + page + ", title=" + title + ")");
291
292 return title;
293 } catch (Exception e) {
294 log.error("Cannot set state '" + state + "'", e);
295 if (previousState.equals(""))
296 previousState = "~";
297 navigateTo(previousState);
298 throw new CmsException("Unexpected issue when accessing #"
299 + newState, e);
300 }
301 }
302
303 protected Node getNode() {
304 return node;
305 }
306
307 protected String getState() {
308 return state;
309 }
310
311 protected Throwable getException() {
312 return exception;
313 }
314
315 protected void resetException() {
316 exception = null;
317 }
318
319 protected Session getSession() {
320 return session;
321 }
322
323 private class CmsNavigationListener implements BrowserNavigationListener {
324 private static final long serialVersionUID = -3591018803430389270L;
325
326 @Override
327 public void navigated(BrowserNavigationEvent event) {
328 setState(event.getState());
329 refresh();
330 }
331 }
332 }