View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.gems.dojo;
18  
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.portlet.PortletException;
25  import javax.portlet.RenderRequest;
26  import javax.portlet.RenderResponse;
27  import org.apache.commons.lang.ObjectUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.jetspeed.headerresource.HeaderResource;
31  import org.apache.jetspeed.portlet.PortletHeaderRequest;
32  import org.apache.jetspeed.portlet.PortletHeaderResponse;
33  import org.apache.jetspeed.portlet.SupportsHeaderPhase;
34  import org.springframework.beans.BeansException;
35  import org.springframework.web.portlet.DispatcherPortlet;
36  
37  
38  /***
39   * Abstract DOJO portlet for inserting in cross context dojo widget includes
40   * 
41   * @author <a href="mailto:smilek@apache.org">Steve Milek</a>
42   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a> 
43   * @version $Id: $
44   */
45  public class DojoSpringMVCPortlet extends DispatcherPortlet implements SupportsHeaderPhase
46  {
47  	protected static final String CRLF = "\r\n";
48      
49      protected static final String DOJO_REQUIRES_CORE_INIT_PARAM = "dojo.requires.core";
50      protected static final String DOJO_REQUIRES_MODULES_INIT_PARAM = "dojo.requires.modules";
51      
52      private String dojoRequiresCoreList = null;
53      private String dojoRequiresAddOnList = null;
54      
55      /*
56       * Class specific logger.
57       */
58      private final static Log log = LogFactory.getLog(DojoSpringMVCPortlet.class);
59  
60      protected String headerPage;
61      
62      /*
63       * Portlet constructor.
64       */
65      public DojoSpringMVCPortlet() 
66      {
67          super();
68      }
69      
70      protected boolean addJavascriptBlock(HeaderResource headerResource, StringBuffer javascriptText)
71      {
72      	return addJavascriptElement( headerResource, null, javascriptText );
73      }
74      protected boolean addJavascriptInclude(HeaderResource headerResource, String src)
75      {
76      	return addJavascriptElement( headerResource, src, null );
77      }
78      protected boolean addJavascriptElement(HeaderResource headerResource, String src, StringBuffer javascriptText)
79      {
80      	if ( ( javascriptText != null && javascriptText.length() > 0 ) || ( src != null && src.length() > 0 ) )
81      	{
82      		Map headerInfoMap = new HashMap(8);
83      		headerInfoMap.put("language", "JavaScript");
84      		headerInfoMap.put("type", "text/javascript");
85      		if ( src != null && src.length() > 0 )
86      		{
87      			headerInfoMap.put("src", src);
88      			headerResource.addHeaderInfo("script", headerInfoMap, "");
89      		}
90      		else
91      		{
92      			headerResource.addHeaderInfo("script", headerInfoMap, CRLF + javascriptText.toString());
93      		}
94      		return true ;
95      	}
96      	return false ;
97      }
98      
99      
100 
101     /*
102      * Portlet lifecycle method.
103      */
104     protected void initFrameworkPortlet() throws PortletException, BeansException
105     {
106         super.initFrameworkPortlet();
107 
108         // access jetspeed heaader resource component
109         synchronized (this) 
110         {
111             this.headerPage = this.getInitParameter("HeaderPage");
112             this.dojoRequiresCoreList = this.getInitParameter( DOJO_REQUIRES_CORE_INIT_PARAM );
113             this.dojoRequiresAddOnList = this.getInitParameter( DOJO_REQUIRES_MODULES_INIT_PARAM );
114         }
115     }
116 
117     /* (non-Javadoc)
118      * @see javax.portlet.GenericPortlet#doDispatch(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
119      */
120     protected void doRenderService(RenderRequest request, RenderResponse response) throws Exception
121     {
122         // dispatch normally
123         super.doRenderService(request, response);
124     }
125 
126     /*
127      * Include Dojo header content using header resource component.
128      *
129      * @param request render request
130      * @param response render response
131      */    
132     public void doHeader(PortletHeaderRequest request, PortletHeaderResponse response)    
133     throws PortletException
134     {
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         {
143             include( request, response, this.headerPage );
144         }
145     }
146     
147     protected void includeHeaderContent( HeaderResource headerResource )
148     {
149         if ( this.dojoRequiresCoreList != null )
150         {
151             headerResource.dojoAddCoreLibraryRequires( this.dojoRequiresCoreList );
152         }
153         if ( this.dojoRequiresAddOnList != null )
154         {
155             headerResource.dojoAddModuleLibraryRequires( this.dojoRequiresAddOnList );
156         }
157     }    
158     
159     public void include(PortletHeaderRequest request, PortletHeaderResponse response, String headerPagePath, StringBuffer headerText) throws PortletException
160     {
161         response.include(request, response, headerPagePath);
162         headerText.append(response.getContent());
163     }
164     
165     public void include(PortletHeaderRequest request, PortletHeaderResponse response, String headerPagePath) throws PortletException
166     {
167         response.include(request, response, headerPagePath);
168         response.getHeaderResource().addHeaderInfo(response.getContent());
169     }
170 
171 
172 	// debugging
173 
174 	protected void dumpAttributes(RenderRequest request)
175 	{
176 		Enumeration attrIter = request.getAttributeNames();
177 		log.info( "request-attributes:");
178 		while ( attrIter.hasMoreElements() )
179 		{
180 			Object attrNm = attrIter.nextElement();
181 			Object attrVal = request.getAttribute(ObjectUtils.toString(attrNm));
182 			String attrValDesc = ( attrVal instanceof String ) ? (String)attrVal : (( attrVal == null ) ? "null" : attrVal.getClass().getName() );
183 			log.info( "   key=" + ObjectUtils.toString(attrNm,"null") + " value=" + attrValDesc);
184 		}
185 	}
186 	protected void dumpSession(RenderRequest request)
187 	{
188 		Enumeration attrIter = request.getPortletSession().getAttributeNames();
189 		log.info( "session-attributes:");
190 		while ( attrIter.hasMoreElements() )
191 		{
192 			Object attrNm = attrIter.nextElement();
193 			Object attrVal = request.getPortletSession().getAttribute(ObjectUtils.toString(attrNm));
194 			String attrValDesc = ( attrVal instanceof String ) ? (String)attrVal : (( attrVal == null ) ? "null" : attrVal.getClass().getName() );
195 			log.info( "   key=" + ObjectUtils.toString(attrNm,"null") + " value=" + attrValDesc);
196 		}
197 	}
198 	protected void dumpNameValue( Map m  )
199 	{
200 		if ( m == null )
201 		{
202 			log.info( "   <null>" );
203 			return;
204 		}
205 		if ( m.size() == 0 )
206 		{
207 			log.info( "   <empty>" );
208 			return;
209 		}
210 		Iterator entryIter = m.entrySet().iterator();
211 		while ( entryIter.hasNext() )
212 		{
213 			Map.Entry e = (Map.Entry)entryIter.next();
214 			Object eKey = e.getKey();
215 			Object eVal = e.getValue();
216 			String eKeyDesc = ( eKey instanceof String ) ? (String)eKey : (( eKey == null ) ? "null" : eKey.getClass().getName() );
217 			String eValDesc = ( eVal instanceof String ) ? (String)eVal : (( eVal == null ) ? "null" : eVal.getClass().getName() );
218 			log.info( "   key=" + eKeyDesc + " value=" + eValDesc);
219 		}
220 	}
221 }