3 import java
.util
.Locale
;
4 import java
.util
.ResourceBundle
;
7 import javax
.jcr
.Repository
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Session
;
10 import javax
.jcr
.nodetype
.NodeType
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.eclipse
.rap
.rwt
.RWT
;
16 import org
.eclipse
.rap
.rwt
.application
.AbstractEntryPoint
;
17 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigation
;
18 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationEvent
;
19 import org
.eclipse
.rap
.rwt
.client
.service
.BrowserNavigationListener
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
22 import org
.springframework
.security
.core
.context
.SecurityContext
;
23 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
25 /** Manages history and navigation */
26 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
27 implements CmsSession
{
28 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
29 private static final String SPRING_SECURITY_CONTEXT_KEY
= "SPRING_SECURITY_CONTEXT";
31 private Repository repository
;
32 private String workspace
;
33 private Session session
;
39 private Throwable exception
;
41 private BrowserNavigation history
;
43 public AbstractCmsEntryPoint(Repository repository
, String workspace
) {
44 if (SecurityContextHolder
.getContext().getAuthentication() == null) {
45 SecurityContext contextFromSessionObject
= (SecurityContext
) RWT
46 .getRequest().getSession()
47 .getAttribute(SPRING_SECURITY_CONTEXT_KEY
);
48 if (contextFromSessionObject
!= null)
49 SecurityContextHolder
.setContext(contextFromSessionObject
);
54 this.repository
= repository
;
55 this.workspace
= workspace
;
58 history
= RWT
.getClient().getService(BrowserNavigation
.class);
60 history
.addBrowserNavigationListener(new CmsNavigationListener());
62 // RWT.setLocale(Locale.FRANCE);
66 protected Shell
createShell(Display display
) {
67 Shell shell
= super.createShell(display
);
68 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
69 display
.disposeExec(new Runnable() {
73 if (log
.isTraceEnabled())
74 log
.trace("Logging out " + session
);
75 JcrUtils
.logoutQuietly(session
);
81 /** Recreate header UI */
82 protected abstract void refreshHeader();
84 /** Recreate body UI */
85 protected abstract void refreshBody();
87 /** Log as anonymous */
88 protected abstract void logAsAnonymous();
91 * The node to return when no node was found (for authenticated users and
94 protected abstract Node
getDefaultNode(Session session
)
95 throws RepositoryException
;
98 * Reasonable default since it is a nt:hierarchyNode and is thus compatible
99 * with the obvious default folder type, nt:folder, conceptual equivalent of
100 * an empty text file in an operating system. To be overridden.
102 protected String
getDefaultNewNodeType() {
103 return CmsTypes
.CMS_TEXT
;
106 /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
107 protected String
getDefaultNewFolderType() {
108 return NodeType
.NT_FOLDER
;
111 public void navigateTo(String state
) {
116 history
.pushState(state
, state
);
120 public void authChange() {
122 String currentPath
= null;
124 currentPath
= node
.getPath();
125 JcrUtils
.logoutQuietly(session
);
127 if (SecurityContextHolder
.getContext().getAuthentication() == null)
129 session
= repository
.login(workspace
);
130 if (currentPath
!= null)
131 node
= session
.getNode(currentPath
);
136 } catch (RepositoryException e
) {
137 throw new CmsException("Cannot perform auth change", e
);
143 public void exception(Throwable e
) {
145 log
.error("Unexpected exception in CMS", e
);
150 public Object
local(Msg msg
) {
151 String key
= msg
.getId();
152 int lastDot
= key
.lastIndexOf('.');
153 String className
= key
.substring(0, lastDot
);
154 String fieldName
= key
.substring(lastDot
+ 1);
155 Locale locale
= RWT
.getLocale();
156 ResourceBundle rb
= ResourceBundle
.getBundle(className
, locale
,
157 msg
.getClassLoader());
158 return rb
.getString(fieldName
);
161 /** Sets the state of the entry point and retrieve the related JCR node. */
162 protected synchronized void setState(String newState
) {
163 String previousState
= this.state
;
167 this.state
= newState
;
170 int firstSlash
= state
.indexOf('/');
171 if (firstSlash
== 0) {
172 if (!session
.nodeExists(state
))
173 node
= addNode(session
, state
, null);
175 node
= session
.getNode(state
);
177 } else if (firstSlash
> 0) {
178 String prefix
= state
.substring(0, firstSlash
);
179 String path
= state
.substring(firstSlash
);
180 if (session
.getWorkspace().getNodeTypeManager()
181 .hasNodeType(prefix
)) {
182 String nodeType
= prefix
;
183 if (!session
.nodeExists(path
))
184 node
= addNode(session
, path
, nodeType
);
186 node
= session
.getNode(path
);
187 if (!node
.isNodeType(nodeType
))
188 throw new CmsException("Node " + path
189 + " not of type " + nodeType
);
191 } else if ("delete".equals(prefix
)) {
192 if (session
.itemExists(path
)) {
193 Node nodeToDelete
= session
.getNode(path
);
194 // TODO "Are you sure?"
195 nodeToDelete
.remove();
197 log
.debug("Deleted " + path
);
198 navigateTo(previousState
);
200 throw new CmsException("Data " + path
201 + " does not exist");
203 if (session
.itemExists(path
))
204 node
= session
.getNode(path
);
206 throw new CmsException("Data " + path
207 + " does not exist");
211 node
= getDefaultNode(session
);
212 if (state
.equals("~"))
218 if (log
.isTraceEnabled())
219 log
.trace("page=" + page
+ ", node=" + node
+ ", state="
222 } catch (RepositoryException e
) {
223 throw new CmsException("Cannot retrieve node", e
);
227 protected Node
addNode(Session session
, String path
, String nodeType
)
228 throws RepositoryException
{
229 return JcrUtils
.mkdirs(session
, path
, nodeType
!= null ? nodeType
230 : getDefaultNewNodeType(), getDefaultNewFolderType(), false);
231 // not saved, so that the UI can discard it later on
234 protected Node
getNode() {
239 public String
getState() {
243 protected String
getPage() {
247 protected Throwable
getException() {
251 protected void resetException() {
255 protected Session
getSession() {
259 private class CmsNavigationListener
implements BrowserNavigationListener
{
260 private static final long serialVersionUID
= -3591018803430389270L;
263 public void navigated(BrowserNavigationEvent event
) {
264 setState(event
.getState());