1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.beans.config.Paragraph; 16 import info.magnolia.cms.beans.config.ParagraphManager; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.util.Resource; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.tagext.BodyTagSupport ; 27 28 import org.apache.commons.lang.exception.NestableRuntimeException; 29 import org.slf4j.Logger; 30 import org.slf4j.LoggerFactory; 31 32 33 39 public class Include extends BodyTagSupport { 40 41 44 private static final long serialVersionUID = 222L; 45 46 49 private static Logger log = LoggerFactory.getLogger(Include.class); 50 51 54 private String path; 55 56 59 private transient List attributes; 60 61 64 private transient Content contentNode; 65 66 69 private String contentNodeName; 70 71 75 public void setContainer(Content contentNode) { 76 this.setContentNode(contentNode); 77 } 78 79 83 public void setContentNode(Content contentNode) { 84 this.contentNode = contentNode; 85 } 86 87 91 public void setPath(String path) { 92 this.path = path; 93 } 94 95 99 public void setContentNodeName(String contentNodeName) { 100 this.contentNodeName = contentNodeName; 101 } 102 103 107 public void setAttribute(String name, Object value) { 108 if (attributes == null) { 109 attributes = new ArrayList (); 110 } 111 Object [] attributesArray = new Object []{name, value}; 112 attributes.add(attributesArray); 113 } 114 115 118 public int doAfterBody() { 119 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 120 if ((attributes != null) && (attributes.size() > 0)) { 121 Iterator i = attributes.iterator(); 122 while (i.hasNext()) { 123 Object [] s = (Object []) i.next(); 124 req.setAttribute((String ) s[0], s[1]); 125 } 126 } 127 return SKIP_BODY; 128 } 129 130 133 public int doEndTag() { 134 boolean localContentNodeSet = false; 135 try { 136 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 137 138 Content content = this.contentNode; 140 if (content == null) { 141 if (this.contentNodeName != null) { 143 content = Resource.getCurrentActivePage(req).getContent(this.contentNodeName); 144 if (content != null) { 145 Resource.setLocalContentNode(req, content); 146 localContentNodeSet = true; 147 } 148 } 149 else { 151 content = Resource.getLocalContentNode(req); 152 if (content == null) { 153 content = Resource.getGlobalContentNode(req); 154 if (content != null) { 155 Resource.setLocalContentNode(req, content); 156 localContentNodeSet = true; 157 } 158 } 159 } 160 if (content == null) { 161 throw new Exception ("no content node found"); } 163 } 164 165 String jspPage = this.path; 166 167 if (jspPage == null) { 168 String paragraphName = content.getMetaData().getTemplate(); 169 Paragraph paragraph = ParagraphManager.getInstance().getInfo(paragraphName); 170 171 if (paragraph == null) { 172 log.error("Paragraph {} not found for page {}", paragraphName, 174 content.getHandle()); 175 } 176 else { 177 jspPage = paragraph.getTemplatePath(); 178 } 179 180 if (jspPage == null) { 181 log.error("Unable to render paragraph {} in page {}: templatePath not set.", paragraphName, 183 content.getHandle()); 184 } 185 } 186 if (jspPage != null) { 187 pageContext.include(jspPage); 188 } 189 } 190 catch (IOException e) { 191 throw new NestableRuntimeException(e); 193 } 194 catch (Exception e) { 195 log.error(e.getMessage(), e); 196 } 197 198 finally { 199 if(localContentNodeSet){ 201 Resource.removeLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 202 203 } 204 } 205 206 this.removeAttributes(); 207 return EVAL_PAGE; 208 } 209 210 213 private void removeAttributes() { 214 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 215 if ((attributes != null) && (attributes.size() > 0)) { 216 Iterator i = attributes.iterator(); 217 while (i.hasNext()) { 218 Object [] s = (Object []) i.next(); 219 req.removeAttribute((String ) s[0]); 220 } 221 } 222 attributes = null; 223 } 224 225 228 public void release() { 229 this.path = null; 230 this.attributes = null; 231 this.contentNode = null; 232 super.release(); 233 } 234 235 } | Popular Tags |