/** * @param json * @return UserPrefs, if any are set for this request. * @throws JSONException */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(JSONObject json) throws JSONException { JSONObject prefs = json.optJSONObject("prefs"); if (prefs == null) { return null; } Map<String, String> p = Maps.newHashMap(); Iterator i = prefs.keys(); while (i.hasNext()) { String key = (String)i.next(); p.put(key, prefs.getString(key)); } return new UserPrefs(p); } }
public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml(value)); } } }
@Override public Object getValue(ELContext context, Object base, Object property) { if (base == null) { if (VIEW_PARAMS_PROPERTY.equals(property)) { context.setPropertyResolved(true); String params = gadgetContext.getParameter("view-params"); if (params != null && !"".equals(params)) { try { // TODO: immutable? return new JSONObject(params); } catch (JSONException e) { throw new ELException(e); } } // Return an empty map - this doesn't allocate anything, whereas an // empty JSONObject would return ImmutableMap.of(); } else if (USER_PREFS_PROPERTY.equals(property)) { context.setPropertyResolved(true); // TODO: immutable? return gadgetContext.getUserPrefs().getPrefs(); } } return null; }
/** * @param json * @return UserPrefs, if any are set for this request. * @throws JSONException */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(JSONObject json) throws JSONException { JSONObject prefs = json.optJSONObject("prefs"); if (prefs == null) { return null; } Map<String, String> p = Maps.newHashMap(); Iterator i = prefs.keys(); while (i.hasNext()) { String key = (String)i.next(); p.put(key, prefs.getString(key)); } return new UserPrefs(p); } }
public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml4(value)); } } }
@Override public Object getValue(ELContext context, Object base, Object property) { if (base == null) { if (VIEW_PARAMS_PROPERTY.equals(property)) { context.setPropertyResolved(true); String params = gadgetContext.getParameter("view-params"); if (params != null && !"".equals(params)) { try { // TODO: immutable? return new JSONObject(params); } catch (JSONException e) { throw new ELException(e); } } // Return an empty map - this doesn't allocate anything, whereas an // empty JSONObject would return ImmutableMap.of(); } else if (USER_PREFS_PROPERTY.equals(property)) { context.setPropertyResolved(true); // TODO: immutable? return gadgetContext.getUserPrefs().getPrefs(); } } return null; }
/** * @param json * @return UserPrefs, if any are set for this request. * @throws JSONException */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(JSONObject json) throws JSONException { JSONObject prefs = json.optJSONObject("prefs"); if (prefs == null) { return null; } Map<String, String> p = Maps.newHashMap(); Iterator i = prefs.keys(); while (i.hasNext()) { String key = (String)i.next(); p.put(key, prefs.getString(key)); } return new UserPrefs(p); } }
public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml(value)); } } }
@Override public Object getValue(ELContext context, Object base, Object property) { if (base == null) { if (VIEW_PARAMS_PROPERTY.equals(property)) { context.setPropertyResolved(true); String params = gadgetContext.getParameter("view-params"); if (params != null && !"".equals(params)) { try { // TODO: immutable? return new JSONObject(params); } catch (JSONException e) { throw new ELException(e); } } // Return an empty map - this doesn't allocate anything, whereas an // empty JSONObject would return ImmutableMap.of(); } else if (USER_PREFS_PROPERTY.equals(property)) { context.setPropertyResolved(true); // TODO: immutable? return gadgetContext.getUserPrefs().getPrefs(); } } return null; }
/** * @param req * @return UserPrefs, if any are set for this request. */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(HttpServletRequest req) { Map<String, String> prefs = Maps.newHashMap(); Enumeration<String> paramNames = req.getParameterNames(); if (paramNames == null) { return null; } while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if (paramName.startsWith(USERPREF_PARAM_PREFIX)) { String prefName = paramName.substring(USERPREF_PARAM_PREFIX.length()); prefs.put(prefName, req.getParameter(paramName)); } } return new UserPrefs(prefs); }
protected void addAllUserPrefs(UriBuilder uri, Gadget gadget, View view, boolean useTpl) { GadgetContext context = gadget.getContext(); UserPrefs prefs = context.getUserPrefs(); for (UserPref up : gadget.getSpec().getUserPrefs().values()) { String name = up.getName(); String data = prefs.getPref(name); if (data == null) { data = up.getDefaultValue(); } boolean upInFragment = !view.needsUserPrefSubstitution(); addParam(uri, UriCommon.USER_PREF_PREFIX + up.getName(), data, useTpl, upInFragment); } }
/** * @param req * @return UserPrefs, if any are set for this request. */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(HttpServletRequest req) { Map<String, String> prefs = Maps.newHashMap(); Enumeration<String> paramNames = req.getParameterNames(); if (paramNames == null) { return null; } while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if (paramName.startsWith(USERPREF_PARAM_PREFIX)) { String prefName = paramName.substring(USERPREF_PARAM_PREFIX.length()); prefs.put(prefName, req.getParameter(paramName)); } } return new UserPrefs(prefs); }
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
/** * @param req * @return UserPrefs, if any are set for this request. */ @SuppressWarnings("unchecked") private static UserPrefs getUserPrefs(HttpServletRequest req) { Map<String, String> prefs = Maps.newHashMap(); Enumeration<String> paramNames = req.getParameterNames(); if (paramNames == null) { return null; } while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); if (paramName.startsWith(USERPREF_PARAM_PREFIX)) { String prefName = paramName.substring(USERPREF_PARAM_PREFIX.length()); prefs.put(prefName, req.getParameter(paramName)); } } return new UserPrefs(prefs); }
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
@Override public UserPrefs getUserPrefs() { return new UserPrefs(ImmutableMap.of("foo", "shindig")); } };
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
@Override public UserPrefs getUserPrefs() { return new UserPrefs(ImmutableMap.of("foo", "shindig")); } };
for (UserPref up : gadget.getSpec().getUserPrefs().values()) { String name = up.getName(); String data = prefs.getPref(name); if (data == null) { data = up.getDefaultValue();
@Override public UserPrefs getUserPrefs() { return new UserPrefs(ImmutableMap.of("foo", "shindig")); } };