1 package org
.argeo
.cms
.util
;
3 import java
.io
.InputStream
;
4 import java
.net
.MalformedURLException
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.servlet
.http
.HttpServletRequest
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.argeo
.cms
.ui
.CmsConstants
;
14 import org
.argeo
.cms
.ui
.CmsView
;
15 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.node
.NodeConstants
;
18 import org
.argeo
.node
.NodeUtils
;
19 import org
.argeo
.node
.security
.NodeAuthenticated
;
20 import org
.eclipse
.rap
.rwt
.RWT
;
21 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.graphics
.Image
;
24 import org
.eclipse
.swt
.graphics
.ImageData
;
25 import org
.eclipse
.swt
.graphics
.Point
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.layout
.RowData
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Table
;
33 import org
.eclipse
.swt
.widgets
.Widget
;
35 /** Static utilities for the CMS framework. */
36 public class CmsUtils
implements CmsConstants
{
37 // private final static Log log = LogFactory.getLog(CmsUtils.class);
40 * The CMS view related to this display, or null if none is available from
43 public static CmsView
getCmsView() {
44 return UiContext
.getData(NodeAuthenticated
.KEY
);
47 public static StringBuilder
getServerBaseUrl(HttpServletRequest request
) {
49 URL url
= new URL(request
.getRequestURL().toString());
50 StringBuilder buf
= new StringBuilder();
51 buf
.append(url
.getProtocol()).append("://").append(url
.getHost());
52 if (url
.getPort() != -1)
53 buf
.append(':').append(url
.getPort());
55 } catch (MalformedURLException e
) {
56 throw new CmsException("Cannot extract server base URL from " + request
.getRequestURL(), e
);
61 public static String
getDataUrl(Node node
, HttpServletRequest request
) throws RepositoryException
{
63 StringBuilder buf
= getServerBaseUrl(request
);
64 buf
.append(getDataPath(node
));
65 return new URL(buf
.toString()).toString();
66 } catch (MalformedURLException e
) {
67 throw new CmsException("Cannot build data URL for " + node
, e
);
71 // private final static String PATH_DATA = "/data";
72 // private final static String WEBDAV_PUBLIC = PATH_DATA + "/public";
73 // private final static String WEBDAV_PRIVATE = PATH_DATA + "/files";
75 /** A path in the node repository */
76 public static String
getDataPath(Node node
) throws RepositoryException
{
77 return getDataPath(NodeConstants
.NODE
, node
);
80 public static String
getDataPath(String cn
, Node node
) throws RepositoryException
{
81 return NodeUtils
.getDataPath(cn
, node
);
82 // assert node != null;
83 // String userId = node.getSession().getUserID();
84 // if (log.isTraceEnabled())
85 // log.trace(userId + " : " + node.getPath());
86 // StringBuilder buf = new StringBuilder();
87 // boolean isAnonymous =
88 // userId.equalsIgnoreCase(NodeConstants.ROLE_ANONYMOUS);
90 // buf.append(WEBDAV_PUBLIC);
92 // buf.append(WEBDAV_PRIVATE);
93 // Session session = node.getSession();
94 // Repository repository = session.getRepository();
96 // if (repository.isSingleValueDescriptor(NodeConstants.CN)) {
97 // cn = repository.getDescriptor(NodeConstants.CN);
99 // log.warn("No cn defined in repository, using " + NodeConstants.NODE);
100 // cn = NodeConstants.NODE;
103 // buf.append('/').append(cn).append('/').append(session.getWorkspace().getName()).append(node.getPath())
107 // public static String getCanonicalUrl(Node node, HttpServletRequest
108 // request) throws RepositoryException {
110 // StringBuilder buf = getServerBaseUrl(request);
111 // buf.append('/').append('!').append(node.getPath());
112 // return new URL(buf.toString()).toString();
113 // } catch (MalformedURLException e) {
114 // throw new CmsException("Cannot build data URL for " + node, e);
116 // // return request.getRequestURL().append('!').append(node.getPath())
120 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
122 public static RowData ROW_DATA_16px
= new RowData(16, 16);
124 public static GridLayout
noSpaceGridLayout() {
125 return noSpaceGridLayout(new GridLayout());
128 public static GridLayout
noSpaceGridLayout(GridLayout layout
) {
129 layout
.horizontalSpacing
= 0;
130 layout
.verticalSpacing
= 0;
131 layout
.marginWidth
= 0;
132 layout
.marginHeight
= 0;
139 public static GridData
fillWidth() {
140 return grabWidth(SWT
.FILL
, SWT
.FILL
);
143 public static GridData
fillAll() {
144 return new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
147 public static GridData
grabWidth(int horizontalAlignment
, int verticalAlignment
) {
148 return new GridData(horizontalAlignment
, horizontalAlignment
, true, false);
151 public static RowData
rowData16px() {
152 return new RowData(16, 16);
156 public static void style(Widget widget
, String style
) {
157 widget
.setData(CmsConstants
.STYLE
, style
);
160 /** Enable markups on widget */
161 public static void markup(Widget widget
) {
162 widget
.setData(CmsConstants
.MARKUP
, true);
165 public static void setItemHeight(Table table
, int height
) {
166 table
.setData(CmsConstants
.ITEM_HEIGHT
, height
);
169 // /** @return the path or null if not instrumented */
171 // public static String getDataPath(Widget widget) {
173 // Object data = widget.getData();
174 // if (data != null && data instanceof Item) {
176 // return ((Item) data).getPath();
177 // } catch (RepositoryException e) {
178 // throw new CmsException("Cannot find data path of " + data + " for " +
184 // data = widget.getData(Property.JCR_PATH);
186 // return data.toString();
191 /** Dispose all children of a Composite */
192 public static void clear(Composite composite
) {
193 for (Control child
: composite
.getChildren())
200 public static Node
getOrAddEmptyFile(Node parent
, Enum
<?
> child
) throws RepositoryException
{
201 if (has(parent
, child
))
202 return child(parent
, child
);
203 return JcrUtils
.copyBytesAsFile(parent
, child
.name(), new byte[0]);
206 public static Node
child(Node parent
, Enum
<?
> en
) throws RepositoryException
{
207 return parent
.getNode(en
.name());
210 public static Boolean
has(Node parent
, Enum
<?
> en
) throws RepositoryException
{
211 return parent
.hasNode(en
.name());
214 public static Node
getOrAdd(Node parent
, Enum
<?
> en
) throws RepositoryException
{
215 return getOrAdd(parent
, en
, null);
218 public static Node
getOrAdd(Node parent
, Enum
<?
> en
, String primaryType
) throws RepositoryException
{
220 return child(parent
, en
);
221 else if (primaryType
== null)
222 return parent
.addNode(en
.name());
224 return parent
.addNode(en
.name(), primaryType
);
228 public static String
img(String src
, String width
, String height
) {
229 return imgBuilder(src
, width
, height
).append("/>").toString();
232 public static String
img(String src
, Point size
) {
233 return img(src
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
236 public static StringBuilder
imgBuilder(String src
, String width
, String height
) {
237 return new StringBuilder(64).append("<img width='").append(width
).append("' height='").append(height
)
238 .append("' src='").append(src
).append("'");
241 public static String
noImg(Point size
) {
242 ResourceManager rm
= RWT
.getResourceManager();
243 return CmsUtils
.img(rm
.getLocation(NO_IMAGE
), size
);
246 public static String
noImg() {
247 return noImg(NO_IMAGE_SIZE
);
250 public static Image
noImage(Point size
) {
251 ResourceManager rm
= RWT
.getResourceManager();
252 InputStream in
= null;
254 in
= rm
.getRegisteredContent(NO_IMAGE
);
255 ImageData id
= new ImageData(in
);
256 ImageData scaled
= id
.scaledTo(size
.x
, size
.y
);
257 Image image
= new Image(Display
.getCurrent(), scaled
);
260 IOUtils
.closeQuietly(in
);