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.jetspeed.portlet;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.portlet.ActionRequest;
024: import javax.portlet.ActionResponse;
025: import javax.portlet.PortletConfig;
026: import javax.portlet.PortletException;
027: import javax.portlet.PortletPreferences;
028: import javax.portlet.RenderRequest;
029: import javax.portlet.RenderResponse;
030: import javax.portlet.WindowState;
031:
032: import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
033:
034: /**
035: * IFrameGenericPortlet
036: *
037: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
038: * @version $Id: IFrameGenericPortlet.java 578925 2007-09-24 19:22:58Z smilek $
039: */
040: public class IFrameGenericPortlet extends GenericVelocityPortlet {
041:
042: private Map attributes = new HashMap();
043:
044: private Map maxAttributes = new HashMap();
045:
046: public void init(PortletConfig config) throws PortletException {
047: super .init(config);
048: attributes.put("SRC", "http://www.apache.org");
049: attributes.put("ALIGN", "BOTTOM");
050: attributes.put("CLASS", "");
051: attributes.put("FRAMEBORDER", "0");
052: attributes.put("ID", "");
053: attributes.put("MARGINHEIGHT", "0");
054: attributes.put("MARGINWIDTH", "0");
055: attributes.put("NAME", "");
056:
057: attributes.put("HEIGHT", "");
058: attributes.put("WIDTH", "100%");
059: attributes.put("SCROLLING", "NO");
060: attributes.put("STYLE", "");
061:
062: maxAttributes.put("HEIGHT", "800");
063: maxAttributes.put("WIDTH", "100%");
064: maxAttributes.put("SCROLLING", "AUTO");
065: maxAttributes.put("STYLE", "");
066: }
067:
068: private String getAttributePreference(PortletPreferences prefs,
069: String attribute) {
070: return this .getMappedAttributePreference(prefs, attribute,
071: attributes);
072: }
073:
074: private String getMaxAttributePreference(PortletPreferences prefs,
075: String attribute) {
076: return this .getMappedAttributePreference(prefs, "MAX-"
077: + attribute, maxAttributes);
078: }
079:
080: private String getMappedAttributePreference(
081: PortletPreferences prefs, String attribute, Map map) {
082: return prefs.getValue(attribute, (String) map.get(attribute));
083: }
084:
085: private void appendAttribute(PortletPreferences prefs,
086: StringBuffer content, String attribute, Map map) {
087: String value;
088:
089: if (map == maxAttributes)
090: value = getMaxAttributePreference(prefs, attribute);
091: else
092: value = getAttributePreference(prefs, attribute);
093:
094: if (value == null || value.length() == 0) {
095: return;
096: }
097: content.append(" ").append(attribute).append("=\"").append(
098: value).append("\"");
099: }
100:
101: private void appendAttribute(PortletPreferences prefs,
102: StringBuffer content, String attribute) {
103: appendAttribute(prefs, content, attribute, attributes);
104: }
105:
106: private void appendMaxAttribute(PortletPreferences prefs,
107: StringBuffer content, String attribute) {
108: appendAttribute(prefs, content, attribute, maxAttributes);
109: }
110:
111: public void doView(RenderRequest request, RenderResponse response)
112: throws PortletException, IOException {
113: String viewPage = (String) request
114: .getAttribute(PARAM_VIEW_PAGE);
115: if (viewPage != null) {
116: super .doView(request, response);
117: } else {
118: doIFrame(request, response);
119: }
120: }
121:
122: public void doEdit(RenderRequest request, RenderResponse response)
123: throws PortletException, IOException {
124: response.setContentType("text/html");
125: doPreferencesEdit(request, response);
126: }
127:
128: /**
129: * Render IFRAME content
130: */
131: protected void doIFrame(RenderRequest request,
132: RenderResponse response) throws IOException {
133: PortletPreferences prefs = request.getPreferences();
134: String source = getURLSource(request, response, prefs);
135: // generate HTML IFRAME content
136: StringBuffer content = new StringBuffer(4096);
137:
138: // fix JS2-349
139: content
140: .append("<TABLE CLASS='iframePortletTableContainer' WIDTH='100%'><TBODY CLASS='iframePortletTbodyContainer'><TR><TD>");
141:
142: content.append("<IFRAME");
143:
144: // special case source
145: content.append(" ").append("SRC").append("=\"").append(source)
146: .append("\"");
147:
148: appendAttribute(prefs, content, "ALIGN");
149: appendAttribute(prefs, content, "CLASS");
150: appendAttribute(prefs, content, "FRAMEBORDER");
151: appendAttribute(prefs, content, "ID");
152: appendAttribute(prefs, content, "MARGINHEIGHT");
153: appendAttribute(prefs, content, "MARGINWIDTH");
154: appendAttribute(prefs, content, "NAME");
155: if (request.getWindowState().equals(WindowState.MAXIMIZED)) {
156: appendMaxAttribute(prefs, content, "HEIGHT");
157: appendMaxAttribute(prefs, content, "WIDTH");
158: appendMaxAttribute(prefs, content, "SCROLLING");
159: appendMaxAttribute(prefs, content, "STYLE");
160: } else {
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=\"")
168: .append(source).append("\">").append(source).append(
169: "</A></P>");
170: content.append("</IFRAME>");
171:
172: // end fix JS2-349
173: content.append("</TD></TR></TBODY></TABLE>");
174:
175: // set required content type and write HTML IFRAME content
176: response.setContentType("text/html");
177: response.getWriter().print(content.toString());
178: }
179:
180: public String getURLSource(RenderRequest request,
181: RenderResponse response, PortletPreferences prefs) {
182: String source = getAttributePreference(prefs, "SRC");
183: if (source == null)
184: source = "";
185: return source;
186: }
187:
188: /**
189: * Save the prefs
190: */
191: public void processAction(ActionRequest request,
192: ActionResponse actionResponse) throws PortletException,
193: IOException {
194: processPreferencesAction(request, actionResponse);
195: }
196:
197: }
|