1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.fatwire.gst.foundation.include;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Locale;
22
23 import org.apache.commons.lang.StringUtils;
24
25 import COM.FutureTense.Interfaces.ICS;
26
27 import com.fatwire.assetapi.data.AssetId;
28 import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate;
29 import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Style;
30 import com.fatwire.gst.foundation.facade.runtag.render.CallTemplate.Type;
31
32
33
34
35
36 public class IncludeTemplate implements Include {
37
38 public static final List<String> FORBIDDEN_VARS = Collections.unmodifiableList(Arrays.asList("c", "cid", "eid",
39 "seid", "packedargs", "variant", "context", "pagename", "childpagename", "site", "sitepfx", "tid",
40 "rendermode", "ft_ss", "SystemAssetsRoot", "cshttp", "errno", "tablename", "empty", "errdetail", "null"));
41
42 private final CallTemplate tag;
43 private final List<String> pc;
44 private final ICS ics;
45
46
47
48
49
50
51 public IncludeTemplate(final ICS ics, final AssetId asset, final String tname) {
52 this.ics = ics;
53 tag = new CallTemplate();
54 tag.setTname(tname);
55
56 final String eid = ics.GetVar("eid");
57 if (eid != null) {
58 tag.setTid(eid);
59 tag.setTtype(Type.CSElement);
60 } else {
61 tag.setTid(ics.GetVar("tid"));
62 tag.setTtype(Type.Template);
63 }
64 tag.setContext("");
65 final String site = ics.GetVar("site");
66 tag.setSite(site);
67 final String packedargs = ics.GetVar("packedargs");
68 if (packedargs != null && packedargs.length() > 0) {
69 tag.setPackedargs(packedargs);
70 }
71
72 tag.setAsset(asset);
73 tag.setFixPageCriteria(false);
74
75 tag.setSlotname("foo");
76 final String target = tname.startsWith("/") ? site + tname : site + "/" + asset.getType() + "/" + tname;
77 final String[] keys = ics.pageCriteriaKeys(target);
78 if (keys == null) {
79 throw new IllegalArgumentException("Can't find page criteria for " + target
80 + ". Please check if pagecriteria are set for " + target + ".");
81 }
82 pc = Arrays.asList(keys);
83
84
85 for (final String key : keys) {
86 if (!FORBIDDEN_VARS.contains(key.toLowerCase(Locale.US))) {
87 final String value = ics.GetVar(key);
88 if (StringUtils.isNotBlank(value)) {
89 tag.setArgument(key, value);
90 }
91 }
92 }
93 }
94
95
96
97
98
99
100
101
102 public void include(final ICS ics) {
103 final String s = tag.execute(ics);
104 if (s != null) {
105 ics.StreamText(s);
106 }
107
108 }
109
110
111
112
113
114
115
116
117 public IncludeTemplate argument(final String name, final String value) {
118 if (StringUtils.isBlank(name)) {
119 return this;
120 }
121 if (FORBIDDEN_VARS.contains(name.toLowerCase(Locale.US))) {
122 throw new IllegalArgumentException("Can't deal with " + name
123 + ". It is a forbidden argument to set as an argument. Forbidden arguments are: "
124 + FORBIDDEN_VARS.toString());
125 }
126 if (pc.contains(name)) {
127 tag.setArgument(name, value);
128 } else {
129 throw new IllegalArgumentException("Can't deal with " + name
130 + ". It not part of page criteria. PageCriteria are: " + pc.toString());
131 }
132 return this;
133 }
134
135
136
137
138
139
140
141 public IncludeTemplate copyArguments(final String... name) {
142 if (name == null) {
143 return this;
144 }
145 for (final String n : name) {
146 argument(n, ics.GetVar(n));
147 }
148 return this;
149 }
150
151
152
153
154
155
156
157
158 public IncludeTemplate packedargs(final String s) {
159 tag.setPackedargs(s);
160 return this;
161 }
162
163
164
165
166
167
168 public IncludeTemplate style(final Style s) {
169 tag.setStyle(s);
170 return this;
171 }
172
173
174
175
176
177
178 public IncludeTemplate element() {
179 return style(Style.element);
180 }
181
182
183
184
185
186
187 public IncludeTemplate embedded() {
188 return style(Style.embedded);
189 }
190
191
192
193
194
195
196 public IncludeTemplate pagelet() {
197 return style(Style.pagelet);
198 }
199
200 }