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     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", //$NON-NLS-1$
185             PageContext.REQUEST_SCOPE);
186         if (this.fileNameOnly.equals("true")) { //$NON-NLS-1$
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                 // we are not in a loop
199                 try {
200                     writeSrc(this.contentNode.getHandle() + "/" //$NON-NLS-1$
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() + "/" //$NON-NLS-1$
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); //$NON-NLS-1$
236             }
237         }
238     }
239 
240     /**
241      * @deprecated
242      */
243     private void setFileProperties() {
244         this.fileExtension = ServerConfiguration.getInstance().getDefaultExtension();
245         Content properties = null;
246         String contentNodeCollectionName = (String) pageContext.getAttribute("contentNodeCollectionName", //$NON-NLS-1$
247             PageContext.REQUEST_SCOPE);
248         if (contentNodeCollectionName == null) {
249             // we are not in a loop
250             try {
251                 properties = Resource.getGlobalContentNode().getContent(this.nodeDataName + "_properties"); //$NON-NLS-1$
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"); //$NON-NLS-1$
262             }
263             catch (Exception e) {
264                 if (log.isDebugEnabled()) {
265                     log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
266                 }
267             }
268         }
269         if (properties != null) {
270             this.fileName = properties.getNodeData("fileName").getString(); //$NON-NLS-1$
271             this.fileExtension = properties.getNodeData("extension").getString(); //$NON-NLS-1$
272             if (StringUtils.isEmpty(this.fileName)) {
273                 this.fileExtendedName = "." + this.fileExtension; //$NON-NLS-1$
274             }
275             else {
276                 this.slash = "/"; //$NON-NLS-1$
277                 this.fileExtendedName = this.fileName;
278                 int posLastDot = this.fileName.lastIndexOf("."); //$NON-NLS-1$
279                 int posExt = this.fileName.lastIndexOf("." + this.fileExtension); //$NON-NLS-1$
280                 if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) {
281                     // magnolia v 1.0: fileName saved with extension
282                     this.fileExtendedName += "." + this.fileExtension; //$NON-NLS-1$
283                 }
284             }
285         }
286     }
287 }