1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.sso;
18
19 import java.io.IOException;
20 import java.sql.Types;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import javax.portlet.ActionRequest;
26 import javax.portlet.ActionResponse;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletException;
29 import javax.portlet.PortletMode;
30 import javax.portlet.RenderRequest;
31 import javax.portlet.RenderResponse;
32
33 import org.apache.jetspeed.CommonPortletServices;
34 import org.apache.jetspeed.sso.SSOException;
35 import org.apache.jetspeed.sso.SSOProvider;
36 import org.apache.jetspeed.sso.SSOSite;
37 import org.apache.portals.gems.browser.BrowserIterator;
38 import org.apache.portals.gems.browser.DatabaseBrowserIterator;
39 import org.apache.portals.gems.browser.BrowserPortlet;
40 import org.apache.portals.gems.util.StatusMessage;
41 import org.apache.portals.messaging.PortletMessaging;
42 import org.apache.velocity.context.Context;
43
44 /***
45 * SSOBrowser
46 *
47 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
48 * @version $Id: SSOBrowser.java 348264 2005-11-22 22:06:45Z taylor $
49 */
50 public class SSOBrowser extends BrowserPortlet
51 {
52 private SSOProvider sso;
53
54 public void init(PortletConfig config)
55 throws PortletException
56 {
57 super.init(config);
58 sso = (SSOProvider)getPortletContext().getAttribute(CommonPortletServices.CPS_SSO_COMPONENT);
59 if (null == sso)
60 {
61 throw new PortletException("Failed to find the SSO Provider on portlet initialization");
62 }
63 }
64
65
66 public void getRows(RenderRequest request, String sql, int windowSize)
67 throws Exception
68 {
69 List resultSetTitleList = new ArrayList();
70 List resultSetTypeList = new ArrayList();
71 try
72 {
73 Iterator sites = sso.getSites("");
74
75
76
77
78
79
80
81 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
82 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
83 resultSetTitleList.add(0, "Url");
84 resultSetTitleList.add(1, "Site");
85
86
87
88 List list = new ArrayList();
89 while (sites.hasNext())
90 {
91 List row = new ArrayList(2);
92 SSOSite site = (SSOSite)sites.next();
93 row.add(0, site.getSiteURL());
94 row.add(1, site.getName());
95 list.add(row);
96 }
97 BrowserIterator iterator = new DatabaseBrowserIterator(
98 list, resultSetTitleList, resultSetTypeList,
99 windowSize);
100 setBrowserIterator(request, iterator);
101 iterator.sort("Site");
102 }
103 catch (Exception e)
104 {
105
106 e.printStackTrace();
107 throw e;
108 }
109 }
110
111 public void doView(RenderRequest request, RenderResponse response)
112 throws PortletException, IOException
113 {
114 String selectedSite = (String)PortletMessaging.receive(request, "site", "selectedUrl");
115 if (selectedSite != null)
116 {
117 Context context = this.getContext(request);
118 context.put("currentUrl", selectedSite);
119 String selectedName = (String)PortletMessaging.receive(request, "site", "selectedName");
120 context.put("currentName", selectedName);
121
122 String realm = (String)PortletMessaging.receive(request, "site", "realm");
123 context.put("currentRealm", realm);
124 String userField = (String)PortletMessaging.receive(request, "site", "idField");
125 context.put("currentFFID", userField);
126 String pwdFiled = (String)PortletMessaging.receive(request, "site", "pwdField");
127 context.put("currentFFPWD", pwdFiled);
128
129
130
131
132 }
133 StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, "SSOBrowser", "status");
134 if (msg != null)
135 {
136 this.getContext(request).put("statusMsg", msg);
137 }
138
139 super.doView(request, response);
140 }
141
142 public void processAction(ActionRequest request, ActionResponse response)
143 throws PortletException, IOException
144 {
145 if (request.getPortletMode() == PortletMode.VIEW)
146 {
147 String selectedSite = request.getParameter("ssoSite");
148 if (selectedSite != null)
149 {
150 SSOSite site = sso.getSite(selectedSite);
151 if (site != null)
152 {
153 PortletMessaging.publish(request, "site", "selectedUrl", selectedSite);
154 PortletMessaging.publish(request, "site", "selectedName", site.getName());
155 PortletMessaging.publish(request, "site", "change", selectedSite);
156 PortletMessaging.publish(request, "site", "realm", site.getRealm());
157 PortletMessaging.publish(request, "site", "idField", site.getFormUserField());
158 PortletMessaging.publish(request, "site", "pwdField", site.getFormPwdField());
159 }
160 }
161 String refresh = request.getParameter("sso.refresh");
162 String save = request.getParameter("sso.save");
163 String neue = request.getParameter("sso.new");
164 String delete = request.getParameter("ssoDelete");
165
166 if (refresh != null)
167 {
168 this.clearBrowserIterator(request);
169 }
170 else if (neue != null)
171 {
172 PortletMessaging.cancel(request, "site", "selected");
173 PortletMessaging.cancel(request, "site", "selectedUrl");
174 PortletMessaging.cancel(request, "site", "realm");
175 PortletMessaging.cancel(request, "site", "idField");
176 PortletMessaging.cancel(request, "site", "pwdField");
177 }
178 else if (delete != null && (!(isEmpty(delete))))
179 {
180 try
181 {
182 SSOSite site = null;
183 site = sso.getSite(delete);
184 if (site != null)
185 {
186 sso.removeSite(site);
187 this.clearBrowserIterator(request);
188 PortletMessaging.cancel(request, "site", "selected");
189 PortletMessaging.cancel(request, "site", "selectedUrl");
190 PortletMessaging.cancel(request, "site", "realm");
191 PortletMessaging.cancel(request, "site", "idField");
192 PortletMessaging.cancel(request, "site", "pwdField");
193 }
194 }
195 catch (SSOException e)
196 {
197 publishStatusMessage(request, "SSOBrowser", "status", e, "Could not remove site");
198 }
199 }
200 else if (save != null)
201 {
202 String siteName = request.getParameter("site.name");
203 String siteUrl = request.getParameter("site.url");
204
205 String siteRealm = request.getParameter("site.realm");
206 String siteFormID = request.getParameter("site.form_field_ID");
207 String siteFormPWD = request.getParameter("site.form_field_PWD");
208
209 if (!(isEmpty(siteName) || isEmpty(siteUrl)))
210 {
211 try
212 {
213 SSOSite site = null;
214 String old = (String)PortletMessaging.receive(request, "site", "selectedUrl");
215 if (old != null)
216 {
217 site = sso.getSite(old);
218 }
219 else
220 {
221 site = sso.getSite(siteUrl);
222 }
223 if (site != null)
224 {
225 site.setName(siteName);
226 site.setSiteURL(siteUrl);
227 site.setRealm(siteRealm);
228 if (siteFormID != null && siteFormID.length() > 0
229 && siteFormPWD != null && siteFormPWD.length() > 0 )
230 {
231
232 site.setFormAuthentication(true);
233 site.setFormUserField(siteFormID);
234 site.setFormPwdField(siteFormPWD);
235 }
236 else
237 {
238
239 site.setChallengeResponseAuthentication(true);
240 }
241
242 sso.updateSite(site);
243 this.clearBrowserIterator(request);
244 PortletMessaging.publish(request, "site", "selectedName", siteName);
245 PortletMessaging.publish(request, "site", "selectedUrl", siteUrl);
246 PortletMessaging.publish(request, "site", "realm", siteRealm);
247 PortletMessaging.publish(request, "site", "idField",siteFormID);
248 PortletMessaging.publish(request, "site", "pwdField", siteFormPWD);
249
250 }
251 else
252 {
253 if (siteFormID != null && siteFormID.length() > 0
254 && siteFormPWD != null && siteFormPWD.length() > 0 )
255 {
256 sso.addSiteFormAuthenticated(siteName, siteUrl, siteRealm, siteFormID,siteFormPWD);
257
258 }
259 else
260 {
261 sso.addSiteChallengeResponse(siteName, siteUrl, siteRealm);
262 }
263 this.clearBrowserIterator(request);
264 }
265 }
266 catch (SSOException e)
267 {
268 publishStatusMessage(request, "SSOBrowser", "status", e, "Could not store site");
269 }
270 }
271 }
272 }
273 super.processAction(request, response);
274
275 }
276
277 private boolean isEmpty(String s)
278 {
279 if (s == null) return true;
280
281 if (s.trim().equals("")) return true;
282
283 return false;
284 }
285
286 }