Search in sources :

Example 1 with OptionRepository

use of org.b3log.symphony.repository.OptionRepository in project symphony by b3log.

the class SymphonyServletListener method resolveSkinDir.

/**
 * Resolve skin (template) for the specified HTTP servlet request.
 *
 * @param request the specified HTTP servlet request
 */
private void resolveSkinDir(final HttpServletRequest request) {
    Stopwatchs.start("Resolve skin");
    request.setAttribute(Keys.TEMAPLTE_DIR_NAME, (Boolean) request.getAttribute(Common.IS_MOBILE) ? "mobile" : "classic");
    try {
        final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);
        final UserRepository userRepository = beanManager.getReference(UserRepository.class);
        final OptionRepository optionRepository = beanManager.getReference(OptionRepository.class);
        final JSONObject optionLang = optionRepository.get(Option.ID_C_MISC_LANGUAGE);
        final String optionLangValue = optionLang.optString(Option.OPTION_VALUE);
        if ("0".equals(optionLangValue)) {
            Locales.setLocale(request.getLocale());
        } else {
            Locales.setLocale(Locales.getLocale(optionLangValue));
        }
        JSONObject user = userQueryService.getCurrentUser(request);
        if (null == user) {
            final Cookie[] cookies = request.getCookies();
            if (null == cookies || 0 == cookies.length) {
                return;
            }
            try {
                for (final Cookie cookie : cookies) {
                    if (!Sessions.COOKIE_NAME.equals(cookie.getName())) {
                        continue;
                    }
                    final String value = Crypts.decryptByAES(cookie.getValue(), Symphonys.get("cookie.secret"));
                    if (StringUtils.isBlank(value)) {
                        break;
                    }
                    final JSONObject cookieJSONObject = new JSONObject(value);
                    final String userId = cookieJSONObject.optString(Keys.OBJECT_ID);
                    if (Strings.isEmptyOrNull(userId)) {
                        break;
                    }
                    user = userRepository.get(userId);
                    if (null == user) {
                        return;
                    } else {
                        break;
                    }
                }
            } catch (final Exception e) {
                LOGGER.log(Level.ERROR, "Read cookie failed", e);
            }
            if (null == user) {
                return;
            }
        }
        request.setAttribute(Keys.TEMAPLTE_DIR_NAME, (Boolean) request.getAttribute(Common.IS_MOBILE) ? user.optString(UserExt.USER_MOBILE_SKIN) : user.optString(UserExt.USER_SKIN));
        request.setAttribute(UserExt.USER_AVATAR_VIEW_MODE, user.optInt(UserExt.USER_AVATAR_VIEW_MODE));
        request.setAttribute(User.USER, user);
        final Locale locale = Locales.getLocale(user.optString(UserExt.USER_LANGUAGE));
        Locales.setLocale(locale);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Resolves skin failed", e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) UserRepository(org.b3log.symphony.repository.UserRepository) JSONObject(org.json.JSONObject) UserQueryService(org.b3log.symphony.service.UserQueryService) OptionRepository(org.b3log.symphony.repository.OptionRepository) ServiceException(org.b3log.latke.service.ServiceException)

Aggregations

Locale (java.util.Locale)1 Cookie (javax.servlet.http.Cookie)1 ServiceException (org.b3log.latke.service.ServiceException)1 OptionRepository (org.b3log.symphony.repository.OptionRepository)1 UserRepository (org.b3log.symphony.repository.UserRepository)1 UserQueryService (org.b3log.symphony.service.UserQueryService)1 JSONObject (org.json.JSONObject)1