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.portlets.security.sso;
018:
019: import java.io.IOException;
020: import java.sql.Types;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletMode;
030: import javax.portlet.RenderRequest;
031: import javax.portlet.RenderResponse;
032:
033: import org.apache.jetspeed.CommonPortletServices;
034: import org.apache.jetspeed.sso.SSOException;
035: import org.apache.jetspeed.sso.SSOProvider;
036: import org.apache.jetspeed.sso.SSOSite;
037: import org.apache.portals.gems.browser.BrowserIterator;
038: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
039: import org.apache.portals.gems.browser.BrowserPortlet;
040: import org.apache.portals.gems.util.StatusMessage;
041: import org.apache.portals.messaging.PortletMessaging;
042: import org.apache.velocity.context.Context;
043:
044: /**
045: * SSOBrowser
046: *
047: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
048: * @version $Id: SSOBrowser.java 348264 2005-11-22 22:06:45Z taylor $
049: */
050: public class SSOBrowser extends BrowserPortlet {
051: private SSOProvider sso;
052:
053: public void init(PortletConfig config) throws PortletException {
054: super .init(config);
055: sso = (SSOProvider) getPortletContext().getAttribute(
056: CommonPortletServices.CPS_SSO_COMPONENT);
057: if (null == sso) {
058: throw new PortletException(
059: "Failed to find the SSO Provider on portlet initialization");
060: }
061: }
062:
063: public void getRows(RenderRequest request, String sql,
064: int windowSize) throws Exception {
065: List resultSetTitleList = new ArrayList();
066: List resultSetTypeList = new ArrayList();
067: try {
068: Iterator sites = sso.getSites("");
069:
070: // List userObjectList = (List)getParameterFromTemp(portlet, rundata, USER_OBJECTS);
071:
072: //
073: // Add MetaData headers, types
074: //
075:
076: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
077: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
078: resultSetTitleList.add(0, "Url");
079: resultSetTitleList.add(1, "Site");
080:
081: //subPopulate(rundata, qResult, repo, folder, null);
082:
083: List list = new ArrayList();
084: while (sites.hasNext()) {
085: List row = new ArrayList(2);
086: SSOSite site = (SSOSite) sites.next();
087: row.add(0, site.getSiteURL());
088: row.add(1, site.getName());
089: list.add(row);
090: }
091: BrowserIterator iterator = new DatabaseBrowserIterator(
092: list, resultSetTitleList, resultSetTypeList,
093: windowSize);
094: setBrowserIterator(request, iterator);
095: iterator.sort("Site");
096: } catch (Exception e) {
097: //log.error("Exception in CMSBrowserAction.getRows: ", e);
098: e.printStackTrace();
099: throw e;
100: }
101: }
102:
103: public void doView(RenderRequest request, RenderResponse response)
104: throws PortletException, IOException {
105: String selectedSite = (String) PortletMessaging.receive(
106: request, "site", "selectedUrl");
107: if (selectedSite != null) {
108: Context context = this .getContext(request);
109: context.put("currentUrl", selectedSite);
110: String selectedName = (String) PortletMessaging.receive(
111: request, "site", "selectedName");
112: context.put("currentName", selectedName);
113:
114: String realm = (String) PortletMessaging.receive(request,
115: "site", "realm");
116: context.put("currentRealm", realm);
117: String userField = (String) PortletMessaging.receive(
118: request, "site", "idField");
119: context.put("currentFFID", userField);
120: String pwdFiled = (String) PortletMessaging.receive(
121: request, "site", "pwdField");
122: context.put("currentFFPWD", pwdFiled);
123:
124: }
125: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
126: request, "SSOBrowser", "status");
127: if (msg != null) {
128: this .getContext(request).put("statusMsg", msg);
129: }
130:
131: super .doView(request, response);
132: }
133:
134: public void processAction(ActionRequest request,
135: ActionResponse response) throws PortletException,
136: IOException {
137: if (request.getPortletMode() == PortletMode.VIEW) {
138: String selectedSite = request.getParameter("ssoSite");
139: if (selectedSite != null) {
140: SSOSite site = sso.getSite(selectedSite);
141: if (site != null) {
142: PortletMessaging.publish(request, "site",
143: "selectedUrl", selectedSite);
144: PortletMessaging.publish(request, "site",
145: "selectedName", site.getName());
146: PortletMessaging.publish(request, "site", "change",
147: selectedSite);
148: PortletMessaging.publish(request, "site", "realm",
149: site.getRealm());
150: PortletMessaging.publish(request, "site",
151: "idField", site.getFormUserField());
152: PortletMessaging.publish(request, "site",
153: "pwdField", site.getFormPwdField());
154: }
155: }
156: String refresh = request.getParameter("sso.refresh");
157: String save = request.getParameter("sso.save");
158: String neue = request.getParameter("sso.new");
159: String delete = request.getParameter("ssoDelete");
160:
161: if (refresh != null) {
162: this .clearBrowserIterator(request);
163: } else if (neue != null) {
164: PortletMessaging.cancel(request, "site", "selected");
165: PortletMessaging.cancel(request, "site", "selectedUrl");
166: PortletMessaging.cancel(request, "site", "realm");
167: PortletMessaging.cancel(request, "site", "idField");
168: PortletMessaging.cancel(request, "site", "pwdField");
169: } else if (delete != null && (!(isEmpty(delete)))) {
170: try {
171: SSOSite site = null;
172: site = sso.getSite(delete);
173: if (site != null) {
174: sso.removeSite(site);
175: this .clearBrowserIterator(request);
176: PortletMessaging.cancel(request, "site",
177: "selected");
178: PortletMessaging.cancel(request, "site",
179: "selectedUrl");
180: PortletMessaging.cancel(request, "site",
181: "realm");
182: PortletMessaging.cancel(request, "site",
183: "idField");
184: PortletMessaging.cancel(request, "site",
185: "pwdField");
186: }
187: } catch (SSOException e) {
188: publishStatusMessage(request, "SSOBrowser",
189: "status", e, "Could not remove site");
190: }
191: } else if (save != null) {
192: String siteName = request.getParameter("site.name");
193: String siteUrl = request.getParameter("site.url");
194:
195: String siteRealm = request.getParameter("site.realm");
196: String siteFormID = request
197: .getParameter("site.form_field_ID");
198: String siteFormPWD = request
199: .getParameter("site.form_field_PWD");
200:
201: if (!(isEmpty(siteName) || isEmpty(siteUrl))) {
202: try {
203: SSOSite site = null;
204: String old = (String) PortletMessaging.receive(
205: request, "site", "selectedUrl");
206: if (old != null) {
207: site = sso.getSite(old);
208: } else {
209: site = sso.getSite(siteUrl);
210: }
211: if (site != null) {
212: site.setName(siteName);
213: site.setSiteURL(siteUrl);
214: site.setRealm(siteRealm);
215: if (siteFormID != null
216: && siteFormID.length() > 0
217: && siteFormPWD != null
218: && siteFormPWD.length() > 0) {
219: // Form authentication
220: site.setFormAuthentication(true);
221: site.setFormUserField(siteFormID);
222: site.setFormPwdField(siteFormPWD);
223: } else {
224: //Challenge response authentication
225: site
226: .setChallengeResponseAuthentication(true);
227: }
228:
229: sso.updateSite(site);
230: this .clearBrowserIterator(request);
231: PortletMessaging.publish(request, "site",
232: "selectedName", siteName);
233: PortletMessaging.publish(request, "site",
234: "selectedUrl", siteUrl);
235: PortletMessaging.publish(request, "site",
236: "realm", siteRealm);
237: PortletMessaging.publish(request, "site",
238: "idField", siteFormID);
239: PortletMessaging.publish(request, "site",
240: "pwdField", siteFormPWD);
241:
242: } else {
243: if (siteFormID != null
244: && siteFormID.length() > 0
245: && siteFormPWD != null
246: && siteFormPWD.length() > 0) {
247: sso.addSiteFormAuthenticated(siteName,
248: siteUrl, siteRealm, siteFormID,
249: siteFormPWD);
250:
251: } else {
252: sso.addSiteChallengeResponse(siteName,
253: siteUrl, siteRealm);
254: }
255: this .clearBrowserIterator(request);
256: }
257: } catch (SSOException e) {
258: publishStatusMessage(request, "SSOBrowser",
259: "status", e, "Could not store site");
260: }
261: }
262: }
263: }
264: super .processAction(request, response);
265:
266: }
267:
268: private boolean isEmpty(String s) {
269: if (s == null)
270: return true;
271:
272: if (s.trim().equals(""))
273: return true;
274:
275: return false;
276: }
277:
278: }
|