Beispiel #1
0
  public static void setRequestAttributes(
      ServletRequest request, Delegator delegator, ServletContext servletContext) {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    // check if multi tenant is enabled
    boolean useMultitenant = EntityUtil.isMultiTenantEnabled();
    if (useMultitenant) {
      // get tenant delegator by domain name
      String serverName = request.getServerName();
      try {
        // if tenant was specified, replace delegator with the new per-tenant delegator and set
        // tenantId to session attribute
        delegator = getDelegator(servletContext);

        // Use base delegator for fetching data from entity of entityGroup org.ofbiz.tenant
        Delegator baseDelegator = DelegatorFactory.getDelegator(delegator.getDelegatorBaseName());
        GenericValue tenantDomainName =
            EntityQuery.use(baseDelegator)
                .from("TenantDomainName")
                .where("domainName", serverName)
                .queryOne();

        if (UtilValidate.isNotEmpty(tenantDomainName)) {
          String tenantId = tenantDomainName.getString("tenantId");
          // make that tenant active, setup a new delegator and a new dispatcher
          String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId;
          httpRequest.getSession().setAttribute("delegatorName", tenantDelegatorName);

          // after this line the delegator is replaced with the new per-tenant delegator
          delegator = DelegatorFactory.getDelegator(tenantDelegatorName);
          servletContext.setAttribute("delegator", delegator);
        }

      } catch (GenericEntityException e) {
        Debug.logWarning(e, "Unable to get Tenant", module);
      }
    }

    // set the web context in the request for future use
    request.setAttribute("servletContext", httpRequest.getSession().getServletContext());
    request.setAttribute("delegator", delegator);

    // set the webSiteId in the session
    if (UtilValidate.isEmpty(httpRequest.getSession().getAttribute("webSiteId"))) {
      httpRequest
          .getSession()
          .setAttribute(
              "webSiteId", httpRequest.getSession().getServletContext().getAttribute("webSiteId"));
    }
  }