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