1 /*
2 * $Id: InsertDefinitionModel.java 1058106 2011-01-12 12:22:58Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.template;
23
24 import java.io.IOException;
25
26 import org.apache.tiles.Attribute;
27 import org.apache.tiles.AttributeContext;
28 import org.apache.tiles.TilesContainer;
29 import org.apache.tiles.access.TilesAccess;
30 import org.apache.tiles.autotag.core.runtime.ModelBody;
31 import org.apache.tiles.autotag.core.runtime.annotation.Parameter;
32 import org.apache.tiles.request.Request;
33
34 /**
35 * <p>
36 * <strong>Insert a definition.</strong>
37 * </p>
38 * <p>
39 * Insert a definition with the possibility to override and specify parameters
40 * (called attributes). A definition can be seen as a (partially or totally)
41 * filled template that can override or complete attribute values.
42 * <code><tiles:insertDefinition></code> allows to define these attributes
43 * and pass them to the inserted jsp page, called template. Attributes are
44 * defined using nested tag <code><tiles:putAttribute></code> or
45 * <code><tiles:putListAttribute></code>.
46 * </p>
47 * <p>
48 * You must specify <code>name</code> tag attribute, for inserting a definition
49 * from definitions factory.
50 * </p>
51 * <p>
52 * <strong>Example : </strong>
53 * </p>
54 *
55 * <pre>
56 * <code>
57 * <tiles:insertDefinition name=".my.tiles.defininition flush="true">
58 * <tiles:putAttribute name="title" value="My first page" />
59 * <tiles:putAttribute name="header" value="/common/header.jsp" />
60 * <tiles:putAttribute name="footer" value="/common/footer.jsp" />
61 * <tiles:putAttribute name="menu" value="/basic/menu.jsp" />
62 * <tiles:putAttribute name="body" value="/basic/helloBody.jsp" />
63 * </tiles:insertDefinition>
64 * </code>
65 * </pre>
66 *
67 * @version $Rev: 1058106 $ $Date: 2011-01-12 23:22:58 +1100 (Wed, 12 Jan 2011) $
68 * @since 2.2.0
69 */
70 public class InsertDefinitionModel {
71
72 /**
73 * Executes the operation.
74 * @param definitionName The name of the definition to render.
75 * @param template If specified, this template will be used instead of the
76 * one used by the definition.
77 * @param templateType The type of the template attribute.
78 * @param templateExpression The expression to evaluate to get the value of the template.
79 * @param role A comma-separated list of roles. If present, the definition
80 * will be rendered only if the current user belongs to one of the roles.
81 * @param preparer The preparer to use to invoke before the definition is
82 * rendered. If specified, it overrides the preparer specified in the
83 * definition itself.
84 * @param flush If <code>true</code>, the response will be flushed after the insert.
85 * @param request The request.
86 * @param modelBody The body.
87 * @throws IOException If something goes wrong.
88 * @since 2.2.0
89 */
90 public void execute(
91 @Parameter(name = "name", required = true) String definitionName,
92 String template, String templateType, String templateExpression,
93 String role, String preparer, boolean flush, Request request, ModelBody modelBody)
94 throws IOException {
95 TilesContainer container = TilesAccess.getCurrentContainer(request);
96 container.startContext(request);
97 modelBody.evaluateWithoutWriting();
98 container = TilesAccess.getCurrentContainer(request);
99 renderDefinition(container, definitionName, template, templateType,
100 templateExpression, role, preparer, flush, request);
101 }
102
103 /**
104 * Renders a definition.
105 *
106 * @param container The container to use.
107 * @param definitionName The name of the definition to render.
108 * @param template If specified, this template will be used instead of the
109 * one used by the definition.
110 * @param templateType The type of the template attribute.
111 * @param templateExpression The expression to evaluate to get the value of the template.
112 * @param role A comma-separated list of roles. If present, the definition
113 * will be rendered only if the current user belongs to one of the roles.
114 * @param preparer The preparer to use to invoke before the definition is
115 * rendered. If specified, it overrides the preparer specified in the
116 * definition itself.
117 * @param flush If <code>true</code>, the response will be flushed after the insert.
118 * @param request The request.
119 * @throws IOException If something goes wrong.
120 */
121 private void renderDefinition(TilesContainer container,
122 String definitionName, String template, String templateType,
123 String templateExpression, String role, String preparer,
124 boolean flush, Request request) throws IOException {
125 try {
126 AttributeContext attributeContext = container
127 .getAttributeContext(request);
128 Attribute templateAttribute = Attribute.createTemplateAttribute(template,
129 templateExpression, templateType, role);
130 attributeContext.setPreparer(preparer);
131 attributeContext.setTemplateAttribute(templateAttribute);
132 container.render(definitionName, request);
133 if (flush) {
134 request.getWriter().flush();
135 }
136 } finally {
137 container.endContext(request);
138 }
139 }
140 }