001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.bpmscript.web;
017:
018: import java.io.StringWriter;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.servlet.ServletConfig;
024: import javax.servlet.ServletContext;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.springframework.web.servlet.ModelAndView;
029: import org.springframework.web.servlet.mvc.ParameterizableViewController;
030:
031: import com.opensymphony.module.sitemesh.Config;
032: import com.opensymphony.module.sitemesh.Decorator;
033: import com.opensymphony.module.sitemesh.Factory;
034: import com.opensymphony.module.sitemesh.HTMLPage;
035: import com.opensymphony.module.sitemesh.RequestConstants;
036: import com.opensymphony.module.sitemesh.util.OutputConverter;
037:
038: /**
039: * @author jmccrindle
040: */
041: public class TemplateController extends ParameterizableViewController {
042:
043: private Map<String, Object> templateModel = new HashMap<String, Object>();
044:
045: /* (non-Javadoc)
046: * @see org.springframework.web.servlet.mvc.ParameterizableViewController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
047: */
048: protected ModelAndView handleRequestInternal(
049: HttpServletRequest request, HttpServletResponse response)
050: throws Exception {
051:
052: HTMLPage htmlPage = (HTMLPage) request
053: .getAttribute(RequestConstants.PAGE);
054:
055: Map<String, Object> context = new HashMap<String, Object>(
056: templateModel);
057:
058: context.put("base", request.getContextPath());
059: context.put("locale", request.getLocale());
060:
061: // For backwards compatability with apps that used the old VelocityDecoratorServlet
062: // that extended VelocityServlet instead of VelocityViewServlet
063: context.put("req", request);
064: context.put("res", response);
065:
066: if (htmlPage == null) {
067: context.put("title", "Title?");
068: context.put("body", "<p>Body?</p>");
069: context.put("head", "<!-- head -->");
070: } else {
071: context.put("title", OutputConverter.convert(htmlPage
072: .getTitle()));
073: {
074: StringWriter buffer = new StringWriter();
075: htmlPage.writeBody(OutputConverter.getWriter(buffer));
076: context.put("body", buffer.toString());
077: }
078: {
079: StringWriter buffer = new StringWriter();
080: htmlPage.writeHead(OutputConverter.getWriter(buffer));
081: context.put("head", buffer.toString());
082: }
083: context.put("page", htmlPage);
084: Factory factory = Factory.getInstance(new Config(
085: new ServletConfig() {
086: public ServletContext getServletContext() {
087: return TemplateController.this
088: .getServletContext();
089: }
090:
091: public String getInitParameter(String name) {
092: return null;
093: }
094:
095: public String getServletName() {
096: return null;
097: }
098:
099: public Enumeration getInitParameterNames() {
100: return null;
101: }
102: }));
103: Decorator decorator = factory.getDecoratorMapper()
104: .getDecorator(request, htmlPage);
105: }
106: return new ModelAndView(getViewName(), context);
107: }
108:
109: public void setTemplateModel(Map<String, Object> templateModel) {
110: this.templateModel = templateModel;
111: }
112: }
|