001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.portals.gems.dojo;
018:
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.portlet.PortletException;
025: import javax.portlet.RenderRequest;
026: import javax.portlet.RenderResponse;
027: import org.apache.commons.lang.ObjectUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.jetspeed.headerresource.HeaderResource;
031: import org.apache.jetspeed.portlet.PortletHeaderRequest;
032: import org.apache.jetspeed.portlet.PortletHeaderResponse;
033: import org.apache.jetspeed.portlet.SupportsHeaderPhase;
034: import org.springframework.beans.BeansException;
035: import org.springframework.web.portlet.DispatcherPortlet;
036:
037: /**
038: * Abstract DOJO portlet for inserting in cross context dojo widget includes
039: *
040: * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
041: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
042: * @version $Id: $
043: */
044: public class DojoSpringMVCPortlet extends DispatcherPortlet implements
045: SupportsHeaderPhase {
046: protected static final String CRLF = "\r\n";
047:
048: protected static final String DOJO_REQUIRES_CORE_INIT_PARAM = "dojo.requires.core";
049: protected static final String DOJO_REQUIRES_MODULES_INIT_PARAM = "dojo.requires.modules";
050:
051: private String dojoRequiresCoreList = null;
052: private String dojoRequiresAddOnList = null;
053:
054: /*
055: * Class specific logger.
056: */
057: private final static Log log = LogFactory
058: .getLog(DojoSpringMVCPortlet.class);
059:
060: protected String headerPage;
061:
062: /*
063: * Portlet constructor.
064: */
065: public DojoSpringMVCPortlet() {
066: super ();
067: }
068:
069: protected boolean addJavascriptBlock(HeaderResource headerResource,
070: StringBuffer javascriptText) {
071: return addJavascriptElement(headerResource, null,
072: javascriptText);
073: }
074:
075: protected boolean addJavascriptInclude(
076: HeaderResource headerResource, String src) {
077: return addJavascriptElement(headerResource, src, null);
078: }
079:
080: protected boolean addJavascriptElement(
081: HeaderResource headerResource, String src,
082: StringBuffer javascriptText) {
083: if ((javascriptText != null && javascriptText.length() > 0)
084: || (src != null && src.length() > 0)) {
085: Map headerInfoMap = new HashMap(8);
086: headerInfoMap.put("language", "JavaScript");
087: headerInfoMap.put("type", "text/javascript");
088: if (src != null && src.length() > 0) {
089: headerInfoMap.put("src", src);
090: headerResource.addHeaderInfo("script", headerInfoMap,
091: "");
092: } else {
093: headerResource.addHeaderInfo("script", headerInfoMap,
094: CRLF + javascriptText.toString());
095: }
096: return true;
097: }
098: return false;
099: }
100:
101: /*
102: * Portlet lifecycle method.
103: */
104: protected void initFrameworkPortlet() throws PortletException,
105: BeansException {
106: super .initFrameworkPortlet();
107:
108: // access jetspeed heaader resource component
109: synchronized (this ) {
110: this .headerPage = this .getInitParameter("HeaderPage");
111: this .dojoRequiresCoreList = this
112: .getInitParameter(DOJO_REQUIRES_CORE_INIT_PARAM);
113: this .dojoRequiresAddOnList = this
114: .getInitParameter(DOJO_REQUIRES_MODULES_INIT_PARAM);
115: }
116: }
117:
118: /* (non-Javadoc)
119: * @see javax.portlet.GenericPortlet#doDispatch(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
120: */
121: protected void doRenderService(RenderRequest request,
122: RenderResponse response) throws Exception {
123: // dispatch normally
124: super .doRenderService(request, response);
125: }
126:
127: /*
128: * Include Dojo header content using header resource component.
129: *
130: * @param request render request
131: * @param response render response
132: */
133: public void doHeader(PortletHeaderRequest request,
134: PortletHeaderResponse response) throws PortletException {
135: // use header resource component to ensure header logic is included only once
136: HeaderResource headerResource = response.getHeaderResource();
137:
138: headerResource.dojoEnable();
139: includeHeaderContent(headerResource);
140:
141: if (this .headerPage != null) {
142: include(request, response, this .headerPage);
143: }
144: }
145:
146: protected void includeHeaderContent(HeaderResource headerResource) {
147: if (this .dojoRequiresCoreList != null) {
148: headerResource
149: .dojoAddCoreLibraryRequires(this .dojoRequiresCoreList);
150: }
151: if (this .dojoRequiresAddOnList != null) {
152: headerResource
153: .dojoAddModuleLibraryRequires(this .dojoRequiresAddOnList);
154: }
155: }
156:
157: public void include(PortletHeaderRequest request,
158: PortletHeaderResponse response, String headerPagePath,
159: StringBuffer headerText) throws PortletException {
160: response.include(request, response, headerPagePath);
161: headerText.append(response.getContent());
162: }
163:
164: public void include(PortletHeaderRequest request,
165: PortletHeaderResponse response, String headerPagePath)
166: throws PortletException {
167: response.include(request, response, headerPagePath);
168: response.getHeaderResource().addHeaderInfo(
169: response.getContent());
170: }
171:
172: // debugging
173:
174: protected void dumpAttributes(RenderRequest request) {
175: Enumeration attrIter = request.getAttributeNames();
176: log.info("request-attributes:");
177: while (attrIter.hasMoreElements()) {
178: Object attrNm = attrIter.nextElement();
179: Object attrVal = request.getAttribute(ObjectUtils
180: .toString(attrNm));
181: String attrValDesc = (attrVal instanceof String) ? (String) attrVal
182: : ((attrVal == null) ? "null" : attrVal.getClass()
183: .getName());
184: log.info(" key=" + ObjectUtils.toString(attrNm, "null")
185: + " value=" + attrValDesc);
186: }
187: }
188:
189: protected void dumpSession(RenderRequest request) {
190: Enumeration attrIter = request.getPortletSession()
191: .getAttributeNames();
192: log.info("session-attributes:");
193: while (attrIter.hasMoreElements()) {
194: Object attrNm = attrIter.nextElement();
195: Object attrVal = request.getPortletSession().getAttribute(
196: ObjectUtils.toString(attrNm));
197: String attrValDesc = (attrVal instanceof String) ? (String) attrVal
198: : ((attrVal == null) ? "null" : attrVal.getClass()
199: .getName());
200: log.info(" key=" + ObjectUtils.toString(attrNm, "null")
201: + " value=" + attrValDesc);
202: }
203: }
204:
205: protected void dumpNameValue(Map m) {
206: if (m == null) {
207: log.info(" <null>");
208: return;
209: }
210: if (m.size() == 0) {
211: log.info(" <empty>");
212: return;
213: }
214: Iterator entryIter = m.entrySet().iterator();
215: while (entryIter.hasNext()) {
216: Map.Entry e = (Map.Entry) entryIter.next();
217: Object eKey = e.getKey();
218: Object eVal = e.getValue();
219: String eKeyDesc = (eKey instanceof String) ? (String) eKey
220: : ((eKey == null) ? "null" : eKey.getClass()
221: .getName());
222: String eValDesc = (eVal instanceof String) ? (String) eVal
223: : ((eVal == null) ? "null" : eVal.getClass()
224: .getName());
225: log.info(" key=" + eKeyDesc + " value=" + eValDesc);
226: }
227: }
228: }
|