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
.swt
.SWT
;
25 import org
.eclipse
.swt
.layout
.FillLayout
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Label
;
30 import org
.eclipse
.swt
.widgets
.Text
;
32 /** Creates and registers an {@link EntryPoint} */
34 class CmsEntryPointFactory
{
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();
54 public EntryPoint
create() {
55 CmsEntryPoint cmsEntryPoint
= new CmsEntryPoint(repository
, workspace
);
56 CmsSession
.current
.set(cmsEntryPoint
);
60 public void init() throws RepositoryException
{
61 if (workspace
== null)
62 throw new CmsException(
63 "Workspace must be set when calling initialization."
64 + " Please make sure that read-only and read-write roles"
65 + " have been properly configured:"
66 + " the defaults are open.");
68 Session session
= null;
70 session
= JcrUtils
.loginOrCreateWorkspace(repository
, workspace
);
71 VersionManager vm
= session
.getWorkspace().getVersionManager();
72 if (!vm
.isCheckedOut("/"))
74 JcrUtils
.mkdirs(session
, basePath
);
75 for (String principal
: rwPrincipals
)
76 JcrUtils
.addPrivilege(session
, basePath
, principal
,
78 for (String principal
: roPrincipals
)
79 JcrUtils
.addPrivilege(session
, basePath
, principal
,
82 for (String pageName
: pages
.keySet()) {
84 initPage(session
, pages
.get(pageName
));
86 } catch (Exception e
) {
87 throw new CmsException(
88 "Cannot initialize page " + pageName
, e
);
93 JcrUtils
.logoutQuietly(session
);
97 protected void initPage(Session adminSession
, CmsUiProvider page
)
98 throws RepositoryException
{
99 if (page
instanceof LifeCycleUiProvider
)
100 ((LifeCycleUiProvider
) page
).init(adminSession
);
103 public void destroy() {
104 for (String pageName
: pages
.keySet()) {
106 CmsUiProvider page
= pages
.get(pageName
);
107 if (page
instanceof LifeCycleUiProvider
)
108 ((LifeCycleUiProvider
) page
).destroy();
109 } catch (Exception e
) {
110 log
.error("Cannot destroy page " + pageName
, e
);
115 public void setRepository(Repository repository
) {
116 this.repository
= repository
;
119 public void setWorkspace(String workspace
) {
120 this.workspace
= workspace
;
123 public void setCmsLogin(CmsLogin cmsLogin
) {
124 this.cmsLogin
= cmsLogin
;
127 public void setHeader(CmsUiProvider header
) {
128 this.header
= header
;
131 public void setPages(Map
<String
, CmsUiProvider
> pages
) {
135 public void setBasePath(String basePath
) {
136 this.basePath
= basePath
;
139 public void setRoPrincipals(List
<String
> roPrincipals
) {
140 this.roPrincipals
= roPrincipals
;
143 public void setRwPrincipals(List
<String
> rwPrincipals
) {
144 this.rwPrincipals
= rwPrincipals
;
147 public void setHeaderHeight(Integer headerHeight
) {
148 this.headerHeight
= headerHeight
;
151 private class CmsEntryPoint
extends AbstractCmsEntryPoint
{
152 private Composite headerArea
;
153 private Composite bodyArea
;
155 public CmsEntryPoint(Repository repository
, String workspace
) {
156 super(repository
, workspace
);
160 protected void createContents(Composite parent
) {
162 getShell().getDisplay().setData(CmsSession
.KEY
, this);
164 parent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
166 parent
.setLayout(CmsUtils
.noSpaceGridLayout());
168 headerArea
= new Composite(parent
, SWT
.NONE
);
169 headerArea
.setLayout(new FillLayout());
170 GridData headerData
= new GridData(SWT
.FILL
, SWT
.FILL
, false,
172 headerData
.heightHint
= headerHeight
;
173 headerArea
.setLayoutData(headerData
);
176 bodyArea
= new Composite(parent
, SWT
.NONE
);
177 bodyArea
.setData(RWT
.CUSTOM_VARIANT
, CmsStyles
.CMS_BODY
);
178 bodyArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
180 bodyArea
.setBackgroundMode(SWT
.INHERIT_DEFAULT
);
181 bodyArea
.setLayout(CmsUtils
.noSpaceGridLayout());
182 } catch (Exception e
) {
183 throw new CmsException("Cannot create entrypoint contents", e
);
188 protected void refreshHeader() {
189 if (headerArea
== null)
191 for (Control child
: headerArea
.getChildren())
194 header
.createUi(headerArea
, getNode());
195 } catch (RepositoryException e
) {
196 throw new CmsException("Cannot refresh header", e
);
198 headerArea
.layout(true, true);
201 @SuppressWarnings("unused")
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();
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() {