1 package org
.argeo
.cms
.util
;
3 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.WEBDAV_PRIVATE
;
4 import static org
.argeo
.cms
.internal
.kernel
.KernelConstants
.WEBDAV_PUBLIC
;
5 import static org
.argeo
.jcr
.ArgeoJcrConstants
.ALIAS_NODE
;
7 import java
.io
.InputStream
;
8 import java
.net
.MalformedURLException
;
11 import javax
.jcr
.Item
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.Property
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.servlet
.http
.HttpServletRequest
;
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
.CmsConstants
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.CmsView
;
23 import org
.argeo
.cms
.auth
.CurrentUser
;
24 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.eclipse
.rap
.rwt
.RWT
;
27 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.graphics
.Image
;
30 import org
.eclipse
.swt
.graphics
.ImageData
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.layout
.RowData
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Table
;
39 import org
.eclipse
.swt
.widgets
.Widget
;
41 /** Static utilities for the CMS framework. */
42 public class CmsUtils
implements CmsConstants
{
43 private final static Log log
= LogFactory
.getLog(CmsUtils
.class);
46 * The CMS view related to this display, or null if none is available from
49 public static CmsView
getCmsView() {
50 return UiContext
.getData(CmsView
.KEY
);
53 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
55 URL url
= new URL(request
.getRequestURL().toString());
56 StringBuilder buf
= new StringBuilder();
57 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
58 if (url
.getPort() != -1)
59 buf
.append(':').append(url
.getPort());
61 } catch (MalformedURLException e
) {
62 throw new CmsException("Cannot extract server base URL from "
63 + request
.getRequestURL(), e
);
67 public static String
getDataUrl(Node node
, HttpServletRequest request
)
68 throws RepositoryException
{
70 StringBuilder buf
= getServerBaseUrl(request
);
71 buf
.append(getDataPath(node
));
72 return new URL(buf
.toString()).toString();
73 } catch (MalformedURLException e
) {
74 throw new CmsException("Cannot build data URL for " + node
, e
);
78 public static String
getDataPath(Node node
) throws RepositoryException
{
79 if (log
.isTraceEnabled())
80 log
.trace(CurrentUser
.getUsername() + " : " + node
.getPath());
81 StringBuilder buf
= new StringBuilder();
82 if (CurrentUser
.isAnonymous())
83 buf
.append(WEBDAV_PUBLIC
);
85 buf
.append(WEBDAV_PRIVATE
);
86 // TODO convey repo alias vie repository properties
87 return buf
.append('/').append(ALIAS_NODE
).append('/')
88 .append(node
.getSession().getWorkspace().getName())
89 .append(node
.getPath()).toString();
92 public static String
getCanonicalUrl(Node node
, HttpServletRequest request
)
93 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
,
133 int verticalAlignment
) {
134 return new GridData(horizontalAlignment
, horizontalAlignment
, true,
138 public static RowData
rowData16px() {
139 return new RowData(16, 16);
143 public static void style(Widget widget
, String style
) {
144 widget
.setData(CmsConstants
.STYLE
, style
);
147 /** Enable markups on widget */
148 public static void markup(Widget widget
) {
149 widget
.setData(CmsConstants
.MARKUP
, true);
152 public static void setItemHeight(Table table
, int height
) {
153 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
156 /** @return the path or null if not instrumented */
157 public static String
getDataPath(Widget widget
) {
159 Object data
= widget
.getData();
160 if (data
!= null && data
instanceof Item
) {
162 return ((Item
) data
).getPath();
163 } catch (RepositoryException e
) {
164 throw new CmsException("Cannot find data path of " + data
170 data
= widget
.getData(Property
.JCR_PATH
);
172 return data
.toString();
177 /** Dispose all children of a Composite */
178 public static void clear(Composite composite
) {
179 for (Control child
: composite
.getChildren())
186 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
)
187 throws RepositoryException
{
188 if (has(parent
, child
))
189 return child(parent
, child
);
190 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
193 public static Node
child(Node parent
, Enum
<?
> en
)
194 throws RepositoryException
{
195 return parent
.getNode(en
.name());
198 public static Boolean
has(Node parent
, Enum
<?
> en
)
199 throws RepositoryException
{
200 return parent
.hasNode(en
.name());
203 public static Node
getOrAdd(Node parent
, Enum
<?
> en
)
204 throws RepositoryException
{
205 return getOrAdd(parent
, en
, null);
208 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
)
209 throws RepositoryException
{
211 return child(parent
, en
);
212 else if (primaryType
== null)
213 return parent
.addNode(en
.name());
215 return parent
.addNode(en
.name(), primaryType
);
219 public static String
img(String src
, String width
, String height
) {
220 return imgBuilder(src
, width
, height
).append("/>").toString();
223 public static String
img(String src
, Point size
) {
224 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
227 public static StringBuilder
imgBuilder(String src
, String width
,
229 return new StringBuilder(64).append("<img width='").append(width
)
230 .append("' height='").append(height
).append("' src='")
231 .append(src
).append("'");
234 public static String
noImg(Point size
) {
235 ResourceManager rm
= RWT
.getResourceManager();
236 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
239 public static String
noImg() {
240 return noImg(NO_IMAGE_SIZE
);
243 public static Image
noImage(Point size
) {
244 ResourceManager rm
= RWT
.getResourceManager();
245 InputStream in
= null;
247 in
= rm
.getRegisteredContent(NO_IMAGE
);
248 ImageData id
= new ImageData(in
);
249 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
250 Image image
= new Image(Display
.getCurrent(), scaled
);
253 IOUtils
.closeQuietly(in
);