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