1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.link;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.beans.runtime.File;
38 import info.magnolia.jcr.RuntimeRepositoryException;
39 import info.magnolia.jcr.util.NodeTypes;
40 import info.magnolia.objectfactory.Components;
41
42 import java.net.URI;
43 import java.net.URISyntaxException;
44
45 import javax.jcr.Node;
46 import javax.jcr.Property;
47 import javax.jcr.RepositoryException;
48
49 import org.apache.commons.lang3.StringUtils;
50 import org.apache.jackrabbit.JcrConstants;
51
52
53
54
55 public class Link {
56
57 private String workspace;
58 private String path;
59 private String uuid;
60 private String extension;
61 private String fileName;
62 private String fallbackPath;
63 private String anchor;
64 private String parameters;
65
66 private Node jcrNode;
67 private Property property;
68 private String propertyName;
69
70
71
72
73 public Link() {
74 }
75
76 public Link(Node node) {
77 try {
78 setJCRNode(node);
79 setWorkspace(node.getSession().getWorkspace().getName());
80 if (node.isNodeType(JcrConstants.MIX_REFERENCEABLE)) {
81 setUUID(node.getIdentifier());
82 }
83 } catch (RepositoryException e) {
84 throw new RuntimeRepositoryException(e);
85 }
86 }
87
88 public Link(Property property) {
89 try {
90 setJCRNode(property.getParent());
91 setWorkspace(property.getSession().getWorkspace().toString());
92 setProperty(property);
93 setPropertyName(property.getName());
94 } catch (RepositoryException e) {
95 throw new RuntimeRepositoryException(e);
96 }
97 }
98
99 public String getExtension() {
100 try {
101 if (StringUtils.isEmpty(this.extension) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)) {
102 File binary = new File(jcrNode);
103 extension = binary.getExtension();
104 }
105 } catch (RepositoryException e) {
106
107 }
108 return StringUtils.defaultIfEmpty(this.extension, Components.getComponent(ServerConfiguration.class).getDefaultExtension());
109 }
110
111 public void setExtension(String extension) {
112 this.extension = extension;
113 }
114
115 public String getFileName() {
116 try {
117 if (StringUtils.isEmpty(this.fileName) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)) {
118 File binary = new File(jcrNode);
119 fileName = new URI(null, null, binary.getFileName(), null).toASCIIString();
120 }
121 } catch (RepositoryException e) {
122
123 } catch (URISyntaxException e) {
124 }
125 return fileName;
126 }
127
128 public void setFileName(String fileName) {
129 this.fileName = fileName;
130 }
131
132 public Node getJCRNode() {
133 return this.jcrNode;
134 }
135
136 public void setJCRNode(Node jcrNode) {
137 this.jcrNode = jcrNode;
138 }
139
140 public Property getProperty() throws LinkException {
141 try {
142 if (this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null && this.getJCRNode().hasProperty(propertyName)) {
143 this.property = this.getJCRNode().getProperty(this.propertyName);
144 }
145 } catch (RepositoryException e) {
146
147 }
148 return this.property;
149 }
150
151 public void setProperty(Property property) {
152 this.property = property;
153 }
154
155 public boolean isEditorBinaryLink() {
156 try {
157 return getJCRNode().isNodeType(NodeTypes.Resource.NAME);
158 } catch (RepositoryException e) {
159 throw new RuntimeRepositoryException(e);
160 }
161 }
162
163 public String getPropertyName() {
164 return this.propertyName;
165 }
166
167 public void setPropertyName(String propertyName) {
168 this.propertyName = propertyName;
169 }
170
171 public String getPath() {
172 if (StringUtils.isEmpty(this.path)) {
173 if (getJCRNode() != null) {
174 try {
175 path = getJCRNode().getPath();
176 } catch (RepositoryException e) {
177 throw new RuntimeRepositoryException(e);
178 }
179 } else {
180 path = this.getFallbackPath();
181 }
182 }
183 return this.path;
184 }
185
186 public void setPath(String path) {
187 this.path = path;
188 }
189
190 public String getWorkspace() {
191 return this.workspace;
192 }
193
194 public void setWorkspace(String workspace) {
195 this.workspace = workspace;
196 }
197
198 public String getUUID() {
199 if (StringUtils.isEmpty(this.uuid) && this.getJCRNode() != null) {
200 try {
201 this.uuid = this.getJCRNode().getIdentifier();
202 } catch (RepositoryException e) {
203 throw new RuntimeRepositoryException(e);
204 }
205 }
206 return this.uuid;
207 }
208
209 public void setUUID(String uuid) {
210 this.uuid = uuid;
211 }
212
213 public String getFallbackPath() {
214 return this.fallbackPath;
215 }
216
217 public void setFallbackPath(String fallbackPath) {
218 this.fallbackPath = fallbackPath;
219 }
220
221 public String getAnchor() {
222 return this.anchor;
223 }
224
225 public void setAnchor(String anchor) {
226 this.anchor = anchor;
227 }
228
229 public String getParameters() {
230 return this.parameters;
231 }
232
233 public void setParameters(String parameters) {
234 this.parameters = parameters;
235 }
236 }