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