]> 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.AccessControlContext;
4 import java.security.PrivilegedAction;
5 import java.util.HashMap;
6 import java.util.Locale;
7 import java.util.Map;
8 import java.util.ResourceBundle;
9
10 import javax.jcr.Node;
11 import javax.jcr.Property;
12 import javax.jcr.Repository;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15 import javax.jcr.nodetype.NodeType;
16 import javax.security.auth.Subject;
17 import javax.security.auth.login.LoginException;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpSession;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.argeo.ArgeoException;
24 import org.argeo.cms.auth.ArgeoLoginContext;
25 import org.argeo.cms.auth.LoginRequiredException;
26 import org.argeo.cms.i18n.Msg;
27 import org.argeo.jcr.JcrUtils;
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
41 implements CmsSession {
42 private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
43
44 private final Subject subject;
45
46 private final Repository repository;
47 private final String workspace;
48 private final String defaultPath;
49 private final Map<String, String> factoryProperties;
50
51 // Current state
52 private Session session;
53 private Node node;
54 private String state;
55 private String page;
56 private Throwable exception;
57
58 // Client services
59 private final JavaScriptExecutor jsExecutor;
60 private final BrowserNavigation browserNavigation;
61
62 public AbstractCmsEntryPoint(Repository repository, String workspace,
63 String defaultPath, Map<String, String> factoryProperties) {
64 this.repository = repository;
65 this.workspace = workspace;
66 this.defaultPath = defaultPath;
67 this.factoryProperties = new HashMap<String, String>(factoryProperties);
68
69 // load context from session
70 HttpServletRequest httpRequest = RWT.getRequest();
71 final HttpSession httpSession = httpRequest.getSession();
72 AccessControlContext acc = (AccessControlContext) httpSession
73 .getAttribute(KernelHeader.ACCESS_CONTROL_CONTEXT);
74 if (acc != null)
75 subject = Subject.getSubject(acc);
76 else
77 subject = new Subject();
78
79 // Initial login
80 try {
81 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_USER, subject)
82 .login();
83 } catch (LoginException e) {
84 // if (log.isTraceEnabled())
85 // log.trace("Cannot authenticate user", e);
86 try {
87 new ArgeoLoginContext(KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
88 subject).login();
89 } catch (LoginException eAnonymous) {
90 throw new ArgeoException("Cannot initialize subject",
91 eAnonymous);
92 }
93 }
94 authChange();
95
96 jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
97 browserNavigation = RWT.getClient().getService(BrowserNavigation.class);
98 if (browserNavigation != null)
99 browserNavigation
100 .addBrowserNavigationListener(new CmsNavigationListener());
101 }
102
103 @Override
104 protected Shell createShell(Display display) {
105 Shell shell = super.createShell(display);
106 shell.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_SHELL);
107 display.disposeExec(new Runnable() {
108
109 @Override
110 public void run() {
111 if (log.isTraceEnabled())
112 log.trace("Logging out " + session);
113 JcrUtils.logoutQuietly(session);
114 }
115 });
116 return shell;
117 }
118
119 @Override
120 protected final void createContents(final Composite parent) {
121 getShell().getDisplay().setData(CmsSession.KEY, this);
122 Subject.doAs(subject, new PrivilegedAction<Void>() {
123 @Override
124 public Void run() {
125 try {
126 createUi(parent);
127 } catch (Exception e) {
128 throw new CmsException("Cannot create entrypoint contents",
129 e);
130 }
131 return null;
132 }
133 });
134 }
135
136 /** Create UI */
137 protected abstract void createUi(Composite parent);
138
139 /** Recreate UI after navigation or auth change */
140 protected abstract void refresh();
141
142 /**
143 * The node to return when no node was found (for authenticated users and
144 * anonymous)
145 */
146 protected Node getDefaultNode(Session session) throws RepositoryException {
147 if (!session.hasPermission(defaultPath, "read")) {
148 if (session.getUserID().equals("anonymous"))
149 throw new LoginRequiredException();
150 else
151 throw new CmsException("Unauthorized");
152 }
153 return session.getNode(defaultPath);
154 }
155
156 protected String getBaseTitle() {
157 return factoryProperties.get(WebClient.PAGE_TITLE);
158 }
159
160 public void navigateTo(String state) {
161 exception = null;
162 String title = setState(state);
163 doRefresh();
164 if (browserNavigation != null)
165 browserNavigation.pushState(state, title);
166 }
167
168 @Override
169 public Subject getSubject() {
170 return subject;
171 }
172
173 @Override
174 public void authChange() {
175 Subject.doAs(subject, new PrivilegedAction<Void>() {
176
177 @Override
178 public Void run() {
179 try {
180 String currentPath = null;
181 if (node != null)
182 currentPath = node.getPath();
183 JcrUtils.logoutQuietly(session);
184
185 session = repository.login(workspace);
186 if (currentPath != null)
187 try {
188 node = session.getNode(currentPath);
189 } catch (Exception e) {
190 try {
191 // TODO find a less hacky way to log out
192 new ArgeoLoginContext(
193 KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
194 subject).logout();
195 new ArgeoLoginContext(
196 KernelHeader.LOGIN_CONTEXT_ANONYMOUS,
197 subject).login();
198 } catch (LoginException eAnonymous) {
199 throw new ArgeoException(
200 "Cannot reset to anonymous", eAnonymous);
201 }
202 JcrUtils.logoutQuietly(session);
203 session = repository.login(workspace);
204 navigateTo("~");
205 throw e;
206 }
207
208 // refresh UI
209 doRefresh();
210 } catch (RepositoryException e) {
211 throw new CmsException("Cannot perform auth change", e);
212 }
213 return null;
214 }
215
216 });
217
218 }
219
220 @Override
221 public void exception(final Throwable e) {
222 AbstractCmsEntryPoint.this.exception = e;
223 log.error("Unexpected exception in CMS", e);
224 doRefresh();
225 }
226
227 protected void doRefresh() {
228 Subject.doAs(subject, new PrivilegedAction<Void>() {
229 @Override
230 public Void run() {
231 refresh();
232 return null;
233 }
234 });
235 }
236
237 @Override
238 public Object local(Msg msg) {
239 String key = msg.getId();
240 int lastDot = key.lastIndexOf('.');
241 String className = key.substring(0, lastDot);
242 String fieldName = key.substring(lastDot + 1);
243 Locale locale = RWT.getLocale();
244 ResourceBundle rb = ResourceBundle.getBundle(className, locale,
245 msg.getClassLoader());
246 return rb.getString(fieldName);
247 }
248
249 /** Sets the state of the entry point and retrieve the related JCR node. */
250 protected synchronized String setState(String newState) {
251 String previousState = this.state;
252
253 node = null;
254 page = null;
255 this.state = newState;
256 if (newState.equals("~"))
257 this.state = "";
258
259 try {
260 int firstSlash = state.indexOf('/');
261 if (firstSlash == 0) {
262 if (session.nodeExists(state))
263 node = session.getNode(state);
264 else
265 throw new CmsException("Data " + state + " does not exist");
266 page = "";
267 } else if (firstSlash > 0) {
268 String prefix = state.substring(0, firstSlash);
269 String path = state.substring(firstSlash);
270 if (session.nodeExists(path))
271 node = session.getNode(path);
272 else
273 throw new CmsException("Data " + path + " does not exist");
274 page = prefix;
275 } else {
276 node = getDefaultNode(session);
277 page = state;
278 }
279
280 // Title
281 String title;
282 if (node.isNodeType(NodeType.MIX_TITLE)
283 && node.hasProperty(Property.JCR_TITLE))
284 title = node.getProperty(Property.JCR_TITLE).getString()
285 + " - " + getBaseTitle();
286 else
287 title = getBaseTitle();
288 jsExecutor.execute("document.title = \"" + title + "\"");
289
290 if (log.isTraceEnabled())
291 log.trace("node=" + node + ", state=" + state + " (page="
292 + page + ", title=" + title + ")");
293
294 return title;
295 } catch (Exception e) {
296 log.error("Cannot set state '" + state + "'", e);
297 if (previousState.equals(""))
298 previousState = "~";
299 navigateTo(previousState);
300 throw new CmsException("Unexpected issue when accessing #"
301 + newState, e);
302 }
303 }
304
305 protected Node getNode() {
306 return node;
307 }
308
309 protected String getState() {
310 return state;
311 }
312
313 protected Throwable getException() {
314 return exception;
315 }
316
317 protected void resetException() {
318 exception = null;
319 }
320
321 protected Session getSession() {
322 return session;
323 }
324
325 private class CmsNavigationListener implements BrowserNavigationListener {
326 private static final long serialVersionUID = -3591018803430389270L;
327
328 @Override
329 public void navigated(BrowserNavigationEvent event) {
330 setState(event.getState());
331 refresh();
332 }
333 }
334 }