1 package org
.argeo
.cms
.util
;
3 import java
.io
.InputStream
;
4 import java
.net
.MalformedURLException
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.servlet
.http
.HttpServletRequest
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.cms
.CmsException
;
17 import org
.argeo
.cms
.auth
.AuthConstants
;
18 import org
.argeo
.cms
.ui
.CmsConstants
;
19 import org
.argeo
.cms
.ui
.CmsView
;
20 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.node
.NodeAuthenticated
;
23 import org
.argeo
.node
.NodeConstants
;
24 import org
.eclipse
.rap
.rwt
.RWT
;
25 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.graphics
.Image
;
28 import org
.eclipse
.swt
.graphics
.ImageData
;
29 import org
.eclipse
.swt
.graphics
.Point
;
30 import org
.eclipse
.swt
.layout
.GridData
;
31 import org
.eclipse
.swt
.layout
.GridLayout
;
32 import org
.eclipse
.swt
.layout
.RowData
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
34 import org
.eclipse
.swt
.widgets
.Control
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Table
;
37 import org
.eclipse
.swt
.widgets
.Widget
;
39 /** Static utilities for the CMS framework. */
40 public class CmsUtils
implements CmsConstants
{
41 private final static Log log
= LogFactory
.getLog(CmsUtils
.class);
44 * The CMS view related to this display, or null if none is available from
47 public static CmsView
getCmsView() {
48 return UiContext
.getData(NodeAuthenticated
.KEY
);
51 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
53 URL url
= new URL(request
.getRequestURL().toString());
54 StringBuilder buf
= new StringBuilder();
55 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
56 if (url
.getPort() != -1)
57 buf
.append(':').append(url
.getPort());
59 } catch (MalformedURLException e
) {
60 throw new CmsException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
64 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
66 StringBuilder buf
= getServerBaseUrl(request
);
67 buf
.append(getDataPath(node
));
68 return new URL(buf
.toString()).toString();
69 } catch (MalformedURLException e
) {
70 throw new CmsException("Cannot build data URL for " + node
, e
);
74 private final static String PATH_DATA
= "/data";
75 private final static String WEBDAV_PUBLIC
= PATH_DATA
+ "/public";
76 private final static String WEBDAV_PRIVATE
= PATH_DATA
+ "/files";
77 public static String
getDataPath(Node node
) throws RepositoryException
{
79 String userId
= node
.getSession().getUserID();
80 if (log
.isTraceEnabled())
81 log
.trace(userId
+ " : " + node
.getPath());
82 StringBuilder buf
= new StringBuilder();
83 boolean isAnonymous
= userId
.equalsIgnoreCase(AuthConstants
.ROLE_ANONYMOUS
);
85 buf
.append(WEBDAV_PUBLIC
);
87 buf
.append(WEBDAV_PRIVATE
);
88 // TODO convey repo alias vie repository properties
89 return buf
.append('/').append(NodeConstants
.ALIAS_NODE
).append('/').append(node
.getSession().getWorkspace().getName())
90 .append(node
.getPath()).toString();
93 // public static String getCanonicalUrl(Node node, HttpServletRequest request) throws RepositoryException {
95 // StringBuilder buf = getServerBaseUrl(request);
96 // buf.append('/').append('!').append(node.getPath());
97 // return new URL(buf.toString()).toString();
98 // } catch (MalformedURLException e) {
99 // throw new CmsException("Cannot build data URL for " + node, e);
101 // // return request.getRequestURL().append('!').append(node.getPath())
105 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
107 public static RowData ROW_DATA_16px
= new RowData(16, 16);
109 public static GridLayout
noSpaceGridLayout() {
110 return noSpaceGridLayout(new GridLayout());
113 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
114 layout
.horizontalSpacing
= 0;
115 layout
.verticalSpacing
= 0;
116 layout
.marginWidth
= 0;
117 layout
.marginHeight
= 0;
124 public static GridData
fillWidth() {
125 return grabWidth(SWT
.FILL
, SWT
.FILL
);
128 public static GridData
fillAll() {
129 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
132 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
133 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
136 public static RowData
rowData16px() {
137 return new RowData(16, 16);
141 public static void style(Widget widget
, String style
) {
142 widget
.setData(CmsConstants
.STYLE
, style
);
145 /** Enable markups on widget */
146 public static void markup(Widget widget
) {
147 widget
.setData(CmsConstants
.MARKUP
, true);
150 public static void setItemHeight(Table table
, int height
) {
151 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
154 /** @return the path or null if not instrumented */
155 public static String
getDataPath(Widget widget
) {
157 Object data
= widget
.getData();
158 if (data
!= null && data
instanceof Item
) {
160 return ((Item
) data
).getPath();
161 } catch (RepositoryException e
) {
162 throw new CmsException("Cannot find data path of " + data
+ " for " + widget
);
167 data
= widget
.getData(Property
.JCR_PATH
);
169 return data
.toString();
174 /** Dispose all children of a Composite */
175 public static void clear(Composite composite
) {
176 for (Control child
: composite
.getChildren())
183 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
184 if (has(parent
, child
))
185 return child(parent
, child
);
186 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
189 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
190 return parent
.getNode(en
.name());
193 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
194 return parent
.hasNode(en
.name());
197 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
198 return getOrAdd(parent
, en
, null);
201 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
203 return child(parent
, en
);
204 else if (primaryType
== null)
205 return parent
.addNode(en
.name());
207 return parent
.addNode(en
.name(), primaryType
);
211 public static String
img(String src
, String width
, String height
) {
212 return imgBuilder(src
, width
, height
).append("/>").toString();
215 public static String
img(String src
, Point size
) {
216 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
219 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
220 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
221 .append("' src='").append(src
).append("'");
224 public static String
noImg(Point size
) {
225 ResourceManager rm
= RWT
.getResourceManager();
226 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
229 public static String
noImg() {
230 return noImg(NO_IMAGE_SIZE
);
233 public static Image
noImage(Point size
) {
234 ResourceManager rm
= RWT
.getResourceManager();
235 InputStream in
= null;
237 in
= rm
.getRegisteredContent(NO_IMAGE
);
238 ImageData id
= new ImageData(in
);
239 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
240 Image image
= new Image(Display
.getCurrent(), scaled
);
243 IOUtils
.closeQuietly(in
);