Author: ssmiweve
Date: 2008-04-19 15:33:14 +0200 (Sat, 19 Apr 2008)
New Revision: 6497

Modified:
   branches/2.16/site-spi/src/main/java/no/sesat/search/site/Site.java
   
branches/2.16/war/src/main/java/no/sesat/search/http/filters/SiteLocatorFilter.java
Log:
SEARCH-4647 - Faulty host lookup leading to seemingly infinite loop + high load 
= crashed server.


Modified: branches/2.16/site-spi/src/main/java/no/sesat/search/site/Site.java
===================================================================
--- branches/2.16/site-spi/src/main/java/no/sesat/search/site/Site.java 
2008-04-18 19:52:34 UTC (rev 6496)
+++ branches/2.16/site-spi/src/main/java/no/sesat/search/site/Site.java 
2008-04-19 13:33:14 UTC (rev 6497)
@@ -31,6 +31,7 @@
 import java.util.Properties;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import no.schibstedsok.commons.ioc.BaseContext;
+import no.sesat.search.site.config.ResourceLoadException;
 import org.apache.log4j.Logger;
 
 /** A Site object identifies a Skin + Locale pairing.
@@ -122,6 +123,7 @@
 
     /** Creates a new instance of Site.
      * A null Context will result in a parentSiteName == siteName
+     * @throws IllegalArgumentException when there exists no skin matching the 
theSiteName argument.
      */
     private Site(final Context cxt, final String theSiteName, final Locale 
theLocale) {
 
@@ -150,9 +152,10 @@
 
             final String parentSiteName;
             if(null != cxt){
-                parentSiteName = null != cxt.getParentSiteName(siteContext)
-                        ? 
ensureTrailingSlash(cxt.getParentSiteName(siteContext))
-                        : null;
+                // cxt.getParentSiteName(siteContext) is an expensive call due 
to resource load every call.
+                final String psn = cxt.getParentSiteName(siteContext);
+                parentSiteName = null != psn ? ensureTrailingSlash(psn) : null;
+                
             }else{
                 parentSiteName = siteName;
             }
@@ -267,7 +270,8 @@
      * @param cxt the cxt to use during creation. null will prevent 
constructing a new site.
      * @param siteName the virtual host name.
      * @param locale the locale desired
-     * @return the site bean.
+     * @throws IllegalArgumentException when there exists no skin matching the 
siteName argument.
+     * @return the site instance. never null.
      */
     public static Site valueOf(final Context cxt, final String siteName, final 
Locale locale) {
 
@@ -280,7 +284,7 @@
         final String uniqueName = getUniqueName(realSiteName,locale);
         try{
             INSTANCES_LOCK.readLock().lock();
-            LOG.debug("INSTANCES.get(" + uniqueName + ")");
+            LOG.trace("INSTANCES.get(" + uniqueName + ")");
             site = INSTANCES.get(uniqueName);
 
         }finally{

Modified: 
branches/2.16/war/src/main/java/no/sesat/search/http/filters/SiteLocatorFilter.java
===================================================================
--- 
branches/2.16/war/src/main/java/no/sesat/search/http/filters/SiteLocatorFilter.java
 2008-04-18 19:52:34 UTC (rev 6496)
+++ 
branches/2.16/war/src/main/java/no/sesat/search/http/filters/SiteLocatorFilter.java
 2008-04-19 13:33:14 UTC (rev 6497)
@@ -88,7 +88,9 @@
     private static final String UNKNOWN = "unknown";
 
     
-    private static final String CONFIGURATION_RESOURCE= "/conf/" + 
Site.CONFIGURATION_FILE;
+    // Any request coming into Sesat with /conf/ is immediately returned as a 
404. 
+    // It should have been directed to a skin.
+    private static final String CONFIGURATION_RESOURCE= "/conf/";
 
     /** Changes to this list must also change the ProxyPass|ProxyPassReverse 
configuration in httpd.conf **/
     private static final Collection<String> EXTERNAL_DIRS =
@@ -96,7 +98,9 @@
                 PUBLISH_DIR, "/css/", "/images/", "/javascript/"
     }));
 
