You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by sh...@apache.org on 2015/09/02 11:09:45 UTC

svn commit: r1700739 - in /ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr: control/OFBizSolrLoginWorker.java webapp/OFBizSolrContextFilter.java webapp/OFBizSolrLoadAdminUiServlet.java webapp/OFBizSolrRedirectServlet.java

Author: shijh
Date: Wed Sep  2 09:09:45 2015
New Revision: 1700739

URL: http://svn.apache.org/r1700739
Log:
OFBIZ-6596 The Solr redirection does not work in trunk demo.

1. Use response.sendRedirect to forward url to login page instead of the response.setHeader("location", url), this avoids the warning messages from EntityUtilProperties. Besides, when testing in similar config as demo-ofbiz-trunk is (port 80 -> 8080, port 443 -> 8443), this change can keep the url to 80 or 443.

2. Replace tabs to spaces in several java files.

Modified:
    ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/control/OFBizSolrLoginWorker.java
    ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrContextFilter.java
    ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrLoadAdminUiServlet.java
    ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrRedirectServlet.java

Modified: ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/control/OFBizSolrLoginWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/control/OFBizSolrLoginWorker.java?rev=1700739&r1=1700738&r2=1700739&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/control/OFBizSolrLoginWorker.java (original)
+++ ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/control/OFBizSolrLoginWorker.java Wed Sep  2 09:09:45 2015
@@ -40,8 +40,8 @@ public class OFBizSolrLoginWorker extend
      *         JSP should generate its own content. This allows an event to override the default content.
      */
     public static String login(HttpServletRequest request, HttpServletResponse response) {
-    	String result = LoginWorker.login(request, response);
-    	if (result.equals("success")) {
+        String result = LoginWorker.login(request, response);
+        if (result.equals("success")) {
             // send the redirect
             try {            
                 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
@@ -51,13 +51,13 @@ public class OFBizSolrLoginWorker extend
                 Debug.logError(ise.getMessage(), module);
                 return "error";
             }
-    	}
-    	return result;
+        }
+        return result;
     }
 
     public static String extensionCheckLogin(HttpServletRequest request, HttpServletResponse response) {
-    	String result = LoginWorker.extensionCheckLogin(request, response);
-    	if (result.equals("success")) {
+        String result = LoginWorker.extensionCheckLogin(request, response);
+        if (result.equals("success")) {
             // send the redirect
             try {            
                 response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
@@ -67,7 +67,7 @@ public class OFBizSolrLoginWorker extend
                 Debug.logError(ise.getMessage(), module);
                 return "error";
             }
-    	}
+        }
         return result;
     }
 }

Modified: ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrContextFilter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrContextFilter.java?rev=1700739&r1=1700738&r2=1700739&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrContextFilter.java (original)
+++ ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrContextFilter.java Wed Sep  2 09:09:45 2015
@@ -82,7 +82,7 @@ public class OFBizSolrContextFilter exte
      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
      */
     public void init(FilterConfig config) throws ServletException {
-    	super.init(config);
+        super.init(config);
         this.config = config;
 
         // puts all init-parameters in ServletContext attributes for easier parameterization without code changes
@@ -169,46 +169,46 @@ public class OFBizSolrContextFilter exte
             }
         }
 
