001: package com.sun.portal.providers.dptest;
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.Set;
008: import java.util.ArrayList;
009: import java.util.Hashtable;
010: import java.util.Date;
011: import java.util.Locale;
012: import java.util.StringTokenizer;
013:
014: import java.text.DateFormat;
015:
016: import java.net.URL;
017: import java.net.URLEncoder;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import com.sun.portal.providers.ProfileProviderAdapter;
023: import com.sun.portal.providers.ProviderException;
024: import com.sun.portal.providers.context.ProviderContext;
025: import com.sun.portal.providers.context.ProviderContextException;
026:
027: import com.sun.portal.desktop.context.DPContext;
028: import com.sun.portal.desktop.context.ContextException;
029: import com.sun.portal.desktop.context.DesktopContextThreadLocalizer;
030: import com.sun.portal.providers.context.PropertiesFilter;
031: import com.sun.portal.providers.context.PropertiesFilterException;
032: import com.sun.portal.providers.context.PropertiesFilterFactory;
033:
034: import com.sun.portal.desktop.DesktopServlet;
035:
036: import com.sun.portal.desktop.dp.DPRoot;
037: import com.sun.portal.desktop.dp.DPProvider;
038: import com.sun.portal.desktop.dp.DPString;
039: import com.sun.portal.desktop.dp.xml.XMLDPProvider;
040: import com.sun.portal.desktop.dp.DPChannel;
041: import com.sun.portal.desktop.dp.DPProperties;
042: import com.sun.portal.desktop.dp.DPCollection;
043: import com.sun.portal.desktop.dp.xml.XMLDPObject;
044:
045: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
046:
047: public class DPTestProvider extends ProfileProviderAdapter {
048: public StringBuffer getContent(HttpServletRequest req,
049: HttpServletResponse res) throws ProviderException {
050: String cmd = req.getParameter(getName() + ".cmd");
051: StringBuffer content = new StringBuffer();
052: DPRoot dpRoot = DesktopContextThreadLocalizer.get().getDPRoot();
053:
054: if (cmd == null) {
055: content.append("< no command received >");
056: } else if (cmd.equals("root.getprovidernames")) {
057: Set names = dpRoot.getProviderNames();
058: content.append("result of DPRoot.getProviderNames(): "
059: + names);
060: } else if (cmd.equals("channel.getprovider")) {
061: String chname = req.getParameter("channelname");
062: DPChannel dpc = dpRoot.getChannel(chname);
063: if (dpc == null) {
064: content
065: .append(" < channel not found: " + chname
066: + " >");
067: } else {
068: DPProvider dpp = dpc.getProvider();
069: if (dpp == null) {
070: content.append(" < provider not found: "
071: + dpc.getProviderName() + " >");
072: } else {
073: XMLDPProvider xmldpp = (XMLDPProvider) dpp;
074: content.append("provider name="
075: + dpc.getProviderName()
076: + ", results of DPChannel.getProvider(): "
077: + URLEncoder.encode(dpp.toString(false)));
078: }
079: }
080:
081: } else if (cmd.equals("remove.test")) {
082: DPChannel bmc = dpRoot.getChannel("Bookmark");
083: DPProperties dpp = bmc.getProperties();
084: DPCollection dpc = (DPCollection) dpp.get("targets");
085: dpc
086: .remove("Everything you want to know about Sun ONE ...|http://www.sun.com/software/products/portal_srvr/home_portal.html");
087:
088: } else if (cmd.equals("filter.test")) {
089: DPChannel dpc = dpRoot.getChannel(getName());
090: DPProperties dpp = dpc.getProperties();
091:
092: //
093: // test getting properties w/ filters
094: //
095: List pflist = new ArrayList();
096: pflist.add(getLocaleFilter("en", false));
097: content.append("locale=en<br><br>");
098: DPString dps = dpp.getString("message", pflist, false);
099: String msg = dps.getStringValue();
100: content.append("message=" + msg + "<br><br>");
101: DPCollection dpcl = dpp.getCollection("messages", pflist,
102: false);
103: Map map = dpcl.getCollectionValue();
104: content.append("messages=" + map + "<br><br>");
105:
106: try {
107: pflist = getProviderContext()
108: .getLocalePropertiesFilters();
109: } catch (ProviderContextException pce) {
110: throw new ProviderException(
111: "DPTestProvider.getContent(): ", pce);
112: }
113: content.append("locale=" + getProviderContext().getLocale()
114: + "<br><br>");
115: dps = dpp.getString("message", pflist, false);
116: msg = dps.getStringValue();
117: content.append("message=" + msg + "<br><br>");
118: dpcl = dpp.getCollection("messages", pflist, false);
119: map = dpcl.getCollectionValue();
120: content.append("messages=" + map + "<br><br>");
121:
122: pflist = new ArrayList();
123: pflist.add(getLocaleFilter("en", false));
124: pflist.add(getLocaleFilter("UK", false));
125: content.append("locale=en_UK<br><br>");
126: dps = dpp.getString("message", pflist, false);
127: msg = dps.getStringValue();
128: content.append("message=" + msg + "<br><br>");
129: dpcl = dpp.getCollection("messages", pflist, false);
130: map = dpcl.getCollectionValue();
131: content.append("messages=" + map + "<br><br>");
132:
133: pflist = new ArrayList();
134: pflist.add(getLocaleFilter("fr"));
135: pflist.add(getLocaleFilter("FR"));
136: content.append("locale=fr_FR<br><br>");
137: dps = dpp.getString("message", pflist, false);
138: msg = dps.getStringValue();
139: content.append("message=" + msg + "<br><br>");
140: dpcl = dpp.getCollection("messages", pflist, false);
141: map = dpcl.getCollectionValue();
142: content.append("messages=" + map + "<br><br>");
143:
144: pflist = new ArrayList();
145: pflist.add(getLocaleFilter("en", false));
146: pflist.add(getLocaleFilter("AU", false));
147: content.append("locale=en_AU<br><br>");
148: dps = dpp.getString("message", pflist, false);
149: msg = dps.getStringValue();
150: content.append("message=" + msg + "<br><br>");
151: dpcl = dpp.getCollection("messages", pflist, false);
152: map = dpcl.getCollectionValue();
153: content.append("messages=" + map + "<br><br>");
154:
155: content.append("locale=<none><br><br>");
156: dps = dpp.getString("message");
157: msg = dps.getStringValue();
158: content.append("message=" + msg + "<br><br>");
159: dpcl = dpp.getCollection("messages");
160: map = dpcl.getCollectionValue();
161: content.append("messages=" + map + "<br><br>");
162:
163: } else if (cmd.equals("filter.test2")) {
164: DPChannel dpc = dpRoot.getChannel(getName());
165: DPProperties dpp = dpc.getProperties();
166:
167: //
168: // test adding properties for various locales
169: //
170: List pflist_en = new ArrayList();
171: pflist_en.add(getLocaleFilter("en"));
172: DPString dps = dpp.getString("message", pflist_en, false);
173: DPString n = (DPString) dps.copy(dpc.getRoot(), false);
174: ((XMLDPObject) n).getElement().setAttribute("name",
175: "message2");
176: n.setStringValue("message2 - en");
177: dpp.add(n, pflist_en);
178:
179: List pflist_enUS = new ArrayList();
180: pflist_enUS.add(getLocaleFilter("en"));
181: pflist_enUS.add(getLocaleFilter("US"));
182: n = (DPString) n.copy(dpc.getRoot(), false);
183: n.setStringValue("message2 - en_US");
184: dpp.add(n, pflist_enUS);
185:
186: List pflist_enUK = new ArrayList();
187: pflist_enUK.add(getLocaleFilter("en"));
188: pflist_enUK.add(getLocaleFilter("UK"));
189: n = (DPString) n.copy(dpc.getRoot(), false);
190: n.setStringValue("message2 - en_UK");
191: dpp.add(n, pflist_enUK);
192:
193: List pflist_frFR = new ArrayList();
194: pflist_frFR.add(getLocaleFilter("fr"));
195: pflist_frFR.add(getLocaleFilter("FR"));
196: n = (DPString) n.copy(dpc.getRoot(), false);
197: n.setStringValue("message2 - fr_FR");
198: dpp.add(n, pflist_frFR);
199:
200: List pflist_enAU = new ArrayList();
201: pflist_enAU.add(getLocaleFilter("en"));
202: pflist_enAU.add(getLocaleFilter("AU"));
203: n = (DPString) n.copy(dpc.getRoot(), false);
204: n.setStringValue("message2 - en_AU");
205: dpp.add(n, pflist_enAU);
206:
207: n = (DPString) n.copy(dpc.getRoot(), false);
208: n.setStringValue("message2 - (no locale)");
209: dpp.add(n);
210:
211: //
212: // test setting properties for various locales
213: //
214: dpp.setString("message2", dpp.getString("message2",
215: pflist_enUS, true).getStringValue()
216: + " - WOW!", pflist_enUS);
217: dpp.setString("message2", dpp.getString("message2",
218: pflist_en, true).getStringValue()
219: + " - WOW!", pflist_en);
220: dpp.setString("message2", dpp.getString("message2",
221: pflist_enUK, true).getStringValue()
222: + " - WOW!", pflist_enUK);
223: dpp.setString("message2", dpp.getString("message2",
224: pflist_frFR, true).getStringValue()
225: + " - WOW!", pflist_frFR);
226: dpp.setString("message2", dpp.getString("message2")
227: .getStringValue()
228: + " - WOW!");
229:
230: }
231:
232: return content;
233: }
234:
235: private PropertiesFilter getLocaleFilter(String locale)
236: throws ProviderException {
237: return getLocaleFilter(locale, true);
238: }
239:
240: private PropertiesFilter getLocaleFilter(String locale,
241: boolean exact) throws ProviderException {
242: PropertiesFilter pf = null;
243: try {
244: pf = PropertiesFilterFactory
245: .get(
246: PropertiesFilterFactory.LOCALE_PROPERTIESFILTER_CLASSNAME,
247: locale, exact);
248: } catch (PropertiesFilterException pfe) {
249: throw new ProviderException(
250: "DPTestProvider.getLocaleFilter(): ", pfe);
251: }
252: return pf;
253: }
254: }
|