List of usage examples for org.apache.commons.lang StringUtils substringBetween
public static String substringBetween(String str, String open, String close)
Gets the String that is nested in between two Strings.
From source file:org.b3log.solo.processor.ArticleProcessor.java
/** * Gets relevant articles with the specified context. * //from ww w .ja va2 s.c o m * @param context the specified context * @param request the specified request * @param response the specified response * @throws Exception exception */ @RequestProcessing(value = "/article/id/*/relevant/articles", method = HTTPRequestMethod.GET) public void getRelevantArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception { final JSONObject jsonObject = new JSONObject(); final JSONObject preference = preferenceQueryService.getPreference(); final int displayCnt = preference.getInt(Preference.RELEVANT_ARTICLES_DISPLAY_CNT); if (0 == displayCnt) { jsonObject.put(Common.RANDOM_ARTICLES, new ArrayList<JSONObject>()); final JSONRenderer renderer = new JSONRenderer(); context.setRenderer(renderer); renderer.setJSONObject(jsonObject); return; } Stopwatchs.start("Get Relevant Articles"); final String requestURI = request.getRequestURI(); final String articleId = StringUtils.substringBetween(requestURI, "/article/id/", "/relevant/articles"); if (Strings.isEmptyOrNull(articleId)) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } final JSONObject article = articleQueryService.getArticleById(articleId); if (null == article) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } final List<JSONObject> relevantArticles = articleQueryService.getRelevantArticles(article, preference); jsonObject.put(Common.RELEVANT_ARTICLES, relevantArticles); final JSONRenderer renderer = new JSONRenderer(); context.setRenderer(renderer); renderer.setJSONObject(jsonObject); Stopwatchs.end(); }
From source file:org.b3log.solo.processor.console.AdminConsole.java
/** * Shows administrator functions with the specified context. * //from w w w. j av a 2s. co m * @param request the specified request * @param context the specified context */ @RequestProcessing(value = { "/admin-article.do", "/admin-article-list.do", "/admin-comment-list.do", "/admin-link-list.do", "/admin-page-list.do", "/admin-others.do", "/admin-draft-list.do", "/admin-user-list.do", "/admin-plugin-list.do", "/admin-main.do", "/admin-about.do" }, method = HTTPRequestMethod.GET) public void showAdminFunctions(final HttpServletRequest request, final HTTPRequestContext context) { final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer(); context.setRenderer(renderer); final String requestURI = request.getRequestURI(); final String templateName = StringUtils.substringBetween(requestURI, Latkes.getContextPath() + '/', ".") + ".ftl"; LOGGER.log(Level.FINEST, "Admin function[templateName={0}]", templateName); renderer.setTemplateName(templateName); final Locale locale = Latkes.getLocale(); final Map<String, String> langs = langPropsService.getAll(locale); final Map<String, Object> dataModel = renderer.getDataModel(); dataModel.putAll(langs); Keys.fillServer(dataModel); Keys.fillRuntime(dataModel); dataModel.put(Preference.LOCALE_STRING, locale.toString()); fireFreeMarkerActionEvent(templateName, dataModel); }
From source file:org.b3log.symphony.processor.ChatRoomProcessor.java
/** * XiaoV replies Stm./*from w w w. ja v a 2s . co m*/ * * @param context the specified context * @param request the specified request * @param response the specified response * @throws IOException io exception * @throws ServletException servlet exception */ @RequestProcessing(value = "/cron/xiaov", method = HTTPRequestMethod.GET) public void xiaoVReply(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { context.renderJSON(); try { final JSONObject xiaoV = userQueryService.getUserByName(TuringQueryService.ROBOT_NAME); if (null == xiaoV) { return; } final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE); final String xiaoVUserId = xiaoV.optString(Keys.OBJECT_ID); final String xiaoVEmail = xiaoV.optString(User.USER_EMAIL); final JSONObject result = notificationQueryService.getAtNotifications(avatarViewMode, xiaoVUserId, 1, 1); // Just get the latest one final List<JSONObject> notifications = (List<JSONObject>) result.get(Keys.RESULTS); for (final JSONObject notification : notifications) { if (notification.optBoolean(Notification.NOTIFICATION_HAS_READ)) { continue; } String xiaoVSaid = ""; final JSONObject comment = new JSONObject(); String articleId = StringUtils.substringBetween(notification.optString(Common.URL), "/article/", "#"); String q = ""; if (!StringUtils.isBlank(articleId)) { final String originalCmtId = StringUtils.substringAfter(notification.optString(Common.URL), "#"); final JSONObject originalCmt = commentQueryService.getCommentById(avatarViewMode, originalCmtId); q = originalCmt.optString(Comment.COMMENT_CONTENT); } else { articleId = StringUtils.substringAfter(notification.optString(Common.URL), "/article/"); final JSONObject article = articleQueryService.getArticleById(avatarViewMode, articleId); q = article.optString(Article.ARTICLE_CONTENT); } if (StringUtils.isNotBlank(q)) { q = Jsoup.parse(q).text(); if (!StringUtils.contains(q, "@" + TuringQueryService.ROBOT_NAME + " ")) { continue; } q = StringUtils.replace(q, "@" + TuringQueryService.ROBOT_NAME + " ", ""); xiaoVSaid = turingQueryService.chat(articleId, q); comment.put(Comment.COMMENT_CONTENT, xiaoVSaid); comment.put(Comment.COMMENT_AUTHOR_ID, xiaoVUserId); comment.put(Comment.COMMENT_AUTHOR_EMAIL, xiaoVEmail); comment.put(Comment.COMMENT_ON_ARTICLE_ID, articleId); xiaoV.remove(UserExt.USER_T_POINT_CC); xiaoV.remove(UserExt.USER_T_POINT_HEX); comment.put(Comment.COMMENT_T_COMMENTER, xiaoV); commentMgmtService.addComment(comment); } notificationMgmtService.makeRead(notification); } context.renderTrueResult(); } catch (final Exception e) { LOGGER.log(Level.ERROR, "Update user latest comment time failed", e); } }
From source file:org.b3log.symphony.service.BookQueryService.java
/** * Get shared books by the specified request json object. * * @param requestJSONObject the specified request json object, for example * "paginationCurrentPageNum": 1, * "paginationPageSize": 20, * "paginationWindowSize": 10 * @param articleFields the specified article fields to return * @return for example, <pre> * {//from w w w .j a v a 2 s . c o m * "pagination": { * "paginationPageCount": 100, * "paginationPageNums": [1, 2, 3, 4, 5] * }, * "articles": [{ * "oId": "", * "articleTitle": "", * "articleContent": "", * .... * }, ....] * } * </pre> * @throws ServiceException service exception * @see Pagination */ public JSONObject getSharedBooks(final JSONObject requestJSONObject, final Map<String, Class<?>> articleFields) throws ServiceException { final JSONObject ret = new JSONObject(); final int currentPageNum = requestJSONObject.optInt(Pagination.PAGINATION_CURRENT_PAGE_NUM); final int pageSize = requestJSONObject.optInt(Pagination.PAGINATION_PAGE_SIZE); final int windowSize = requestJSONObject.optInt(Pagination.PAGINATION_WINDOW_SIZE); final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize) .addSort(Keys.OBJECT_ID, SortDirection.DESCENDING).setFilter(new PropertyFilter( Article.ARTICLE_TYPE, FilterOperator.EQUAL, Article.ARTICLE_TYPE_C_BOOK)); for (final Map.Entry<String, Class<?>> articleField : articleFields.entrySet()) { query.addProjection(articleField.getKey(), articleField.getValue()); } JSONObject result = null; try { result = articleRepository.get(query); } catch (final RepositoryException e) { LOGGER.log(Level.ERROR, "Gets articles failed", e); throw new ServiceException(e); } final int pageCount = result.optJSONObject(Pagination.PAGINATION).optInt(Pagination.PAGINATION_PAGE_COUNT); final JSONObject pagination = new JSONObject(); ret.put(Pagination.PAGINATION, pagination); final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize); pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount); pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums); final JSONArray data = result.optJSONArray(Keys.RESULTS); final List<JSONObject> articles = CollectionUtils.<JSONObject>jsonArrayToList(data); try { articleQueryService.organizeArticles(UserExt.USER_AVATAR_VIEW_MODE_C_STATIC, articles); } catch (final RepositoryException e) { LOGGER.log(Level.ERROR, "Organizes articles failed", e); throw new ServiceException(e); } final List<JSONObject> retArticles = new ArrayList<>(); for (final JSONObject article : articles) { final String articleId = article.optString(Keys.OBJECT_ID); final JSONObject retArticle = new JSONObject(); retArticle.put(Article.ARTICLE_T_ID, articleId); retArticle.put(Article.ARTICLE_TITLE, StringUtils.substringBetween(article.optString(Article.ARTICLE_TITLE), "", "")); retArticle.put(Article.ARTICLE_T_AUTHOR_THUMBNAIL_URL, article.optString(Article.ARTICLE_T_AUTHOR_THUMBNAIL_URL + "48")); try { final JSONObject userBookArticleRel = userBookArticleRepository.getByArticleId(articleId); final String bookId = userBookArticleRel.optString(Book.BOOK_T_ID); final JSONObject book = bookRepository.get(bookId); retArticle.put(Book.BOOK_ISBN13, book.optString(Book.BOOK_ISBN13)); } catch (final Exception e) { LOGGER.log(Level.ERROR, "Get user book article rel failed [articleId=" + articleId + "]", e); continue; } retArticles.add(retArticle); } ret.put(Article.ARTICLES, retArticles); return ret; }
From source file:org.b3log.symphony.service.ShortLinkQueryService.java
/** * Processes article short link (article id). * * @param content the specified content/*from w ww. ja v a2 s . co m*/ * @return processed content */ public String linkArticle(final String content) { final Matcher matcher = ID_PATTERN.matcher(content); final StringBuffer contentBuilder = new StringBuffer(); try { while (matcher.find()) { final String linkId = StringUtils.substringBetween(matcher.group(), "[", "]"); final Query query = new Query().addProjection(Article.ARTICLE_TITLE, String.class) .setFilter(new PropertyFilter(Keys.OBJECT_ID, FilterOperator.EQUAL, linkId)); final JSONArray results = articleRepository.get(query).optJSONArray(Keys.RESULTS); if (0 == results.length()) { continue; } final JSONObject linkArticle = results.optJSONObject(0); final String linkTitle = linkArticle.optString(Article.ARTICLE_TITLE); final String link = " [" + linkTitle + "](" + Latkes.getServePath() + "/article/" + linkId + ") "; matcher.appendReplacement(contentBuilder, link); } matcher.appendTail(contentBuilder); } catch (final RepositoryException e) { LOGGER.log(Level.ERROR, "Generates article link error", e); } return contentBuilder.toString(); }
From source file:org.b3log.symphony.service.ShortLinkQueryService.java
/** * Processes tag short link (tag id)./* w ww . ja v a 2s .c o m*/ * * @param content the specified content * @return processed content */ public String linkTag(final String content) { final Matcher matcher = TAG_TITLE_PATTERN.matcher(content); final StringBuffer contentBuilder = new StringBuffer(); try { while (matcher.find()) { final String linkTagTitle = StringUtils.substringBetween(matcher.group(), "[", "]"); final Query query = new Query().addProjection(Tag.TAG_TITLE, String.class) .setFilter(new PropertyFilter(Tag.TAG_TITLE, FilterOperator.EQUAL, linkTagTitle)); final JSONArray results = tagRepository.get(query).optJSONArray(Keys.RESULTS); if (0 == results.length()) { continue; } final JSONObject linkTag = results.optJSONObject(0); final String linkTitle = linkTag.optString(Tag.TAG_TITLE); final String link = " [" + linkTitle + "](" + Latkes.getServePath() + "/tags/" + linkTitle + ") "; matcher.appendReplacement(contentBuilder, link); } matcher.appendTail(contentBuilder); } catch (final RepositoryException e) { LOGGER.log(Level.ERROR, "Generates tag link error", e); } return contentBuilder.toString(); }
From source file:org.b3log.symphony.util.MP3Players.java
/** * Renders the specified content with MP3 player if need. * * @param content the specified content/*from w ww.j a v a 2 s .c o m*/ * @return rendered content */ public static final String render(final String content) { final StringBuffer contentBuilder = new StringBuffer(); final Matcher m = PATTERN.matcher(content); while (m.find()) { String mp3URL = m.group(); String mp3Name = StringUtils.substringBetween(mp3URL, "\">", ".mp3</a>"); mp3URL = StringUtils.substringBetween(mp3URL, "href=\"", "\" rel="); m.appendReplacement(contentBuilder, "<div class=\"aplayer content-audio\" data-title=\"" + mp3Name + "\" data-url=\"" + mp3URL + "\" ></div>\n"); } m.appendTail(contentBuilder); return contentBuilder.toString(); }
From source file:org.b3log.symphony.util.VideoPlayers.java
/** * Renders the specified content with video player if need. * * @param content the specified content// w ww .ja v a 2s . c o m * @return rendered content */ public static final String render(final String content) { final LatkeBeanManager beanManager = Lifecycle.getBeanManager(); final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class); final StringBuffer contentBuilder = new StringBuffer(); final Matcher m = PATTERN.matcher(content); while (m.find()) { String videoURL = m.group(); videoURL = StringUtils.substringBetween(videoURL, "href=\"", "\" rel="); m.appendReplacement(contentBuilder, "<video src=\"" + videoURL + "\" controls=\"controls\">" + langPropsService.get("notSupportPlayLabel") + "</video>\n"); } m.appendTail(contentBuilder); return contentBuilder.toString(); }
From source file:org.bdval.BDVModel.java
/** * Load the BDVModel training platform from the specified zip file. * * @param zipFile The file to read the platform from * @return A populated platform object/*from w w w . ja va 2 s.co m*/ * @throws IOException if there is a problem reading from the file */ private GEOPlatformIndexed loadPlatform(final ZipFile zipFile) throws IOException { final String platformEntryName = FilenameUtils.getName(platformFilename); final Map<String, java.util.Properties> propertyMap = new HashMap<String, java.util.Properties>(); final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final String entryName = entry.getName(); if (entryName.startsWith(platformEntryName)) { // we have a platform entry final String propertyName = StringUtils.substringBetween(entryName, platformEntryName + ".", "." + ModelFileExtension.properties.toString()); final java.util.Properties properties = new java.util.Properties(); properties.load(zipFile.getInputStream(entry)); propertyMap.put(propertyName, properties); } } return new GEOPlatformIndexed(propertyMap); }
From source file:org.beanfuse.bean.comparators.PropertyComparator.java
/** * new OrderedBeanComparator("id") or<br> * new OrderedBeanComparator("name desc"); new * OrderedBeanComparator("[0].name desc"); * /* w ww . j a v a2 s .c om*/ * @param cmpStr */ public PropertyComparator(final String cmpStr) { if (StringUtils.isEmpty(cmpStr)) { return; } if (StringUtils.contains(cmpStr, ',')) { throw new RuntimeException( "PropertyComparator don't suport comma based order by." + " Use MultiPropertyComparator "); } cmpWhat = cmpStr.trim(); // ?[]? if ('[' == cmpWhat.charAt(0)) { index = NumberUtils.toInt(StringUtils.substringBetween(cmpWhat, "[", "]")); cmpWhat = StringUtils.substringAfter(cmpWhat, "]"); if ('.' == cmpWhat.charAt(0)) { cmpWhat = cmpWhat.substring(1); } } // ?? asc = true; if (StringUtils.contains(cmpWhat, ' ')) { if (StringUtils.contains(cmpWhat, " desc")) { asc = false; } cmpWhat = cmpWhat.substring(0, cmpWhat.indexOf(' ')); } stringComparator = new CollatorStringComparator(asc); }