-    	String servletPath = httpRequest.getServletPath();
+        String servletPath = httpRequest.getServletPath();
         if (UtilValidate.isNotEmpty(servletPath) && servletPath.equals("/control")) {
-        	doControlFilter(request, response, chain);
+            doControlFilter(request, response, chain);
             // we're done checking; continue on
             chain.doFilter(request, response);
         } else {
-        	// check if the request is from an authorized user
-        	if (UtilValidate.isNotEmpty(servletPath) && servletPath.startsWith("/admin/")) {
+            // check if the request is from an authorized user
+            if (UtilValidate.isNotEmpty(servletPath) && servletPath.startsWith("/admin/")) {
                 HttpSession session = httpRequest.getSession();
                 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
                 Security security = (Security) request.getAttribute("security");
                 if (security == null) {
-                	security = (Security) httpRequest.getServletContext().getAttribute("security");
-                	if (security != null) {
+                    security = (Security) httpRequest.getServletContext().getAttribute("security");
+                    if (security != null) {
                         request.setAttribute("security", security);
-                	}
+                    }
                 }
                 if (security == null) {
-                	security = getSecurity();
-                	if (security != null) {
+                    security = getSecurity();
+                    if (security != null) {
                         request.setAttribute("security", security);
-                	}
+                    }
                 }
                 if (UtilValidate.isEmpty(userLogin) || !LoginWorker.hasBasePermission(userLogin, httpRequest)) {
-                	response.setContentType("application/x-json");
-                	MapToJSON mapToJson = new MapToJSON();
-                	JSON json;
-					try {
-						json = mapToJson.convert(UtilMisc.toMap("ofbizLogin", (Object) "true"));
-	                	OutputStream os = response.getOutputStream();
-	                	os.write(json.toString().getBytes());
-	                	os.flush();
-					} catch (ConversionException e) {
-						Debug.logError("Error while converting ofbizLogin map to JSON.", module);
-					}
-                	return;
-                }
-        	}
-        	// NOTE: there's a chain.doFilter in SolrDispatchFilter's doFilter
-        	super.doFilter(request, response, chain);
+                    response.setContentType("application/x-json");
+                    MapToJSON mapToJson = new MapToJSON();
+                    JSON json;
+                    try {
+                        json = mapToJson.convert(UtilMisc.toMap("ofbizLogin", (Object) "true"));
+                        OutputStream os = response.getOutputStream();
+                        os.write(json.toString().getBytes());
+                        os.flush();
+                    } catch (ConversionException e) {
+                        Debug.logError("Error while converting ofbizLogin map to JSON.", module);
+                    }
+                    return;
+                }
+            }
+            // NOTE: there's a chain.doFilter in SolrDispatchFilter's doFilter
+            super.doFilter(request, response, chain);
         }
     }
 
@@ -311,7 +311,7 @@ public class OFBizSolrContextFilter exte
             // get tenant delegator by domain name
             String serverName = httpRequest.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());
 
@@ -373,9 +373,9 @@ public class OFBizSolrContextFilter exte
                 Debug.logWarning(e, "Unable to get Tenant", module);
             }
         }
-	}
+    }
 
-	/**
+    /**
      * @see javax.servlet.Filter#destroy()
      */
     public void destroy() {

Modified: ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrLoadAdminUiServlet.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrLoadAdminUiServlet.java?rev=1700739&r1=1700738&r2=1700739&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrLoadAdminUiServlet.java (original)
+++ ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrLoadAdminUiServlet.java Wed Sep  2 09:09:45 2015
@@ -44,11 +44,11 @@ public class OFBizSolrLoadAdminUiServlet
 
     @Override
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
-    	boolean isForwarded = forwardUrl(request, response);
-    	if (isForwarded) {
-    		return;
-    	}
-    	
+        boolean isForwarded = forwardUrl(request, response);
+        if (isForwarded) {
+            return;
+        }
+        
         // This attribute is set by the SolrDispatchFilter
         CoreContainer cores = (CoreContainer) request.getAttribute("org.apache.solr.CoreContainer");
 

Modified: ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrRedirectServlet.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrRedirectServlet.java?rev=1700739&r1=1700738&r2=1700739&view=diff
==============================================================================
--- ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrRedirectServlet.java (original)
+++ ofbiz/trunk/specialpurpose/solr/src/org/ofbiz/solr/webapp/OFBizSolrRedirectServlet.java Wed Sep  2 09:09:45 2015
@@ -28,9 +28,14 @@ import javax.servlet.http.HttpSession;
 import org.apache.solr.servlet.RedirectServlet;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericValue;
+import org.ofbiz.security.Security;
+import org.ofbiz.security.SecurityConfigurationException;
+import org.ofbiz.security.SecurityFactory;
 import org.ofbiz.webapp.OfbizUrlBuilder;
+import org.ofbiz.webapp.control.LoginWorker;
 import org.ofbiz.webapp.control.WebAppConfigurationException;
 
 /**
@@ -46,24 +51,55 @@ public class OFBizSolrRedirectServlet ex
      */
     @Override
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-    	boolean isForwarded = forwardUrl(request, response);
-    	if (isForwarded) {
-    		return;
-    	}
-    	
-    	super.doGet(request, response);
+        boolean isForwarded = forwardUrl(request, response);
+        if (isForwarded) {
+            return;
+        }
+        
+        super.doGet(request, response);
     }
 
