View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.spring;
17  
18  import java.io.StringWriter;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.springframework.web.servlet.ModelAndView;
29  import org.springframework.web.servlet.mvc.ParameterizableViewController;
30  
31  import com.opensymphony.module.sitemesh.Config;
32  import com.opensymphony.module.sitemesh.Decorator;
33  import com.opensymphony.module.sitemesh.Factory;
34  import com.opensymphony.module.sitemesh.HTMLPage;
35  import com.opensymphony.module.sitemesh.RequestConstants;
36  import com.opensymphony.module.sitemesh.util.OutputConverter;
37  
38  /***
39   * @author jmccrindle
40   */
41  public class TemplateController extends ParameterizableViewController {
42  
43      private Map templateModel = new HashMap();
44      
45      /* (non-Javadoc)
46       * @see org.springframework.web.servlet.mvc.ParameterizableViewController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
47       */
48      protected ModelAndView handleRequestInternal(HttpServletRequest request,
49              HttpServletResponse response) throws Exception {
50          
51          HTMLPage htmlPage = (HTMLPage) request.getAttribute(RequestConstants.PAGE);
52  
53          Map context = new HashMap(templateModel);
54          
55          context.put("base", request.getContextPath());
56          context.put("locale", request.getLocale());
57  
58          // For backwards compatability with apps that used the old VelocityDecoratorServlet
59          // that extended VelocityServlet instead of VelocityViewServlet
60          context.put("req", request);
61          context.put("res", response);
62  
63          if (htmlPage == null) {
64              context.put("title", "Title?");
65              context.put("body", "<p>Body?</p>");
66              context.put("head", "<!-- head -->");
67          }
68          else {
69              context.put("title", OutputConverter.convert(htmlPage.getTitle()));
70              {
71                  StringWriter buffer = new StringWriter();
72                  htmlPage.writeBody(OutputConverter.getWriter(buffer));
73                  context.put("body", buffer.toString());
74              }
75              {
76                  StringWriter buffer = new StringWriter();
77                  htmlPage.writeHead(OutputConverter.getWriter(buffer));
78                  context.put("head", buffer.toString());
79              }
80              context.put("page", htmlPage);
81              Factory factory = Factory.getInstance(new Config(new ServletConfig() {
82                  public ServletContext getServletContext() {
83                      return TemplateController.this.getServletContext();
84                  }
85  
86                  public String getInitParameter(String name) {return null;}
87                  public String getServletName() {return null;}
88                  public Enumeration getInitParameterNames() {return null;}
89              }));
90              Decorator decorator = factory.getDecoratorMapper().getDecorator(request, htmlPage);
91          }
92          return new ModelAndView(getViewName(), context);        
93      }
94  
95      public void setTemplateModel(Map templateModel) {
96          this.templateModel = templateModel;
97      }
98  }