]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.suite.e4.rap/src/org/argeo/suite/e4/rap/ArgeoSuiteLoginLifecycle.java
Use tabbed area.
[gpl/argeo-suite.git] / org.argeo.suite.e4.rap / src / org / argeo / suite / e4 / rap / ArgeoSuiteLoginLifecycle.java
1 package org.argeo.suite.e4.rap;
2
3 import java.security.PrivilegedActionException;
4 import java.security.PrivilegedExceptionAction;
5
6 import javax.inject.Inject;
7 import javax.jcr.Node;
8 import javax.jcr.Repository;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Session;
11 import javax.security.auth.Subject;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.cms.e4.rap.CmsLoginLifecycle;
16 import org.argeo.connect.ui.SystemWorkbenchService;
17 import org.argeo.jcr.JcrUtils;
18
19 /** Extends the CMS login lifecycle by managing the state of the current context. */
20 public class ArgeoSuiteLoginLifecycle extends CmsLoginLifecycle {
21 private final static Log log = LogFactory.getLog(ArgeoSuiteLoginLifecycle.class);
22 @Inject
23 SystemWorkbenchService systemWorkbenchService;
24
25 @Inject
26 Repository repository;
27
28 @Override
29 protected void startupComplete() {
30 loadState();
31 }
32
33 @Override
34 protected void stateChanged() {
35 loadState();
36 }
37
38 private void loadState() {
39 String state = getState();
40 // for the time being we systematically open a session, in order to make sure
41 // that home is initialised
42 Session session = null;
43 try {
44 if (state != null && state.startsWith("/")) {
45 String path = state.substring(1);
46 String workspace;
47 if (path.equals("")) {
48 workspace = null;
49 path = "/";
50 } else {
51 int index = path.indexOf('/');
52 if (index == 0) {
53 log.error("Cannot interpret // " + state);
54 getBrowserNavigation().pushState("~", null);
55 return;
56 } else if (index > 0) {
57 workspace = path.substring(0, index);
58 path = path.substring(index);
59 } else {// index<0, assuming root node
60 workspace = path;
61 path = "/";
62 }
63 }
64 Subject subject = getSubject();
65 session = Subject.doAs(subject, new PrivilegedExceptionAction<Session>() {
66
67 @Override
68 public Session run() throws PrivilegedActionException {
69 try {
70 return repository.login(workspace);
71 } catch (RepositoryException e) {
72 throw new PrivilegedActionException(e);
73 }
74 }
75
76 });
77 Node node = session.getNode(path);
78 systemWorkbenchService.openEntityEditor(node);
79 }
80 } catch (RepositoryException | PrivilegedActionException e) {
81 log.error("Cannot load state " + state, e);
82 getBrowserNavigation().pushState("~", null);
83 } finally {
84 JcrUtils.logoutQuietly(session);
85 }
86 }
87 }