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.cms.taglibs.util;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.taglibs.Resource;
40
41 import javax.jcr.RepositoryException;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.jsp.JspWriter;
44 import javax.servlet.jsp.PageContext;
45 import javax.servlet.jsp.tagext.TagSupport;
46
47 import org.apache.commons.lang.StringUtils;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57
58
59
60
61 public class FileSrc extends TagSupport {
62
63
64
65
66 private static final long serialVersionUID = 222L;
67
68
69
70
71 private static Logger log = LoggerFactory.getLogger(FileSrc.class);
72
73 private transient NodeData nodeData;
74
75 private transient Content contentNode;
76
77 private transient Content actpage;
78
79 private String nodeDataName = StringUtils.EMPTY;
80
81 private String contentNodeName = StringUtils.EMPTY;
82
83 private String fileNameOnly = StringUtils.EMPTY;
84
85 private HttpServletRequest request;
86
87 private String fileExtension;
88
89 private String fileName;
90
91 private String fileExtendedName;
92
93 private String slash = StringUtils.EMPTY;
94
95
96
97
98 public void setAtomName(String name) {
99 this.setNodeDataName(name);
100 }
101
102
103
104
105
106 public void setNodeDataName(String nodeDataName) {
107 this.nodeDataName = nodeDataName;
108 }
109
110
111
112
113 public void setContainerName(String name) {
114 this.setContentNodeName(name);
115 }
116
117
118
119
120
121
122
123 public void setContentNodeName(String contentNodeName) {
124 this.contentNodeName = contentNodeName;
125 }
126
127
128
129
130
131
132 public void setFileNameOnly(String value) {
133 this.fileNameOnly = "true";
134 }
135
136
137
138
139 @Override
140 public int doStartTag() {
141 this.request = (HttpServletRequest) pageContext.getRequest();
142 this.actpage = Resource.getCurrentActivePage();
143 if (StringUtils.isNotEmpty(this.contentNodeName)) {
144 try {
145 this.contentNode = this.actpage.getContent(this.contentNodeName);
146 }
147 catch (RepositoryException re) {
148 writeSrc(StringUtils.EMPTY);
149 }
150 if (this.contentNode == null) {
151 writeSrc(StringUtils.EMPTY);
152 return SKIP_BODY;
153 }
154 }
155 else {
156 this.contentNode = Resource.getLocalContentNode();
157 if (this.contentNode == null) {
158 this.contentNode = Resource.getGlobalContentNode();
159 }
160 if (this.contentNode != null) {
161 this.contentNodeName = this.contentNode.getName();
162 }
163 else {
164 writeSrc(StringUtils.EMPTY);
165 return SKIP_BODY;
166 }
167 }
168 if (StringUtils.isEmpty(this.nodeDataName)) {
169 writeSrc(StringUtils.EMPTY);
170 return SKIP_BODY;
171 }
172 try {
173 this.nodeData = this.contentNode.getNodeData(this.contentNodeName);
174 }
175 catch (Exception e) {
176 writeSrc(StringUtils.EMPTY);
177 return SKIP_BODY;
178 }
179 if (this.nodeData == null) {
180 writeSrc(StringUtils.EMPTY);
181 return SKIP_BODY;
182 }
183 setFileProperties();
184
185 String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName",
186 PageContext.REQUEST_SCOPE);
187 if (this.fileNameOnly.equals("true")) {
188 try {
189 writeSrc(this.fileExtendedName);
190 }
191 catch (Exception e) {
192 if (log.isDebugEnabled()) {
193 log.debug(e.getMessage());
194 }
195 }
196 }
197 else {
198 if (contentNodeCollectionName == null) {
199
200 try {
201 writeSrc(this.contentNode.getHandle() + "/"
202 + this.nodeDataName
203 + this.slash
204 + this.fileExtendedName);
205 }
206 catch (Exception e) {
207 if (log.isDebugEnabled()) {
208 log.debug(e.getMessage());
209 }
210 }
211 }
212 else {
213 try {
214 writeSrc(Resource.getLocalContentNode().getHandle() + "/"
215 + this.nodeDataName
216 + this.slash
217 + this.fileExtendedName);
218 }
219 catch (Exception e) {
220 if (log.isDebugEnabled()) {
221 log.debug(e.getMessage());
222 }
223 }
224 }
225 }
226 return EVAL_PAGE;
227 }
228
229 private void writeSrc(String src) {
230 JspWriter out = pageContext.getOut();
231 try {
232 out.print(src);
233 }
234 catch (Exception e) {
235 if (log.isDebugEnabled()) {
236 log.debug("Exception caught: " + e.getMessage(), e);
237 }
238 }
239 }
240
241
242
243
244 private void setFileProperties() {
245 this.fileExtension = ServerConfiguration.getInstance().getDefaultExtension();
246 Content properties = null;
247 String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName",
248 PageContext.REQUEST_SCOPE);
249 if (contentNodeCollectionName == null) {
250
251 try {
252 properties = Resource.getGlobalContentNode().getContent(this.nodeDataName + "_properties");
253 }
254 catch (Exception e) {
255 if (log.isDebugEnabled()) {
256 log.debug(e.getMessage());
257 }
258 }
259 }
260 else {
261 try {
262 properties = Resource.getLocalContentNode().getContent(this.nodeDataName + "_properties");
263 }
264 catch (Exception e) {
265 if (log.isDebugEnabled()) {
266 log.debug("Exception caught: " + e.getMessage(), e);
267 }
268 }
269 }
270 if (properties != null) {
271 this.fileName = properties.getNodeData("fileName").getString();
272 this.fileExtension = properties.getNodeData("extension").getString();
273 if (StringUtils.isEmpty(this.fileName)) {
274 this.fileExtendedName = "." + this.fileExtension;
275 }
276 else {
277 this.slash = "/";
278 this.fileExtendedName = this.fileName;
279 int posLastDot = this.fileName.lastIndexOf(".");
280 int posExt = this.fileName.lastIndexOf("." + this.fileExtension);
281 if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) {
282
283 this.fileExtendedName += "." + this.fileExtension;
284 }
285 }
286 }
287 }
288 }