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 java.net.URI;
37 import java.net.URISyntaxException;
38
39 import info.magnolia.cms.beans.config.ServerConfiguration;
40 import info.magnolia.cms.beans.runtime.File;
41 import info.magnolia.cms.core.Content;
42 import info.magnolia.cms.core.ItemType;
43 import info.magnolia.cms.core.NodeData;
44
45 import javax.jcr.PropertyType;
46 import javax.jcr.RepositoryException;
47
48 import org.apache.commons.lang.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57 public class Link {
58
59 private static final Logger log = LoggerFactory.getLogger(Link.class);
60
61 private String repository;
62 private String handle;
63 private String uuid;
64 private String nodeDataName;
65 private String extension;
66 private Content node;
67 private NodeData nodeData;
68 private String fileName;
69 private String fallbackHandle;
70 private String anchor;
71 private String parameters;
72
73
74
75
76 public Link() {
77 }
78
79
80
81
82 public Link(Content content) {
83 setNode(content);
84 try {
85 setRepository(content.getWorkspace().getName());
86 } catch (RepositoryException e) {
87 throw new RuntimeException(e);
88 }
89
90 if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
91 setUUID(content.getUUID());
92 }
93 }
94
95 public Link(String repoName, Content parent, NodeData nodedata) {
96 setNode(parent);
97 setRepository(repoName);
98 setNodeData(nodedata);
99 setNodeDataName(nodedata.getName());
100 }
101
102 public String getExtension() {
103 if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
104 File binary = new File(nodeData);
105 extension = binary.getExtension();
106 }
107 return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
108 }
109
110
111 public void setExtension(String extension) {
112 this.extension = extension;
113 }
114
115
116 public String getFileName() {
117 try {
118 if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
119 File binary = new File(nodeData);
120 fileName = new URI(null, null, binary.getFileName(), null).toASCIIString();
121 }
122 } catch (URISyntaxException e) {
123 }
124 return fileName;
125 }
126
127
128 public void setFileName(String fileName) {
129 this.fileName = fileName;
130 }
131
132 public Content getNode() {
133 return this.node;
134 }
135
136
137 public void setNode(Content node) {
138 this.node = node;
139 }
140
141 public NodeData getNodeData() {
142 if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
143 this.nodeData = this.getNode().getNodeData(this.nodeDataName);
144 }
145 return this.nodeData;
146 }
147
148 public void setNodeData(NodeData nodeData) {
149 this.nodeData = nodeData;
150 }
151
152 public boolean isEditorBinaryLink(){
153 return getNodeData() != null;
154 }
155
156 public String getNodeDataName() {
157 return this.nodeDataName;
158 }
159
160 public void setNodeDataName(String nodeDataName) {
161 this.nodeDataName = nodeDataName;
162 }
163
164 public String getHandle() {
165 if(StringUtils.isEmpty(this.handle)){
166 if(getNode() != null){
167 handle = getNode().getHandle();
168 } else {
169 handle = this.getFallbackHandle();
170 }
171 }
172 return this.handle;
173 }
174
175 public void setHandle(String path) {
176 this.handle = path;
177 }
178
179 public String getRepository() {
180 return this.repository;
181 }
182
183 public void setRepository(String repository) {
184 this.repository = repository;
185 }
186
187 public String getUUID() {
188 if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
189 this.uuid = this.getNode().getUUID();
190 }
191 return this.uuid;
192 }
193
194 public void setUUID(String uuid) {
195 this.uuid = uuid;
196 }
197
198 public String getFallbackHandle() {
199 return this.fallbackHandle;
200 }
201
202 public void setFallbackHandle(String fallbackPath) {
203 this.fallbackHandle = fallbackPath;
204 }
205
206 public String getAnchor() {
207 return this.anchor;
208 }
209
210 public void setAnchor(String anchor) {
211 this.anchor = anchor;
212 }
213
214 public String getParameters() {
215 return this.parameters;
216 }
217
218 public void setParameters(String parameters) {
219 this.parameters = parameters;
220 }
221 }