]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractCmsEntryPoint.java
60758da887d63b66b80af07bf4317cab780b0690
[lgpl/argeo-commons.git] / AbstractCmsEntryPoint.java
1 package org.argeo.cms;
2
3 import static org.argeo.cms.internal.kernel.KernelConstants.SPRING_SECURITY_CONTEXT_KEY;
4
5 import java.util.Locale;
6 import java.util.ResourceBundle;
7
8 import javax.jcr.Node;
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.nodetype.NodeType;
13 import javax.servlet.http.HttpSession;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.cms.i18n.Msg;
18 import org.argeo.jcr.JcrUtils;
19 import org.eclipse.rap.rwt.RWT;
20 import org.eclipse.rap.rwt.application.AbstractEntryPoint;
21 import org.eclipse.rap.rwt.client.service.BrowserNavigation;
22 import org.eclipse.rap.rwt.client.service.BrowserNavigationEvent;
23 import org.eclipse.rap.rwt.client.service.BrowserNavigationListener;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Shell;
26 import org.springframework.security.core.context.SecurityContext;
27 import org.springframework.security.core.context.SecurityContextHolder;
28
29 /** Manages history and navigation */
30 abstract class AbstractCmsEntryPoint extends AbstractEntryPoint implements
31 CmsSession {
32 private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class);
33
34 private Repository repository;
35 private String workspace;
36 private Session session;
37
38 // current state
39 private Node node;
40 private String state;
41 private String page;
42 private Throwable exception;
43
44 private BrowserNavigation history;
45
46 public AbstractCmsEntryPoint(Repository repository, String workspace) {
47 if (SecurityContextHolder.getContext().getAuthentication() == null) {
48 HttpSession httpSession = RWT.getRequest().getSession();
49 // log.debug("Session: " + httpSession.getId());
50 SecurityContext contextFromSessionObject = (SecurityContext) httpSession
51 .getAttribute(SPRING_SECURITY_CONTEXT_KEY);
52 if (contextFromSessionObject != null)
53 SecurityContextHolder.setContext(contextFromSessionObject);
54 else
55 logAsAnonymous();
56 }
57
58 this.repository = repository;
59 this.workspace = workspace;
60 authChange();
61
62 history = RWT.getClient().getService(BrowserNavigation.class);
63 if (history != null)
64 history.addBrowserNavigationListener(new CmsNavigationListener());
65
66 // RWT.setLocale(Locale.FRANCE);
67 }
68
69 @Override
70 protected Shell createShell(Display display) {
71 Shell shell = super.createShell(display);
72 shell.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_SHELL);
73 display.disposeExec(new Runnable() {
74
75 @Override
76 public void run() {
77 if (log.isTraceEnabled())
78 log.trace("Logging out " + session);
79 JcrUtils.logoutQuietly(session);
80 }
81 });
82 return shell;
83 }
84
85 /** Recreate header UI */
86 protected abstract void refreshHeader();
87
88 /** Recreate body UI */
89 protected abstract void refreshBody();
90
91 /** Log as anonymous */
92 protected abstract void logAsAnonymous();
93
94 /**
95 * The node to return when no node was found (for authenticated users and
96 * anonymous)
97 */
98 protected abstract Node getDefaultNode(Session session)
99 throws RepositoryException;
100
101 /**
102 * Reasonable default since it is a nt:hierarchyNode and is thus compatible
103 * with the obvious default folder type, nt:folder, conceptual equivalent of
104 * an empty text file in an operating system. To be overridden.
105 */
106 protected String getDefaultNewNodeType() {
107 return CmsTypes.CMS_TEXT;
108 }
109
110 /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
111 protected String getDefaultNewFolderType() {
112 return NodeType.NT_FOLDER;
113 }
114
115 public void navigateTo(String state) {
116 exception = null;
117 setState(state);
118 refreshBody();
119 if (history != null)
120 history.pushState(state, state);
121 }
122
123 @Override
124 public void authChange() {
125 try {
126 String currentPath = null;
127 if (node != null)
128 currentPath = node.getPath();
129 JcrUtils.logoutQuietly(session);
130
131 if (SecurityContextHolder.getContext().getAuthentication() == null)
132 logAsAnonymous();
133 session = repository.login(workspace);
134 if (currentPath != null)
135 node = session.getNode(currentPath);
136
137 // refresh UI
138 refreshHeader();
139 refreshBody();
140 } catch (RepositoryException e) {
141 throw new CmsException("Cannot perform auth change", e);
142 }
143
144 }
145
146 @Override
147 public void exception(Throwable e) {
148 this.exception = e;
149 log.error("Unexpected exception in CMS", e);
150 refreshBody();
151 }
152
153 @Override
154 public Object local(Msg msg) {
155 String key = msg.getId();
156 int lastDot = key.lastIndexOf('.');
157 String className = key.substring(0, lastDot);
158 String fieldName = key.substring(lastDot + 1);
159 Locale locale = RWT.getLocale();
160 ResourceBundle rb = ResourceBundle.getBundle(className, locale,
161 msg.getClassLoader());
162 return rb.getString(fieldName);
163 }
164
165 /** Sets the state of the entry point and retrieve the related JCR node. */
166 protected synchronized void setState(String newState) {
167 String previousState = this.state;
168
169 node = null;
170 page = null;
171 this.state = newState;
172
173 try {
174 int firstSlash = state.indexOf('/');
175 if (firstSlash == 0) {
176 if (!session.nodeExists(state))
177 node = addNode(session, state, null);
178 else
179 node = session.getNode(state);
180 page = "";
181 } else if (firstSlash > 0) {
182 String prefix = state.substring(0, firstSlash);
183 String path = state.substring(firstSlash);
184 if (session.getWorkspace().getNodeTypeManager()
185 .hasNodeType(prefix)) {
186 String nodeType = prefix;
187 if (!session.nodeExists(path))
188 node = addNode(session, path, nodeType);
189 else {
190 node = session.getNode(path);
191 if (!node.isNodeType(nodeType))
192 throw new CmsException("Node " + path
193 + " not of type " + nodeType);
194 }
195 } else if ("delete".equals(prefix)) {
196 if (session.itemExists(path)) {
197 Node nodeToDelete = session.getNode(path);
198 // TODO "Are you sure?"
199 nodeToDelete.remove();
200 session.save();
201 log.debug("Deleted " + path);
202 navigateTo(previousState);
203 } else
204 throw new CmsException("Data " + path
205 + " does not exist");
206 } else {
207 if (session.itemExists(path))
208 node = session.getNode(path);
209 else
210 throw new CmsException("Data " + path
211 + " does not exist");
212 }
213 page = prefix;
214 } else {
215 node = getDefaultNode(session);
216 page = state;
217 }
218
219 if (log.isTraceEnabled())
220 log.trace("node=" + node + ", state=" + state + " (page="
221 + page);
222
223 } catch (RepositoryException e) {
224 throw new CmsException("Cannot retrieve node", e);
225 }
226 }
227
228 protected Node addNode(Session session, String path, String nodeType)
229 throws RepositoryException {
230 return JcrUtils.mkdirs(session, path, nodeType != null ? nodeType
231 : getDefaultNewNodeType(), getDefaultNewFolderType(), false);
232 // not saved, so that the UI can discard it later on
233 }
234
235 protected Node getNode() {
236 return node;
237 }
238
239 @Override
240 public String getState() {
241 return state;
242 }
243
244 // String getPage() {
245 // return page;
246 // }
247
248 protected Throwable getException() {
249 return exception;
250 }
251
252 protected void resetException() {
253 exception = null;
254 }
255
256 protected Session getSession() {
257 return session;
258 }
259
260 private class CmsNavigationListener implements BrowserNavigationListener {
261 private static final long serialVersionUID = -3591018803430389270L;
262
263 @Override
264 public void navigated(BrowserNavigationEvent event) {
265 setState(event.getState());
266 refreshBody();
267 }
268 }
269
270 }