001: package com.sun.portal.providers.bookmark;
002:
003: import java.net.URL;
004: import java.net.URLEncoder;
005:
006: import java.util.Map;
007: import java.util.List;
008: import java.util.ArrayList;
009: import java.util.Vector;
010: import java.util.Hashtable;
011: import java.util.ResourceBundle;
012: import java.util.StringTokenizer;
013: import java.util.logging.Level;
014: import java.util.logging.LogRecord;
015: import java.util.logging.Logger;
016:
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019:
020: import com.sun.portal.providers.ProviderException;
021: import com.sun.portal.providers.ProfileProviderAdapter;
022: import com.sun.portal.providers.AsciiFormInputExpectedException;
023: import com.sun.portal.providers.util.ProviderProperties;
024: import com.sun.portal.providers.context.ProviderContext;
025: import com.sun.portal.providers.context.ProviderContextException;
026:
027: import com.sun.portal.desktop.util.I18n;
028: import com.sun.portal.desktop.util.Target;
029: import com.sun.portal.desktop.util.Integers;
030: import com.sun.portal.desktop.encode.Encoder;
031:
032: import com.sun.portal.desktop.context.ContextException;
033: import com.sun.portal.log.common.PortalLogger;
034:
035: public class BookmarkProvider extends ProfileProviderAdapter implements
036: ProviderProperties {
037: private ResourceBundle bundle = null;
038: private String editContainer = null;
039: private String container = null;
040: private static Logger logger = PortalLogger
041: .getLogger(BookmarkProvider.class);
042:
043: private static final String contentTemplate = "display.template";
044: private static final String urlWrapTemplate = "urlWrapper.template";
045: private static final String EDIT_URL_WRAPPER_TEMPLATE = "editUrlWrapper.template";
046: private static final String EDIT_WINDOW_OPTION_TEMPLATE = "editWindowOption.template";
047:
048: public void init(String n, HttpServletRequest req)
049: throws ProviderException {
050: super .init(n, req);
051: }
052:
053: public BookmarkProvider() {
054: }
055:
056: private List setTargets(List targets) throws ProviderException {
057: try {
058: List clientAndLocaleFilters = getProviderContext()
059: .getClientAndLocalePropertiesFilters();
060: getProviderContext().setCollectionProperty(getName(),
061: "targets", targets, clientAndLocaleFilters);
062: } catch (ProviderContextException pce) {
063: if (logger.isLoggable(Level.WARNING)) {
064: LogRecord rec = new LogRecord(Level.WARNING,
065: "PSDT_CSPPB0001");
066: rec.setLoggerName(logger.getName());
067: String[] param = { "properties filter list" };
068: rec.setParameters(param);
069: rec.setThrown(pce);
070: logger.log(rec);
071: }
072: }
073: return targets;
074: }
075:
076: private List getTargets() throws ProviderException {
077: List targets = null;
078: try {
079: List clientAndLocaleFilters = getProviderContext()
080: .getClientAndLocalePropertiesFilters();
081: targets = new ArrayList(getProviderContext()
082: .getCollectionProperty(getName(), "targets",
083: clientAndLocaleFilters).values());
084: } catch (ProviderContextException pce) {
085: if (logger.isLoggable(Level.WARNING)) {
086: LogRecord rec = new LogRecord(Level.WARNING,
087: "PSDT_CSPPB0001");
088: rec.setLoggerName(logger.getName());
089: String[] param = { "properties filter list" };
090: rec.setParameters(param);
091: rec.setThrown(pce);
092: logger.log(rec);
093: }
094: }
095: if (targets == null) {
096: targets = getListProperty("targets");
097: }
098: return targets;
099:
100: }
101:
102: /**
103: * Determine if the provider is presentable
104: *
105: */
106: public boolean isPresentable(HttpServletRequest request) {
107: boolean isPresentable = false;
108: ProviderContext pc = getProviderContext();
109: try {
110: if ((getTemplatePath(contentTemplate) != null)
111: && (getTemplatePath(urlWrapTemplate) != null)) {
112: isPresentable = true;
113: }
114: } catch (ProviderException pe) {
115: // probably provider class name cannot be obtained
116: // log a warning and return false.
117: if (logger.isLoggable(Level.WARNING))
118: logger.log(Level.WARNING, "PSDT_CSPPB0002", pe);
119: }
120:
121: return isPresentable;
122: }
123:
124: private List getLinks() {
125: List links = null;
126: try {
127: links = new ArrayList(getProviderContext()
128: .getCollectionProperty(getName(), "targets")
129: .values());
130: } catch (ProviderContextException pce) {
131: if (logger.isLoggable(Level.WARNING)) {
132: LogRecord rec = new LogRecord(Level.WARNING,
133: "PSDT_CSPPB0001");
134: rec.setLoggerName(logger.getName());
135: String[] param = { "properties filter list" };
136: rec.setParameters(param);
137: rec.setThrown(pce);
138: logger.log(rec);
139: }
140: }
141: return links;
142: }
143:
144: private void setLinks(List links) throws ProviderException {
145: try {
146: getProviderContext().setCollectionProperty(getName(),
147: "targets", links);
148: } catch (ProviderContextException pce) {
149: if (logger.isLoggable(Level.WARNING)) {
150: LogRecord rec = new LogRecord(Level.WARNING,
151: "PSDT_CSPPB0001");
152: rec.setLoggerName(logger.getName());
153: String[] param = { "properties filter list" };
154: rec.setParameters(param);
155: rec.setThrown(pce);
156: logger.log(rec);
157: }
158: }
159: }
160:
161: public StringBuffer getContent(HttpServletRequest req,
162: HttpServletResponse res) throws ProviderException {
163: Hashtable tagTable = new Hashtable();
164: StringBuffer content = new StringBuffer();
165: StringBuffer ob = new StringBuffer();
166: List clientAndLocaleFilters = null;
167:
168: //Check the version for Bookmark provider if it is greater than or equal to 2
169: //Then use links instead of targets.
170: List targets = null;
171: try {
172: if (getProviderContext().getProviderVersion(getName()) >= 2) {
173: targets = getLinks();
174: } else {
175: targets = getTargets();
176: }
177: } catch (ProviderContextException pce) {
178: if (logger.isLoggable(Level.SEVERE)) {
179: logger.log(Level.SEVERE, "PSDT_CSPPB0003", pce);
180: }
181: }
182:
183: for (int x = 0; x < targets.size(); x++) {
184: String targ = (String) targets.get(x);
185: Target target = new Target(targ);
186:
187: String name = target.getName();
188: String link = target.getValue();
189: try {
190: name = getProviderContext().escape(name);
191: link = getProviderContext().escape(link);
192: clientAndLocaleFilters = getProviderContext()
193: .getClientAndLocalePropertiesFilters();
194: } catch (ProviderContextException pce) {
195: if (logger.isLoggable(Level.SEVERE))
196: logger.log(Level.SEVERE, "PSDT_CSPPB0003", pce);
197: }
198:
199: StringBuffer windowName = new StringBuffer();
200: StringTokenizer tokens = new StringTokenizer(target
201: .getName(), " (),=+#[]@<>$%./!");
202: while (tokens.hasMoreTokens()) {
203: windowName.append((String) tokens.nextToken());
204: if (tokens.hasMoreElements()) {
205: windowName.append("_");
206: }
207: }
208: tagTable.put("fontFace1", getStringProperty("fontFace1",
209: clientAndLocaleFilters));
210: tagTable.put("channelName", Encoder.FORMNAME_ENCODER
211: .encode(getName()));
212: tagTable.put("windowName", windowName);
213: tagTable.put("link", link);
214: tagTable.put("name", name);
215:
216: content.append(getTemplate(urlWrapTemplate, tagTable));
217: tagTable.clear();
218: }
219: String windowPref = getStringProperty("windowPref",
220: clientAndLocaleFilters);
221:
222: tagTable.put("windowOption", windowPref);
223: tagTable.put("bookmarks", content.toString());
224: tagTable.put("channelName", Encoder.FORMNAME_ENCODER
225: .encode(getName()));
226: tagTable.put("fontFace1", getStringProperty("fontFace1",
227: clientAndLocaleFilters));
228:
229: ob = getTemplate(contentTemplate, tagTable);
230: return ob;
231: }
232:
233: public boolean isEditable() throws ProviderException {
234: boolean isEditable = false;
235: if ((getTemplatePath(EDIT_TEMPLATE) != null)
236: && getBooleanProperty("isEditable")) {
237: isEditable = true;
238: }
239: return isEditable;
240: }
241:
242: public StringBuffer getEdit(HttpServletRequest req,
243: HttpServletResponse res) throws ProviderException {
244:
245: StringBuffer content = new StringBuffer();
246: StringBuffer resourceList = new StringBuffer("");
247: Hashtable windowOptions = new Hashtable();
248: editContainer = req.getParameter("provider");
249: container = req.getParameter("containerName");
250: //List targets = getTargets();
251: List clientAndLocaleFilters = null;
252:
253: List targets = null;
254: try {
255: if (getProviderContext().getProviderVersion(getName()) >= 2) {
256: targets = getLinks();
257: } else {
258: targets = getTargets();
259: }
260: } catch (ProviderContextException pce) {
261: if (logger.isLoggable(Level.SEVERE)) {
262: logger.log(Level.SEVERE, "PSDT_CSPPB0003", pce);
263: }
264: }
265:
266: try {
267: clientAndLocaleFilters = getProviderContext()
268: .getClientAndLocalePropertiesFilters();
269: } catch (ProviderContextException pce) {
270: if (logger.isLoggable(Level.WARNING)) {
271: LogRecord rec = new LogRecord(Level.WARNING,
272: "PSDT_CSPPB0001");
273: rec.setLoggerName(logger.getName());
274: String[] param = { "properties filter list" };
275: rec.setParameters(param);
276: rec.setThrown(pce);
277: logger.log(rec);
278: }
279: }
280:
281: //construct the html for the [] NAME URL listing
282:
283: for (int i = 0; i < targets.size(); i++) {
284: String targ = (String) targets.get(i);
285: Target target = new Target(targ);
286: Hashtable existingBM = new Hashtable();
287:
288: existingBM.put("targetName", Encoder.HTML_ENCODER
289: .encode(target.getName()));
290: existingBM.put("targetValue", Encoder.HTML_ENCODER
291: .encode(target.getValue()));
292: existingBM.put("fontFace1", getStringProperty("fontFace1",
293: clientAndLocaleFilters));
294: existingBM.put("index", String.valueOf(i));
295:
296: resourceList.append(getTemplate(EDIT_URL_WRAPPER_TEMPLATE,
297: existingBM));
298: }
299:
300: Hashtable tagTable = new Hashtable();
301:
302: String all_new_checked = new String("");
303: String one_new_checked = new String("");
304: String same_checked = new String("");
305:
306: String wp = getStringProperty("windowPref",
307: clientAndLocaleFilters);
308: if (wp.equals("all_new")) {
309: all_new_checked = "CHECKED";
310: }
311:
312: if (wp.equals("one_new")) {
313: one_new_checked = "CHECKED";
314: }
315:
316: if (wp.equals("same")) {
317: same_checked = "CHECKED";
318: }
319:
320: // Get Resource Bundle if it is null
321: if (bundle == null) {
322: bundle = getResourceBundle();
323: }
324:
325: windowOptions.put("all_new_checked", all_new_checked);
326: windowOptions.put("one_new_checked", one_new_checked);
327: windowOptions.put("same_checked", same_checked);
328: windowOptions.put("ownWindow", bundle.getString("ownWindow"));
329: windowOptions.put("singleWindow", bundle
330: .getString("singleWindow"));
331: windowOptions.put("mainWindow", bundle.getString("mainWindow"));
332:
333: String resourceCountString = Integer.toString(targets.size());
334:
335: // error tags (if any)
336: String err = getProviderContext().decodeURLParameter(
337: req.getParameter("error"));
338: if (err != null) {
339: tagTable.put(ERR_MESSAGE, err);
340: tagTable.put("inlineError",
341: getTemplate("inlineError.template"));
342: } else {
343: // no error, put in dummy tags so lookup doesn't fail
344: tagTable.put(ERR_MESSAGE, "");
345: tagTable.put(INLINE_ERROR, "");
346: }
347:
348: String resourceName = req.getParameter("resourceName");
349: if (resourceName != null) {
350: tagTable.put("resourceName", resourceName);
351: } else {
352: tagTable.put("resourceName", "");
353: }
354: String resourceURL = req.getParameter("resourceURL");
355:
356: if (resourceURL != null) {
357: tagTable.put("resourceURL", resourceURL);
358: } else {
359: tagTable.put("resourceURL", "");
360: }
361: tagTable.put("resourceCount", resourceCountString);
362: tagTable.put("resourceList", resourceList.toString());
363: tagTable.put("windowOptions", getTemplate(
364: EDIT_WINDOW_OPTION_TEMPLATE, windowOptions));
365: tagTable.put("fontFace1", getStringProperty("fontFace1",
366: clientAndLocaleFilters));
367:
368: content = getTemplate(EDIT_TEMPLATE, tagTable);
369:
370: return content;
371: }
372:
373: public URL processEdit(HttpServletRequest req,
374: HttpServletResponse res) throws ProviderException {
375: List clientAndLocaleFilters = null;
376: editContainer = req.getParameter("provider");
377: container = req.getParameter("containerName");
378: getProviderContext().contentChanged(getName());
379:
380: String error = null;
381: Vector targets = new Vector();
382: // To repaint the edit page after proccessing edit.
383: // Error messages will be appended at the end.
384: StringBuffer nextURLString = new StringBuffer();
385: if (editContainer != null && container != null) {
386: nextURLString.append(
387: getProviderContext().getDesktopURL(req)).append(
388: "?action=edit&provider=").append(
389: getProviderContext().encodeURLParameter(
390: editContainer)).append("&targetprovider=")
391: .append(
392: getProviderContext().encodeURLParameter(
393: getName())).append(
394: "&containerName=").append(
395: getProviderContext().encodeURLParameter(
396: container));
397: } else {
398: // processEdit has been called directly, hence we cannot
399: // gracefully send back any valid error message to any edit form
400: nextURLString = null;
401: }
402:
403: //
404: // find out which book marks were checked to remove
405: //
406: String rc = req.getParameter("resourceCount");
407: int resourceCount = Integer.parseInt(rc);
408:
409: // Get Resource Bundle if it is null
410: if (bundle == null) {
411: bundle = getResourceBundle();
412: }
413:
414: URL next = null;
415:
416: for (int i = 0; i < resourceCount; i++) {
417:
418: String checkBoxValue = req.getParameter("remove" + i);
419: Integer removeFlag = (checkBoxValue == null) ? Integers
420: .get(0) : Integer.valueOf(checkBoxValue);
421:
422: //
423: // if the box was NOT check then we will keep it
424: // in the bookmark list. Build the name and url
425: // vectors to store in the hashtable
426: //
427: if (removeFlag.intValue() != 1) {
428: String nameString = "name" + i;
429: String urlString = "url" + i;
430:
431: String name = req.getParameter(nameString);
432: String url = req.getParameter(urlString);
433: boolean errorFlag = false;
434:
435: // Error handle when one of the existing bookmarks is changed
436: if ((name == null || name.trim().equals(""))
437: && (url == null || url.trim().equals(""))) {
438: if (nextURLString != null) {
439: nextURLString
440: .append("&error=")
441: .append(
442: getProviderContext()
443: .encodeURLParameter(
444: bundle
445: .getString("removeError")));
446: errorFlag = true;
447: }
448:
449: } else if (name == null || name.trim().equals("")) {
450: if (nextURLString != null) {
451: nextURLString
452: .append("&error=")
453: .append(
454: getProviderContext()
455: .encodeURLParameter(
456: bundle
457: .getString("emptyName")
458: + " : "
459: + url));
460: errorFlag = true;
461: }
462: } else if (url == null || url.trim().equals("")) {
463: if (nextURLString != null) {
464: nextURLString
465: .append("&error=")
466: .append(
467: getProviderContext()
468: .encodeURLParameter(
469: bundle
470: .getString("emptyURL")
471: + " : "
472: + name));
473: errorFlag = true;
474: }
475: }
476:
477: if (errorFlag) {
478: try {
479: if (nextURLString != null) {
480: next = new URL(nextURLString.toString());
481: }
482: } catch (java.net.MalformedURLException e) {
483: throw new ProviderException(
484: "BookmarkProvider.processEdit()", e);
485: }
486: return next;
487: }
488:
489: if (!I18n.isAscii(url)) {
490: String err = bundle.getString("nonAsciiURL");
491: throw new AsciiFormInputExpectedException(err);
492: }
493:
494: Target target = new Target(name, url);
495:
496: if ((target.getValue().indexOf("://") == -1)
497: && (!target.getValue().startsWith("/"))) {
498:
499: // add http if there's no protocol specificed
500: // and it doesn't start with a '/'
501: target.setValue("http://" + target.getValue());
502: }
503:
504: targets.add(target.toString());
505: }
506: }
507:
508: //
509: // get the new bookmark information
510: //
511: String newName = req.getParameter("resourceName");
512: String newURL = req.getParameter("resourceURL");
513: String addMore = req.getParameter("add_more");
514:
515: if (addMore != null && !addMore.trim().equals("")) {
516: if (newName != null && newURL != null
517: && !newName.trim().equals("")
518: && !newURL.trim().equals("")) {
519:
520: if (!I18n.isAscii(newURL)) {
521: String err = bundle.getString("nonAsciiURL");
522: throw new AsciiFormInputExpectedException(err);
523: }
524:
525: Target target = new Target(newName, newURL);
526:
527: if ((target.getValue().indexOf("://") == -1)
528: && (!target.getValue().startsWith("/"))) {
529:
530: // add http if there's no protocol specificed
531: // and it doesn't start with a '/'
532: target.setValue("http://" + target.getValue());
533: }
534:
535: //
536: // add the new target to the vector of all targets
537: //
538: targets.add(target.toString());
539: } else {
540: if ((newName == null || newName.trim().equals(""))
541: && (newURL == null || newURL.trim().equals(""))) {
542: if (addMore != null && !addMore.trim().equals("")) {
543: error = bundle.getString("emptyNameURL");
544: }
545: } else if (newName == null || newName.trim().equals("")) {
546: error = bundle.getString("emptyName");
547: } else {
548: error = bundle.getString("emptyURL");
549: }
550: }
551: }
552:
553: if (error != null) {
554: if (nextURLString != null && newName != null
555: && newURL != null) {
556: /*******************************************************
557: * Need to this tmp string comparsion beacuse
558: * ProvideContext.encodeURLParameter
559: * returns a null if its parameter is a empty string("").
560: *********************************************************/
561: String urltmp = getProviderContext()
562: .encodeURLParameter(newURL);
563: String nametmp = getProviderContext()
564: .encodeURLParameter(newName);
565: if (urltmp == null)
566: urltmp = "";
567: if (nametmp == null)
568: nametmp = "";
569: nextURLString.append("&resourceName=").append(nametmp)
570: .append("&resourceURL=").append(urltmp).append(
571: "&error=").append(
572: getProviderContext()
573: .encodeURLParameter(error));
574: }
575: }
576: if (error == null
577: && (addMore == null || addMore.trim().equals(""))) {
578: // If the user clicks on "Finish" button, do this
579: next = null;
580: } else {
581: try {
582: if (nextURLString != null) {
583: next = new URL(nextURLString.toString());
584: }
585: } catch (java.net.MalformedURLException e) {
586: throw new ProviderException(
587: "BookmarkProvider.processEdit()", e);
588: }
589: }
590:
591: //
592: // put the targets vector into the data hash
593: //
594: //setTargets(targets);
595: try {
596: if (getProviderContext().getProviderVersion(getName()) >= 2) {
597: setLinks(targets);
598: } else {
599: setTargets(targets);
600: }
601: } catch (ProviderContextException pce) {
602: if (logger.isLoggable(Level.SEVERE)) {
603: logger.log(Level.SEVERE, "PSDT_CSPPB0003", pce);
604: }
605: }
606:
607: String windowPref = req.getParameter("windowPref");
608: try {
609: clientAndLocaleFilters = getProviderContext()
610: .getClientAndLocalePropertiesFilters();
611: //Window Preference
612: if (windowPref != null
613: && !windowPref.equals(getStringProperty(
614: "windowPref", clientAndLocaleFilters))) {
615: getProviderContext().setStringProperty(getName(),
616: "windowPref", windowPref,
617: clientAndLocaleFilters);
618: }
619: } catch (ProviderContextException pce) {
620: if (logger.isLoggable(Level.WARNING)) {
621: LogRecord rec = new LogRecord(Level.WARNING,
622: "PSDT_CSPPB0001");
623: rec.setLoggerName(logger.getName());
624: String[] param = { "windowPref" };
625: rec.setParameters(param);
626: rec.setThrown(pce);
627: logger.log(rec);
628: }
629: }
630: return next;
631: }
632: }
|