001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.wsrp.wssso.common;
015:
016: import java.net.MalformedURLException;
017: import java.net.URL;
018: import java.util.Iterator;
019: import java.util.Set;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022: import java.util.List;
023: import java.net.URLEncoder;
024: import java.util.Properties;
025: import java.io.UnsupportedEncodingException;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028:
029: import com.iplanet.am.sdk.AMException;
030: import com.iplanet.am.sdk.AMStoreConnection;
031: import com.iplanet.am.sdk.AMUser;
032: import com.iplanet.sso.SSOException;
033: import com.iplanet.sso.SSOToken;
034:
035: import com.sun.ssoadapter.SSOAdapter;
036: import com.sun.ssoadapter.SSOAdapterFactory;
037: import com.sun.ssoadapter.SSOAdapterSession;
038: import com.sun.ssoadapter.SSOAdapterException;
039:
040: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
041: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
042: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
043: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManagerFactory;
044:
045: import com.sun.portal.log.common.PortalLogger;
046: import com.sun.portal.util.ResourceLoader;
047: import com.sun.portal.util.SSOUtil;
048:
049: public class WSSSOMigrator implements WSSSOConstants {
050:
051: private String orgDN = null;
052: private SSOAdapterFactory adapterFactory = null;
053: private SSOAdapterSession session = null;
054:
055: private static Logger debugLogger = PortalLogger
056: .getLogger(WSSSOMigrator.class);
057: private static String portalId = System
058: .getProperty("com.sun.portal.portal.id");
059: private static Properties locales = getWSSSOProperties();
060:
061: public WSSSOMigrator(SSOToken userToken) {
062: try {
063: orgDN = getUserOrganization(userToken);
064: } catch (SSOException ssoe) {
065: debugLogger.log(Level.SEVERE, "", ssoe);
066: } catch (AMException ame) {
067: debugLogger.log(Level.SEVERE, "", ame);
068: }
069: adapterFactory = SSOAdapterFactory.getInstance();
070: session = new SSOAdapterSession(userToken);
071: }
072:
073: public String getIdentityPropagationType(SSOAdapter adapter,
074: String markupURL) {
075:
076: if (orgDN == null) {
077: return null;
078: }
079: try {
080: ProducerEntityManager pem = ProducerEntityManagerFactory
081: .getInstance().getProducerEntityManager(
082: getAdminSSOToken(), portalId, orgDN);
083: URL inputURL = new URL(markupURL);
084: URL comparisonURL = null;
085: Set producerEntityIds = pem.getProducerEntityIds();
086: if ((producerEntityIds != null)
087: && (producerEntityIds.size() > 0)) {
088: for (Iterator i = producerEntityIds.iterator(); i
089: .hasNext();) {
090: String producerEntityId = (String) i.next();
091: ProducerEntity pe = pem
092: .getProducerEntity(producerEntityId);
093: comparisonURL = new URL(pe.getMarkupEndpoint()
094: .toString());
095: if (inputURL.equals(comparisonURL)) {
096: migrateUserConfiguration(adapter, pem, pe);
097: return pe.getIdentityPropagationType();
098: }
099: }
100: }
101: } catch (WSRPConsumerException we) {
102: debugLogger.log(Level.SEVERE, "", we);
103: } catch (MalformedURLException me) {
104: debugLogger.log(Level.SEVERE, "", me);
105: }
106: return null;
107: }
108:
109: private void migrateUserConfiguration(SSOAdapter adapter,
110: ProducerEntityManager pem, ProducerEntity pe) {
111: URL wsURL = pe.getURL();
112: List endPoints = null;
113:
114: try {
115: endPoints = pem.getEndpoints(wsURL);
116: } catch (WSRPConsumerException wce) {
117: //Abort migration Producer may be down or not reacheable
118: debugLogger.log(Level.SEVERE, "", wce);
119: return;
120: }
121:
122: String idType = pe.getIdentityPropagationType();
123: String endPointURLs = processEndPoints(endPoints);
124: //getUsername and Password from Old SSOAdapter
125: String userName = null;
126: String password = null;
127:
128: Properties props = adapter.getProperties();
129: Object temp = props.get(WEBSERVICE_USERNAME);
130: if (temp != null) {
131: userName = temp.toString();
132: }
133: temp = props.get(WEBSERVICE_PASSWORD);
134: if (temp != null) {
135: password = temp.toString();
136: }
137:
138: //create a new SSOAdapter
139: SSOAdapter migratedAdapter = getDefaultSSOAdapter();
140: if (migratedAdapter == null) {
141: return;
142: }
143: String newName = getNewSSOAdapterName(wsURL);
144: Properties migratedProps = migratedAdapter.getProperties();
145: migratedProps.put(WEBSERVICE_WSDL_URL, wsURL.toString());
146: migratedProps.put(WEBSERVICE_IDENTITY_TYPE, idType);
147: migratedProps.put(WEBSERVICE_NAME, DEFAULT_WSRP_NAME);
148: migratedProps.put(WEBSERVICE_ENDPOINT_URLS, endPointURLs);
149: migratedProps.put(WEBSERVICE_DESC, DEFAULT_WSRP_DESC);
150: if (userName != null) {
151: migratedProps.put(WEBSERVICE_USERNAME, userName);
152: }
153: if (password != null && password.trim().length() != 0) {
154: migratedProps.put(WEBSERVICE_PASSWORD, password);
155: }
156: migratedProps.put(CHANNEL_NAME, newName);
157: try {
158: SSOAdapter newssoAdapter = (SSOAdapter) adapterFactory
159: .setSSOAdapter(migratedAdapter, migratedProps,
160: session);
161: } catch (SSOAdapterException sae) {
162: debugLogger.log(Level.SEVERE, "", sae);
163: //Abort deletion as new SSOAdapter creation failed
164: return;
165: }
166: //delete the Old SSOAdapter
167: deleteSSOAdapter(adapter);
168: }
169:
170: private String processEndPoints(List endPoints) {
171: Iterator it = endPoints.iterator();
172: StringBuffer buf = new StringBuffer();
173: while (it.hasNext()) {
174: buf.append(it.next().toString()).append("^");
175: }
176: return buf.toString();
177: }
178:
179: private String getNewSSOAdapterName(URL wsURL) {
180: int port = wsURL.getPort();
181: if (port == -1) {
182: port = wsURL.getDefaultPort();
183: }
184: String newURL = wsURL.getProtocol() + "://" + wsURL.getHost()
185: + ":" + port + wsURL.getPath();
186: try {
187: String encoded = URLEncoder.encode(newURL, "UTF-8");
188: return portalId + SEPARATOR + encoded;
189: } catch (UnsupportedEncodingException e) {
190: debugLogger.log(Level.SEVERE, "", e);
191: }
192:
193: return null;
194: }
195:
196: private void deleteSSOAdapter(SSOAdapter adapter) {
197: try {
198: adapterFactory.removeConfiguration(adapter.getName(),
199: session);
200: } catch (SSOAdapterException sae) {
201: debugLogger.log(Level.SEVERE, "", sae);
202: }
203: }
204:
205: private SSOAdapter getDefaultSSOAdapter() {
206: SSOAdapter adapter = null;
207: try {
208: adapter = adapterFactory
209: .getSSOAdapter(CONFIG_NAME, session);
210: return adapter;
211: } catch (SSOAdapterException e) {
212: debugLogger.log(Level.SEVERE, "", e);
213: }
214: return null;
215: }
216:
217: private SSOToken getAdminSSOToken() {
218: return SSOUtil.getAdminSSOToken();
219: }
220:
221: private String getUserOrganization(SSOToken token)
222: throws SSOException, AMException {
223: String userDN = token.getPrincipal().getName();
224: AMStoreConnection amsc = new AMStoreConnection(
225: getAdminSSOToken());
226: AMUser user = amsc.getUser(userDN);
227: return user.getOrganizationDN();
228: }
229:
230: private String getWSRPConsumerName() {
231: if (locales == null || locales.size() == 0) {
232: return DEFAULT_WSRP_NAME;
233: }
234: Object temp = locales.getProperty(WSSSO_NAME_PROPERTY);
235: if (temp != null) {
236: return temp.toString();
237: }
238: return null;
239:
240: }
241:
242: private String getWSRPConsumerDesc() {
243: if (locales == null || locales.size() == 0) {
244: return DEFAULT_WSRP_DESC;
245: }
246: Object temp = locales.getProperty(WSSSO_DESC_PROPERTY);
247: if (temp != null) {
248: return temp.toString();
249: }
250: return null;
251: }
252:
253: private static Properties getWSSSOProperties() {
254: Properties props = null;
255:
256: ResourceLoader resourceLoader = ResourceLoader
257: .getInstance(System.getProperties());
258:
259: try {
260: props = resourceLoader.getProperties(WSSSO_PROPERTIES);
261:
262: } catch (FileNotFoundException fnfe) {
263: debugLogger.log(Level.SEVERE, "", fnfe);
264: } catch (IOException ioe) {
265: debugLogger.log(Level.SEVERE, "", ioe);
266: }
267: return props;
268: }
269: }
|