1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.beans.config.Server; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.NodeData; 18 import info.magnolia.cms.util.Resource; 19 20 import javax.jcr.RepositoryException; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.jsp.JspWriter ; 23 import javax.servlet.jsp.PageContext ; 24 import javax.servlet.jsp.tagext.TagSupport ; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.log4j.Logger; 28 29 30 35 public class FileSrc extends TagSupport { 36 37 40 private static final long serialVersionUID = 222L; 41 42 45 private static Logger log = Logger.getLogger(FileSrc.class); 46 47 private transient NodeData nodeData; 48 49 private transient Content contentNode; 50 51 private transient Content actpage; 52 53 private String nodeDataName = StringUtils.EMPTY; 54 55 private String contentNodeName = StringUtils.EMPTY; 56 57 private String fileNameOnly = StringUtils.EMPTY; 58 59 private HttpServletRequest request; 60 61 private String fileExtension; 62 63 private String fileName; 64 65 private String fileExtendedName; 66 67 private String slash = StringUtils.EMPTY; 68 69 72 public void setAtomName(String name) { 73 this.setNodeDataName(name); 74 } 75 76 80 public void setNodeDataName(String nodeDataName) { 81 this.nodeDataName = nodeDataName; 82 } 83 84 87 public void setContainerName(String name) { 88 this.setContentNodeName(name); 89 } 90 91 95 public void setContentNodeName(String contentNodeName) { 96 this.contentNodeName = contentNodeName; 97 } 98 99 103 public void setFileNameOnly(String value) { 104 this.fileNameOnly = "true"; } 106 107 110 public int doStartTag() { 111 this.request = (HttpServletRequest ) pageContext.getRequest(); 112 this.actpage = Resource.getCurrentActivePage(request); 113 if (StringUtils.isNotEmpty(this.contentNodeName)) { 114 try { 115 this.contentNode = this.actpage.getContent(this.contentNodeName); 116 } 117 catch (RepositoryException re) { 118 writeSrc(StringUtils.EMPTY); 119 } 120 if (this.contentNode == null) { 121 writeSrc(StringUtils.EMPTY); 122 return SKIP_BODY; 123 } 124 } 125 else { 126 this.contentNode = Resource.getLocalContentNode(request); 127 if (this.contentNode == null) { 128 this.contentNode = Resource.getGlobalContentNode(request); 129 } 130 if (this.contentNode != null) { 131 this.contentNodeName = this.contentNode.getName(); 132 } 133 else { 134 writeSrc(StringUtils.EMPTY); 135 return SKIP_BODY; 136 } 137 } 138 if (StringUtils.isEmpty(this.nodeDataName)) { 139 writeSrc(StringUtils.EMPTY); 140 return SKIP_BODY; 141 } 142 try { 143 this.nodeData = this.contentNode.getNodeData(this.contentNodeName); 144 } 145 catch (Exception e) { 146 writeSrc(StringUtils.EMPTY); 147 return SKIP_BODY; 148 } 149 if (this.nodeData == null) { 150 writeSrc(StringUtils.EMPTY); 151 return SKIP_BODY; 152 } 153 setFileProperties(); 154 155 String contentNodeCollectionName = (String ) pageContext.getAttribute("contentNodeCollectionName", PageContext.REQUEST_SCOPE); 157 if (this.fileNameOnly.equals("true")) { try { 159 writeSrc(this.fileExtendedName); 160 } 161 catch (Exception e) { 162 log.debug(e.getMessage()); 163 } 164 } 165 else { 166 if (contentNodeCollectionName == null) { 167 try { 169 writeSrc(this.contentNode.getHandle() + "/" + this.nodeDataName + this.slash + this.fileExtendedName); 171 } 172 catch (Exception e) { 173 log.debug(e.getMessage()); 174 } 175 } 176 else { 177 try { 178 writeSrc(Resource.getLocalContentNode(request).getHandle() + "/" + this.nodeDataName + this.slash + this.fileExtendedName); 180 } 181 catch (Exception e) { 182 log.debug(e.getMessage()); 183 } 184 } 185 } 186 return EVAL_PAGE; 187 } 188 189 private void writeSrc(String src) { 190 JspWriter out = pageContext.getOut(); 191 try { 192 out.print(src); 193 } 194 catch (Exception e) { 195 log.debug("Exception caught: " + e.getMessage(), e); } 197 } 198 199 202 private void setFileProperties() { 203 this.fileExtension = Server.getDefaultExtension(); 204 Content properties = null; 205 String contentNodeCollectionName = (String ) pageContext.getAttribute("contentNodeCollectionName", PageContext.REQUEST_SCOPE); 207 if (contentNodeCollectionName == null) { 208 try { 210 properties = Resource.getGlobalContentNode(this.request).getContent(this.nodeDataName + "_properties"); } 212 catch (Exception e) { 213 log.debug(e.getMessage()); 214 } 215 } 216 else { 217 try { 218 properties = Resource.getLocalContentNode(this.request).getContent(this.nodeDataName + "_properties"); } 220 catch (Exception e) { 221 log.debug("Exception caught: " + e.getMessage(), e); } 223 } 224 if (properties != null) { 225 this.fileName = properties.getNodeData("fileName").getString(); this.fileExtension = properties.getNodeData("extension").getString(); if (StringUtils.isEmpty(this.fileName)) { 228 this.fileExtendedName = "." + this.fileExtension; } 230 else { 231 this.slash = "/"; this.fileExtendedName = this.fileName; 233 int posLastDot = this.fileName.lastIndexOf("."); int posExt = this.fileName.lastIndexOf("." + this.fileExtension); if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) { 236 this.fileExtendedName += "." + this.fileExtension; } 239 } 240 } 241 } 242 } 243 | Popular Tags |