View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.aggregator;
17  
18  import java.io.IOException;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.http.client.methods.CloseableHttpResponse;
23  import org.esigate.Driver;
24  import org.esigate.HttpErrorPage;
25  import org.esigate.http.HttpResponseUtils;
26  import org.esigate.parser.Adapter;
27  import org.esigate.parser.Element;
28  import org.esigate.parser.ElementType;
29  import org.esigate.parser.ParserContext;
30  import org.esigate.tags.TemplateRenderer;
31  
32  class IncludeTemplateElement implements Element {
33      public static final ElementType TYPE = new ElementType() {
34          @Override
35          public boolean isStartTag(String tag) {
36              return tag.startsWith("<!--$includetemplate$");
37          }
38  
39          @Override
40          public boolean isEndTag(String tag) {
41              return tag.startsWith("<!--$endincludetemplate$");
42          }
43  
44          @Override
45          public Element newInstance() {
46              return new IncludeTemplateElement();
47          }
48  
49          @Override
50          public boolean isSelfClosing(String tag) {
51              return false;
52          }
53  
54      };
55  
56      private Driver driver;
57      private String page;
58      private String name;
59      private final Map<String, String> params = new HashMap<>();
60      private Appendable out;
61  
62      @Override
63      public boolean onError(Exception e, ParserContext ctx) {
64          return false;
65      }
66  
67      @Override
68      public boolean onTagStart(String tag, ParserContext ctx) {
69          this.out = new Adapter(ctx.getCurrent());
70  
71          ElementAttributes tagAttributes = ElementAttributesFactory.createElementAttributes(tag);
72          this.driver = tagAttributes.getDriver();
73          this.page = tagAttributes.getPage();
74          this.name = tagAttributes.getName();
75  
76          return true;
77      }
78  
79      @Override
80      public void onTagEnd(String tag, ParserContext ctx) throws IOException, HttpErrorPage {
81          CloseableHttpResponse response =
82                  driver.render(page, ctx.getHttpRequest().getOriginalRequest(),
83                          new TemplateRenderer(name, params, page), new AggregateRenderer());
84          out.append(HttpResponseUtils.toString(response));
85      }
86  
87      public void addParam(String paramName, String value) {
88          params.put(paramName, value);
89      }
90  
91      @Override
92      public void characters(CharSequence csq, int start, int end) {
93          // Just ignore tag body
94      }
95  
96  }