View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Returns informations as path, name or size of a file.
54   * @deprecated see cms:out.
55   *
56   * @jsp.tag name="fileSrc" body-content="empty"
57   *
58   * @author Marcel Salathe
59   * @version $Revision $ ($Author $)
60   */
61  public class FileSrc extends TagSupport {
62  
63      /**
64       * Stable serialVersionUID.
65       */
66      private static final long serialVersionUID = 222L;
67  
68      /**
69       * Logger.
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       * @deprecated
97       */
98      public void setAtomName(String name) {
99          this.setNodeDataName(name);
100     }
101 
102     /**
103      * @deprecated
104      * @jsp.attribute required="true" rtexprvalue="true"
105      */
106     public void setNodeDataName(String nodeDataName) {
107         this.nodeDataName = nodeDataName;
108     }
109 
110     /**
111      * @deprecated
112      */
113     public void setContainerName(String name) {
114         this.setContentNodeName(name);
115     }
116 
117     /**
118      * If left empty, local contentNode is used as set by contentNodeIterator, otherwise the global
119      * contentNode is used.
120      * @deprecated
121      * @jsp.attribute required="false" rtexprvalue="true"
122      */
123     public void setContentNodeName(String contentNodeName) {
124         this.contentNodeName = contentNodeName;
125     }
126 
127     /**
128      * If "true", the file name without the path will be generated.
129      * @deprecated
130      * @jsp.attribute required="false" rtexprvalue="true"
131      */
132     public void setFileNameOnly(String value) {
133         this.fileNameOnly = "true"; //$NON-NLS-1$
134     }
135 
136     /**
137      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
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", //$NON-NLS-1$
186             PageContext.REQUEST_SCOPE);
187         if (this.fileNameOnly.equals("true")) { //$NON-NLS-1$
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                 // we are not in a loop
200                 try {
201                     writeSrc(this.contentNode.getHandle() + "/" //$NON-NLS-1$
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() + "/" //$NON-NLS-1$
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); //$NON-NLS-1$
237             }
238         }
239     }
240 
241     /**
242      * @deprecated
243      */
244     private void setFileProperties() {
245         this.fileExtension = ServerConfiguration.getInstance().getDefaultExtension();
246         Content properties = null;
247         String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName", //$NON-NLS-1$
248             PageContext.REQUEST_SCOPE);
249         if (contentNodeCollectionName == null) {
250             // we are not in a loop
251             try {
252                 properties = Resource.getGlobalContentNode().getContent(this.nodeDataName + "_properties"); //$NON-NLS-1$
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"); //$NON-NLS-1$
263             }
264             catch (Exception e) {
265                 if (log.isDebugEnabled()) {
266                     log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
267                 }
268             }
269         }
270         if (properties != null) {
271             this.fileName = properties.getNodeData("fileName").getString(); //$NON-NLS-1$
272             this.fileExtension = properties.getNodeData("extension").getString(); //$NON-NLS-1$
273             if (StringUtils.isEmpty(this.fileName)) {
274                 this.fileExtendedName = "." + this.fileExtension; //$NON-NLS-1$
275             }
276             else {
277                 this.slash = "/"; //$NON-NLS-1$
278                 this.fileExtendedName = this.fileName;
279                 int posLastDot = this.fileName.lastIndexOf("."); //$NON-NLS-1$
280                 int posExt = this.fileName.lastIndexOf("." + this.fileExtension); //$NON-NLS-1$
281                 if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) {
282                     // magnolia v 1.0: fileName saved with extension
283                     this.fileExtendedName += "." + this.fileExtension; //$NON-NLS-1$
284                 }
285             }
286         }
287     }
288 }