3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
5 import java
.util
.Arrays
;
6 import java
.util
.LinkedHashMap
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Session
;
14 import javax
.jcr
.security
.Privilege
;
15 import javax
.jcr
.version
.VersionManager
;
17 import org
.apache
.commons
.io
.IOUtils
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.cms
.internal
.ImageManagerImpl
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.eclipse
.rap
.rwt
.RWT
;
23 import org
.eclipse
.rap
.rwt
.application
.EntryPoint
;
24 import org
.eclipse
.rap
.rwt
.application
.EntryPointFactory
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.layout
.FillLayout
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Label
;
31 import org
.eclipse
.swt
.widgets
.Text
;
33 /** Creates and registers an {@link EntryPoint} */
34 public class CmsEntryPointFactory
implements EntryPointFactory
{
35 private final static Log log
= LogFactory
36 .getLog(CmsEntryPointFactory
.class);
38 private Repository repository
;
39 private String workspace
= null;
40 private String basePath
= "/";
41 private List
<String
> roPrincipals
= Arrays
.asList("anonymous", "everyone");
42 private List
<String
> rwPrincipals
= Arrays
.asList("everyone");
44 private CmsLogin cmsLogin
;
46 private CmsUiProvider header
;
47 private Map
<String
, CmsUiProvider
> pages
= new LinkedHashMap
<String
, CmsUiProvider
>();
49 private Integer headerHeight
= 40;
52 private CmsImageManager imageManager
= new ImageManagerImpl();
55 public EntryPoint
create() {
56 CmsEntryPoint cmsEntryPoint
= new CmsEntryPoint(repository
, workspace
);
57 CmsSession
.current
.set(cmsEntryPoint
);
61 public void init() throws RepositoryException
{
62 if (workspace
== null)
63 throw new CmsException(
64 "Workspace must be set when calling initialization."
65 + " Please make sure that read-only and read-write roles"
66 + " have been properly configured:"
67 + " the defaults are open.");
69 Session session
= null;
71 session
= JcrUtils
.loginOrCreateWorkspace(repository
, workspace
);
72 VersionManager vm
= session
.getWorkspace().getVersionManager();
73 if (!vm
.isCheckedOut("/"))
75 JcrUtils
.mkdirs(session
, basePath
);
76 for (String principal
: rwPrincipals
)
77 JcrUtils
.addPrivilege(session
, basePath
, principal
,
79 for (String principal
: roPrincipals
)
80 JcrUtils
.addPrivilege(session
, basePath
, principal
,
83 for (String pageName
: pages
.keySet()) {
85 initPage(session
, pages
.get(pageName
));
87 } catch (Exception e
) {
88 throw new CmsException(
89 "Cannot initialize page " + pageName
, e
);
94 JcrUtils
.logoutQuietly(session
);
98 protected void initPage(Session adminSession
, CmsUiProvider page
)
99 throws RepositoryException
{
100 if (page
instanceof LifeCycleUiProvider
)
101 ((LifeCycleUiProvider
) page
).init(adminSession
);
104 public void destroy() {
105 for (String pageName
: pages
.keySet()) {
107 CmsUiProvider page
= pages
.get(pageName
);
108 if (page
instanceof LifeCycleUiProvider
)
109 ((LifeCycleUiProvider
) page
).destroy();
110 } catch (Exception e
) {
111 log
.error("Cannot destroy page " + pageName
, e
);
116 public void setRepository(Repository repository
) {
117 this.repository
= repository
;
120 public void setWorkspace(String workspace
) {
121 this.workspace
= workspace
;
124 public void setCmsLogin(CmsLogin cmsLogin
) {
125 this.cmsLogin
= cmsLogin
;
128 public void setHeader(CmsUiProvider header
) {
129 this.header
= header
;
132 public void setPages(Map
<String
, CmsUiProvider
> pages
) {
136 public void setBasePath(String basePath
) {
137 this.basePath
= basePath
;
140 public void setRoPrincipals(List
<String
> roPrincipals
) {
141 this.roPrincipals
= roPrincipals
;
144 public void setRwPrincipals(List
<String
> rwPrincipals
) {
145 this.rwPrincipals
= rwPrincipals
;
148 public void setHeaderHeight(Integer headerHeight
) {
149 this.headerHeight
= headerHeight
;
152 private class CmsEntryPoint
extends AbstractCmsEntryPoint
{
153 private Composite headerArea
;
154 private Composite bodyArea
;
156 public CmsEntryPoint(Repository repository
, String workspace
) {
157 super(repository
, workspace
);
161 protected void createContents(Composite parent
) {
163 getShell().getDisplay().setData(CmsSession
.KEY
, this);
165 parent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
167 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
169 headerArea
= new Composite(parent
, SWT
.NONE
);
170 headerArea
.setLayout(new FillLayout());
171 GridData headerData
= new GridData(SWT
.FILL
, SWT
.FILL
, false,
173 headerData
.heightHint
= headerHeight
;
174 headerArea
.setLayoutData(headerData
);
177 bodyArea
= new Composite(parent
, SWT
.NONE
);
178 bodyArea
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_BODY
);
179 bodyArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
181 bodyArea
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
182 bodyArea
.setLayout(CmsUtils
.noSpaceGridLayout());
183 } catch (Exception e
) {
184 throw new CmsException("Cannot create entrypoint contents", e
);
189 protected void refreshHeader() {
190 if (headerArea
== null)
192 for (Control child
: headerArea
.getChildren())
195 header
.createUi(headerArea
, getNode());
196 } catch (RepositoryException e
) {
197 throw new CmsException("Cannot refresh header", e
);
199 headerArea
.layout(true, true);
203 protected void refreshBody() {
204 if (bodyArea
== null)
207 for (Control child
: bodyArea
.getChildren())
209 bodyArea
.setLayout(CmsUtils
.noSpaceGridLayout());
212 Throwable exception
= getException();
213 if (exception
!= null) {
214 new Label(bodyArea
, SWT
.NONE
).setText("Unreachable state : "
216 if (getNode() != null)
217 new Label(bodyArea
, SWT
.NONE
).setText("Context : "
220 Text errorText
= new Text(bodyArea
, SWT
.MULTI
| SWT
.H_SCROLL
222 errorText
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
224 StringWriter sw
= new StringWriter();
225 exception
.printStackTrace(new PrintWriter(sw
));
226 errorText
.setText(sw
.toString());
227 IOUtils
.closeQuietly(sw
);
231 String state
= getState();
232 String page
= getPage();
235 throw new CmsException("State cannot be null");
237 throw new CmsException("Page cannot be null");
238 // else if (state.length() == 0)
239 // log.debug("empty state");
240 else if (pages
.containsKey(page
))
241 pages
.get(page
).createUi(bodyArea
, getNode());
244 // RWT.getResponse().sendError(404);
245 // } catch (IOException e) {
246 // log.error("Cannot send 404 code", e);
248 throw new CmsException("Unsupported state " + state
);
250 } catch (RepositoryException e
) {
251 throw new CmsException("Cannot refresh body", e
);
254 bodyArea
.layout(true, true);
258 protected void logAsAnonymous() {
259 cmsLogin
.logInAsAnonymous();
263 protected Node
getDefaultNode(Session session
)
264 throws RepositoryException
{
265 if (!session
.hasPermission(basePath
, "read")) {
266 if (session
.getUserID().equals("anonymous"))
267 throw new CmsLoginRequiredException();
269 throw new CmsException("Unauthorized");
271 return session
.getNode(basePath
);
275 public CmsImageManager
getImageManager() {