next to setting of the tenent id, now also the initial mountpoint if entered also refactoring:

- set delegator, dispatcher and security from session, servlet context or request's attribute to app context of birt engine before render and send email
- add the initialPath field to Tenant entity
- create the tenant context which as a default servlet using org.ofbiz.webapp.control.TenantServlet in the catalina container and check the multitenant property for initial the tenant context with root mount /
- move the changing a multi tenant delegator statement from ControlServlet to ContextFilter that could apply to every servlets
(implementation by Chattree Richard)


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1032472 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hans Bakker
2010-11-08 06:54:17 +00:00
parent 6abf950e9d
commit eaa8f6b5d4
11 changed files with 250 additions and 43 deletions
@@ -23,6 +23,11 @@ import java.sql.SQLException;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.eclipse.birt.report.engine.api.EXCELRenderOption;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
@@ -36,7 +41,11 @@ import org.eclipse.birt.report.engine.api.RenderOption;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.birt.container.BirtContainer;
import org.ofbiz.entity.Delegator;
import org.ofbiz.security.Security;
import org.ofbiz.service.LocalDispatcher;
public class BirtWorker {
@@ -138,4 +147,45 @@ public class BirtWorker {
task.run();
task.close();
}
public static void setWebContextObjects(IReportEngine engine, HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
ServletContext servletContext = session.getServletContext();
// set delegator
Delegator delegator = (Delegator) session.getAttribute("delegator");
if (UtilValidate.isEmpty(delegator)) {
delegator = (Delegator) servletContext.getAttribute("delegator");
}
if (UtilValidate.isEmpty(delegator)) {
delegator = (Delegator) request.getAttribute("delegator");
}
if (UtilValidate.isNotEmpty(delegator)) {
engine.getConfig().getAppContext().put("delegator", delegator);
}
// set delegator
LocalDispatcher dispatcher = (LocalDispatcher) session.getAttribute("dispatcher");
if (UtilValidate.isEmpty(dispatcher)) {
dispatcher = (LocalDispatcher) servletContext.getAttribute("dispatcher");
}
if (UtilValidate.isEmpty(dispatcher)) {
dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
}
if (UtilValidate.isNotEmpty(dispatcher)) {
engine.getConfig().getAppContext().put("dispatcher", dispatcher);
}
// set security
Security security = (Security) session.getAttribute("security");
if (UtilValidate.isEmpty(security)) {
security = (Security) servletContext.getAttribute("security");
}
if (UtilValidate.isEmpty(security)) {
security = (Security) request.getAttribute("security");
}
if (UtilValidate.isNotEmpty(security)) {
engine.getConfig().getAppContext().put("security", dispatcher);
}
}
}
@@ -46,6 +46,8 @@ import org.ofbiz.base.util.string.FlexibleStringExpander;
import org.ofbiz.birt.BirtWorker;
import org.ofbiz.birt.container.BirtContainer;
import org.ofbiz.common.email.NotificationServices;
import org.ofbiz.entity.Delegator;
import org.ofbiz.security.Security;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
@@ -69,7 +71,10 @@ public class BirtEmailServices {
*/
public static Map<String, Object> sendBirtMail(DispatchContext ctx, Map<String, ? extends Object> context) {
Map<String, Object> serviceContext = UtilMisc.makeMapWritable(context);
Delegator delegator = ctx.getDelegator();
LocalDispatcher dispatcher = ctx.getDispatcher();
Security security = ctx.getSecurity();
String webSiteId = (String) serviceContext.remove("webSiteId");
String bodyText = (String) serviceContext.remove("bodyText");
String bodyScreenUri = (String) serviceContext.remove("bodyScreenUri");
@@ -153,11 +158,14 @@ public class BirtEmailServices {
birtContentType = "application/pdf";
}
IReportEngine engine = BirtContainer.getReportEngine();
engine.getConfig().getAppContext().put("delegator", delegator);
engine.getConfig().getAppContext().put("dispatcher", dispatcher);
engine.getConfig().getAppContext().put("security", security);
InputStream reportInputStream = BirtFactory.getReportInputStreamFromLocation(birtReportLocation);
IReportRunnable design = engine.openReportDesign(reportInputStream);
Debug.logInfo("Export report as content type:" + birtContentType, module);
BirtWorker.exportReport(design, context, birtContentType, baos);
// and generate the PDF
Debug.logInfo("Export report as content type:" + birtContentType, module);
BirtWorker.exportReport(design, context, birtContentType, baos);
baos.flush();
baos.close();
@@ -24,9 +24,12 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.context.IContext;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.presentation.aggregation.layout.EngineFragment;
import org.eclipse.birt.report.presentation.aggregation.layout.RequesterFragment;
import org.eclipse.birt.report.service.BirtReportServiceFactory;
import org.ofbiz.birt.BirtWorker;
import org.ofbiz.birt.container.BirtContainer;
import org.ofbiz.birt.report.context.OFBizBirtContext;
import org.ofbiz.birt.report.service.OFBizBirtViewerReportService;
@@ -52,6 +55,9 @@ public class BirtEngineServlet extends org.eclipse.birt.report.servlet.BirtEngin
protected IContext __getContext( HttpServletRequest request,
HttpServletResponse response ) throws BirtException
{
IReportEngine reportEngine = BirtContainer.getReportEngine();
BirtWorker.setWebContextObjects(reportEngine, request, response);
BirtReportServiceFactory.getReportService( ).setContext(
getServletContext( ), null );
return new OFBizBirtContext( request, response );
@@ -24,10 +24,13 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.context.IContext;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.presentation.aggregation.layout.FramesetFragment;
import org.eclipse.birt.report.presentation.aggregation.layout.RunFragment;
import org.eclipse.birt.report.service.BirtReportServiceFactory;
import org.eclipse.birt.report.servlet.ViewerServlet;
import org.ofbiz.birt.BirtWorker;
import org.ofbiz.birt.container.BirtContainer;
import org.ofbiz.birt.report.context.OFBizBirtContext;
import org.ofbiz.birt.report.service.OFBizBirtViewerReportService;
@@ -56,6 +59,9 @@ public class BirtViewerServlet extends ViewerServlet {
protected IContext __getContext( HttpServletRequest request,
HttpServletResponse response ) throws BirtException
{
IReportEngine reportEngine = BirtContainer.getReportEngine();
BirtWorker.setWebContextObjects(reportEngine, request, response);
BirtReportServiceFactory.getReportService( ).setContext(
getServletContext( ), null );
return new OFBizBirtContext( request, response );
@@ -70,6 +70,7 @@ public class BirtViewHandler implements ViewHandler {
public void render(String name, String page, String info,
String contentType, String encoding, HttpServletRequest request,
HttpServletResponse response) throws ViewHandlerException {
try {
IReportEngine engine = BirtContainer.getReportEngine();
// open report design
@@ -80,6 +81,8 @@ public class BirtViewHandler implements ViewHandler {
} else {
design = engine.openReportDesign(servletContext.getRealPath(page));
}
BirtWorker.setWebContextObjects(engine, request, response);
Map<String, Object> context = FastMap.newInstance();
// set parameters from request
@@ -67,6 +67,7 @@ import org.ofbiz.base.container.ContainerException;
import org.ofbiz.base.container.ContainerConfig.Container.Property;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.SSLUtil;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilURL;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
@@ -620,6 +621,43 @@ public class CatalinaContainer implements Container {
return context;
}
protected Context createTenantContext() throws ContainerException {
String server = "default-server";
Engine engine = engines.get(server);
if (engine == null) {
Debug.logWarning("Server with name [" + server + "] not found;", module);
return null;
}
// create the web application context
StandardContext context = (StandardContext) embedded.createContext("/", System.getProperty("ofbiz.home"));
context.setJ2EEApplication(J2EE_APP);
context.setJ2EEServer(J2EE_SERVER);
context.setLoader(embedded.createLoader(ClassLoaderContainer.getClassLoader()));
context.setReloadable(contextReloadable);
context.setDistributable(distribute);
context.setCrossContext(crossContext);
// create the Default Servlet instance to mount
StandardWrapper defaultServlet = new StandardWrapper();
defaultServlet.setServletClass("org.ofbiz.webapp.control.TenantServlet");
defaultServlet.setServletName("default");
defaultServlet.setLoadOnStartup(1);
defaultServlet.addInitParameter("debug", "0");
defaultServlet.addInitParameter("listing", "true");
defaultServlet.addMapping("/");
context.addChild(defaultServlet);
context.addServletMapping("/", "default");
Host host = hosts.get(engine.getName() + "._DEFAULT");
context.setRealm(host.getRealm());
host.addChild(context);
context.getMapper().setDefaultHostName(host.getName());
return context;
}
protected void loadComponents() throws ContainerException {
if (embedded == null) {
throw new ContainerException("Cannot load web applications without Embedded instance!");
@@ -641,6 +679,12 @@ public class CatalinaContainer implements Container {
}
}
}
// if the multitenant is enabled then create the tenant context
String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant");
if ("Y".equals(useMultitenant) && UtilValidate.isEmpty(delegator.getDelegatorTenantId())) {
createTenantContext();
}
}
public void stop() throws ContainerException {
+9 -7
View File
@@ -42,13 +42,15 @@ under the License.
<td class="label">${uiLabelMap.CommonPassword}</td>
<td><input type="password" name="PASSWORD" value="" size="20"/></td>
</tr>
<#if ("Y" == useMultitenant && !sessionAttributes.tenantId?exists) >
<tr>
<td class="label">${uiLabelMap.CommonTenantId}</td>
<td><input type="text" name="tenantId" value="${parameters.tenantId?if_exists}" size="20"/></td>
</tr>
<#elseif ("Y" == useMultitenant && sessionAttributes.tenantId?exists) >
<input type="hidden" name="tenantId" value="${sessionAttributes.tenantId?if_exists}"/>
<#if ("Y" == useMultitenant) >
<#if !requestAttributes.tenantId?exists>
<tr>
<td class="label">${uiLabelMap.CommonTenantId}</td>
<td><input type="text" name="tenantId" value="${parameters.tenantId?if_exists}" size="20"/></td>
</tr>
<#else>
<input type="hidden" name="tenantId" value="${requestAttributes.tenantId?if_exists}"/>
</#if>
</#if>
<tr>
<td colspan="2" align="center">
@@ -67,6 +67,7 @@ under the License.
<field name="tenantId" type="id-ne"/>
<field name="tenantName" type="name"/>
<field name="domainName" type="long-varchar"/>
<field name="initialPath" type="value"/>
<field name="disabled" type="indicator"><description>Disabled if 'Y', defaults to 'N' (not disabled).</description></field>
<prim-key field="tenantId"/>
</entity>
@@ -50,10 +50,16 @@ import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilObject;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.security.Security;
import org.ofbiz.security.SecurityConfigurationException;
import org.ofbiz.security.SecurityFactory;
@@ -266,6 +272,52 @@ public class ContextFilter implements Filter {
return;
}
}
// check if multi tenant is enabled
String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant");
if ("Y".equals(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 delegator = getDelegator(config.getServletContext());
List<EntityCondition> conds = FastList.newInstance();
conds.add(EntityCondition.makeCondition("domainName", serverName));
List<GenericValue> tenants = delegator.findList("Tenant", EntityCondition.makeCondition(conds), null, UtilMisc.toList("-createdStamp"), null, false);
if (UtilValidate.isNotEmpty(tenants)) {
GenericValue tenant = EntityUtil.getFirst(tenants);
String tenantId = tenant.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);
config.getServletContext().setAttribute("delegator", delegator);
// clear web context objects
config.getServletContext().setAttribute("authorization", null);
config.getServletContext().setAttribute("security", null);
config.getServletContext().setAttribute("dispatcher", null);
// initialize authorizer
getAuthz();
// initialize security
Security security = getSecurity();
// initialize the services dispatcher
LocalDispatcher dispatcher = getDispatcher(config.getServletContext());
// set web context objects
httpRequest.getSession().setAttribute("dispatcher", dispatcher);
httpRequest.getSession().setAttribute("security", security);
request.setAttribute("tenantId", tenantId);
}
} catch (GenericEntityException e) {
Debug.logWarning(e, "Unable to get Tenant", module);
}
}
// we're done checking; continue on
chain.doFilter(request, response);
@@ -226,39 +226,6 @@ public class ControlServlet extends HttpServlet {
String errorPage = null;
try {
String useMultitenant = UtilProperties.getPropertyValue("general.properties", "multitenant");
if ("Y".equals(useMultitenant) && UtilValidate.isEmpty(delegator.getDelegatorTenantId())) {
// get tenant delegator by domain name
try {
// if a domain name was specified for tenant, replace delegator with the new per-tenant delegator and set tenantId to session attribute
List<GenericValue> tenants = delegator.findList("Tenant", EntityCondition.makeCondition("domainName", request.getServerName()), null, UtilMisc.toList("-createdStamp"), null, false);
if (UtilValidate.isNotEmpty(tenants)) {
GenericValue tenant = EntityUtil.getFirst(tenants);
String tenantId = tenant.getString("tenantId");
// make that tenant active, setup a new delegator and a new dispatcher
String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId;
// after this line the delegator is replaced with the new per-tenant delegator
delegator = DelegatorFactory.getDelegator(tenantDelegatorName);
session.setAttribute("tenantId", tenantId);
session.setAttribute("delegatorName", tenantDelegatorName);
}
} catch (GenericEntityException e) {
String errMsg = "Error getting tenant by domain name: " + request.getServerName();
Debug.logError(e, errMsg, module);
throw new RequestHandlerException(errMsg, e);
}
}
if ("Y".equals(useMultitenant) && UtilValidate.isNotEmpty(delegator.getDelegatorTenantId())) {
// re-make dispatcher from tenant delegator and change delegator of security to use tanent delegator
dispatcher = ContextFilter.makeWebappDispatcher(session.getServletContext(), delegator);
security.setDelegator(delegator);
request.setAttribute("delegator", delegator);
request.setAttribute("dispatcher", dispatcher);
}
// the ServerHitBin call for the event is done inside the doRequest method
requestHandler.doRequest(request, response, null, userLogin, delegator);
} catch (RequestHandlerException e) {
@@ -0,0 +1,68 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.ofbiz.webapp.control;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.util.EntityUtil;
/**
* TenantServlet.java - Tenant servlet for the web application.
*/
@SuppressWarnings("serial")
public class TenantServlet extends HttpServlet {
public static String module = TenantServlet.class.getName();
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// get default delegator
Delegator delegator = DelegatorFactory.getDelegator("default");
try {
// if a domain name was specified for tenant, redirect to initial path
List<GenericValue> tenants = delegator.findList("Tenant", EntityCondition.makeCondition("domainName", request.getServerName()), null, UtilMisc.toList("-createdStamp"), null, false);
if (UtilValidate.isNotEmpty(tenants)) {
GenericValue tenant = EntityUtil.getFirst(tenants);
String initialPath = tenant.getString("initialPath");
response.sendRedirect(initialPath);
}
} catch (GenericEntityException e) {
String errMsg = "Error getting tenant by domain name: " + request.getServerName();
Debug.logError(e, errMsg, module);
throw new ServletException(errMsg, e);
}
}
}