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: */package com.sun.portal.wsrp.consumer.admin.mbeans;
013:
014: import java.net.URL;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.util.Properties;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import com.sun.portal.log.common.PortalLogger;
025: import com.sun.portal.util.ResourceLoader;
026:
027: import com.sun.portal.wsrp.common.IdentityPropagationConstants;
028: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
029: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
030: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
031: import com.sun.portal.wsrp.WSRPException;
032:
033: public class WSSSOHelper implements IdentityPropagationConstants {
034:
035: private static Logger debugLogger = PortalLogger
036: .getLogger(WSSSOHelper.class);
037:
038: public static void createWSSSOConfiguration(URL wsURL,
039: String idpType, String orgDN, ProducerEntityManager pem,
040: String domainId, String portalId)
041: throws WSRPConsumerException {
042: // Create the SSO Configuration for the Consumer
043: if (idpType != NO_IDENTITY_PROPAGATION
044: && idpType != SSOTOKEN_IDENTITY_PROPAGATION) {
045:
046: WSSSOConfigurator usc = new WSSSOConfigurator(domainId,
047: portalId);
048: Iterator endPoints = pem.getEndpoints(wsURL).iterator();
049: StringBuffer buf = new StringBuffer();
050: while (endPoints.hasNext()) {
051: buf.append(endPoints.next().toString()).append("^");
052: }
053: Map p = new HashMap();
054: p.put(SSOADAPTER_WS_WSDL_URL, wsURL);
055:
056: p.put(SSOADAPTER_WS_ENDPOINT_URLS, buf.toString());
057: //Add default username and password here when UI in psconsole
058: // provides this option
059: //p.put(USERNAME,"www.sun.com");
060: //p.put(PASSWORD,"www.sun.com");
061: p.put(SSOADAPTER_WS_IDENTITY_TYPE, idpType);
062: Properties locales = null;
063: try {
064: locales = getWSSSOProperties(portalId);
065: } catch (WSRPException we) {
066: debugLogger.log(Level.SEVERE, "", we);
067: }
068: p.put(SSOADAPTER_WS_NAME, getWSRPConsumerName(locales));
069: p.put(SSOADAPTER_WS_DESC, getWSRPConsumerDesc(locales));
070: usc.createUserConfiguration(wsURL, orgDN, p);
071: }
072: }
073:
074: public static void deleteWSSSOConfiguration(URL wsURL,
075: String idpType, String orgDN, ProducerEntityManager pem,
076: String domainId, String portalId)
077: throws WSRPConsumerException {
078: // Delete the SSO Configuration for the Consumer
079: if (idpType != NO_IDENTITY_PROPAGATION
080: && idpType != SSOTOKEN_IDENTITY_PROPAGATION) {
081: Iterator entityIds = pem.getProducerEntityIds().iterator();
082: boolean duplicateExists = false;
083: String producerId = null;
084: ProducerEntity entity = null;
085: String tempidpType = null;
086: //Check if there are other consumers that use this SSOAdapter
087: while (entityIds.hasNext()) {
088: producerId = entityIds.next().toString();
089: entity = pem.getProducerEntity(producerId);
090: //Check the URL first
091: if (entity.getURL().equals(wsURL)) {
092: tempidpType = entity.getIdentityPropagationType();
093: //Check the Identity Propagationtype for that Consumer
094: if (tempidpType != NO_IDENTITY_PROPAGATION
095: && tempidpType != SSOTOKEN_IDENTITY_PROPAGATION) {
096: duplicateExists = true;
097: }
098: }
099: }
100: if (!duplicateExists) {
101: WSSSOConfigurator usc = new WSSSOConfigurator(domainId,
102: portalId);
103: usc.deleteUserConfiguration(wsURL, orgDN);
104: }
105: }
106: }
107:
108: public static void modifyWSSOConfiguration(String newIdType,
109: String orgDN, ProducerEntityManager pem, String producerId,
110: String domainId, String portalId)
111: throws WSRPConsumerException {
112: ProducerEntity pe = pem.getProducerEntity(producerId);
113: String oldIdType = pe.getIdentityPropagationType();
114: URL wsURL = pe.getURL();
115:
116: if (newIdType != NO_IDENTITY_PROPAGATION
117: && newIdType != SSOTOKEN_IDENTITY_PROPAGATION) {
118: //create new WSSOConfiguration
119: createWSSSOConfiguration(wsURL, newIdType, orgDN, pem,
120: domainId, portalId);
121: } else {
122: // delete WSSOConfiguration
123: deleteWSSSOConfiguration(wsURL, oldIdType, orgDN, pem,
124: domainId, portalId);
125: }
126: }
127:
128: private static String getWSRPConsumerName(Properties props) {
129: if (props == null || props.size() == 0) {
130: return DEFAULT_WSRP_NAME;
131: }
132: Object temp = props.getProperty(WSSSO_NAME_PROPERTY);
133: if (temp != null) {
134: return temp.toString();
135: }
136: return null;
137:
138: }
139:
140: private static String getWSRPConsumerDesc(Properties props) {
141: if (props == null || props.size() == 0) {
142: return DEFAULT_WSRP_DESC;
143: }
144: Object temp = props.getProperty(WSSSO_DESC_PROPERTY);
145: if (temp != null) {
146: return temp.toString();
147: }
148: return null;
149: }
150:
151: private static Properties getWSSSOProperties(String portalId)
152: throws WSRPException {
153: ResourceLoader resourceLoader = null;
154: Properties properties = null;
155: if (portalId == null) {
156: resourceLoader = ResourceLoader.getInstance(System
157: .getProperties());
158: } else {
159: resourceLoader = ResourceLoader.getInstance(portalId);
160: }
161: try {
162: properties = resourceLoader.getProperties(WSSSO_PROPERTIES);
163:
164: } catch (FileNotFoundException fnfe) {
165: throw new WSRPException("WSSSO Config not found: ", fnfe);
166: } catch (IOException ioe) {
167: throw new WSRPException("Cannot WSSSO config file: ", ioe);
168: }
169: return properties;
170: }
171: }
|