List of usage examples for org.json JSONObject getString
public String getString(String key) throws JSONException
From source file:org.b3log.solo.processor.util.Filler.java
/** * Fills post comments recently.//ww w.j a v a2 s . c o m * * @param dataModel data model * @param preference the specified preference * @throws ServiceException service exception */ public void fillRecentComments(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException { Stopwatchs.start("Fill Recent Comments"); try { LOGGER.finer("Filling recent comments...."); final int recentCommentDisplayCnt = preference.getInt(Preference.RECENT_COMMENT_DISPLAY_CNT); final List<JSONObject> recentComments = commentRepository.getRecentComments(recentCommentDisplayCnt); for (final JSONObject comment : recentComments) { final String content = comment.getString(Comment.COMMENT_CONTENT) .replaceAll(SoloServletListener.ENTER_ESC, " "); comment.put(Comment.COMMENT_CONTENT, content); comment.put(Comment.COMMENT_NAME, StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_NAME))); comment.put(Comment.COMMENT_URL, StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_URL))); comment.remove(Comment.COMMENT_EMAIL); // Erases email for security reason } dataModel.put(Common.RECENT_COMMENTS, recentComments); } catch (final JSONException e) { LOGGER.log(Level.SEVERE, "Fills recent comments failed", e); throw new ServiceException(e); } catch (final RepositoryException e) { LOGGER.log(Level.SEVERE, "Fills recent comments failed", e); throw new ServiceException(e); } finally { Stopwatchs.end(); } }
From source file:org.b3log.solo.processor.util.Filler.java
/** * Fills footer.ftl./*from w w w . j av a 2 s . c o m*/ * * @param dataModel data model * @param preference the specified preference * @throws ServiceException service exception */ public void fillBlogFooter(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException { Stopwatchs.start("Fill Footer"); try { LOGGER.finer("Filling footer...."); final String blogTitle = preference.getString(Preference.BLOG_TITLE); dataModel.put(Preference.BLOG_TITLE, blogTitle); final String blogHost = preference.getString(Preference.BLOG_HOST); dataModel.put(Preference.BLOG_HOST, blogHost); 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))); dataModel.put(Keys.Server.STATIC_SERVER, Latkes.getStaticServer()); dataModel.put(Keys.Server.SERVER, Latkes.getServer()); // Activates plugins try { final ViewLoadEventData data = new ViewLoadEventData(); data.setViewName("footer.ftl"); data.setDataModel(dataModel); EventManager.getInstance() .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.WARNING, "Event[FREEMARKER_ACTION] handle failed, ignores this exception for kernel health", e); } } catch (final JSONException e) { LOGGER.log(Level.SEVERE, "Fills blog footer failed", e); throw new ServiceException(e); } finally { Stopwatchs.end(); } }
From source file:org.b3log.solo.processor.util.Filler.java
/** * Fills header.ftl.//w w w. j ava 2 s . co m * * @param request the specified HTTP servlet request * @param dataModel data model * @param preference the specified preference * @throws ServiceException service exception */ public void fillBlogHeader(final HttpServletRequest request, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException { Stopwatchs.start("Fill Header"); try { LOGGER.fine("Filling header...."); dataModel.put(Preference.ARTICLE_LIST_DISPLAY_COUNT, preference.getInt(Preference.ARTICLE_LIST_DISPLAY_COUNT)); dataModel.put(Preference.ARTICLE_LIST_PAGINATION_WINDOW_SIZE, preference.getInt(Preference.ARTICLE_LIST_PAGINATION_WINDOW_SIZE)); dataModel.put(Preference.LOCALE_STRING, preference.getString(Preference.LOCALE_STRING)); dataModel.put(Preference.BLOG_TITLE, preference.getString(Preference.BLOG_TITLE)); dataModel.put(Preference.BLOG_SUBTITLE, preference.getString(Preference.BLOG_SUBTITLE)); dataModel.put(Preference.HTML_HEAD, preference.getString(Preference.HTML_HEAD)); dataModel.put(Preference.META_KEYWORDS, preference.getString(Preference.META_KEYWORDS)); dataModel.put(Preference.META_DESCRIPTION, preference.getString(Preference.META_DESCRIPTION)); dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR))); final String noticeBoard = preference.getString(Preference.NOTICE_BOARD); dataModel.put(Preference.NOTICE_BOARD, noticeBoard); final Query query = new Query().setPageCount(1); final JSONObject result = userRepository.get(query); final JSONArray users = result.getJSONArray(Keys.RESULTS); final List<JSONObject> userList = CollectionUtils.jsonArrayToList(users); dataModel.put(User.USERS, userList); for (final JSONObject user : userList) { user.remove(User.USER_EMAIL); } final String skinDirName = (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME); dataModel.put(Skin.SKIN_DIR_NAME, skinDirName); Keys.fillServer(dataModel); Keys.fillRuntime(dataModel); fillMinified(dataModel); fillPageNavigations(dataModel); fillStatistic(dataModel); } catch (final JSONException e) { LOGGER.log(Level.SEVERE, "Fills blog header failed", e); throw new ServiceException(e); } catch (final RepositoryException e) { LOGGER.log(Level.SEVERE, "Fills blog header failed", e); throw new ServiceException(e); } finally { Stopwatchs.end(); } }
From source file:org.b3log.solo.processor.util.Filler.java
/** * Fills the specified template.//from w ww . j a v a 2s.c o m * * @param template the specified template * @param dataModel data model * @param preference the specified preference * @throws ServiceException service exception */ public void fillUserTemplate(final Template template, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException { Stopwatchs.start("Fill User Template[name=" + template.getName() + "]"); try { LOGGER.log(Level.FINE, "Filling user template[name{0}]", template.getName()); if (Templates.hasExpression(template, "<#list links as link>")) { fillLinks(dataModel); } if (Templates.hasExpression(template, "<#list recentComments as comment>")) { fillRecentComments(dataModel, preference); } if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) { fillMostUsedTags(dataModel, preference); } if (Templates.hasExpression(template, "<#list mostCommentArticles as article>")) { fillMostCommentArticles(dataModel, preference); } if (Templates.hasExpression(template, "<#list mostViewCountArticles as article>")) { fillMostViewCountArticles(dataModel, preference); } if (Templates.hasExpression(template, "<#list archiveDates as archiveDate>")) { fillArchiveDates(dataModel, preference); } final String noticeBoard = preference.getString(Preference.NOTICE_BOARD); dataModel.put(Preference.NOTICE_BOARD, noticeBoard); } catch (final JSONException e) { LOGGER.log(Level.SEVERE, "Fills user template failed", e); throw new ServiceException(e); } finally { Stopwatchs.end(); } }
From source file:org.b3log.solo.processor.util.Filler.java
/** * Sets some extra properties into the specified article with the specified author and preference, performs content and * abstract editor processing./*from w w w .j a v a2s .c om*/ * * <p> * Article ext properties: * <pre> * { * ...., * "authorName": "", * "authorId": "", * "hasUpdated": boolean * } * </pre> * </p> * * @param article the specified article * @param author the specified author * @param preference the specified preference * @throws ServiceException service exception * @see #setArticlesExProperties(java.util.List, org.json.JSONObject) */ private void setArticleExProperties(final JSONObject article, final JSONObject author, final JSONObject preference) throws ServiceException { try { final String authorName = author.getString(User.USER_NAME); article.put(Common.AUTHOR_NAME, authorName); final String authorId = author.getString(Keys.OBJECT_ID); article.put(Common.AUTHOR_ID, authorId); if (preference.getBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) { article.put(Common.HAS_UPDATED, articleUtils.hasUpdated(article)); } else { article.put(Common.HAS_UPDATED, false); } processArticleAbstract(preference, article); articleQueryService.markdown(article); } catch (final Exception e) { LOGGER.log(Level.SEVERE, "Sets article extra properties failed", e); throw new ServiceException(e); } }
From source file:org.b3log.solo.processor.util.Filler.java
/** * Sets some extra properties into the specified article with the specified preference, performs content and * abstract editor processing.//from w w w.j a v a 2s . com * * <p> * Article ext properties: * <pre> * { * ...., * "authorName": "", * "authorId": "", * "hasUpdated": boolean * } * </pre> * </p> * * @param article the specified article * @param preference the specified preference * @throws ServiceException service exception * @see #setArticlesExProperties(java.util.List, org.json.JSONObject) */ private void setArticleExProperties(final JSONObject article, final JSONObject preference) throws ServiceException { try { final JSONObject author = articleUtils.getAuthor(article); final String authorName = author.getString(User.USER_NAME); article.put(Common.AUTHOR_NAME, authorName); final String authorId = author.getString(Keys.OBJECT_ID); article.put(Common.AUTHOR_ID, authorId); if (preference.getBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) { article.put(Common.HAS_UPDATED, articleUtils.hasUpdated(article)); } else { article.put(Common.HAS_UPDATED, false); } processArticleAbstract(preference, article); articleQueryService.markdown(article); } catch (final Exception e) { LOGGER.log(Level.SEVERE, "Sets article extra properties failed", e); throw new ServiceException(e); } }
From source file:org.softcatala.corrector.LanguageToolParsing.java
public Suggestion[] GetSuggestions(String jsonText) { ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>(); try {/* www .java2 s .c om*/ JSONObject json = new JSONObject(jsonText); JSONArray matches = json.getJSONArray("matches"); for (int i = 0; i < matches.length(); i++) { JSONObject match = matches.getJSONObject(i); JSONArray replacements = match.getJSONArray("replacements"); JSONObject rule = match.getJSONObject("rule"); String ruleId = rule.getString("id"); // Since we process fragments we need to skip the upper case // suggestion if (ruleId.equals("UPPERCASE_SENTENCE_START") == true) continue; Suggestion suggestion = new Suggestion(); if (replacements.length() == 0) { String message = match.getString("message"); String msgText = String.format("(%s)", message); suggestion.Text = new String[] { msgText }; } else { ArrayList<String> list = new ArrayList<String>(); for (int r = 0; r < replacements.length(); r++) { JSONObject replacement = replacements.getJSONObject(r); String value = replacement.getString("value"); list.add(value); } suggestion.Text = list.toArray(new String[list.size()]); } suggestion.Position = match.getInt("offset"); suggestion.Length = match.getInt("length"); suggestions.add(suggestion); Log.d(TAG, "Request result: " + suggestion.Position + " Len:" + suggestion.Length); } } catch (Exception e) { Log.e(TAG, "GetSuggestions", e); } return suggestions.toArray(new Suggestion[0]); }
From source file:com.norman0406.slimgress.API.Item.ItemFlipCard.java
public ItemFlipCard(JSONArray json) throws JSONException { super(ItemType.FlipCard, json); JSONObject item = json.getJSONObject(2); JSONObject flipCard = item.getJSONObject("flipCard"); if (flipCard.getString("flipCardType").equals("JARVIS")) mVirusType = FlipCardType.Jarvis; else if (flipCard.getString("flipCardType").equals("ADA")) mVirusType = FlipCardType.Ada;//from w ww . j a v a2s. c om else System.out.println("unknown virus type"); }
From source file:com.bw.hawksword.wiktionary.ExtendedWikiHelper.java
/** * Query the Wiktionary API to pick a random dictionary word. Will try * multiple times to find a valid word before giving up. * * @return Random dictionary word, or null if no valid word was found. * @throws ApiException If any connection or server error occurs. * @throws ParseException If there are problems parsing the response. */// w w w.jav a 2 s.c om public static String getRandomWord() throws ApiException, ParseException { // Keep trying a few times until we find a valid word int tries = 0; while (tries++ < RANDOM_TRIES) { // Query the API for a random word String content = getUrlContent(WIKTIONARY_RANDOM); try { // Drill into the JSON response to find the returned word JSONObject response = new JSONObject(content); JSONObject query = response.getJSONObject("query"); JSONArray random = query.getJSONArray("random"); JSONObject word = random.getJSONObject(0); String foundWord = word.getString("title"); // If we found an actual word, and it wasn't rejected by our invalid // filter, then accept and return it. if (foundWord != null && !sInvalidWord.matcher(foundWord).find()) { return foundWord; } } catch (JSONException e) { throw new ParseException("Problem parsing API response", e); } } // No valid word found in number of tries, so return null return null; }
From source file:org.brickred.socialauth.provider.FourSquareImpl.java
private Profile getProfile() throws Exception { LOG.debug("Obtaining user profile"); Profile profile = new Profile(); Response serviceResponse;/*from ww w.ja v a 2s . c o m*/ try { serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL); } catch (Exception e) { throw new SocialAuthException("Failed to retrieve the user profile from " + PROFILE_URL, e); } String res; try { res = serviceResponse.getResponseBodyAsString(Constants.ENCODING); } catch (Exception exc) { throw new SocialAuthException("Failed to read response from " + PROFILE_URL, exc); } JSONObject jobj = new JSONObject(res); JSONObject rObj; JSONObject uObj; if (jobj.has("response")) { rObj = jobj.getJSONObject("response"); } else { throw new SocialAuthException("Failed to parse the user profile json : " + res); } if (rObj.has("user")) { uObj = rObj.getJSONObject("user"); } else { throw new SocialAuthException("Failed to parse the user profile json : " + res); } if (uObj.has("id")) { profile.setValidatedId(uObj.getString("id")); } if (uObj.has("firstName")) { profile.setFirstName(uObj.getString("firstName")); } if (uObj.has("lastName")) { profile.setLastName(uObj.getString("lastName")); } if (uObj.has("photo")) { profile.setProfileImageURL(uObj.getString("photo")); } if (uObj.has("gender")) { profile.setGender(uObj.getString("gender")); } if (uObj.has("homeCity")) { profile.setLocation(uObj.getString("homeCity")); } if (uObj.has("contact")) { JSONObject cobj = uObj.getJSONObject("contact"); if (cobj.has("email")) { profile.setEmail(cobj.getString("email")); } } profile.setProviderId(getProviderId()); userProfile = profile; return profile; }