1
22
23 package com.liferay.portlet.iframe.action;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.model.Portlet;
31 import com.liferay.portal.service.PortletLocalServiceUtil;
32 import com.liferay.portal.struts.PortletAction;
33 import com.liferay.portal.theme.ThemeDisplay;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.portlet.PortletConfigImpl;
37
38 import javax.portlet.PortletConfig;
39 import javax.portlet.PortletPreferences;
40 import javax.portlet.RenderRequest;
41 import javax.portlet.RenderResponse;
42
43 import org.apache.struts.action.ActionForm;
44 import org.apache.struts.action.ActionForward;
45 import org.apache.struts.action.ActionMapping;
46
47
53 public class ViewAction extends PortletAction {
54
55 public ActionForward render(
56 ActionMapping mapping, ActionForm form, PortletConfig config,
57 RenderRequest req, RenderResponse res)
58 throws Exception {
59
60 String src = transformSrc(config, req, res);
61
62 if (Validator.isNull(src)) {
63 return mapping.findForward("/portal/portlet_not_setup");
64 }
65
66 req.setAttribute(WebKeys.IFRAME_SRC, src);
67
68 return mapping.findForward("portlet.iframe.view");
69 }
70
71 protected String getSrc(RenderRequest req, RenderResponse res) {
72 PortletPreferences prefs = req.getPreferences();
73
74 return prefs.getValue("src", StringPool.BLANK);
75 }
76
77 protected String getUserName(RenderRequest req, RenderResponse res) {
78 PortletPreferences prefs = req.getPreferences();
79
80 String userName = prefs.getValue("user-name", StringPool.BLANK);
81
82 if (Validator.isNull(userName)) {
83 userName = req.getRemoteUser();
84 }
85
86 return userName;
87 }
88
89 protected String getPassword(RenderRequest req, RenderResponse res)
90 throws PortalException, SystemException {
91
92 PortletPreferences prefs = req.getPreferences();
93
94 String password = prefs.getValue("password", StringPool.BLANK);
95
96 if (Validator.isNull(password)) {
97 password = PortalUtil.getUserPassword(req);
98 }
99
100 return password;
101 }
102
103 protected String transformSrc(
104 PortletConfig config, RenderRequest req, RenderResponse res)
105 throws PortalException, SystemException {
106
107 PortletPreferences prefs = req.getPreferences();
108
109 String src = getSrc(req, res);
110
111 boolean auth = GetterUtil.getBoolean(
112 prefs.getValue("auth", StringPool.BLANK));
113 String authType = prefs.getValue("auth-type", StringPool.BLANK);
114 String userName = getUserName(req, res);
115 String password = getPassword(req, res);
116
117 if (auth) {
118 if (authType.equals("basic")) {
119 int pos = src.indexOf("://");
120
121 String protocol = src.substring(0, pos + 3);
122 String url = src.substring(pos + 3, src.length());
123
124 src =
125 protocol + userName + ":" + password +
126 "@" + url;
127 }
128 else {
129 ThemeDisplay themeDisplay =
130 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
131
132 PortletConfigImpl configImpl = (PortletConfigImpl)config;
133
134 String portletId = configImpl.getPortletId();
135
136 Portlet portlet = PortletLocalServiceUtil.getPortletById(
137 themeDisplay.getCompanyId(), portletId);
138
139 src =
140 themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
141 "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
142 portletId;
143 }
144 }
145
146 return src;
147 }
148
149 }