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.producer.test;
013:
014: import java.util.Set;
015: import java.util.Iterator;
016: import java.util.HashSet;
017: import java.util.StringTokenizer;
018: import java.util.Locale;
019: import java.util.logging.Level;
020: import java.util.logging.Logger;
021:
022: import java.io.PrintWriter;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletContext;
027:
028: import javax.servlet.http.HttpServlet;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import com.iplanet.sso.SSOToken;
033:
034: import com.sun.identity.sm.ServiceListener;
035:
036: import com.iplanet.am.util.Debug;
037:
038: import com.sun.portal.providers.context.ProviderContext;
039:
040: import com.sun.portal.desktop.dp.DPRoot;
041:
042: import com.sun.portal.wsrp.producer.Producer;
043: import com.sun.portal.wsrp.producer.ISConnection;
044: import com.sun.portal.wsrp.producer.ISConstants;
045: import com.sun.portal.wsrp.producer.ProducerRegistryManager;
046: import com.sun.portal.wsrp.producer.DPConnection;
047:
048: import com.sun.portal.wsrp.producer.impl.ProducerImpl;
049:
050: import com.sun.portal.wsrp.producer.filter.ProducerThreadLocalizer;
051:
052: import com.sun.portal.wsrp.producer.servicedescription.ServiceDescriptionManager;
053:
054: import com.sun.portal.wsrp.producer.registration.RegistrationManager;
055: import com.sun.portal.wsrp.producer.registration.RegistrationRecord;
056:
057: import com.sun.portal.wsrp.producer.UserManager;
058:
059: import com.sun.portal.wsrp.producer.registration.validator.RegistrationValidator;
060:
061: import com.sun.portal.wsrp.common.stubs.ServiceDescription;
062: import com.sun.portal.wsrp.common.stubs.PropertyDescription;
063: import com.sun.portal.wsrp.common.stubs.LocalizedString;
064: import com.sun.portal.wsrp.common.stubs.RegistrationContext;
065: import com.sun.portal.wsrp.common.stubs.RegistrationData;
066: import com.sun.portal.wsrp.common.stubs.ModifyRegistration;
067: import com.sun.portal.wsrp.common.stubs.Property;
068:
069: import com.sun.portal.wsrp.common.WSRPFactory;
070: import com.sun.portal.log.common.PortalLogger;
071:
072: public class TestServlet extends HttpServlet implements ISConstants,
073: ServiceListener {
074: private static Logger logger = PortalLogger
075: .getLogger(TestServlet.class);
076:
077: protected void doGet(HttpServletRequest req, HttpServletResponse res)
078: throws ServletException, java.io.IOException {
079: res.setContentType("text/plain");
080: PrintWriter out = res.getWriter();
081:
082: try {
083: SSOToken adminToken = ISConnection.getAdminToken();
084: ProducerRegistryManager registry = ProducerRegistryManager
085: .getRegistryManager();
086: ServletContext ctx = ProducerThreadLocalizer.getContext();
087:
088: String test = req.getParameter("test");
089: if (test == null) {
090: out.println("please specify test to run");
091: } else if (test.equals("tl.get")) {
092: ServletRequest sreq = ProducerThreadLocalizer
093: .getRequest();
094: out.println("sreq=" + sreq);
095: } else if (test.equals("registry.getkeys")) {
096: //
097: // producer registry tests
098: //
099: Set keys = registry.getKeys();
100: out.println(keys);
101: } else if (test.equals("registry.remove")) {
102: String key = req.getParameter("registry.key");
103: registry.remove(key);
104: out.println("producer registry producer key=" + key
105: + " removed");
106: } else if (test.equals("registry.add")) {
107: String key = req.getParameter("registry.key");
108: String org = req.getParameter("registry.org");
109: String instance = req.getParameter("registry.instance");
110: registry.add(key, org, instance);
111: } else if (test.equals("registry.getxml")) {
112: String s = registry.getXML();
113: out.println(s);
114: } else if (test.equals("usermanager.getprovidercontext")) {
115: //
116: // user manager tests
117: //
118: String userContextKey = req
119: .getParameter("usermanager.usercontextkey");
120: String producerKey = req
121: .getParameter("usermanager.producerkey");
122: String consumerName = req
123: .getParameter("usermanager.consumername");
124: String expiresString = req
125: .getParameter("usermanager.expires");
126: if (expiresString == null) {
127: expiresString = "300";
128: }
129: int expires = Integer.parseInt(expiresString);
130: UserManager um = new UserManager(getServletConfig()
131: .getServletContext(), req, adminToken,
132: producerKey, consumerName);
133: ProviderContext pc = um.getProviderContext(
134: userContextKey, expires);
135: if (pc == null) {
136: out
137: .println("could not get provider context for userContextKey="
138: + userContextKey
139: + ", producerKey="
140: + producerKey
141: + ", consumerName="
142: + consumerName);
143: } else {
144: out
145: .println("got provider context for userContextKey="
146: + userContextKey
147: + ", producerKey="
148: + producerKey
149: + ", consumerName="
150: + consumerName);
151: out.println("userID=" + pc.getUserID());
152: out.println("sessionID=" + pc.getSessionID());
153: }
154:
155: String key = req.getParameter("usermanager.cpkey");
156: String val = req.getParameter("usermanager.cpval");
157:
158: if (key != null) {
159: if (val == null) {
160: String v = pc.getClientProperty(key);
161: out.println("GOT client property key=" + key
162: + ", value=" + v);
163: } else {
164: pc.setClientProperty(key, val);
165: out.println("SET client property key=" + key
166: + ", value=" + val);
167: }
168: }
169:
170: um.store(userContextKey);
171: } else if (test.equals("sd.get")) {
172: //
173: // service description manager tests
174: //
175: String key = req.getParameter("sd.key");
176: String m = req.getParameter("sd.min");
177: boolean min = new Boolean(m).booleanValue();
178:
179: Producer producer = new ProducerImpl(req, ctx,
180: adminToken, key);
181: ServiceDescriptionManager sdm = producer
182: .getServiceDescriptionManager();
183: ServiceDescription sd = sdm.getServiceDescription(null,
184: min);
185: out.println(WSRPFactory.getInstance()
186: .getServiceDescriptionXML(sd, true));
187: } else if (test.equals("sd.getopnames")) {
188: String key = req.getParameter("sd.key");
189: Producer producer = new ProducerImpl(req, ctx,
190: adminToken, key);
191: ServiceDescriptionManager sdm = producer
192: .getServiceDescriptionManager();
193:
194: Set opNames = sdm.getOfferedPortletNames();
195: out.println("opNames=" + opNames);
196: } else if (test.equals("sd.setopnames")) {
197: String key = req.getParameter("sd.key");
198: String names = req.getParameter("sd.names");
199: Set opNames = parseURLParameterList(names);
200: out.println("opNames=" + opNames);
201:
202: Producer producer = new ProducerImpl(req, ctx,
203: adminToken, key);
204: ServiceDescriptionManager sdm = producer
205: .getServiceDescriptionManager();
206: sdm.setOfferedPortletNames(opNames);
207: } else if (test.equals("sd.addrpd")) {
208: String key = req.getParameter("sd.key");
209: String name = req.getParameter("sd.rpdname");
210: String lang = req.getParameter("sd.lang");
211: if (lang == null) {
212: lang = Locale.getDefault().toString().replace('_',
213: '-');
214: }
215:
216: javax.xml.namespace.QName type = new javax.xml.namespace.QName(
217: req.getParameter("sd.rpdtype"));
218: LocalizedString label = new LocalizedString(lang, name,
219: "aLabel");
220: LocalizedString hint = new LocalizedString(lang, name,
221: "aHint");
222:
223: PropertyDescription pd = new PropertyDescription(name,
224: type, label, hint, null);
225:
226: Producer producer = new ProducerImpl(req, ctx,
227: adminToken, key);
228: ServiceDescriptionManager sdm = producer
229: .getServiceDescriptionManager();
230: sdm.addRegistrationPropertyDescription(pd);
231: } else if (test.equals("dp.get")) {
232: //
233: // display profile tests
234: //
235: String org = req.getParameter("dp.org");
236: DPConnection dpc = new DPConnection(req);
237: DPRoot dpr = dpc.getDPRoot(org);
238: out.println(dpr.toDebugString());
239: } else if (test.equals("producer.getpch")) {
240: //
241: // producer tests
242: //
243: String key = req.getParameter("producer.key");
244: Producer producer = new ProducerImpl(req, ctx,
245: adminToken, key);
246: Set portletChannelNames = producer
247: .getPortletChannelNames();
248: out.println("portletChannelNames="
249: + portletChannelNames);
250: } else if (test.equals("reg.add")) {
251: //
252: // registration manager tests
253: //
254: String key = req.getParameter("reg.key");
255: String name = req.getParameter("reg.name");
256: String agent = req.getParameter("reg.agent");
257:
258: Producer producer = new ProducerImpl(req, ctx,
259: adminToken, key);
260:
261: RegistrationManager rm = producer
262: .getRegistrationManager();
263:
264: RegistrationData rd = getRegistrationData(name, agent);
265: String rdXML = WSRPFactory.getInstance()
266: .getRegistrationDataXML(rd, true);
267: out.println("rd=\n" + rdXML);
268:
269: RegistrationContext rc = rm.register(rd);
270:
271: String rcXML = WSRPFactory.getInstance()
272: .getRegistrationContextXML(rc, true);
273: out.println("rc=\n" + rcXML);
274: } else if (test.equals("reg.delete")) {
275: String key = req.getParameter("reg.key");
276: String handle = req.getParameter("reg.handle");
277:
278: RegistrationContext rc = new RegistrationContext(
279: handle, null, null);
280: Producer producer = new ProducerImpl(req, ctx,
281: adminToken, key);
282: RegistrationManager rm = producer
283: .getRegistrationManager();
284: rm.deregister(rc);
285:
286: out.println("handle=" + handle + ", deregistered");
287: } else if (test.equals("reg.modify")) {
288: String key = req.getParameter("reg.key");
289: String handle = req.getParameter("reg.handle");
290: String name = req.getParameter("reg.name");
291: String agent = req.getParameter("reg.agent");
292:
293: Producer producer = new ProducerImpl(req, ctx,
294: adminToken, key);
295: RegistrationManager rm = producer
296: .getRegistrationManager();
297:
298: RegistrationData rd = getRegistrationData(name, agent);
299: String rdXML = WSRPFactory.getInstance()
300: .getRegistrationDataXML(rd, true);
301: out.println("rd=\n" + rdXML);
302:
303: RegistrationContext rc = new RegistrationContext(
304: handle, null, null);
305: ModifyRegistration mr = new ModifyRegistration(rc, rd);
306: rm.modifyRegistration(mr);
307: } else if (test.equals("reg.getall")) {
308: String key = req.getParameter("reg.key");
309:
310: Producer producer = new ProducerImpl(req, ctx,
311: adminToken, key);
312: RegistrationManager rm = producer
313: .getRegistrationManager();
314:
315: Set rrs = rm.getRegistrationRecords();
316: for (Iterator i = rrs.iterator(); i.hasNext();) {
317: RegistrationRecord rr = (RegistrationRecord) i
318: .next();
319: out.println(rr);
320: out.println();
321: }
322: } else if (test.equals("reg.validate")) {
323: String key = req.getParameter("reg.key");
324: String handle = req.getParameter("reg.handle");
325:
326: Producer producer = new ProducerImpl(req, ctx,
327: adminToken, key);
328: RegistrationManager rm = producer
329: .getRegistrationManager();
330: ServiceDescriptionManager sdm = producer
331: .getServiceDescriptionManager();
332:
333: RegistrationRecord rr = rm
334: .getRegistrationRecord(handle);
335: RegistrationData rd = rr.getRegistrationData();
336: ServiceDescription sd = sdm.getServiceDescription(null);
337:
338: RegistrationValidator rv = rm
339: .getRegistrationValidator();
340: if (!(rv.validate(rd, sd) < 0)) {
341: out.println("handle=" + handle + " is valid");
342: } else {
343: out.println("handle=" + handle + " is not valid");
344: }
345: } else if (test.equals("sl")) {
346: ISConnection isConnection = new ISConnection(adminToken);
347: isConnection.addConfigListener(this );
348: if (logger.isLoggable(Level.SEVERE))
349: logger.log(Level.SEVERE, "PSWS_CSPWPT0001");
350: } else {
351: out.println("unknown test=" + test);
352: }
353: } catch (Throwable t) {
354: t.printStackTrace(out);
355: } finally {
356: out.close();
357: }
358: }
359:
360: private static RegistrationData getRegistrationData(String name,
361: String agent) {
362: String[] modes = new String[3];
363: modes[0] = "wsrp:view";
364: modes[1] = "wsrp:edit";
365: modes[2] = "wsrp:help";
366:
367: String[] userScopes = null;
368: userScopes = new String[1];
369: userScopes[0] = "wsrp:perUser";
370:
371: Property[] properties = null;
372: properties = new Property[0];
373:
374: String[] customUserProfileData = null;
375: //customUserProfileData = new String[0];
376:
377: String[] windowStates = new String[3];
378: windowStates[0] = "wsrp:normal";
379: windowStates[1] = "wsrp:minimize";
380: windowStates[2] = "wsrp:maximize";
381:
382: RegistrationData rd = new RegistrationData(name, agent, false,
383: modes, windowStates, userScopes, customUserProfileData,
384: properties, null);
385:
386: return rd;
387: }
388:
389: private static Set parseURLParameterList(String list) {
390: StringTokenizer tok = new StringTokenizer(list, ",");
391: Set set = new HashSet();
392: while (tok.hasMoreTokens()) {
393: String element = tok.nextToken();
394: set.add(element);
395: }
396:
397: return set;
398: }
399:
400: public void schemaChanged(String serviceName, String version) {
401: if (logger.isLoggable(Level.SEVERE))
402: logger.log(Level.SEVERE, "PSWS_CSPWPT0002", serviceName);
403: }
404:
405: public void globalConfigChanged(String serviceName, String version,
406: String groupName, String serviceComponent, int type) {
407: }
408:
409: public void organizationConfigChanged(String serviceName,
410: String version, String orgName, String groupName,
411: String serviceComponent, int type) {
412: }
413: }
|