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