private static void _populateThreadLocalsFromContext(Map<String, Serializable> context) {

    long companyId = GetterUtil.getLong(context.get("companyId"));

    if (companyId > 0) {
      CompanyThreadLocal.setCompanyId(companyId);
    }

    Locale defaultLocale = (Locale) context.get("defaultLocale");

    if (defaultLocale != null) {
      LocaleThreadLocal.setDefaultLocale(defaultLocale);
    }

    long groupId = GetterUtil.getLong(context.get("groupId"));

    if (groupId > 0) {
      GroupThreadLocal.setGroupId(groupId);
    }

    String principalName = GetterUtil.getString(context.get("principalName"));

    if (Validator.isNotNull(principalName)) {
      PrincipalThreadLocal.setName(principalName);
    }

    PermissionChecker permissionChecker = null;

    if (Validator.isNotNull(principalName)) {
      try {
        User user = UserLocalServiceUtil.fetchUser(PrincipalThreadLocal.getUserId());

        permissionChecker = PermissionCheckerFactoryUtil.create(user);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    if (permissionChecker != null) {
      PermissionThreadLocal.setPermissionChecker(permissionChecker);
    }

    String principalPassword = GetterUtil.getString(context.get("principalPassword"));

    if (Validator.isNotNull(principalPassword)) {
      PrincipalThreadLocal.setPassword(principalPassword);
    }

    Locale siteDefaultLocale = (Locale) context.get("siteDefaultLocale");

    if (siteDefaultLocale != null) {
      LocaleThreadLocal.setSiteDefaultLocale(siteDefaultLocale);
    }

    Locale themeDisplayLocale = (Locale) context.get("themeDisplayLocale");

    if (themeDisplayLocale != null) {
      LocaleThreadLocal.setThemeDisplayLocale(themeDisplayLocale);
    }
  }
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    if (PortalUtil.isMultipartRequest(request)) {
      UploadServletRequest uploadServletRequest = new UploadServletRequestImpl(request);

      request = uploadServletRequest;
    }

    String path = GetterUtil.getString(request.getPathInfo());

    if ((!path.equals(StringPool.BLANK) && !path.equals(StringPool.SLASH))
        || (request.getParameter("discover") != null)) {

      Locale locale = PortalUtil.getLocale(request, response, true);

      LocaleThreadLocal.setThemeDisplayLocale(locale);

      super.service(request, response);

      return;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Servlet context " + request.getContextPath());
    }

    String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";

    HttpSession session = request.getSession();

    ServletContext servletContext = session.getServletContext();

    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();

    try {
      AccessControlThreadLocal.setRemoteAccess(true);

      String contextPath = PortalContextLoaderListener.getPortalServletContextPath();

      if (servletContext.getContext(contextPath) != null) {
        if (!contextPath.equals(StringPool.SLASH) && apiPath.startsWith(contextPath)) {

          apiPath = apiPath.substring(contextPath.length());
        }

        RequestDispatcher requestDispatcher = request.getRequestDispatcher(apiPath);

        requestDispatcher.forward(request, response);
      } else {
        String servletContextPath = ContextPathUtil.getContextPath(servletContext);

        String redirectPath = "/api/jsonws?contextPath=" + HttpUtil.encodeURL(servletContextPath);

        response.sendRedirect(redirectPath);
      }
    } finally {
      AccessControlThreadLocal.setRemoteAccess(remoteAccess);
    }
  }
Esempio n. 3
0
  public void setLocale(Locale locale) {
    _locale = locale;

    LocaleThreadLocal.setThemeDisplayLocale(locale);
  }