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
.context
.SecurityContextHolder
;
24 /** Manages history and navigation */
25 public abstract class AbstractCmsEntryPoint
extends AbstractEntryPoint
26 implements CmsSession
{
27 private final Log log
= LogFactory
.getLog(AbstractCmsEntryPoint
.class);
29 private Repository repository
;
30 private String workspace
;
31 private Session session
;
37 private Throwable exception
;
39 private BrowserNavigation history
;
41 public AbstractCmsEntryPoint(Repository repository
, String workspace
) {
42 if (SecurityContextHolder
.getContext().getAuthentication() == null)
45 this.repository
= repository
;
46 this.workspace
= workspace
;
49 history
= RWT
.getClient().getService(BrowserNavigation
.class);
51 history
.addBrowserNavigationListener(new CmsNavigationListener());
53 // RWT.setLocale(Locale.FRANCE);
57 protected Shell
createShell(Display display
) {
58 Shell shell
= super.createShell(display
);
59 shell
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_SHELL
);
60 display
.disposeExec(new Runnable() {
64 if (log
.isTraceEnabled())
65 log
.trace("Logging out " + session
);
66 JcrUtils
.logoutQuietly(session
);
72 /** Recreate header UI */
73 protected abstract void refreshHeader();
75 /** Recreate body UI */
76 protected abstract void refreshBody();
78 /** Log as anonymous */
79 protected abstract void logAsAnonymous();
82 * The node to return when no node was found (for authenticated users and
85 protected abstract Node
getDefaultNode(Session session
)
86 throws RepositoryException
;
89 * Reasonable default since it is a nt:hierarchyNode and is thus compatible
90 * with the obvious default folder type, nt:folder, conceptual equivalent of
91 * an empty text file in an operating system. To be overridden.
93 protected String
getDefaultNewNodeType() {
94 return CmsTypes
.CMS_TEXT
;
97 /** Default new folder type (used in mkdirs) is nt:folder. To be overridden. */
98 protected String
getDefaultNewFolderType() {
99 return NodeType
.NT_FOLDER
;
102 public void navigateTo(String state
) {
107 history
.pushState(state
, state
);
111 public void authChange() {
113 String currentPath
= null;
115 currentPath
= node
.getPath();
116 JcrUtils
.logoutQuietly(session
);
118 if (SecurityContextHolder
.getContext().getAuthentication() == null)
120 session
= repository
.login(workspace
);
121 if (currentPath
!= null)
122 node
= session
.getNode(currentPath
);
127 } catch (RepositoryException e
) {
128 throw new CmsException("Cannot perform auth change", e
);
134 public void exception(Throwable e
) {
136 log
.error("Unexpected exception in CMS", e
);
141 public Object
local(Msg msg
) {
142 String key
= msg
.getId();
143 int lastDot
= key
.lastIndexOf('.');
144 String className
= key
.substring(0, lastDot
);
145 String fieldName
= key
.substring(lastDot
+ 1);
146 Locale locale
= RWT
.getLocale();
147 ResourceBundle rb
= ResourceBundle
.getBundle(className
, locale
,
148 msg
.getClassLoader());
149 return rb
.getString(fieldName
);
152 /** Sets the state of the entry point and retrieve the related JCR node. */
153 protected synchronized void setState(String newState
) {
154 String previousState
= this.state
;
158 this.state
= newState
;
161 int firstSlash
= state
.indexOf('/');
162 if (firstSlash
== 0) {
163 if (!session
.nodeExists(state
))
164 node
= addNode(session
, state
, null);
166 node
= session
.getNode(state
);
168 } else if (firstSlash
> 0) {
169 String prefix
= state
.substring(0, firstSlash
);
170 String path
= state
.substring(firstSlash
);
171 if (session
.getWorkspace().getNodeTypeManager()
172 .hasNodeType(prefix
)) {
173 String nodeType
= prefix
;
174 if (!session
.nodeExists(path
))
175 node
= addNode(session
, path
, nodeType
);
177 node
= session
.getNode(path
);
178 if (!node
.isNodeType(nodeType
))
179 throw new CmsException("Node " + path
180 + " not of type " + nodeType
);
182 } else if ("delete".equals(prefix
)) {
183 if (session
.itemExists(path
)) {
184 Node nodeToDelete
= session
.getNode(path
);
185 // TODO "Are you sure?"
186 nodeToDelete
.remove();
188 log
.debug("Deleted " + path
);
189 navigateTo(previousState
);
191 throw new CmsException("Data " + path
192 + " does not exist");
194 if (session
.itemExists(path
))
195 node
= session
.getNode(path
);
197 throw new CmsException("Data " + path
198 + " does not exist");
202 node
= getDefaultNode(session
);
203 if (state
.equals("~"))
209 if (log
.isTraceEnabled())
210 log
.trace("page=" + page
+ ", node=" + node
+ ", state="
213 } catch (RepositoryException e
) {
214 throw new CmsException("Cannot retrieve node", e
);
218 protected Node
addNode(Session session
, String path
, String nodeType
)
219 throws RepositoryException
{
220 return JcrUtils
.mkdirs(session
, path
, nodeType
!= null ? nodeType
221 : getDefaultNewNodeType(), getDefaultNewFolderType(), false);
222 // not saved, so that the UI can discard it later on
225 protected Node
getNode() {
230 public String
getState() {
234 protected String
getPage() {
238 protected Throwable
getException() {
242 protected void resetException() {
246 protected Session
getSession() {
250 private class CmsNavigationListener
implements BrowserNavigationListener
{
251 private static final long serialVersionUID
= -3591018803430389270L;
254 public void navigated(BrowserNavigationEvent event
) {
255 setState(event
.getState());