@Override
  public void updateCookie(
      HttpServletRequest request, HttpServletResponse response, Locale locale) {

    String languageId = LocaleUtil.toLanguageId(locale);

    Cookie languageIdCookie = new Cookie(CookieKeys.GUEST_LANGUAGE_ID, languageId);

    languageIdCookie.setPath(StringPool.SLASH);
    languageIdCookie.setMaxAge(CookieKeys.MAX_AGE);

    CookieKeys.addCookie(request, response, languageIdCookie);
  }
  public static List<BreadcrumbEntry> getPortletBreadcrumbEntries(HttpServletRequest request) {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

    String name = WebKeys.PORTLET_BREADCRUMBS;

    List<BreadcrumbEntry> breadcrumbEntries = (List<BreadcrumbEntry>) request.getAttribute(name);

    if (Validator.isNotNull(portletDisplay.getId())
        && !portletDisplay.isFocused()
        && !Validator.equals(
            portletDisplay.getId(),
            PortletProviderUtil.getPortletId(
                BreadcrumbEntry.class.getName(), PortletProvider.Action.VIEW))) {

      name = name.concat(StringPool.UNDERLINE.concat(portletDisplay.getId()));

      List<BreadcrumbEntry> portletBreadcrumbEntries =
          (List<BreadcrumbEntry>) request.getAttribute(name);

      if (portletBreadcrumbEntries != null) {
        breadcrumbEntries = portletBreadcrumbEntries;
      }
    }

    if (breadcrumbEntries == null) {
      return Collections.emptyList();
    }

    for (int i = 0; i < breadcrumbEntries.size() - 1; i++) {
      BreadcrumbEntry portletBreadcrumbEntry = breadcrumbEntries.get(i);

      String url = portletBreadcrumbEntry.getURL();

      if (Validator.isNotNull(url) && !CookieKeys.hasSessionId(request)) {
        HttpSession session = request.getSession();

        portletBreadcrumbEntry.setURL(PortalUtil.getURLWithSessionId(url, session.getId()));
      }
    }

    return breadcrumbEntries;
  }
  private String _getURL(
      HttpServletRequest request, boolean resetMaxState, boolean resetRenderParameters)
      throws PortalException, SystemException {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    if (resetMaxState) {
      Layout layout = themeDisplay.getLayout();

      LayoutTypePortlet layoutTypePortlet = null;

      if (layout.equals(this)) {
        layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
      } else {
        try {
          layoutTypePortlet = _getLayoutTypePortletClone(request);
        } catch (IOException ioe) {
          _log.error("Unable to clone layout settings", ioe);

          layoutTypePortlet = (LayoutTypePortlet) getLayoutType();
        }
      }

      if (layoutTypePortlet.hasStateMax()) {
        String portletId = StringUtil.split(layoutTypePortlet.getStateMax())[0];

        PortletURLImpl portletURLImpl =
            new PortletURLImpl(request, portletId, getPlid(), PortletRequest.ACTION_PHASE);

        try {
          portletURLImpl.setWindowState(WindowState.NORMAL);
          portletURLImpl.setPortletMode(PortletMode.VIEW);
        } catch (PortletException pe) {
          throw new SystemException(pe);
        }

        portletURLImpl.setAnchor(false);

        if (PropsValues.LAYOUT_DEFAULT_P_L_RESET && !resetRenderParameters) {

          portletURLImpl.setParameter("p_l_reset", "0");
        } else if (!PropsValues.LAYOUT_DEFAULT_P_L_RESET && resetRenderParameters) {

          portletURLImpl.setParameter("p_l_reset", "1");
        }

        return portletURLImpl.toString();
      }
    }

    String portalURL = PortalUtil.getPortalURL(request);

    String url = PortalUtil.getLayoutURL(this, themeDisplay);

    if (!CookieKeys.hasSessionId(request)
        && (url.startsWith(portalURL) || url.startsWith(StringPool.SLASH))) {

      url = PortalUtil.getURLWithSessionId(url, request.getSession().getId());
    }

    if (!resetMaxState) {
      return url;
    }

    if (PropsValues.LAYOUT_DEFAULT_P_L_RESET && !resetRenderParameters) {
      url = HttpUtil.addParameter(url, "p_l_reset", 0);
    } else if (!PropsValues.LAYOUT_DEFAULT_P_L_RESET && resetRenderParameters) {

      url = HttpUtil.addParameter(url, "p_l_reset", 1);
    }

    return url;
  }
  private long _getCompanyId(HttpServletRequest request) {
    if (_log.isDebugEnabled()) {
      _log.debug("Get company id");
    }

    Long companyIdObj = (Long) request.getAttribute(WebKeys.COMPANY_ID);

    if (_log.isDebugEnabled()) {
      _log.debug("Company id from request " + companyIdObj);
    }

    if (companyIdObj != null) {
      return companyIdObj.longValue();
    }

    long companyId = _getCompanyIdByVirtualHosts(request);

    if (_log.isDebugEnabled()) {
      _log.debug("Company id from host " + companyId);
    }

    if (companyId <= 0) {
      long cookieCompanyId =
          GetterUtil.getLong(CookieKeys.getCookie(request, CookieKeys.COMPANY_ID, false));

      if (cookieCompanyId > 0) {
        try {
          CompanyLocalServiceUtil.getCompanyById(cookieCompanyId);

          companyId = cookieCompanyId;

          if (_log.isDebugEnabled()) {
            _log.debug("Company id from cookie " + companyId);
          }
        } catch (NoSuchCompanyException nsce) {
          if (_log.isWarnEnabled()) {
            _log.warn("Company id from cookie " + cookieCompanyId + " does not exist");
          }
        } catch (Exception e) {
          _log.error(e, e);
        }
      }
    }

    if (companyId <= 0) {
      companyId = _getDefaultCompanyId();

      if (_log.isDebugEnabled()) {
        _log.debug("Default company id " + companyId);
      }
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Set company id " + companyId);
    }

    request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));

    CompanyThreadLocal.setCompanyId(companyId);

    if (Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME)
        && (request.getAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET) == null)) {

      try {
        Group group =
            GroupLocalServiceUtil.getGroup(companyId, PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);

        LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(group.getGroupId(), false);

        if (Validator.isNull(layoutSet.getVirtualHostname())) {
          request.setAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
        }
      } catch (Exception e) {
        _log.error(e, e);
      }
    }

    return companyId;
  }