svn commit: r1761155 - in /ofbiz/trunk/specialpurpose/cmssite/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/ofbiz/ main/java/org/apache/ofbiz/cmssite/ main/java/org/apache/ofbiz/cmssite/multisite/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1761155 - in /ofbiz/trunk/specialpurpose/cmssite/src: ./ main/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/ofbiz/ main/java/org/apache/ofbiz/cmssite/ main/java/org/apache/ofbiz/cmssite/multisite/

pranayp
Author: pranayp
Date: Sat Sep 17 08:12:25 2016
New Revision: 1761155

URL: http://svn.apache.org/viewvc?rev=1761155&view=rev
Log:
Implemented: Multisite support in specialpurpose/cmssite
(OFBIZ-7120)

Adding missing files from last commit r1761150.

Thanks Mohammed Rehan Khan for the contribution.

Added:
    ofbiz/trunk/specialpurpose/cmssite/src/
    ofbiz/trunk/specialpurpose/cmssite/src/main/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/MultiSiteRequestWrapper.java
    ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/WebSiteFilter.java

Added: ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/MultiSiteRequestWrapper.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/MultiSiteRequestWrapper.java?rev=1761155&view=auto
==============================================================================
--- ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/MultiSiteRequestWrapper.java (added)
+++ ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/MultiSiteRequestWrapper.java Sat Sep 17 08:12:25 2016
@@ -0,0 +1,412 @@
+/*******************************************************************************
+ * 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.apache.ofbiz.cmssite.multisite;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.AsyncContext;
+import javax.servlet.DispatcherType;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpUpgradeHandler;
+import javax.servlet.http.Part;
+
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilHttp;
+
+
+public class MultiSiteRequestWrapper implements HttpServletRequest {
+
+    private final HttpServletRequest request;
+
+    public MultiSiteRequestWrapper(HttpServletRequest request) {
+        this.request = request;
+    }
+
+    @Override
+    public String changeSessionId() { return request.changeSessionId(); }
+
+    @Override
+    public String getAuthType() {
+        return request.getAuthType();
+    }
+
+    @Override
+    public String getContextPath() {
+        return request.getContextPath();
+    }
+
+    @Override
+    public Cookie[] getCookies() {
+        return request.getCookies();
+    }
+
+    @Override
+    public long getDateHeader(String arg0) {
+        return request.getDateHeader(arg0);
+    }
+
+    @Override
+    public String getHeader(String arg0) {
+        return request.getHeader(arg0);
+    }
+
+    @Override
+    public Enumeration getHeaderNames() {
+        return request.getHeaderNames();
+    }
+
+    @Override
+    public Enumeration getHeaders(String arg0) {
+        return request.getHeaders(arg0);
+    }
+
+    @Override
+    public int getIntHeader(String arg0) {
+        return request.getIntHeader(arg0);
+    }
+
+    @Override
+    public String getMethod() {
+        return request.getMethod();
+    }
+
+    @Override
+    public String getPathInfo() {
+        boolean removePathAlias = (Boolean) request.getAttribute("removePathAlias");
+        String pathInfo = request.getPathInfo();
+        if (removePathAlias && pathInfo != null) {
+            int nextPathSegmentStart = pathInfo.indexOf('/', 1);
+            if (nextPathSegmentStart == -1) {
+                nextPathSegmentStart = pathInfo.indexOf('?', 1);
+                if (nextPathSegmentStart == -1) {
+                    return "/";
+                }
+            }
+            return pathInfo.substring(nextPathSegmentStart);
+       } else {
+           return pathInfo;
+       }
+    }
+
+    @Override
+    public String getPathTranslated() {
+        return request.getPathTranslated();
+    }
+
+    @Override
+    public String getQueryString() {
+        return request.getQueryString();
+    }
+
+    @Override
+    public String getRemoteUser() {
+        return request.getRemoteUser();
+    }
+
+    @Override
+    public String getRequestURI() {
+        return request.getRequestURI();
+    }
+
+    @Override
+    public StringBuffer getRequestURL() {
+        return request.getRequestURL();
+    }
+
+    @Override
+    public String getRequestedSessionId() {
+        return request.getRequestedSessionId();
+    }
+
+    @Override
+    public String getServletPath() {
+        return request.getServletPath();
+    }
+
+    @Override
+    public HttpSession getSession() {
+        return request.getSession();
+    }
+
+    @Override
+    public HttpSession getSession(boolean arg0) {
+        return request.getSession(arg0);
+    }
+
+    @Override
+    public Principal getUserPrincipal() {
+        return request.getUserPrincipal();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromCookie() {
+        return request.isRequestedSessionIdFromCookie();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdFromURL() {
+        return request.isRequestedSessionIdFromURL();
+    }
+
+    @Deprecated @Override
+    public boolean isRequestedSessionIdFromUrl() {
+        return request.isRequestedSessionIdFromURL();
+    }
+
+    @Override
+    public boolean isRequestedSessionIdValid() {
+        return request.isRequestedSessionIdValid();
+    }
+
+    @Override
+    public boolean isUserInRole(String arg0) {
+        return request.isUserInRole(arg0);
+    }
+
+    @Override
+    public Object getAttribute(String arg0) {
+        return request.getAttribute(arg0);
+    }
+
+    @Override
+    public Enumeration getAttributeNames() {
+        return request.getAttributeNames();
+    }
+
+    @Override
+    public String getCharacterEncoding() {
+        return request.getCharacterEncoding();
+    }
+
+    @Override
+    public int getContentLength() {
+        return request.getContentLength();
+    }
+    
+    @Override
+    public long getContentLengthLong() {
+        return request.getContentLengthLong();
+    }
+
+    @Override
+    public String getContentType() {
+        return request.getContentType();
+    }
+
+    @Override
+    public ServletInputStream getInputStream() throws IOException {
+        return request.getInputStream();
+    }
+
+    @Override
+    public String getLocalAddr() {
+        return request.getLocalAddr();
+    }
+
+    @Override
+    public String getLocalName() {
+        return request.getLocalName();
+    }
+
+    @Override
+    public int getLocalPort() {
+        return request.getLocalPort();
+    }
+
+    @Override
+    public Locale getLocale() {
+        return UtilHttp.getLocale(request);
+    }
+
+    @Override
+    public Enumeration getLocales() {
+        return request.getLocales();
+    }
+
+    @Override
+    public String getParameter(String arg0) {
+        return request.getParameter(arg0);
+    }
+
+    @Override
+    public Map getParameterMap() {
+        return request.getParameterMap();
+    }
+
+    @Override
+    public Enumeration getParameterNames() {
+        return request.getParameterNames();
+    }
+
+    @Override
+    public String[] getParameterValues(String arg0) {
+        return request.getParameterValues(arg0);
+    }
+
+    @Override
+    public String getProtocol() {
+        return request.getProtocol();
+    }
+
+    @Override
+    public BufferedReader getReader() throws IOException {
+        return request.getReader();
+    }
+
+    @Override @Deprecated
+    public String getRealPath(String arg0) {
+        return request.getServletContext().getRealPath(arg0);
+    }
+
+    @Override
+    public String getRemoteAddr() {
+        return request.getRemoteAddr();
+    }
+
+    @Override
+    public String getRemoteHost() {
+        return request.getRemoteHost();
+    }
+
+    @Override
+    public int getRemotePort() {
+        return request.getRemotePort();
+    }
+
+    @Override
+    public RequestDispatcher getRequestDispatcher(String arg0) {
+        return request.getRequestDispatcher(arg0);
+    }
+
+    @Override
+    public String getScheme() {
+        return request.getScheme();
+    }
+
+    @Override
+    public String getServerName() {
+        return request.getServerName();
+    }
+
+    @Override
+    public int getServerPort() {
+        return request.getServerPort();
+    }
+
+    @Override
+    public boolean isSecure() {
+        return request.isSecure();
+    }
+
+    @Override
+    public void removeAttribute(String arg0) {
+        request.removeAttribute(arg0);
+    }
+
+    @Override
+    public void setAttribute(String arg0, Object arg1) {
+        request.setAttribute(arg0, arg1);
+    }
+
+    @Override
+    public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
+        request.setCharacterEncoding(arg0);
+    }
+
+    @Override
+    public boolean authenticate(HttpServletResponse arg0) throws IOException, ServletException {
+        return request.authenticate(arg0);
+    }
+
+    @Override
+    public Part getPart(String arg0) throws IOException, IllegalStateException, ServletException {
+        return request.getPart(arg0);
+    }
+
+    @Override
+    public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
+        return request.getParts();
+    }
+
+    @Override
+    public void login(String arg0, String arg1) throws ServletException {
+        request.login(arg0, arg1);
+    }
+
+    @Override
+    public void logout() throws ServletException {
+        request.logout();
+    }
+
+    @Override
+    public AsyncContext getAsyncContext() {
+        return request.getAsyncContext();
+    }
+
+    @Override
+    public DispatcherType getDispatcherType() {
+        return request.getDispatcherType();
+    }
+
+    @Override
+    public ServletContext getServletContext() {
+        return request.getServletContext();
+    }
+
+    @Override
+    public boolean isAsyncStarted() {
+        return request.isAsyncStarted();
+    }
+
+    @Override
+    public boolean isAsyncSupported() {
+        return request.isAsyncSupported();
+    }
+
+    @Override
+    public AsyncContext startAsync() {
+        return request.startAsync();
+    }
+
+    @Override
+    public AsyncContext startAsync(ServletRequest arg0, ServletResponse arg1) {
+        return request.startAsync(arg0, arg1);
+    }
+
+    @Override
+    public HttpUpgradeHandler upgrade (Class handlerClass) throws IOException, ServletException {
+        return request.upgrade(handlerClass);
+    }
+}
\ No newline at end of file

Added: ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/WebSiteFilter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/WebSiteFilter.java?rev=1761155&view=auto
==============================================================================
--- ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/WebSiteFilter.java (added)
+++ ofbiz/trunk/specialpurpose/cmssite/src/main/java/org/apache/ofbiz/cmssite/multisite/WebSiteFilter.java Sat Sep 17 08:12:25 2016
@@ -0,0 +1,184 @@
+/*******************************************************************************
+ * 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.apache.ofbiz.cmssite.multisite;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilHttp;
+import org.apache.ofbiz.base.util.UtilMisc;
+import org.apache.ofbiz.base.util.UtilValidate;
+import org.apache.ofbiz.entity.Delegator;
+import org.apache.ofbiz.entity.DelegatorFactory;
+import org.apache.ofbiz.entity.GenericEntityException;
+import org.apache.ofbiz.entity.GenericValue;
+import org.apache.ofbiz.entity.util.EntityQuery;
+import org.apache.ofbiz.order.shoppingcart.CartItemModifyException;
+import org.apache.ofbiz.order.shoppingcart.ShoppingCart;
+import org.apache.ofbiz.security.Security;
+import org.apache.ofbiz.security.SecurityConfigurationException;
+import org.apache.ofbiz.security.SecurityFactory;
+import org.apache.ofbiz.service.LocalDispatcher;
+import org.apache.ofbiz.webapp.WebAppUtil;
+import org.apache.ofbiz.webapp.control.RequestHandler;
+import org.apache.ofbiz.webapp.stats.VisitHandler;
+
+// Used to filter website on the basis of hosted pathAlias.
+public class WebSiteFilter implements Filter {
+
+    public static final String MODULE = WebSiteFilter.class.getName();
+
+    protected FilterConfig m_config = null;
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        m_config = filterConfig;
+        m_config.getServletContext().setAttribute("MULTI_SITE_ENABLED", true);
+    }
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+        HttpServletRequest httpRequest = (HttpServletRequest) request;
+        HttpServletResponse httpResponse = (HttpServletResponse) response;
+        HttpSession session = httpRequest.getSession();
+
+        String webSiteId = (String) m_config.getServletContext().getAttribute("webSiteId");
+        String pathInfo = httpRequest.getPathInfo();
+        // get the WebSite id segment, cheat here and use existing logic
+        String webSiteAlias = RequestHandler.getRequestUri(pathInfo);
+        Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator");
+        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
+
+        WebAppUtil.setCharacterEncoding(request);
+        WebAppUtil.setAttributesFromRequestBody(request);
+
+        // set the web context in the request for future use
+        dispatcher = WebAppUtil.makeWebappDispatcher(session.getServletContext(), delegator);
+        setWebContextObjects(httpRequest, httpResponse, delegator, dispatcher);
+
+        request.setAttribute("servletContext", httpRequest.getSession().getServletContext());
+        request.setAttribute("delegator", delegator);
+
+        GenericValue webSite = null;
+        try {
+            if (UtilValidate.isNotEmpty(webSiteAlias) && UtilValidate.isEmpty(webSite)) {
+                webSite = EntityQuery.use(delegator).from("WebSite").where("hostedPathAlias", webSiteAlias).cache().queryFirst();
+            }
+            if (UtilValidate.isEmpty(webSite)) {
+                webSite = EntityQuery.use(delegator).from("WebSite").where("isDefault", "Y").cache().queryFirst();
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, MODULE);
+        }
+        if (webSite != null) {
+            webSiteId = webSite.getString("webSiteId");
+            GenericValue productStore = null;
+            try {
+                productStore = webSite.getRelatedOne("ProductStore", false);
+            } catch (GenericEntityException e) {
+                Debug.logError(e, MODULE);
+            }
+
+            String newLocale = request.getParameter("newLocale");
+            if (productStore != null && newLocale == null && session.getAttribute("locale") == null) {
+                newLocale = productStore.getString("defaultLocaleString");
+            } else if (newLocale == null && session.getAttribute("locale") != null) {
+                newLocale = session.getAttribute("locale").toString();
+            }
+
+            if (newLocale == null)
+                newLocale = UtilHttp.getLocale(httpRequest).toString();
+            // If the webSiteId has changed then invalidate the existing session
+            if (!webSiteId.equals(session.getAttribute("webSiteId"))) {
+                ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingCart");
+                if (cart != null && !(webSite.getString("productStoreId").equals(cart.getProductStoreId())) ) {
+                    // clearing cart items from previous store
+                    cart.clear();
+                    // Put product Store for this webSite in cart
+                    cart.setProductStoreId(webSite.getString("productStoreId"));
+                }
+                if (cart != null && productStore != null) {
+                    Locale localeObj = UtilMisc.parseLocale(newLocale);
+                    cart.setLocale(localeObj);
+                    try {
+                        cart.setCurrency(dispatcher, productStore.getString("defaultCurrencyUomId"));
+                    } catch (CartItemModifyException e) {
+                        Debug.logError(e, MODULE);
+                    }
+                }
+                session.removeAttribute("webSiteId");
+                session.setAttribute("webSiteId", webSiteId);
+                session.setAttribute("displayMaintenancePage", webSite.getString("displayMaintenancePage"));
+            }
+            request.setAttribute("webSiteId", webSiteId);
+            session.setAttribute("displayMaintenancePage", webSite.getString("displayMaintenancePage"));
+            if(UtilValidate.isEmpty(webSite.getString("hostedPathAlias"))) {
+                request.setAttribute("removePathAlias", false);
+            } else {
+                request.setAttribute("removePathAlias", true);
+            }
+            httpRequest = new MultiSiteRequestWrapper(httpRequest);
+            UtilHttp.setLocale(httpRequest, newLocale);
+        }
+        if (webSiteId != null) {
+            request.setAttribute("webSiteId", webSiteId);
+        }
+        chain.doFilter(httpRequest, response);
+    }
+
+    private static void setWebContextObjects(HttpServletRequest request, HttpServletResponse response, Delegator delegator, LocalDispatcher dispatcher) {
+        HttpSession session = request.getSession();
+        Security security = null;
+        try {
+            security = SecurityFactory.getInstance(delegator);
+        } catch (SecurityConfigurationException e) {
+            Debug.logError(e, MODULE);
+        }
+        request.setAttribute("delegator", delegator);
+        request.setAttribute("dispatcher", dispatcher);
+        request.setAttribute("security", security);
+
+        session.setAttribute("delegatorName", delegator.getDelegatorName());
+        session.setAttribute("delegator", delegator);
+        session.setAttribute("dispatcher", dispatcher);
+        session.setAttribute("security", security);
+        session.setAttribute("_WEBAPP_NAME_", UtilHttp.getApplicationName(request));
+        
+        // get rid of the visit info since it was pointing to the previous database, and get a new one
+        session.removeAttribute("visitor");
+        session.removeAttribute("visit");
+        VisitHandler.getVisitor(request, response);
+        VisitHandler.getVisit(session);
+    }
+    @Override
+    public void destroy() {
+    }
+}
\ No newline at end of file