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;
35
36 import info.magnolia.cms.core.AggregationState;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.context.WebContext;
40 import info.magnolia.module.templating.engine.RenderingEngine;
41 import info.magnolia.objectfactory.Components;
42
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.List;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.jsp.tagext.BodyTagSupport;
50
51
52
53
54
55
56
57
58
59
60
61
62 public class Include extends BodyTagSupport {
63 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Include.class);
64
65 protected RenderingEngine renderingEngine = Components.getSingleton(RenderingEngine.class);
66
67
68
69
70
71 private String path;
72
73
74
75
76 private transient List attributes;
77
78
79
80
81 private transient Content contentNode;
82
83
84
85
86 private String contentNodeName;
87
88
89
90
91 private boolean noEditBars = false;
92
93
94
95
96
97
98 public void setContainer(Content contentNode) {
99 this.setContentNode(contentNode);
100 }
101
102
103
104
105
106 public void setContentNode(Content contentNode) {
107 this.contentNode = contentNode;
108 }
109
110
111
112
113
114
115 public void setPath(String path) {
116 this.path = path;
117 }
118
119
120
121
122
123 public void setContentNodeName(String contentNodeName) {
124 this.contentNodeName = contentNodeName;
125 }
126
127
128
129
130
131 public void setAttribute(String name, Object value) {
132 if (attributes == null) {
133 attributes = new ArrayList();
134 }
135 Object[] attributesArray = new Object[]{name, value};
136 attributes.add(attributesArray);
137 }
138
139
140
141
142
143 public void setNoEditBars(boolean noEditBars) {
144 this.noEditBars = noEditBars;
145 }
146
147 public int doAfterBody() {
148 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
149 if ((attributes != null) && (attributes.size() > 0)) {
150 Iterator i = attributes.iterator();
151 while (i.hasNext()) {
152 Object[] s = (Object[]) i.next();
153 req.setAttribute((String) s[0], s[1]);
154 }
155 }
156 return SKIP_BODY;
157 }
158
159 public int doEndTag() {
160 boolean localContentNodeSet = false;
161 Content oldContentNode = Resource.getLocalContentNode();
162
163
164 String oldLocalContentNodeCollectionName = Resource.getLocalContentNodeCollectionName();
165 Resource.setLocalContentNodeCollectionName(null);
166
167 try {
168
169 Content content = this.contentNode;
170 if (content == null) {
171
172 if (this.contentNodeName != null) {
173 content = Resource.getCurrentActivePage().getContent(this.contentNodeName);
174 if (content != null) {
175 Resource.setLocalContentNode(content);
176 localContentNodeSet = true;
177 }
178 }
179
180 else {
181 content = Resource.getLocalContentNode();
182 if (content == null) {
183 content = Resource.getGlobalContentNode();
184 if (content != null) {
185 Resource.setLocalContentNode(content);
186 localContentNodeSet = true;
187 }
188 }
189 }
190 if (content == null) {
191 throw new Exception("no content node found");
192 }
193 }
194
195 if (content != Resource.getCurrentActivePage() && !localContentNodeSet && content != null) {
196 Resource.setLocalContentNode(content);
197 localContentNodeSet = true;
198 }
199
200 final AggregationState aggregationState = MgnlContext.getAggregationState();
201 boolean orgShowPreview = aggregationState.isPreviewMode();
202 if(noEditBars && !orgShowPreview){
203 aggregationState.setPreviewMode(true);
204 }
205
206 if (this.path != null) {
207 log.warn("You are using the deprecated path attribute of the include tag. Your jsp will be included for now, but you might want to update your code to avoid bad surprises in the future.");
208 pageContext.include(this.path);
209 } else {
210 WebContext webContext = MgnlContext.getWebContext();
211 webContext.setPageContext(pageContext);
212 try{
213 renderingEngine.render(content, pageContext.getOut());
214 }
215 finally{
216 webContext.setPageContext(null);
217 }
218 }
219 if(noEditBars){
220 aggregationState.setPreviewMode(orgShowPreview);
221 }
222
223 } catch (IOException e) {
224
225 throw new RuntimeException(e);
226 } catch (Exception e) {
227 log.error(e.getMessage(), e);
228 }
229
230 finally {
231
232 if(localContentNodeSet){
233 if(oldContentNode != null){
234 Resource.setLocalContentNode(oldContentNode);
235 }
236 else{
237 Resource.removeLocalContentNode();
238 }
239 }
240
241 Resource.setLocalContentNodeCollectionName(oldLocalContentNodeCollectionName);
242 }
243
244 this.removeAttributes();
245 return EVAL_PAGE;
246 }
247
248 private void removeAttributes() {
249 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
250 if ((attributes != null) && (attributes.size() > 0)) {
251 Iterator i = attributes.iterator();
252 while (i.hasNext()) {
253 Object[] s = (Object[]) i.next();
254 req.removeAttribute((String) s[0]);
255 }
256 }
257 attributes = null;
258 }
259
260 public void release() {
261 this.path = null;
262 this.attributes = null;
263 this.contentNode = null;
264 super.release();
265 }
266
267 }