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 public int doStartTag() {
140 this.request = (HttpServletRequest) pageContext.getRequest();
141 this.actpage = Resource.getCurrentActivePage();
142 if (StringUtils.isNotEmpty(this.contentNodeName)) {
143 try {
144 this.contentNode = this.actpage.getContent(this.contentNodeName);
145 }
146 catch (RepositoryException re) {
147 writeSrc(StringUtils.EMPTY);
148 }
149 if (this.contentNode == null) {
150 writeSrc(StringUtils.EMPTY);
151 return SKIP_BODY;
152 }
153 }
154 else {
155 this.contentNode = Resource.getLocalContentNode();
156 if (this.contentNode == null) {
157 this.contentNode = Resource.getGlobalContentNode();
158 }
159 if (this.contentNode != null) {
160 this.contentNodeName = this.contentNode.getName();
161 }
162 else {
163 writeSrc(StringUtils.EMPTY);
164 return SKIP_BODY;
165 }
166 }
167 if (StringUtils.isEmpty(this.nodeDataName)) {
168 writeSrc(StringUtils.EMPTY);
169 return SKIP_BODY;
170 }
171 try {
172 this.nodeData = this.contentNode.getNodeData(this.contentNodeName);
173 }
174 catch (Exception e) {
175 writeSrc(StringUtils.EMPTY);
176 return SKIP_BODY;
177 }
178 if (this.nodeData == null) {
179 writeSrc(StringUtils.EMPTY);
180 return SKIP_BODY;
181 }
182 setFileProperties();
183
184 String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName",
185 PageContext.REQUEST_SCOPE);
186 if (this.fileNameOnly.equals("true")) {
187 try {
188 writeSrc(this.fileExtendedName);
189 }
190 catch (Exception e) {
191 if (log.isDebugEnabled()) {
192 log.debug(e.getMessage());
193 }
194 }
195 }
196 else {
197 if (contentNodeCollectionName == null) {
198
199 try {
200 writeSrc(this.contentNode.getHandle() + "/"
201 + this.nodeDataName
202 + this.slash
203 + this.fileExtendedName);
204 }
205 catch (Exception e) {
206 if (log.isDebugEnabled()) {
207 log.debug(e.getMessage());
208 }
209 }
210 }
211 else {
212 try {
213 writeSrc(Resource.getLocalContentNode().getHandle() + "/"
214 + this.nodeDataName
215 + this.slash
216 + this.fileExtendedName);
217 }
218 catch (Exception e) {
219 if (log.isDebugEnabled()) {
220 log.debug(e.getMessage());
221 }
222 }
223 }
224 }
225 return EVAL_PAGE;
226 }
227
228 private void writeSrc(String src) {
229 JspWriter out = pageContext.getOut();
230 try {
231 out.print(src);
232 }
233 catch (Exception e) {
234 if (log.isDebugEnabled()) {
235 log.debug("Exception caught: " + e.getMessage(), e);
236 }
237 }
238 }
239
240
241
242
243 private void setFileProperties() {
244 this.fileExtension = ServerConfiguration.getInstance().getDefaultExtension();
245 Content properties = null;
246 String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName",
247 PageContext.REQUEST_SCOPE);
248 if (contentNodeCollectionName == null) {
249
250 try {
251 properties = Resource.getGlobalContentNode().getContent(this.nodeDataName + "_properties");
252 }
253 catch (Exception e) {
254 if (log.isDebugEnabled()) {
255 log.debug(e.getMessage());
256 }
257 }
258 }
259 else {
260 try {
261 properties = Resource.getLocalContentNode().getContent(this.nodeDataName + "_properties");
262 }
263 catch (Exception e) {
264 if (log.isDebugEnabled()) {
265 log.debug("Exception caught: " + e.getMessage(), e);
266 }
267 }
268 }
269 if (properties != null) {
270 this.fileName = properties.getNodeData("fileName").getString();
271 this.fileExtension = properties.getNodeData("extension").getString();
272 if (StringUtils.isEmpty(this.fileName)) {
273 this.fileExtendedName = "." + this.fileExtension;
274 }
275 else {
276 this.slash = "/";
277 this.fileExtendedName = this.fileName;
278 int posLastDot = this.fileName.lastIndexOf(".");
279 int posExt = this.fileName.lastIndexOf("." + this.fileExtension);
280 if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) {
281
282 this.fileExtendedName += "." + this.fileExtension;
283 }
284 }
285 }
286 }
287 }