-    /** The context that we'll need to use every invocation of doFilter(..)  
**/
+    /** The context that we'll need to use every invocation of doFilter(..).
+     * @throws IllegalArgumentException when there exists no skin matching the 
siteContext.getSite() argument.
+     **/
     public static final Site.Context SITE_CONTEXT = new Site.Context(){
         public String getParentSiteName(final SiteContext siteContext) {
             // we have to do this manually instead of using SiteConfiguration,
@@ -106,7 +110,11 @@
             final PropertiesLoader loader
                     = UrlResourceLoader.newPropertiesLoader(siteContext, 
Site.CONFIGURATION_FILE, props);
             loader.abut();
-            return props.getProperty(Site.PARENT_SITE_KEY);
+            final String parentName = props.getProperty(Site.PARENT_SITE_KEY);
+            if(null == parentName && 0 == props.size()){
+                throw new IllegalArgumentException("Invalid site " + 
siteContext.getSite());
+            }
+            return parentName;
         }
     };
 
@@ -158,9 +166,9 @@
             if(request instanceof HttpServletRequest) {
                 final HttpServletRequest httpServletRequest = 
(HttpServletRequest) request;
                 final HttpServletResponse  httpServletResponse = 
(HttpServletResponse) response;
-                if 
(httpServletRequest.getRequestURI().endsWith(CONFIGURATION_RESOURCE)){
+                if 
(httpServletRequest.getRequestURI().contains(CONFIGURATION_RESOURCE)){
                     /* We are looping, looking for a site search which does 
not exsist */
-                    LOG.debug("We are looping, looking for a site search which 
does not exist");
+                    LOG.info("We are looping, looking for a site search which 
does not exist");
                     httpServletResponse.reset();
                     
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
                     return;
@@ -295,7 +303,7 @@
     /** The method to obtain the correct Site from the request.
      * It only returns a site with a locale supported by that site.
      ** @param servletRequest 
-     * @return 
+     * @return the site instance. or null if no such skin has been deployed.
      */
     public static Site getSite(final ServletRequest servletRequest) {
         // find the current site. Since we are behind a ajp13 connection 
request.getServerName() won't work!
@@ -313,52 +321,58 @@
 
         // Construct the site object off the browser's locale, even if it 
won't finally be used.
         final Locale locale = servletRequest.getLocale();
-        final Site result = Site.valueOf(SITE_CONTEXT, correctedVhost, locale);
-        final SiteConfiguration.Context siteConfCxt = new 
SiteConfiguration.Context(){
-            public PropertiesLoader newPropertiesLoader(
-                    final SiteContext siteCxt,
-                    final String resource,
-                    final Properties properties) {
+        final Site result;
+        try{
+            result = Site.valueOf(SITE_CONTEXT, correctedVhost, locale);
+            
+            final SiteConfiguration.Context siteConfCxt = new 
SiteConfiguration.Context(){
+                public PropertiesLoader newPropertiesLoader(
+                        final SiteContext siteCxt,
+                        final String resource,
+                        final Properties properties) {
 
-                return UrlResourceLoader.newPropertiesLoader(siteCxt, 
resource, properties);
+                    return UrlResourceLoader.newPropertiesLoader(siteCxt, 
resource, properties);
+                }
+                public Site getSite() {
+                    return result;
+                }
+            };
+            final SiteConfiguration siteConf = 
SiteConfiguration.instanceOf(siteConfCxt);
+            servletRequest.setAttribute(SiteConfiguration.NAME_KEY, siteConf);
+
+            if(LOG.isTraceEnabled()){ // MessageFormat.format(..) is expensive
+                LOG.trace(MessageFormat.format(
+                        LOCALE_DETAILS, locale.getLanguage(), 
locale.getCountry(), locale.getVariant()));
             }
-            public Site getSite() {
+
+            // Check if the browser's locale is supported by this skin. Use it 
if so.
+            if( siteConf.isSiteLocaleSupported(locale) ){
                 return result;
             }
-        };
-        final SiteConfiguration siteConf = 
SiteConfiguration.instanceOf(siteConfCxt);
-        servletRequest.setAttribute(SiteConfiguration.NAME_KEY, siteConf);
 
-        if(LOG.isTraceEnabled()){ // MessageFormat.format(..) is expensive
-            LOG.trace(MessageFormat.format(
-                    LOCALE_DETAILS, locale.getLanguage(), locale.getCountry(), 
locale.getVariant()));
-        }
-        
-        // Check if the browser's locale is supported by this skin. Use it if 
so.
-        if( siteConf.isSiteLocaleSupported(locale) ){
-            return result;
-        }
-        
-        // Use the skin's default locale. For some reason that fails use JVM's 
default.
-        final String[] prefLocale = null != 
siteConf.getProperty(SiteConfiguration.SITE_LOCALE_DEFAULT)
-                ? 
siteConf.getProperty(SiteConfiguration.SITE_LOCALE_DEFAULT).split("_")
-                : new String[]{Locale.getDefault().toString()};
+            // Use the skin's default locale. For some reason that fails use 
JVM's default.
+            final String[] prefLocale = null != 
siteConf.getProperty(SiteConfiguration.SITE_LOCALE_DEFAULT)
+                    ? 
siteConf.getProperty(SiteConfiguration.SITE_LOCALE_DEFAULT).split("_")
+                    : new String[]{Locale.getDefault().toString()};
 
-        switch(prefLocale.length){
+            switch(prefLocale.length){
 
-            case 3:
-                LOG.trace(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0] + 
'_' + prefLocale[1] + '_' + prefLocale[2]);
-                return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0], prefLocale[1], prefLocale[2]));
+                case 3:
+                    LOG.trace(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0] 
+ '_' + prefLocale[1] + '_' + prefLocale[2]);
+                    return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0], prefLocale[1], prefLocale[2]));
 
-            case 2:
-                LOG.trace(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0] + 
'_' + prefLocale[1]);
-                return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0], prefLocale[1]));
+                case 2:
+                    LOG.trace(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0] 
+ '_' + prefLocale[1]);
+                    return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0], prefLocale[1]));
 
