001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/vm/VmServlet.java $
003: * $Id: VmServlet.java 7247 2006-03-29 21:09:51Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.vm;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.RequestDispatcher;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.sakaiproject.util.Web;
030:
031: /**
032: * <p>
033: * VmServlet is a Servlet that makes use of the Velocity Template Engine.
034: * </p>
035: * <p>
036: * This extends our ComponentServlet, giving us also the ability to find registered service components.
037: * </p>
038: */
039: public abstract class VmServlet extends ComponentServlet {
040: /**
041: * Access the object set in the velocity context for this name, if any.
042: *
043: * @param name
044: * The reference name.
045: * @param request
046: * The request.
047: * @return The reference value object, or null if none
048: */
049: public Object getVmReference(String name, HttpServletRequest request) {
050: return request.getAttribute(name);
051: }
052:
053: /**
054: * Add a reference object to the velocity context by name - if it's not already defined
055: *
056: * @param name
057: * The reference name.
058: * @param value
059: * The reference value object.
060: * @param request
061: * The request.
062: */
063: public void setVmReference(String name, Object value,
064: HttpServletRequest request) {
065: if (request.getAttribute(name) == null) {
066: request.setAttribute(name, value);
067: }
068: // else
069: // {
070: // Object old = request.getAttribute(name);
071: // if (!old.equals(value))
072: // {
073: // log("double setting vmReference: " + name + " was: " + old + " to: " + value);
074: // }
075: // }
076: }
077:
078: /**
079: * Add some standard references to the vm context.
080: *
081: * @param request
082: * The request.
083: * @param response
084: * The response.
085: */
086: protected void setVmStdRef(HttpServletRequest request,
087: HttpServletResponse response) {
088: // include some standard references
089: setVmReference("sakai_ActionURL", getActionURL(request),
090: request);
091:
092: // get the include (from the portal) for the HTML HEAD
093: if (getVmReference("sakai_head", request) == null) {
094: String headInclude = (String) request
095: .getAttribute("sakai.html.head");
096: if (headInclude != null) {
097: setVmReference("sakai_head", headInclude, request);
098: }
099: }
100:
101: // get the include (from the portal) for the HTML BODY onload
102: if (getVmReference("sakai_onload", request) == null) {
103: String onload = (String) request
104: .getAttribute("sakai.html.body.onload");
105: if (onload != null) {
106: setVmReference("sakai_onload", onload, request);
107: }
108: }
109:
110: // set the ref to the images
111: if (getVmReference("sakai_image_path", request) == null) {
112: setVmReference("sakai_image_path", "/library/image/",
113: request);
114: }
115:
116: // set the ref to the javscripts
117: if (getVmReference("sakai_script_path", request) == null) {
118: setVmReference("sakai_script_path", "/library/js/", request);
119: }
120:
121: // set the ref to the library
122: if (getVmReference("sakai_library_path", request) == null) {
123: setVmReference("sakai_library_path", "/library/", request);
124: }
125:
126: }
127:
128: /**
129: * Include the Velocity template, expanded with the current set of references
130: *
131: * @param template
132: * The path, relative to the webapp context, of the template file
133: * @param request
134: * The render request.
135: * @param response
136: * The render response.
137: * @throws PortletException
138: * if something goes wrong.
139: */
140: protected void includeVm(String template,
141: HttpServletRequest request, HttpServletResponse response)
142: throws ServletException {
143: // include some standard references
144: setVmStdRef(request, response);
145:
146: response.setContentType("text/html");
147:
148: // tell our vm processor to use this template
149: request.setAttribute("sakai.vm.path", template);
150:
151: try {
152: // get the vm by name so it does not have to have a URL mapping
153: RequestDispatcher dispatcher = getServletContext()
154: .getNamedDispatcher("sakai.vm");
155: dispatcher.include(request, response);
156: } catch (IOException e) {
157: throw new ServletException("includeVm: template: "
158: + template, e);
159: }
160: }
161:
162: /**
163: * Get a new ActionURL.
164: *
165: * @param req
166: * The current request.
167: * @return A new ActionURL.
168: */
169: protected ActionURL getActionURL(HttpServletRequest request) {
170: ActionURL a = new ActionURL(Web.returnUrl(request, null),
171: request);
172:
173: // set the pid and panel, if present in the request
174: // a.setPid(request.getParameter(ActionURL.PARAM_PID));
175: a.setPanel(request.getParameter(ActionURL.PARAM_PANEL));
176: a.setSite(request.getParameter(ActionURL.PARAM_SITE));
177: a.setPage(request.getParameter(ActionURL.PARAM_PAGE));
178:
179: return a;
180: }
181: }
|