001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.iframe.action;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Portlet;
030: import com.liferay.portal.service.PortletLocalServiceUtil;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.theme.ThemeDisplay;
033: import com.liferay.portal.util.PortalUtil;
034: import com.liferay.portal.util.WebKeys;
035: import com.liferay.portlet.PortletConfigImpl;
036:
037: import javax.portlet.PortletConfig;
038: import javax.portlet.PortletPreferences;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041:
042: import org.apache.struts.action.ActionForm;
043: import org.apache.struts.action.ActionForward;
044: import org.apache.struts.action.ActionMapping;
045:
046: /**
047: * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class ViewAction extends PortletAction {
053:
054: public ActionForward render(ActionMapping mapping, ActionForm form,
055: PortletConfig config, RenderRequest req, RenderResponse res)
056: throws Exception {
057:
058: String src = transformSrc(config, req, res);
059:
060: if (Validator.isNull(src)) {
061: return mapping.findForward("/portal/portlet_not_setup");
062: }
063:
064: req.setAttribute(WebKeys.IFRAME_SRC, src);
065:
066: return mapping.findForward("portlet.iframe.view");
067: }
068:
069: protected String getSrc(RenderRequest req, RenderResponse res) {
070: PortletPreferences prefs = req.getPreferences();
071:
072: String src = prefs.getValue("src", StringPool.BLANK);
073:
074: src = ParamUtil.getString(req, "src", src);
075:
076: return src;
077: }
078:
079: protected String getUserName(RenderRequest req, RenderResponse res) {
080: PortletPreferences prefs = req.getPreferences();
081:
082: String userName = prefs.getValue("user-name", StringPool.BLANK);
083:
084: if (Validator.isNull(userName)) {
085: userName = req.getRemoteUser();
086: }
087:
088: return userName;
089: }
090:
091: protected String getPassword(RenderRequest req, RenderResponse res)
092: throws PortalException, SystemException {
093:
094: PortletPreferences prefs = req.getPreferences();
095:
096: String password = prefs.getValue("password", StringPool.BLANK);
097:
098: if (Validator.isNull(password)) {
099: password = PortalUtil.getUserPassword(req);
100: }
101:
102: return password;
103: }
104:
105: protected String transformSrc(PortletConfig config,
106: RenderRequest req, RenderResponse res)
107: throws PortalException, SystemException {
108:
109: PortletPreferences prefs = req.getPreferences();
110:
111: String src = getSrc(req, res);
112:
113: boolean auth = GetterUtil.getBoolean(prefs.getValue("auth",
114: StringPool.BLANK));
115: String authType = prefs.getValue("auth-type", StringPool.BLANK);
116: String userName = getUserName(req, res);
117: String password = getPassword(req, res);
118:
119: if (auth) {
120: if (authType.equals("basic")) {
121: int pos = src.indexOf("://");
122:
123: String protocol = src.substring(0, pos + 3);
124: String url = src.substring(pos + 3, src.length());
125:
126: src = protocol + userName + ":" + password + "@" + url;
127: } else {
128: ThemeDisplay themeDisplay = (ThemeDisplay) req
129: .getAttribute(WebKeys.THEME_DISPLAY);
130:
131: PortletConfigImpl configImpl = (PortletConfigImpl) config;
132:
133: String portletId = configImpl.getPortletId();
134:
135: Portlet portlet = PortletLocalServiceUtil
136: .getPortletById(themeDisplay.getCompanyId(),
137: portletId);
138:
139: src = themeDisplay.getPathMain() + "/"
140: + portlet.getStrutsPath() + "/proxy?p_l_id="
141: + themeDisplay.getPlid() + "&p_p_id="
142: + portletId;
143: }
144: }
145:
146: return src;
147: }
148:
149: }
|