3 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.SPRING_SECURITY_CONTEXT_KEY
;
5 import java
.util
.Locale
;
6 import java
.util
.ResourceBundle
;
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
;
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
;
29 /** Manages history and navigation */
30 abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
implements
32 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
34 private Repository repository
;
35 private String workspace
;
36 private Session session
;
42 private Throwable exception
;
44 private BrowserNavigation history
;
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
);
58 this.repository
= repository
;
59 this.workspace
= workspace
;
62 history
= RWT
.getClient().getService(BrowserNavigation
.class);
64 history
.addBrowserNavigationListener(new CmsNavigationListener());
66 // RWT.setLocale(Locale.FRANCE);
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() {
77 if (log
.isTraceEnabled())
78 log
.trace("Logging out " + session
);
79 JcrUtils
.logoutQuietly(session
);
85 /** Recreate header UI */
86 protected abstract void refreshHeader();
88 /** Recreate body UI */
89 protected abstract void refreshBody();
91 /** Log as anonymous */
92 protected abstract void logAsAnonymous();
95 * The node to return when no node was found (for authenticated users and
98 protected abstract Node
getDefaultNode(Session session
)
99 throws RepositoryException
;
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.
106 protected String
getDefaultNewNodeType() {
107 return CmsTypes
.CMS_TEXT
;
110 /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
111 protected String
getDefaultNewFolderType() {
112 return NodeType
.NT_FOLDER
;
115 public void navigateTo(String state
) {
120 history
.pushState(state
, state
);
124 public void authChange() {
126 String currentPath
= null;
128 currentPath
= node
.getPath();
129 JcrUtils
.logoutQuietly(session
);
131 if (SecurityContextHolder
.getContext().getAuthentication() == null)
133 session
= repository
.login(workspace
);
134 if (currentPath
!= null)
135 node
= session
.getNode(currentPath
);
140 } catch (RepositoryException e
) {
141 throw new CmsException("Cannot perform auth change", e
);
147 public void exception(Throwable e
) {
149 log
.error("Unexpected exception in CMS", e
);
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
);
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
;
171 this.state
= newState
;
174 int firstSlash
= state
.indexOf('/');
175 if (firstSlash
== 0) {
176 if (!session
.nodeExists(state
))
177 node
= addNode(session
, state
, null);
179 node
= session
.getNode(state
);
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
);
190 node
= session
.getNode(path
);
191 if (!node
.isNodeType(nodeType
))
192 throw new CmsException("Node " + path
193 + " not of type " + nodeType
);
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();
201 log
.debug("Deleted " + path
);
202 navigateTo(previousState
);
204 throw new CmsException("Data " + path
205 + " does not exist");
207 if (session
.itemExists(path
))
208 node
= session
.getNode(path
);
210 throw new CmsException("Data " + path
211 + " does not exist");
215 node
= getDefaultNode(session
);
219 if (log
.isTraceEnabled())
220 log
.trace("node=" + node
+ ", state=" + state
+ " (page="
223 } catch (RepositoryException e
) {
224 throw new CmsException("Cannot retrieve node", e
);
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
235 protected Node
getNode() {
240 public String
getState() {
244 // String getPage() {
248 protected Throwable
getException() {
252 protected void resetException() {
256 protected Session
getSession() {
260 private class CmsNavigationListener
implements BrowserNavigationListener
{
261 private static final long serialVersionUID
= -3591018803430389270L;
264 public void navigated(BrowserNavigationEvent event
) {
265 setState(event
.getState());