use of org.json.JSONException in project realm-java by realm.
the class MainActivity method createTestUser.
private static SyncUser createTestUser(long expires) {
Token userToken = new Token(USER_TOKEN, "JohnDoe", null, expires, null);
Token accessToken = new Token(REALM_TOKEN, "JohnDoe", "/foo", expires, new Token.Permission[] { Token.Permission.DOWNLOAD });
ObjectServerUser.AccessDescription desc = new ObjectServerUser.AccessDescription(accessToken, "/data/data/myapp/files/default", false);
JSONObject obj = new JSONObject();
try {
JSONArray realmList = new JSONArray();
JSONObject realmDesc = new JSONObject();
realmDesc.put("uri", "realm://objectserver.realm.io/default");
realmDesc.put("description", desc.toJson());
realmList.put(realmDesc);
obj.put("authUrl", "http://objectserver.realm.io/auth");
obj.put("userToken", userToken.toJson());
obj.put("realms", realmList);
return SyncUser.fromJson(obj.toString());
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
use of org.json.JSONException in project databus by linkedin.
the class KeyFilterConfigJSONFactory method parseSrcIdFilterConfigMap.
/*
* Converts a JSON String to a map of SourceIds to DbusKeyFilter
*
* @param String containing JSON representation of DbusKeyFilter object
* @return Map of SourceIds to DbusKeyFilter
*
* @throws
* JSONException if JSON Parser is unable to parse the data
* IOException if there is any IO errors while parsing
*/
@SuppressWarnings("unchecked")
public static Map<Long, DbusKeyFilter> parseSrcIdFilterConfigMap(String s) throws JSONException, IOException {
HashMap<Long, DbusKeyFilter> filterConfigMap = new HashMap<Long, DbusKeyFilter>();
HashMap<Long, JSONObject> genericMap = new HashMap<Long, JSONObject>();
ObjectMapper mapper = new ObjectMapper();
try {
JSONObject obj2 = new JSONObject(s);
Iterator<String> itr = obj2.keys();
while (itr.hasNext()) {
String k = itr.next();
Long key = Long.valueOf(k);
genericMap.put(key, obj2.getJSONObject(key.toString()));
}
} catch (Exception ex) {
LOG.error("Got exception while parsing filterConfig", ex);
throw new JSONException(ex);
}
Iterator<Entry<Long, JSONObject>> itr = genericMap.entrySet().iterator();
while (itr.hasNext()) {
Entry<Long, JSONObject> obj = itr.next();
filterConfigMap.put(obj.getKey(), getDbusKeyFilter(obj.getValue(), mapper));
}
return filterConfigMap;
}
use of org.json.JSONException in project solo by b3log.
the class Filler method fillBlogFooter.
/**
* Fills footer.ftl.
*
* @param request the specified HTTP servlet request
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillBlogFooter(final HttpServletRequest request, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill Footer");
try {
LOGGER.debug("Filling footer....");
final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
dataModel.put(Option.ID_C_BLOG_TITLE, blogTitle);
dataModel.put("blogHost", Latkes.getServerHost() + ":" + Latkes.getServerPort());
dataModel.put(Common.VERSION, SoloServletListener.VERSION);
dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
String footerContent = "";
final JSONObject opt = optionQueryService.getOptionById(Option.ID_C_FOOTER_CONTENT);
if (null != opt) {
footerContent = opt.optString(Option.OPTION_VALUE);
}
dataModel.put(Option.ID_C_FOOTER_CONTENT, footerContent);
dataModel.put(Keys.Server.STATIC_SERVER, Latkes.getStaticServer());
dataModel.put(Keys.Server.SERVER, Latkes.getServer());
dataModel.put(Common.IS_INDEX, "/".equals(request.getRequestURI()));
dataModel.put(User.USER_NAME, "");
final JSONObject currentUser = userQueryService.getCurrentUser(request);
if (null != currentUser) {
final String userAvatar = currentUser.optString(UserExt.USER_AVATAR);
if (!Strings.isEmptyOrNull(userAvatar)) {
dataModel.put(Common.GRAVATAR, userAvatar);
} else {
final String email = currentUser.optString(User.USER_EMAIL);
final String gravatar = Thumbnails.getGravatarURL(email, "128");
dataModel.put(Common.GRAVATAR, gravatar);
}
dataModel.put(User.USER_NAME, currentUser.optString(User.USER_NAME));
}
// Activates plugins
try {
final ViewLoadEventData data = new ViewLoadEventData();
data.setViewName("footer.ftl");
data.setDataModel(dataModel);
eventManager.fireEventSynchronously(new Event<ViewLoadEventData>(Keys.FREEMARKER_ACTION, data));
if (Strings.isEmptyOrNull((String) dataModel.get(Plugin.PLUGINS))) {
// There is no plugin for this template, fill ${plugins} with blank.
dataModel.put(Plugin.PLUGINS, "");
}
} catch (final EventException e) {
LOGGER.log(Level.WARN, "Event[FREEMARKER_ACTION] handle failed, ignores this exception for kernel health", e);
}
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, "Fills blog footer failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.json.JSONException in project solo by b3log.
the class TopBars method getTopBarHTML.
/**
* Generates top bar HTML.
*
* @param request the specified request
* @param response the specified response
* @return top bar HTML
* @throws ServiceException service exception
*/
public String getTopBarHTML(final HttpServletRequest request, final HttpServletResponse response) throws ServiceException {
Stopwatchs.start("Gens Top Bar HTML");
try {
final Template topBarTemplate = ConsoleRenderer.TEMPLATE_CFG.getTemplate("top-bar.ftl");
final StringWriter stringWriter = new StringWriter();
final Map<String, Object> topBarModel = new HashMap<String, Object>();
userMgmtService.tryLogInWithCookie(request, response);
final JSONObject currentUser = userQueryService.getCurrentUser(request);
Keys.fillServer(topBarModel);
topBarModel.put(Common.IS_LOGGED_IN, false);
topBarModel.put(Common.IS_MOBILE_REQUEST, Requests.mobileRequest(request));
topBarModel.put("mobileLabel", langPropsService.get("mobileLabel"));
topBarModel.put("onlineVisitor1Label", langPropsService.get("onlineVisitor1Label"));
topBarModel.put(Common.ONLINE_VISITOR_CNT, statisticQueryService.getOnlineVisitorCount());
if (null == currentUser) {
topBarModel.put(Common.LOGIN_URL, userService.createLoginURL(Common.ADMIN_INDEX_URI));
topBarModel.put("loginLabel", langPropsService.get("loginLabel"));
topBarModel.put("registerLabel", langPropsService.get("registerLabel"));
topBarTemplate.process(topBarModel, stringWriter);
return stringWriter.toString();
}
topBarModel.put(Common.IS_LOGGED_IN, true);
topBarModel.put(Common.LOGOUT_URL, userService.createLogoutURL("/"));
topBarModel.put(Common.IS_ADMIN, Role.ADMIN_ROLE.equals(currentUser.getString(User.USER_ROLE)));
topBarModel.put(Common.IS_VISITOR, Role.VISITOR_ROLE.equals(currentUser.getString(User.USER_ROLE)));
topBarModel.put("adminLabel", langPropsService.get("adminLabel"));
topBarModel.put("logoutLabel", langPropsService.get("logoutLabel"));
final String userName = currentUser.getString(User.USER_NAME);
topBarModel.put(User.USER_NAME, userName);
topBarTemplate.process(topBarModel, stringWriter);
return stringWriter.toString();
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
throw new ServiceException(e);
} catch (final IOException e) {
LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
throw new ServiceException(e);
} catch (final TemplateException e) {
LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.json.JSONException in project solo by b3log.
the class ArticleRepositoryImpl method getNextArticle.
@Override
public JSONObject getNextArticle(final String articleId) throws RepositoryException {
final JSONObject currentArticle = get(articleId);
final Date currentArticleCreateDate = (Date) currentArticle.opt(Article.ARTICLE_CREATE_DATE);
final Query query = new Query().setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_CREATE_DATE, FilterOperator.GREATER_THAN, currentArticleCreateDate), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true))).addSort(Article.ARTICLE_CREATE_DATE, SortDirection.ASCENDING).setCurrentPageNum(1).setPageSize(1).setPageCount(1).addProjection(Article.ARTICLE_TITLE, String.class).addProjection(Article.ARTICLE_PERMALINK, String.class).addProjection(Article.ARTICLE_ABSTRACT, String.class);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (1 != array.length()) {
return null;
}
final JSONObject ret = new JSONObject();
final JSONObject article = array.optJSONObject(0);
try {
ret.put(Article.ARTICLE_TITLE, article.getString(Article.ARTICLE_TITLE));
ret.put(Article.ARTICLE_PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
ret.put(Article.ARTICLE_ABSTRACT, article.getString((Article.ARTICLE_ABSTRACT)));
} catch (final JSONException e) {
throw new RepositoryException(e);
}
return ret;
}
Aggregations