use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class IndexProcessor method register.
/**
* Show register page.
*
* @param context the specified context
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
*/
@RequestProcessing(value = "/register", method = HTTPRequestMethod.GET)
public void register(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("register.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
try {
final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
dataModel.putAll(langs);
final JSONObject preference = preferenceQueryService.getPreference();
filler.fillBlogFooter(request, dataModel, preference);
filler.fillMinified(dataModel);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class Filler method fillStatistic.
/**
* Fills statistic.
*
* @param dataModel data model
* @throws ServiceException service exception
*/
private void fillStatistic(final Map<String, Object> dataModel) throws ServiceException {
Stopwatchs.start("Fill Statistic");
try {
LOGGER.debug("Filling statistic....");
final JSONObject statistic = statisticQueryService.getStatistic();
dataModel.put(Statistic.STATISTIC, statistic);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, "Fills statistic failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class Filler method fillMostCommentArticles.
/**
* Fills most comments articles.
*
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillMostCommentArticles(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill Most CMMTs Articles");
try {
LOGGER.debug("Filling most comment articles....");
final int mostCommentArticleDisplayCnt = preference.getInt(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT);
final List<JSONObject> mostCommentArticles = articleRepository.getMostCommentArticles(mostCommentArticleDisplayCnt);
dataModel.put(Common.MOST_COMMENT_ARTICLES, mostCommentArticles);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Fills most comment articles failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.b3log.latke.service.ServiceException 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.b3log.latke.service.ServiceException 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();
}
}
Aggregations