001: package com.sun.portal.providers.pctest;
002:
003: import java.util.Iterator;
004: import java.util.Map;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.ArrayList;
008: import java.util.Hashtable;
009: import java.util.Date;
010: import java.util.StringTokenizer;
011: import java.util.logging.Level;
012: import java.util.logging.Logger;
013:
014: import java.text.DateFormat;
015:
016: import java.net.URL;
017:
018: import javax.servlet.http.HttpServletRequest;
019: import javax.servlet.http.HttpServletResponse;
020:
021: import com.sun.portal.providers.ProfileProviderAdapter;
022: import com.sun.portal.providers.ProviderException;
023: import com.sun.portal.providers.context.ProviderContext;
024: import com.sun.portal.providers.context.ProviderContextException;
025: import com.sun.portal.providers.context.PropertiesFilter;
026: import com.sun.portal.providers.context.PropertiesFilterException;
027: import com.sun.portal.providers.context.PropertiesFilterFactory;
028:
029: import com.sun.portal.desktop.context.ContextException;
030:
031: import com.sun.portal.desktop.DesktopServlet;
032: import com.sun.portal.log.common.PortalLogger;
033:
034: public class PCTestProvider extends ProfileProviderAdapter {
035: private static Logger logger = PortalLogger
036: .getLogger(PCTestProvider.class);
037:
038: private static String getParamString(String key, Map p) {
039: String[] a = (String[]) p.get(key);
040:
041: if (a == null) {
042: return null;
043: }
044:
045: return a[0];
046: }
047:
048: public StringBuffer getContent(Map params) throws ProviderException {
049: Hashtable tags = new Hashtable();
050:
051: tags.put("message", getStringProperty("message", true));
052: //tags.put("properties", "");
053: tags.put("properties", getClientProperties());
054:
055: String cmd = getParamString(getName() + ".cmd", params);
056:
057: if (cmd != null) {
058: if (cmd.equals("get")) {
059: tags.put("properties", getProperties());
060: } else if (cmd.equals("getstring")) {
061: String name = getParamString("name", params);
062:
063: String l = getParamString("language", params);
064: String c = getParamString("country", params);
065: String v = getParamString("variant", params);
066:
067: tags.put("properties", getStringProperty(name));
068: } else if (cmd.equals("getboolean")) {
069: String pcname = getParamString("name", params);
070: tags.put("properties", new Boolean(
071: getBooleanProperty(pcname)));
072: } else if (cmd.equals("getlist")) {
073: String pcname = getParamString("name", params);
074: tags.put("properties", getListProperty(pcname));
075: } else if (cmd.equals("setstring")) {
076: String name = getParamString("name", params);
077: String value = getParamString("value", params);
078: setStringProperty(name, value);
079: tags.put("properties", getProperties());
080: } else if (cmd.equals("setlist")) {
081: List vals = new ArrayList();
082: String dpname = getParamString("name", params);
083: for (int i = 0;; i++) {
084: String dpval = getParamString("val" + i, params);
085: if (dpval == null) {
086: break;
087: }
088: vals.add(dpval);
089: }
090: setListProperty(dpname, vals);
091: tags.put("properties", getProperties());
092: } else if (cmd.equals("setboolean")) {
093: String dpname = getParamString("name", params);
094: boolean dpval = new Boolean(getParamString("val",
095: params)).booleanValue();
096: setBooleanProperty(dpname, dpval);
097: tags.put("properties", getProperties());
098: } else if (cmd.equals("setinteger")) {
099: String dpname = getParamString("name", params);
100: int dpval = Integer.parseInt(getParamString("val",
101: params));
102: setIntegerProperty(dpname, dpval);
103: tags.put("properties", getProperties());
104: } else if (cmd.equals("setcollection")) {
105: String name = getParamString("name", params);
106: String val = getParamString("value", params);
107:
108: StringTokenizer vals = new StringTokenizer(val, ",");
109: Map c = new HashMap();
110: while (vals.hasMoreTokens()) {
111: String v = vals.nextToken();
112: StringTokenizer pair = new StringTokenizer(v, ":");
113: String pairName = pair.nextToken();
114: String pairVal = pair.nextToken();
115:
116: c.put(pairName, pairVal);
117: }
118:
119: setMapProperty(name, c);
120: } else if (cmd.equals("getcollection")) {
121: String name = getParamString("name", params);
122: tags.put("properties", getMapProperty(name, true));
123: } else if (cmd.equals("putcollection")) {
124: String name = getParamString("name", params);
125: String val = getParamString("value", params);
126:
127: StringTokenizer pair = new StringTokenizer(val, ":");
128: String pairName = pair.nextToken();
129: String pairVal = pair.nextToken();
130:
131: ProviderContext pc = getProviderContext();
132: Map m = getMapProperty(name);
133: logger.log(Level.FINEST, "PSDT_CSPPPC0001",
134: new Object[] { m, "get" });
135: m.put(pairName, pairVal);
136: logger.log(Level.FINEST, "PSDT_CSPPPC0001",
137: new Object[] { m, "put" });
138: setMapProperty(name, m);
139: logger.log(Level.FINEST, "PSDT_CSPPPC0001",
140: new Object[] { m, "set" });
141: } else if (cmd.equals("getpfstring")) {
142: //
143: // sample parameter: provider=_PCTest&_PCTest.cmd=getpfstring&name=message&filter=com.sun.portal.providers.context.LocalePropertiesFilter:en:true;com.sun.portal.providers.context.LocalePropertiesFilter:US:true;com.sun.portal.providers.pctest.DateLaterThanPropertiesFilter:02/02/2003:true
144: //
145: String name = getParamString("name", params);
146: String filters = getParamString("filter", params);
147:
148: List pflist = getFilters(filters);
149: tags.put("properties", "name=" + name + "<br> filters="
150: + pflist + "<br> value="
151: + getStringProperty(name, pflist));
152: } else if (cmd.equals("getpfinteger")) {
153: //
154: // sample parameter: provider=_PCTest&_PCTest.cmd=getpfinteger&name=number&filter=com.sun.portal.providers.context.LocalePropertiesFilter:ja:false;com.sun.portal.providers.context.LocalePropertiesFilter:JP:false;com.sun.portal.providers.pctest.DateLaterThanPropertiesFilter:04/04/2003:true
155: //
156: String name = getParamString("name", params);
157: String filters = getParamString("filter", params);
158:
159: List pflist = getFilters(filters);
160: tags.put("properties", "name=" + name + "<br> filters="
161: + pflist + "<br> value="
162: + getIntegerProperty(name, pflist));
163: } else if (cmd.equals("getpfboolean")) {
164: //
165: // sample parameter: provider=_PCTest&_PCTest.cmd=getpfboolean&name=number&filter=com.sun.portal.providers.context.LocalePropertiesFilter:fr:true
166: //
167: String name = getParamString("name", params);
168: String filters = getParamString("filter", params);
169:
170: List pflist = getFilters(filters);
171: tags.put("properties", "name=" + name + "<br> filters="
172: + pflist + "<br> value="
173: + getBooleanProperty(name, pflist));
174: } else if (cmd.equals("getpfcollection")) {
175: //
176: // sample parameter: provider=_PCTest&_PCTest.cmd=getpfcollection&name=messages&filter=com.sun.portal.providers.context.LocalePropertiesFilter:en:true;com.sun.portal.providers.context.LocalePropertiesFilter:AU:false;com.sun.portal.providers.pctest.DateLaterThanPropertiesFilter:02/02/2003:true;
177: //
178: String name = getParamString("name", params);
179: String filters = getParamString("filter", params);
180:
181: List pflist = getFilters(filters);
182: tags.put("properties", "name=" + name + "<br> filters="
183: + pflist + "<br> value="
184: + getMapProperty(name, pflist));
185: } else if (cmd.equals("setpfstring")) {
186: //
187: // sample parameter: provider=_PCTest&_PCTest.cmd=setpfstring&name=message&value=frtestvalue&filter=com.sun.portal.providers.context.LocalePropertiesFilter:fr:true;com.sun.portal.providers.context.LocalePropertiesFilter:FR:true
188: //
189: String name = getParamString("name", params);
190: String value = getParamString("value", params);
191: String filters = getParamString("filter", params);
192:
193: List pflist = getFilters(filters);
194: try {
195: getProviderContext().setStringProperty(getName(),
196: name, value, pflist);
197: } catch (ProviderContextException pce) {
198: throw new ProviderException(
199: "PCTestProvider.getContent(): ", pce);
200: }
201: tags.put("properties", "name=" + name + ", filters="
202: + pflist + ", value="
203: + getStringProperty(name, pflist));
204: } else if (cmd.equals("setpfinteger")) {
205: //
206: // sample parameter: provider=_PCTest&_PCTest.cmd=setpfinteger&name=integer&value=1&filter=com.sun.portal.providers.context.LocalePropertiesFilter:fr:true;com.sun.portal.providers.context.LocalePropertiesFilter:FR:true
207: //
208: String name = getParamString("name", params);
209: String value = getParamString("value", params);
210: String filters = getParamString("filter", params);
211:
212: List pflist = getFilters(filters);
213: try {
214: getProviderContext().setIntegerProperty(getName(),
215: name, Integer.parseInt(value), pflist);
216: } catch (ProviderContextException pce) {
217: throw new ProviderException(
218: "PCTestProvider.getContent(): ", pce);
219: }
220: tags.put("properties", "name=" + name + ", filters="
221: + pflist + ", value="
222: + getIntegerProperty(name, pflist));
223: } else if (cmd.equals("setpfboolean")) {
224: //
225: // sample parameter: provider=_PCTest&_PCTest.cmd=setpfboolean&name=bool&value=true&filter=com.sun.portal.providers.context.LocalePropertiesFilter:fr:true;com.sun.portal.providers.context.LocalePropertiesFilter:FR:true
226: //
227: String name = getParamString("name", params);
228: String value = getParamString("value", params);
229: String filters = getParamString("filter", params);
230:
231: List pflist = getFilters(filters);
232: try {
233: getProviderContext().setBooleanProperty(getName(),
234: name, Boolean.getBoolean(value), pflist);
235: } catch (ProviderContextException pce) {
236: throw new ProviderException(
237: "PCTestProvider.getContent(): ", pce);
238: }
239: tags.put("properties", "name=" + name + ", filters="
240: + pflist + ", value="
241: + getBooleanProperty(name, pflist));
242: } else if (cmd.equals("setpfcollection")) {
243: //
244: // sample parameter: provider=_PCTest&_PCTest.cmd=setpfcollection&name=messages&value=col1:val1,col2:val2&filter=com.sun.portal.providers.context.LocalePropertiesFilter:fr:true;com.sun.portal.providers.context.LocalePropertiesFilter:FR:true
245: //
246: String name = getParamString("name", params);
247: String value = getParamString("value", params);
248: String filters = getParamString("filter", params);
249:
250: StringTokenizer vals = new StringTokenizer(value, ",");
251: Map c = new HashMap();
252: while (vals.hasMoreTokens()) {
253: String v = vals.nextToken();
254: StringTokenizer pair = new StringTokenizer(v, ":");
255: String pairName = pair.nextToken();
256: String pairVal = pair.nextToken();
257:
258: c.put(pairName, pairVal);
259: }
260:
261: List pflist = getFilters(filters);
262: try {
263: getProviderContext().setCollectionProperty(
264: getName(), name, c, pflist);
265: } catch (ProviderContextException pce) {
266: throw new ProviderException(
267: "PCTestProvider.getContent(): ", pce);
268: }
269: tags.put("properties", "name=" + name + ", filters="
270: + pflist + ", value="
271: + getMapProperty(name, pflist));
272: }
273: }
274:
275: StringBuffer b = getTemplate("content.template", tags);
276:
277: b.append("<br><br>").append(testSession());
278:
279: return b;
280: }
281:
282: protected StringBuffer getClientProperties()
283: throws ProviderException {
284: StringBuffer b = new StringBuffer();
285: String val = null;
286: ProviderContext ctx = getProviderContext();
287: String countStr = ctx.getClientProperty("count");
288: val = ctx.getClientType();
289: b.append("clientType=" + val + "<br>");
290: val = ctx.getContentType();
291: b.append("contentType=" + val + "<br>");
292: val = ctx.getClientPath();
293: b.append("clientPath=" + val + "<br>");
294: val = ctx.getCharset();
295: b.append("charset=" + val + "<br>");
296: val = ctx.getClientTypeProperty("cookieSupport");
297: b.append("client property/cookieSupport=" + val + "<br>");
298:
299: return b;
300: }
301:
302: protected StringBuffer getProperties() throws ProviderException {
303: StringBuffer b = new StringBuffer();
304:
305: try {
306: Iterator i = getProviderContext().getNames(getName());
307:
308: for (; i.hasNext();) {
309: String name = (String) i.next();
310: Object val = getProviderContext().getProperty(
311: getName(), name);
312: b.append(name + "=" + val.toString() + "<br>");
313: }
314: } catch (ProviderContextException pce) {
315: throw new ProviderException(
316: "PCTestProvider.getProperties(): ", pce);
317: }
318: return b;
319:
320: }
321:
322: protected StringBuffer testSession() throws ProviderException {
323:
324: StringBuffer buf = new StringBuffer();
325:
326: ProviderContext ctx = getProviderContext();
327: String countStr = ctx.getClientProperty("count");
328: int count = 0;
329: if (countStr != null && countStr.length() > 0) {
330: try {
331: count = Integer.parseInt(countStr);
332: } catch (NumberFormatException nfe) {
333: logger.log(Level.FINEST, "PSDT_CSPPPC0002", nfe);
334: }
335: }
336: buf
337: .append(
338: "This number should increment by 1 everytime you load this page: <b>")
339: .append(Integer.toString(++count)).append("</b><br>");
340: if (count > 1) {
341: buf.append("Your last visit was at ").append(
342: ctx.getClientProperty("timestamp"));
343: }
344:
345: DateFormat dateFormat = DateFormat.getDateTimeInstance(
346: DateFormat.SHORT, DateFormat.LONG, java.util.Locale.US);
347: String timestamp = dateFormat.format(new Date());
348: ctx.setClientProperty("count", Integer.toString(count));
349: ctx.setClientProperty("timestamp", timestamp);
350:
351: return buf;
352: }
353:
354: public StringBuffer getEdit(HttpServletRequest req,
355: HttpServletResponse res) throws ProviderException {
356:
357: Hashtable tags = new Hashtable();
358: tags.put("message", getStringProperty("message"));
359: tags.put("properties", "");
360: StringBuffer b = getTemplate("content.template", tags);
361: return b;
362:
363: }
364:
365: public List getFilters(String filters) throws ProviderException {
366: List pflist = new ArrayList();
367: for (StringTokenizer st = new StringTokenizer(filters, ";"); st
368: .hasMoreTokens();) {
369: String filter = st.nextToken();
370: StringTokenizer stz = new StringTokenizer(filter, ":");
371: String type = stz.nextToken();
372: String value = stz.nextToken();
373: boolean exact = stz.nextToken().toLowerCase()
374: .equals("true") ? true : false;
375: PropertiesFilter pf = null;
376: try {
377: pf = PropertiesFilterFactory.get(type, value, exact);
378: } catch (PropertiesFilterException pfe) {
379: throw new ProviderException(
380: "PCTestProvider.getFilters(): ", pfe);
381: }
382: pflist.add(pf);
383: }
384: return pflist;
385: }
386: }
|