001: /**
002: * Copyright 2003 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.cli;
013:
014: import java.util.Map;
015: import java.util.HashMap;
016: import java.util.Set;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019:
020: import java.net.URL;
021: import java.net.MalformedURLException;
022:
023: import com.sun.portal.wsrp.common.stubs.ServiceDescription;
024: import com.sun.portal.wsrp.common.stubs.PortletDescription;
025: import com.sun.portal.wsrp.common.stubs.ItemDescription;
026: import com.sun.portal.wsrp.common.stubs.RegistrationContext;
027: import com.sun.portal.wsrp.common.stubs.RegistrationData;
028:
029: import com.sun.portal.wsrp.common.WSRPFactory;
030: import com.sun.portal.wsrp.WSRPException;
031:
032: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
033:
034: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
035: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
036: import com.sun.portal.wsrp.consumer.producermanager.InbandRegistrationNotSupportedException;
037: import com.sun.portal.log.common.PortalLogger;
038:
039: class PEAAdd {
040:
041: private static Logger logger = PortalLogger.getLogger(PEAAdd.class);
042:
043: public String process(ProducerEntityManager pem, String dn,
044: String purl, String pname, String identityPropagationType,
045: String regDataXML, String regHandle, boolean verbose,
046: boolean dryrun) throws PEAException {
047:
048: URL url = null;
049: try {
050: url = new URL(purl);
051: } catch (MalformedURLException mue) {
052: Object[] tokens = { purl };
053: throw new PEAException("errorInvalidURL", mue, tokens);
054: }
055:
056: //
057: // if regHandle was specified, assume out-of-band operation
058: //
059: boolean outofband = false;
060: if (regHandle != null && regHandle.length() > 0) {
061: outofband = true;
062: }
063:
064: //
065: // sanity check: reg data only needed for inband operation
066: //
067: if (outofband) {
068: if (regDataXML != null && regDataXML.length() > 0) {
069: Object[] tokens = { dn, purl };
070: throw new PEAException("errorNoRegData", tokens);
071: }
072: }
073:
074: //
075: // if producer name is undefined, use url
076: //
077: if (pname == null) {
078: pname = purl.toString();
079: }
080:
081: //
082: // restrict "|" from pname
083: //
084: if (pname.indexOf('|') > -1) {
085: Object[] tokens = { pname };
086: throw new PEAException("errorInvalidPName", tokens);
087: }
088:
089: if (verbose) {
090: if (!outofband) {
091: if (logger.isLoggable(Level.FINEST)) {
092: Object[] tokens = { purl, pname };
093: logger
094: .log(Level.FINEST, "PSWS_CSPWCCL0001",
095: tokens);
096: }
097: } else {
098: if (logger.isLoggable(Level.FINEST)) {
099: Object[] tokens = { purl, pname, regHandle };
100: logger
101: .log(Level.FINEST, "PSWS_CSPWCCL0002",
102: tokens);
103: }
104: }
105: }
106:
107: String peId = null;
108: if (!outofband) {
109: peId = processInband(pem, dn, url, pname,
110: identityPropagationType, regDataXML, verbose,
111: dryrun);
112: } else {
113: peId = processOutofband(pem, dn, url, pname,
114: identityPropagationType, regHandle, verbose, dryrun);
115: }
116:
117: if (!dryrun) {
118: //
119: // set user mappings
120: //
121: setUserMappings(pem, dn, peId, verbose);
122: }
123:
124: return peId;
125: }
126:
127: private String processInband(ProducerEntityManager pem, String dn,
128: URL url, String pname, String identityPropagationType,
129: String regDataXML, boolean verbose, boolean dryrun)
130: throws PEAException {
131:
132: //
133: // get service description first to determine if registration
134: // is required or not
135: //
136: ServiceDescription sd = null;
137: try {
138: sd = pem.getServiceDescription(url);
139: } catch (WSRPConsumerException wce) {
140: Object[] tokens = { url };
141: throw new PEAException("errorGetSD", wce, tokens);
142: }
143:
144: RegistrationData regData = null;
145: if (sd.isRequiresRegistration()) {
146: if (regDataXML != null && regDataXML.length() > 0) {
147: //
148: // parse reg data xml
149: //
150: try {
151: WSRPFactory factory = WSRPFactory.getInstance();
152: regData = factory.getRegistrationData(regDataXML);
153: } catch (WSRPException we) {
154: throw new PEAException("errorParseXML", we);
155: }
156: } else {
157: //
158: // if registration is required and regData is not supplied,
159: // use default reg data.
160: //
161: if (logger.isLoggable(Level.FINE))
162: logger.log(Level.FINE, "PSWS_CSPWCCL0003");
163: try {
164: regData = pem.getDefaultRegistrationData();
165: } catch (WSRPConsumerException wce) {
166: Object[] tokens = { dn };
167: throw new PEAException("errorGetDefReg", wce,
168: tokens);
169: }
170: }
171: }
172:
173: String peId = null;
174: if (!dryrun) {
175: try {
176: //
177: // user profile mapping cannot be established
178: // prior to registration, thus passing null's
179: //
180: peId = pem.addProducerEntity(pname, url,
181: identityPropagationType, regData, null, null,
182: null);
183: } catch (InbandRegistrationNotSupportedException irnse) {
184: Object[] tokens = { dn, url };
185: throw new PEAException("errorNoInband", irnse, tokens);
186: } catch (WSRPConsumerException wce) {
187: Object[] tokens = { dn, url };
188: throw new PEAException("errorAddPE", wce, tokens);
189: }
190: }
191:
192: return peId;
193: }
194:
195: private String processOutofband(ProducerEntityManager pem,
196: String dn, URL url, String pname,
197: String identityPropagationType, String regHandle,
198: boolean verbose, boolean dryrun) throws PEAException {
199:
200: String peId = null;
201: if (!dryrun) {
202: try {
203: //
204: // user profile mapping cannot be established
205: // prior to registration, thus passing null's
206: //
207: peId = pem.addProducerEntity(pname, url,
208: identityPropagationType, regHandle, null, null,
209: null);
210: } catch (WSRPConsumerException wce) {
211: Object[] tokens = { dn, url };
212: throw new PEAException("errorAddPE", wce, tokens);
213: }
214: }
215:
216: return peId;
217: }
218:
219: private void setUserMappings(ProducerEntityManager pem, String dn,
220: String peId, boolean verbose) throws PEAException {
221:
222: ProducerEntity pe = null;
223: try {
224: pe = pem.getProducerEntity(peId);
225: } catch (WSRPConsumerException wce) {
226: Object[] tokens = { dn, peId };
227: throw new PEAException("errorGetPE", wce, tokens);
228: }
229:
230: if (pe == null) {
231: Object[] tokens = { peId };
232: throw new PEAException("errorInvalidPID", tokens);
233: }
234: ServiceDescription sd = pe.getServiceDescription();
235:
236: //
237: // get user categories supported by the portlet
238: //
239: ItemDescription[] ucs = sd.getUserCategoryDescriptions();
240: if (ucs != null && ucs.length > 0) {
241:
242: if (verbose) {
243: Object tokens[] = { peId };
244: PEAUtil.warning("warnUpdateUC", tokens);
245: }
246:
247: /*
248: if (verbose) {
249: Object[] tokens = { peId };
250: PEAUtil.debug("dbgSetUCMap", tokens);
251: }
252:
253: Map ucMap = null;
254: try {
255: pem.setUserCategoryMapping(peId, ucMap);
256: } catch (WSRPConsumerException wce) {
257: Object tokens[] = { peId, ucMap };
258: throw new PEAException("errorSetUC", wce, tokens);
259: }
260: */
261: }
262:
263: //
264: // get standard user profile mapping
265: //
266: Map stdMap = null;
267: try {
268: stdMap = pem.getStandardUserProfileMapping();
269: } catch (WSRPConsumerException wce) {
270: throw new PEAException("errorGetStdUserProfile", wce);
271: }
272: if (stdMap == null) {
273: throw new PEAException("errorGetStdUserProfile");
274: }
275: Set stdKeys = stdMap.keySet();
276:
277: //
278: // by default, set allowed user profile mapping same as
279: // the standard user profiles mapping
280: //
281: if (logger.isLoggable(Level.FINE)) {
282: Object[] tokens = { peId };
283: logger.log(Level.FINE, "PSWS_CSPWCCL0004", tokens);
284: }
285:
286: try {
287: pem.setAllowedUserProfileMapping(peId, stdMap);
288: } catch (WSRPConsumerException wce) {
289: Object tokens[] = { peId, stdMap };
290: throw new PEAException("errorSetAUP", wce, tokens);
291: }
292:
293: //
294: // custom user profile mapping
295: //
296: ItemDescription[] cups = sd
297: .getCustomUserProfileItemDescriptions();
298: if (cups != null && cups.length > 0) {
299:
300: if (verbose) {
301: Object[] tokens = { peId };
302: PEAUtil.warning("warnUpdateCUP", tokens);
303: }
304:
305: /*
306: if (verbose) {
307: Object[] tokens = { peId };
308: PEAUtil.debug("dbgSetCUPMap", tokens);
309: }
310:
311: Map cupMap = null;
312: try {
313: pem.setCustomUserProfileMapping(peId, cupMap);
314: } catch (WSRPConsumerException wce) {
315: Object tokens[] = { peId, stdMap };
316: throw new PEAException("errorSetCUP", wce, tokens);
317: }
318: */
319: }
320:
321: }
322: }
|