View Javadoc

1   package org.apache.jetspeed.portal.portlets;
2   
3   /*
4    * Copyright 2000-2001,2004 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  // jetspeed
20  import org.apache.jetspeed.portal.Portlet;
21  import org.apache.jetspeed.services.PortletFactory;
22  import org.apache.jetspeed.portal.PortletControl;
23  import org.apache.jetspeed.services.PortalToolkit;
24  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
25  import org.apache.jetspeed.services.logging.JetspeedLogger;
26  import org.apache.jetspeed.services.rundata.JetspeedRunData;
27  import org.apache.jetspeed.util.template.JetspeedLink;
28  import org.apache.jetspeed.util.template.JetspeedLinkFactory;
29  
30  // ecs
31  import org.apache.jetspeed.util.JetspeedClearElement;
32  import org.apache.ecs.ConcreteElement;
33  
34  // turbine
35  import org.apache.turbine.util.RunData;
36  import org.apache.turbine.services.velocity.TurbineVelocity;
37  import org.apache.velocity.context.Context;
38  
39  /***
40   * Preview portlet. Displays content of potlet
41   * defined in parameter "previewedPortletName".
42   * 
43   * @version $Id: PreviewPortlet.java,v 1.5 2004/02/23 04:03:33 jford Exp $ 
44   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a> 
45   * @author <a href="mailto:mark_orciuch@ngsltd.com">Mark Orciuch</a>
46   */
47  public class PreviewPortlet extends AbstractPortlet
48  {
49  
50      /***
51       * Static initialization of the logger for this class
52       */    
53      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PreviewPortlet.class.getName());
54      
55      /***
56       * @param rundata The RunData object for the current request
57       */
58      public ConcreteElement getContent(RunData rundata)
59      {
60  
61          Context context = TurbineVelocity.getContext(rundata);
62          context.put( "data", rundata);
63          TurbineVelocity.getContext(rundata).put("jetspeed", new org.apache.jetspeed.util.template.JetspeedTool(rundata));
64  
65          ConcreteElement result = null;
66          String controlName = rundata.getParameters().getString("previewedControlName");
67          String portletName = rundata.getParameters().getString("previewedPortletName");
68          if ( portletName == null )
69          {
70              JetspeedLink jsLink = null;
71  
72              try
73              {
74                  jsLink = JetspeedLinkFactory.getInstance(rundata);
75                  rundata.setRedirectURI(
76                      jsLink.setUser(
77                          rundata.getUser().getUserName(), "default.psml")
78                      .addPathInfo("template", "Customize")
79                      .addPathInfo("media-type", "html")
80                      .toString()
81                      );
82                  JetspeedLinkFactory.putInstance(jsLink);
83                  jsLink = null;
84                  return new JetspeedClearElement("");
85              } catch (Exception e)
86              {
87                  logger.error("Exception",  e);
88                  rundata.setScreenTemplate("Home");
89                  return new JetspeedClearElement("");
90              }
91              //return new JetspeedClearElement("You must specify portlet to preview using [previewedPortletName] parameter");
92          }
93          Portlet portlet = null;
94  
95          try
96          {
97              portlet = PortletFactory.getPortlet(portletName, "PreviewPortlet");
98              PortletControl control = controlName == null ? PortalToolkit.getControl((String)null) :
99                                       PortalToolkit.getControl(controlName);
100             control = null;
101             if ( control != null )
102             {
103                 JetspeedRunData jdata = (JetspeedRunData)rundata;
104                 // Use the profile's skin
105                 //portlet.getPortletConfig().setSkin(PortalToolkit.getSkin(jdata.getProfile().getDocument().getPortlets().getSkin()));
106                 control.setPortlet(portlet);
107                 control.init();
108                 result = control.getContent(rundata);
109             } 
110             else if ( portlet != null )
111             {
112                 result = portlet.getContent(rundata);
113             }
114 
115             if ( result != null && !result.toString().equals("") )
116             {
117                 /*String html =  result.toString();
118                 String currentURL = rundata.getRequest().getRequestURI();
119                 Log.debug("PreviewPortlet: currentURL = " + currentURL + " reader value = " + html);
120                 PreviewRewriter rewriter = new PreviewRewriter(currentURL, currentURL , currentURL, portletName);
121                 result = new JetspeedClearElement(rewriter.rewrite(new StringReader(html)));*/
122             } 
123             else
124             {
125                 // The portlet already streamed its content - return a stub
126                 result = new JetspeedClearElement("");
127             }
128 
129         } 
130         catch ( Exception e )
131         {
132             logger.error("Exception",  e);
133             result = new JetspeedClearElement("This resource is temporarily unavailable");
134         }
135 
136         rundata.setLayout("preview");
137         return result;
138     }
139 
140 }