001: package com.ecyrd.jspwiki.preferences;
002:
003: import java.text.ParseException;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.Properties;
007:
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpSession;
010: import javax.servlet.jsp.PageContext;
011:
012: import org.json.JSONObject;
013:
014: import com.ecyrd.jspwiki.PropertyReader;
015: import com.ecyrd.jspwiki.TextUtil;
016: import com.ecyrd.jspwiki.WikiContext;
017: import com.ecyrd.jspwiki.util.HttpUtil;
018:
019: /**
020: * Represents an object which is used to store user preferences.
021: *
022: * @author jalkanen
023: */
024: public class Preferences extends HashMap {
025: private static final long serialVersionUID = 1L;
026:
027: /**
028: * The name under which a Preferences object is stored in the HttpSession.
029: * Its value is {@value}.
030: */
031: public static final String SESSIONPREFS = "prefs";
032:
033: /**
034: * This is an utility method which is called to make sure that the
035: * JSP pages do have proper access to any user preferences. It should be
036: * called from the commonheader.jsp.
037: * <p>
038: * This method reads user cookie preferences and mixes them up with any
039: * default preferences (and in the future, any user-specific preferences)
040: * and puts them all in the session, so that they do not have to be rewritten
041: * again.
042: * <p>
043: * This method will remember if the user has already changed his prefs.
044: *
045: * @param pageContext The JSP PageContext.
046: */
047: public static void setupPreferences(PageContext pageContext) {
048: HttpSession session = pageContext.getSession();
049:
050: if (session.getAttribute(SESSIONPREFS) == null) {
051: reloadPreferences(pageContext);
052: }
053: }
054:
055: public static void reloadPreferences(PageContext pageContext) {
056: Preferences prefs = new Preferences();
057: Properties props = PropertyReader.loadWebAppProps(pageContext
058: .getServletContext());
059:
060: prefs.put("SkinName", TextUtil.getStringProperty(props,
061: "jspwiki.defaultprefs.template.skinname",
062: "PlainVanilla"));
063: prefs.put("DateFormat", TextUtil.getStringProperty(props,
064: "jspwiki.defaultprefs.template.dateformat",
065: "dd-MMM-yyyy HH:mm"));
066: prefs.put("TimeZone", TextUtil.getStringProperty(props,
067: "jspwiki.defaultprefs.template.timezone",
068: java.util.TimeZone.getDefault().getID()));
069: prefs.put("orientation", TextUtil
070: .getStringProperty(props,
071: "jspwiki.defaultprefs.template.orientation",
072: "fav-left"));
073:
074: // FIXME: "editor" property does not get registered, may be related with http://bugs.jspwiki.org/show_bug.cgi?id=117
075: // disabling it until knowing why it's happening
076: // FIXME: editomanager reads jspwiki.editor -- which of both properties should continue
077: prefs.put("editor", TextUtil.getStringProperty(props,
078: "jspwiki.defaultprefs.template.editor", "plain"));
079:
080: parseJSONPreferences((HttpServletRequest) pageContext
081: .getRequest(), prefs);
082:
083: pageContext.getSession().setAttribute(SESSIONPREFS, prefs);
084: }
085:
086: /**
087: * Parses new-style preferences stored as JSON objects and stores them
088: * in the session. Everything in the cookie is stored.
089: *
090: * @param request
091: * @param prefs The default hashmap of preferences
092: *
093: */
094: private static void parseJSONPreferences(
095: HttpServletRequest request, Preferences prefs) {
096: //FIXME: urlDecodeUTF8 should better go in HttpUtil ??
097: String prefVal = TextUtil.urlDecodeUTF8(HttpUtil
098: .retrieveCookieValue(request, "JSPWikiUserPrefs"));
099:
100: if (prefVal != null) {
101: try {
102: JSONObject jo = new JSONObject(prefVal);
103:
104: for (Iterator i = jo.keys(); i.hasNext();) {
105: String key = TextUtil.replaceEntities((String) i
106: .next());
107: prefs.put(key, jo.getString(key));
108: }
109: } catch (ParseException e) {
110: }
111: }
112: }
113:
114: /**
115: * Returns a preference value programmatically.
116: * FIXME
117: *
118: * @param wikiContext
119: * @param name
120: * @return
121: */
122: public static String getPreference(WikiContext wikiContext,
123: String name) {
124: Preferences prefs = (Preferences) wikiContext.getHttpRequest()
125: .getSession().getAttribute(SESSIONPREFS);
126:
127: if (prefs != null)
128: return (String) prefs.get(name);
129:
130: return null;
131: }
132:
133: /**
134: * Returns a preference value programmatically.
135: * FIXME
136: *
137: * @param pageContext
138: * @param name
139: * @return
140: */
141: public static String getPreference(PageContext pageContext,
142: String name) {
143: Preferences prefs = (Preferences) pageContext.getSession()
144: .getAttribute(SESSIONPREFS);
145:
146: if (prefs != null)
147: return (String) prefs.get(name);
148:
149: return null;
150: }
151: }
|