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.jetspeed.portlet;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.PortletConfig;
26  import javax.portlet.PortletException;
27  import javax.portlet.PortletPreferences;
28  import javax.portlet.RenderRequest;
29  import javax.portlet.RenderResponse;
30  import javax.portlet.WindowState;
31  
32  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
33  
34  /***
35   * IFrameGenericPortlet
36   * 
37   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
38   * @version $Id: IFrameGenericPortlet.java 578925 2007-09-24 19:22:58Z smilek $
39   */
40  public class IFrameGenericPortlet extends GenericVelocityPortlet
41  {
42  
43      private Map attributes = new HashMap();
44  
45      private Map maxAttributes = new HashMap();
46  
47      public void init(PortletConfig config) throws PortletException
48      {
49          super.init(config);
50          attributes.put("SRC", "http://www.apache.org");
51          attributes.put("ALIGN", "BOTTOM");
52          attributes.put("CLASS", "");
53          attributes.put("FRAMEBORDER", "0");
54          attributes.put("ID", "");
55          attributes.put("MARGINHEIGHT", "0");
56          attributes.put("MARGINWIDTH", "0");
57          attributes.put("NAME", "");
58  
59          attributes.put("HEIGHT", "");
60          attributes.put("WIDTH", "100%");
61          attributes.put("SCROLLING", "NO");
62          attributes.put("STYLE", "");
63  
64          maxAttributes.put("HEIGHT", "800");
65          maxAttributes.put("WIDTH", "100%");
66          maxAttributes.put("SCROLLING", "AUTO");
67          maxAttributes.put("STYLE", "");
68      }
69  
70      private String getAttributePreference(PortletPreferences prefs, String attribute)
71      {
72          return this.getMappedAttributePreference(prefs, attribute, attributes);
73      }
74  
75      private String getMaxAttributePreference(PortletPreferences prefs, String attribute)
76      {
77          return this.getMappedAttributePreference(prefs, "MAX-" + attribute, maxAttributes);
78      }
79  
80      private String getMappedAttributePreference(PortletPreferences prefs, String attribute, Map map)
81      {
82          return prefs.getValue(attribute, (String) map.get(attribute));
83      }
84  
85      private void appendAttribute(PortletPreferences prefs, StringBuffer content, String attribute, Map map)
86      {
87          String value;
88  
89          if (map == maxAttributes)
90              value = getMaxAttributePreference(prefs, attribute);
91          else
92              value = getAttributePreference(prefs, attribute);
93  
94          if (value == null || value.length() == 0) { return; }
95          content.append(" ").append(attribute).append("=\"").append(value).append("\"");
96      }
97  
98      private void appendAttribute(PortletPreferences prefs, StringBuffer content, String attribute)
99      {
100         appendAttribute(prefs, content, attribute, attributes);
101     }
102 
103     private void appendMaxAttribute(PortletPreferences prefs, StringBuffer content, String attribute)
104     {
105         appendAttribute(prefs, content, attribute, maxAttributes);
106     }
107 
108     public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
109     {
110         String viewPage = (String)request.getAttribute(PARAM_VIEW_PAGE);
111         if (viewPage != null)
112         {
113             super.doView(request, response);
114         }
115         else
116         {
117             doIFrame(request, response);
118         }
119     }
120 
121     public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
122     {
123         response.setContentType("text/html");
124         doPreferencesEdit(request, response);
125     }
126 
127     /***
128      * Render IFRAME content
129      */
130     protected void doIFrame(RenderRequest request, RenderResponse response) throws IOException
131     {
132         PortletPreferences prefs = request.getPreferences();
133         String source = getURLSource(request, response, prefs);
134         // generate HTML IFRAME content
135         StringBuffer content = new StringBuffer(4096);
136 
137         // fix JS2-349
138         content.append("<TABLE CLASS='iframePortletTableContainer' WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
139 
140         content.append("<IFRAME");
141 
142         // special case source
143         content.append(" ").append("SRC").append("=\"").append(source).append("\"");
144 
145         appendAttribute(prefs, content, "ALIGN");
146         appendAttribute(prefs, content, "CLASS");
147         appendAttribute(prefs, content, "FRAMEBORDER");
148         appendAttribute(prefs, content, "ID");
149         appendAttribute(prefs, content, "MARGINHEIGHT");
150         appendAttribute(prefs, content, "MARGINWIDTH");
151         appendAttribute(prefs, content, "NAME");
152         if (request.getWindowState().equals(WindowState.MAXIMIZED))
153         {
154             appendMaxAttribute(prefs, content, "HEIGHT");
155             appendMaxAttribute(prefs, content, "WIDTH");
156             appendMaxAttribute(prefs, content, "SCROLLING");
157             appendMaxAttribute(prefs, content, "STYLE");
158         }
159         else
160         {
161             appendAttribute(prefs, content, "HEIGHT");
162             appendAttribute(prefs, content, "WIDTH");
163             appendAttribute(prefs, content, "SCROLLING");
164             appendAttribute(prefs, content, "STYLE");
165         }
166         content.append(">");
167         content.append("<P STYLE=\"textAlign:center\"><A HREF=\"").append(source).append("\">").append(source).append(
168                 "</A></P>");
169         content.append("</IFRAME>");
170 
171         // end fix JS2-349
172         content.append("</TD></TR></TBODY></TABLE>");
173 
174         // set required content type and write HTML IFRAME content
175         response.setContentType("text/html");
176         response.getWriter().print(content.toString());
177     }
178 
179     public String getURLSource(RenderRequest request, RenderResponse response, PortletPreferences prefs)
180     {
181         String source = getAttributePreference(prefs, "SRC");
182         if (source == null) source = "";
183         return source;
184     }
185 
186     /***
187      * Save the prefs
188      */
189     public void processAction(ActionRequest request, ActionResponse actionResponse) throws PortletException,
190             IOException
191     {
192         processPreferencesAction(request, actionResponse);
193     }
194 
195 }