-            case 1:
-            default:
-                LOG.trace(result+INFO_USING_DEFAULT_LOCALE + prefLocale[0]);
-                return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0]));
+                case 1:
+                default:
+                    LOG.trace(result+INFO_USING_DEFAULT_LOCALE + 
prefLocale[0]);
+                    return Site.valueOf(SITE_CONTEXT, correctedVhost, new 
Locale(prefLocale[0]));
 
+            }
+        }catch(IllegalArgumentException iae){
+            return null;
         }
     }
 
@@ -421,22 +435,28 @@
         LOG.trace("doBeforeProcessing()");
 
         final Site site = getSite(request);
+        
+        if(null != site){
 
-        final DataModel dataModel = getDataModel(request);
+            final DataModel dataModel = getDataModel(request);
 
-        if (null != dataModel && !dataModel.getSite().getSite().equals(site)) {
-            LOG.warn(WARN_FAULTY_BROWSER + 
dataModel.getBrowser().getUserAgent().getXmlEscaped());
-            // DataModelFilter will correct it
-        }
+            if (null != dataModel && 
!dataModel.getSite().getSite().equals(site)) {
+                LOG.warn(WARN_FAULTY_BROWSER + 
dataModel.getBrowser().getUserAgent().getXmlEscaped());
+                // DataModelFilter will correct it
+            }
 
-        request.setAttribute(Site.NAME_KEY, site);
-        request.setAttribute("startTime", FindResource.START_TIME);
-        MDC.put(Site.NAME_KEY, site.getName());
-        MDC.put("UNIQUE_ID", getRequestId(request));
+            request.setAttribute(Site.NAME_KEY, site);
+            request.setAttribute("startTime", FindResource.START_TIME);
+            MDC.put(Site.NAME_KEY, site.getName());
+            MDC.put("UNIQUE_ID", getRequestId(request));
+
+            /* Setting default encoding */
+            request.setCharacterEncoding("UTF-8");
+            response.setCharacterEncoding("UTF-8");
         
-        /* Setting default encoding */
-        request.setCharacterEncoding("UTF-8");
-        response.setCharacterEncoding("UTF-8");
+        }else{
+            throw new ServletException("SiteLocatorFilter with no Site :-(");
+        }
     }
 
     private void doAfterProcessing(final ServletRequest request, final 
ServletResponse response)

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to