-	protected static boolean forwardUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
+    protected static boolean forwardUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {
         HttpSession session = request.getSession();
         GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
         boolean forwardToLogin = false;
         if (UtilValidate.isEmpty(userLogin)) {
-        	forwardToLogin = true;
+            forwardToLogin = true;
+        } else {
+            Security security = (Security) request.getAttribute("security");
+            if (security == null) {
+                security = (Security) session.getAttribute("security");
+                if (security != null) {
+                	request.setAttribute("security", security);
+                }
+            }
+            if (security == null) {
+                security = (Security) request.getServletContext().getAttribute("security");
+                if (security != null) {
+                	request.setAttribute("security", security);
+                }
+            }
+            if (!LoginWorker.hasBasePermission(userLogin, request)) {
+                forwardToLogin = true;
+            }
         }
-		
-    	// check request schema
-    	if (forwardToLogin || !request.getScheme().equals("https")) {
+        
+        if (forwardToLogin) {
+            String contextPath = request.getContextPath();
+            String uri = request.getRequestURI();
+            if (UtilValidate.isNotEmpty(contextPath) && uri.startsWith(contextPath)) {
+                uri = uri.replaceFirst(request.getContextPath(), "");
+            }
+            String servletPath = request.getServletPath();
+            if (UtilValidate.isNotEmpty(servletPath) && uri.startsWith(servletPath)) {
+                uri = uri.replaceFirst(servletPath, "");
+            }
+            response.sendRedirect(contextPath + "/control/checkLogin" + uri);
+            return true;
+        }
+        
+        // check request schema
+        if (!request.getScheme().equals("https")) {
             StringBuilder newURL = new StringBuilder(250);
             // Build the scheme and host part
             try {
@@ -82,24 +118,7 @@ public class OFBizSolrRedirectServlet ex
                 Debug.logError(e, "Exception thrown while writing to StringBuilder: ", module);
                 return false;
             }
-            if (forwardToLogin) {
-            	String contextPath = request.getContextPath();
-            	if (UtilValidate.isNotEmpty(contextPath)) {
-            		newURL.append(contextPath);
-            	}
-            	newURL.append("/control/checkLogin");
-            	String uri = request.getRequestURI();
-            	if (UtilValidate.isNotEmpty(contextPath) && uri.startsWith(contextPath)) {
-            		uri = uri.replaceFirst(request.getContextPath(), "");
-            	}
-            	String servletPath = request.getServletPath();
-            	if (UtilValidate.isNotEmpty(servletPath) && uri.startsWith(servletPath)) {
-            		uri = uri.replaceFirst(servletPath, "");
-            	}
-                newURL.append(uri);
-            } else {
-                newURL.append(request.getRequestURI());
-            }
+            newURL.append(request.getRequestURI());
 
             // send the redirect
             try {            
@@ -109,8 +128,28 @@ public class OFBizSolrRedirectServlet ex
             } catch (IllegalStateException ise) {
                 throw new IOException(ise.getMessage(), ise);
             }
-    		return true;
-    	}
-		return false;
-	}
+            return true;
+        }
+        return false;
+    }
+
+    protected Security getSecurity(HttpServletRequest request) {
+        Security security = (Security) request.getServletContext().getAttribute("security");
+        if (security == null) {
+            Delegator delegator = (Delegator) request.getServletContext().getAttribute("delegator");
+
+            if (delegator != null) {
+                try {
+                    security = SecurityFactory.getInstance(delegator);
+                } catch (SecurityConfigurationException e) {
+                    Debug.logError(e, "Unable to obtain an instance of the security object.", module);
+                }
+            }
+            request.getServletContext().setAttribute("security", security);
+            if (security == null) {
+                Debug.logError("An invalid (null) Security object has been set in the servlet context.", module);
+            }
+        }
+        return security;
+    }
 }