Author: ssmaeklu
Date: 2007-06-09 18:16:40 +0200 (Sat, 09 Jun 2007)
New Revision: 5296

Modified:
   
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLResourceLoader.java
   
trunk/core-api/src/test/java/no/schibstedsok/searchportal/mode/config/UrlResourcesSiteConfigurationtest.java
   
trunk/core-api/src/test/java/no/schibstedsok/searchportal/view/velocity/VelocityTemplateTest.java
   
trunk/httpclient-api/src/main/java/no/schibstedsok/searchportal/http/HTTPClient.java
   
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/AbstractResourceLoader.java
   
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/UrlResourceLoader.java
   
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/SiteTestCase.java
   
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/config/FileResourceLoader.java
   trunk/site-spi/src/test/resources/log4j.xml
   
trunk/war/src/main/java/no/schibstedsok/searchportal/http/filters/SiteLocatorFilter.java
Log:
Rollback of r5295

Modified: 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLResourceLoader.java
===================================================================
--- 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLResourceLoader.java
      2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLResourceLoader.java
      2007-06-09 16:16:40 UTC (rev 5296)
@@ -3,7 +3,6 @@
 
 import java.io.IOException;
 import java.net.URL;
-import java.net.MalformedURLException;
 import java.io.InputStream;
 import no.schibstedsok.searchportal.http.HTTPClient;
 import no.schibstedsok.searchportal.site.config.UrlResourceLoader;
@@ -36,8 +35,9 @@
 public class URLResourceLoader extends ResourceLoader {
     
     public interface Context{
-        boolean doesUrlExist(final URL url);
-        URL getURL(final String resource, final Site site);
+        boolean doesUrlExist(final String url, final String hostHeader);
+        String getURL(final String resource);
+        String getHostHeader(final String resource);
     }
 
     // Constants -----------------------------------------------------
@@ -60,7 +60,7 @@
     // Static --------------------------------------------------------
     
     private static Context context = new DefaultContext();
-
+    
     // Allows the tests to switch the Velocity ResourceLoader over to a file 
based one.
     static void setContext(final Context context){
         URLResourceLoader.context = context;
@@ -113,17 +113,31 @@
     /** [EMAIL PROTECTED]
      */
     public long getLastModified(Resource resource){
+
         try{
-            final URL url = findUrl(resource.getName(), site);
-            final HTTPClient client = HTTPClient.instance(url, "localhost");
 
-            return client.getLastModified("");
+            final String url = findUrl(resource.getName(), site);
+            final URL u = new URL(url);
+
+            if( LOG.isTraceEnabled() ){
+                LOG.trace(DEBUG_FULL_URL_IS + u);
+                LOG.trace(DEBUG_HOST_HEADER_IS + context.getHostHeader(url));
+            }
+
+            final HTTPClient client = HTTPClient.instance(
+                    u.getProtocol() + "://" + u.getHost(), 
+                    u.getPort(), 
+                    context.getHostHeader(url));
+
+            return client.getLastModified(u.getPath());
+
         }catch( ResourceNotFoundException e ){
             LOG.error( ERR_RESOURCE_NOT_FOUND + resource.getName() );
         }catch( IOException e ){
             LOG.error( ERR_RESOURCE_NOT_FOUND + resource.getName() );
         }
         return 0;
+
     }
 
     
@@ -133,7 +147,7 @@
 
     // Private -------------------------------------------------------
     
-    private static URL findUrl(final String url, final Site currentSite) 
throws ResourceNotFoundException{
+    private static String findUrl(final String url, final Site currentSite) 
throws ResourceNotFoundException{
 
         try{
             LOG.trace(DEBUG_LOOKING_FOR + url );
@@ -145,13 +159,11 @@
         }
     }
 
-    private static URL findUrlImpl(final String url, final Site currentSite)
+    private static String findUrlImpl(final String url, final Site currentSite)
             throws IOException, ResourceNotFoundException {
 
-        final URL u = context.getURL(url, currentSite);
-
-        if (context.doesUrlExist(u)) {
-            return u;
+        if 
(context.doesUrlExist(context.getURL(url),context.getHostHeader(url))) {
+            return url;
         } else {
             final Site parent = currentSite.getParent();
 
@@ -177,11 +189,24 @@
     }
 
 
-    private static InputStream getStream(final URL url) throws IOException{
+    private static InputStream getStream(final String url) throws IOException{
 
-        final HTTPClient client = HTTPClient.instance(url, "localhost");
+        LOG.trace(DEBUG_EXISTS + url);
+        final URL u = new URL(context.getURL(url));
+        
+        if( LOG.isTraceEnabled() ){
+            LOG.trace(DEBUG_FULL_URL_IS + u);
+            LOG.trace(DEBUG_HOST_HEADER_IS + context.getHostHeader(url));
+        }
+        
+        final HTTPClient client = HTTPClient.instance(
+                u.getProtocol() + "://" +u.getHost(), 
+                u.getPort(), 
+                context.getHostHeader(url));
+        
         try{
-            return client.getBufferedStream("");
+            return client.getBufferedStream(u.getPath());
+            
         }catch(IOException ioe){
             throw client.interceptIOException(ioe);
         }
@@ -189,19 +214,20 @@
     
     
     // Inner classes -------------------------------------------------
-
+    
     private static final class DefaultContext implements Context{
-        public boolean doesUrlExist(final URL url) {
-            return UrlResourceLoader.doesUrlExist(url);
+        
+        public boolean doesUrlExist(final String url, final String hostHeader) 
{
+            return UrlResourceLoader.doesUrlExist(url, hostHeader);
         }
 
-        public URL getURL(final String resource, final Site site) {
-            try {
-                return new URL(resource);
-            } catch (MalformedURLException e) {
-                throw new ResourceNotFoundException(e); 
-            }
+        public String getURL(final String resource) {
+            return UrlResourceLoader.getURL(resource);
         }
+
+        public String getHostHeader(final String resource) {
+            return UrlResourceLoader.getHostHeader(resource);
+        }
     }
 }
 

Modified: 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/mode/config/UrlResourcesSiteConfigurationtest.java
===================================================================
--- 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/mode/config/UrlResourcesSiteConfigurationtest.java
        2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/mode/config/UrlResourcesSiteConfigurationtest.java
        2007-06-09 16:16:40 UTC (rev 5296)
@@ -23,7 +23,6 @@
  * @author <a href="mailto:[EMAIL PROTECTED]">Michael Semb Wever</a>
  * @version $Id: UrlResourcesSiteConfigurationtest.java 3359 2006-08-03 
08:13:22Z mickw $
  */
[EMAIL PROTECTED](groups = {"requires-tomcat"})
 public final class UrlResourcesSiteConfigurationtest extends SiteTestCase {
 
     private static final String FAIL_CONFIG_NOT_RUNNING =

Modified: 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/view/velocity/VelocityTemplateTest.java
===================================================================
--- 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/view/velocity/VelocityTemplateTest.java
   2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/view/velocity/VelocityTemplateTest.java
   2007-06-09 16:16:40 UTC (rev 5296)
@@ -14,8 +14,6 @@
 import java.io.StringWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
-import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -136,27 +134,18 @@
             });
         }
 
-        public boolean doesUrlExist(final URL url) {
-            LOG.info("XYZ Checking existence of " + url);
-
-            boolean exists = urlExists(url);
-
-            if (exists) {
-                LOG.info("XYZ exists " + url);
-            } else {
-                LOG.info("XYZ NOT exists " + url);
-            }
-
-
-            return exists;
+        public boolean doesUrlExist(final String url, final String hostHeader) 
{
+            return urlExists(url);
         }
 
-        public URL getURL(final String resource, final Site site) {
+        public String getURL(final String resource) {
+
             LOG.trace("getURL(" + resource + ')');
 
             try{
 
-                final String siteFolder = site.getConfigContext();
+                String siteFolder = resource.substring(resource.indexOf("//") 
+ 2);
+                siteFolder = siteFolder.substring(0, siteFolder.indexOf('/'));
 
                 final String base = System.getProperty("basedir") // test jvm 
sets this property
                         + (System.getProperty("basedir").endsWith("war") ? 
"/../../" : "/../")
@@ -171,13 +160,15 @@
                 return new URI("file://"
                         + base
                         + (wf.exists() && wf.isDirectory() ? 
"/war/src/main/templates/" : "/src/main/templates/")
-                        + rsc).normalize().toURL();
+                        + rsc).normalize().toString();
 
             }catch (URISyntaxException ex) {
                 throw new ResourceLoadException(ex.getMessage(), ex);
-            } catch (final MalformedURLException ex) {
-                throw new ResourceLoadException(ex.getMessage(), ex);
             }
         }
+
+        public String getHostHeader(final String resource) {
+            return "";
+        }
     }
 }

Modified: 
trunk/httpclient-api/src/main/java/no/schibstedsok/searchportal/http/HTTPClient.java
===================================================================
--- 
trunk/httpclient-api/src/main/java/no/schibstedsok/searchportal/http/HTTPClient.java
        2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/httpclient-api/src/main/java/no/schibstedsok/searchportal/http/HTTPClient.java
        2007-06-09 16:16:40 UTC (rev 5296)
@@ -19,10 +19,7 @@
 import java.net.SocketTimeoutException;
 import java.net.URL;
 import java.net.URLConnection;
-import java.net.URLStreamHandler;
-import java.net.JarURLConnection;
 import java.text.DecimalFormat;
-import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -47,115 +44,70 @@
     // Constants -----------------------------------------------------
 
     private static final Logger LOG = Logger.getLogger(HTTPClient.class);
-    private static final String DEBUG_USING_URL = "Using url {0} and 
Host-header {1} ";
+    private static final String DEBUG_USING_URL = "Using url ";
+    private static final String DEBUG_USING_HOSTHEADER = "Using host header: ";
 
     private final String id;
-    private URLConnection urlConn;
-    private final URL u;
+    private final String host, hostHeader;
+    private final int port;
+    private volatile URLConnection urlConn;
 
-    private HTTPClient(final URL u) {
-        this(u, null);
-    }
-                                                                               
            
-    private HTTPClient(final URL u, final String physicalHost) {
-        try {
-            this.u = new URL(u, "", new 
PhysicalHostStreamHandler(physicalHost));
-            this.id = u.getHost() + ':' + u.getPort();
-        } catch (final MalformedURLException e) {
-            throw new RuntimeException(e);
-        }
-    }
+    private HTTPClient(final String host, final int port, final String 
hostHeader) {
 
-    private HTTPClient(final String hostHeader, final URL u) {
-        try {
-            this.u = new URL(u, "", new HostHeaderStreamHandler(hostHeader));
-            this.id = u.getHost() + ':' + u.getPort();
-        } catch (MalformedURLException e) {
-            throw new RuntimeException(e);
+        this.host = host;
+        this.port = port;
+        this.hostHeader = hostHeader;
+
+        String id = host + ':' + port;
+        if (id.contains("://")) {
+            id = id.substring(id.indexOf("://") + 3);
         }
+        this.id = id;
+
     }
 
-
     /**
-     * Returns client for specified host and port.
-     *
-     * @param host The host to use. If no protocol is given then http is 
assumed.
-     * @param port The port to use.
-     *
-     * @return a client.
+     * @param host is really protocol AND host in the format: 
http://myhost.com. If only host is supplied http will be used.
+     * @param port if theis is < 0 default port for protocol is used.
+     * @return
      */
     public static HTTPClient instance(final String host, final int port) {
-        return instance(ensureProtocol(host), port, null);
+
+        return new HTTPClient(host, port, host);
     }
 
     /**
-     * Returns client for specified host, port and host header. Useful if you 
need to use a virtual host different
-     * from the physical host.
-     *
-     * @param host the physical host to use.
-     * @param port the port to use.
-     * @param hostHeader virtual host to use.
-     *
-     * @return a client.
+     * @param host       is really protocol AND host in the format: 
http://myhost.com. If only host is supplied http will be used.
+     * @param port       if theis is < 0 default port for protocol is used.
+     * @param hostHeader
+     * @return
      */
     public static HTTPClient instance(final String host, final int port, final 
String hostHeader) {
-        try {
-            return new HTTPClient(hostHeader, new URL(ensureProtocol(host) + 
':' + port));
-        } catch (MalformedURLException e) {
-            throw new RuntimeException(e);
-        }
-    }
 
-    /**
-     * Returns client instance for the specified URL. The URL can either be 
complete or just contain the host.
-     * The path can be supplied later when using the querying methods like
-     * [EMAIL PROTECTED] HTTPClient#getBufferedStream(String path)}.
-     *
-     * @param url The URL.
-     * @return a client.
-     */
-    public static HTTPClient instance(final URL url) {
-        return new HTTPClient(url);
+        return new HTTPClient(host, port, hostHeader);
     }
 
-    /**
-     * Returns client instance for the specified URL and physical host. Use 
this if the virtual host is different from
-     * the physcical host. The original host in the URL will be replaced by 
the supplied physical host and and the
-     * original host will instead be used as a host header.
-     *
-     * @param url The url.
-     * @param physicalHost The physical host.
-     *
-     * @return a client.
-     */
-    public static HTTPClient instance(final URL url, final String 
physicalHost) {
-        return new HTTPClient(url, physicalHost);
-    }
+    private URL getURL(final String path) throws MalformedURLException {
 
-    /**
-     * Returns client for the url. The client will use the supplied host 
haeder for all requests.
-     *
-     * @param hostHeader host haeder to use.
-     * @param url url.
-     * @return a client.
-     */
-    public static HTTPClient instance(final String hostHeader, final URL url) {
-        return new HTTPClient(hostHeader, url);
-    }
+        if (port == 0) {
+            throw new MalformedURLException("Null port");
+        }
 
-    /**
-     * Convenience method to create a URL with an attached URLStreamHandler.
-     * This stream handler will replace the host of the supplied
-     * URL with the supplied physical host. The original host will be used as 
a host header.
-     *
-     * @param url the original url.
-     * @param physicalHost the physical host to use.
-     * @return a url with the host replaces.
-     *
-     * @throws MalformedURLException on error.
-     */
-    public static URL getURL(final URL url, final String physicalHost) throws 
MalformedURLException {
-        return new URL(url, "", new PhysicalHostStreamHandler(physicalHost));
+        final boolean hasProtocol = host.startsWith("http://";) || 
host.startsWith("https://";)
+                || host.startsWith("ftp://";) || host.startsWith("file://");
+
+        final StringBuilder sb = new StringBuilder();
+        if (!hasProtocol) {
+            sb.append("http://";);
+        }
+        sb.append(host);
+        if (port > 0) {
+            sb.append(':').append(port);
+        }
+        sb.append(path);
+        final URL url = new URL(sb.toString());
+        LOG.trace(DEBUG_USING_URL + url);
+        return url;
     }
 
     /**
@@ -168,6 +120,12 @@
 
         loadUrlConnection(path);
 
+        if (!hostHeader.equals(host)) {
+
+            LOG.debug(DEBUG_USING_HOSTHEADER + hostHeader);
+            urlConn.addRequestProperty("host", hostHeader);
+        }
+
         final DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
         final DocumentBuilder builder;
 
@@ -276,27 +234,29 @@
         boolean success = false;
         loadUrlConnection(path);
 
-        if (urlConn instanceof HttpURLConnection  || urlConn instanceof 
JarURLConnection) {
+        if (urlConn instanceof HttpURLConnection) {
             try {
 
-                if (urlConn instanceof HttpURLConnection) {
-                    
((HttpURLConnection)urlConn).setInstanceFollowRedirects(false);
-                    ((HttpURLConnection)urlConn).setRequestMethod("HEAD");
-                    success = HttpURLConnection.HTTP_OK == 
((HttpURLConnection)urlConn).getResponseCode();
-                } else {
-                    success = urlConn.getContentLength() > 0;
-                }
+                final HttpURLConnection con = (HttpURLConnection) urlConn;
+                con.setInstanceFollowRedirects(false);
+                con.setRequestMethod("HEAD");
+                con.addRequestProperty("host", hostHeader);
+                con.setConnectTimeout(1000);
+                con.setReadTimeout(1000);
+                success = HttpURLConnection.HTTP_OK == con.getResponseCode();
+
             } catch (IOException e) {
                 throw interceptIOException(e);
 
             } finally {
                 urlConn = null;
             }
+
         } else {
+
             final File file = new File(path);
             success = file.exists();
         }
-
         return success;
     }
 
@@ -367,16 +327,18 @@
     }
 
     private URLConnection loadUrlConnection(final String path) throws 
IOException {
+
         if (null == urlConn) {
-            urlConn = new URL(u, path).openConnection();
+            urlConn = getURL(path).openConnection();
+
+            if (!hostHeader.equals(host)) {
+                LOG.trace(DEBUG_USING_HOSTHEADER + hostHeader);
+                urlConn.addRequestProperty("host", hostHeader);
+            }
         }
         return urlConn;
     }
 
-    private static String ensureProtocol(final String host) {
-        return host.contains("://") ? host : "http://"; + host;
-    }
-
     private static void cleanErrorStream(final HttpURLConnection con) {
 
         if (null != con.getErrorStream()) {
@@ -396,63 +358,64 @@
         }
     }
 
-    private static class PhysicalHostStreamHandler extends URLStreamHandler {
+    // --HTTPClient implementation to allow keepalive or pipelining.
+    // --  see revision 3596 for original implementation
 
-        private final String physicalHost;
+//    private static final String DEBUG_ADDING_CONF = "Adding configuration ";
 
-        public PhysicalHostStreamHandler(final String physicalHost) {
-            this.physicalHost = physicalHost;
-        }
+//    private static final Map<String,HostConfiguration> hostConfigurations = 
new HashMap<String,HostConfiguration>();
+//
+//    private static final HttpConnectionManager cMgr = new 
ConnectionManagerWithoutKeepAlive();
+//    private HttpClient commonsHttpClient;
 
-        protected URLConnection openConnection(final URL u) throws IOException 
{
-            final URL url = new URL(u.getProtocol(), physicalHost, 
u.getPort(), u.getFile());
+//    private static final HTTPClient client = new HTTPClient();
 
-            final URLConnection connection = url.openConnection();
+//    private HTTPClient() {
+//        final HttpConnectionManagerParams params = new 
HttpConnectionManagerParams();
+//        params.setStaleCheckingEnabled(true);
+//        params.setMaxTotalConnections(Integer.MAX_VALUE                      
                                                                                
                                                                                
                                                                                
                                                                                
   );
+//        if(Logger.getRootLogger().getLevel().isGreaterOrEqual(Level.INFO)){
+//            params.setSoTimeout(3000);
+//        }
+//        cMgr.setParams(params);
+//        commonsHttpClient = new HttpClient(cMgr);
+//    }
 
-            connection.addRequestProperty("host", u.getHost());
-            connection.setConnectTimeout(1000);
-            connection.setReadTimeout(1000);
+//    public static HTTPClient instance(final String id, final String host, 
final int port) {
+//
+//        if (!hostConfigurations.containsKey(id)) {
+//            addHostConfiguration(id, host, port);
+//        }
+//    }
 
-            if (LOG.isTraceEnabled()) {
-                LOG.trace(MessageFormat.format(DEBUG_USING_URL, url, 
u.getHost()));
-            }
+//    private HttpMethod executeGet(final String id, final String path) throws 
IOException {
+//
+//        final HostConfiguration conf = (HostConfiguration) 
hostConfigurations.get(id);
+//        final HttpMethod method = new GetMethod(path);
+//        commonsHttpClient.executeMethod(conf, method);
+//        return method;
+//    }
 
-            return connection;
-        }
-    }
+//    private void release(final HttpMethod method) {
+//        method.releaseConnection();
+//    }
+//
+//    private synchronized static void addHostConfiguration(final String id, 
final String host, final int port) {
+//
+//        if (! hostConfigurations.containsKey(id)) {
+//
+//            final HostConfiguration conf = new HostConfiguration();
+//            LOG.debug(DEBUG_ADDING_CONF + host + ":" + port);
+//            conf.setHost(host, port, "http");
+//            cMgr.getParams().setMaxConnectionsPerHost(conf, 1);
+//            hostConfigurations.put(id, conf);
+//        }
+//    }
 
-    private static class HostHeaderStreamHandler extends URLStreamHandler {
 
-        private final String hostHeader;
+    private static class Statistic implements Comparable<Statistic> {
 
-        public HostHeaderStreamHandler(final String hostHeader) {
-            this.hostHeader = hostHeader;
-        }
 
-        protected URLConnection openConnection(final URL u) throws IOException 
{
-
-
-            final URL url = new URL(u.getProtocol(), u.getHost(), u.getPort(), 
u.getFile());
-            final URLConnection connection = url.openConnection();
-
-            if (u.getHost().equals(hostHeader)) {
-                connection.addRequestProperty("host", hostHeader);
-            }
-
-            connection.setConnectTimeout(1000);
-            connection.setReadTimeout(1000);
-
-            if (LOG.isTraceEnabled()) {
-                LOG.trace(MessageFormat.format(DEBUG_USING_URL, url, 
hostHeader));
-            }
-
-            return connection;
-        }
-    }
-
-    private static final class Statistic implements Comparable<Statistic> {
-
-
         private static final Map<String, Statistic> STATISTICS = new 
ConcurrentHashMap<String, Statistic>();
 
         private static final Logger STATISTICS_LOG = 
Logger.getLogger(Statistic.class);

Modified: 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/AbstractResourceLoader.java
===================================================================
--- 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/AbstractResourceLoader.java
   2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/AbstractResourceLoader.java
   2007-06-09 16:16:40 UTC (rev 5296)
@@ -20,7 +20,6 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.ThreadPoolExecutor;
-import java.net.URL;
 import javax.xml.parsers.DocumentBuilder;
 import no.schibstedsok.searchportal.site.Site;
 import no.schibstedsok.searchportal.site.SiteContext;
@@ -107,11 +106,13 @@
         context = cxt;
     }
     
-    public abstract boolean urlExists(URL url);
+    public abstract boolean urlExists(String url);
     
-    protected abstract URL getResource(final Site site);
+    protected abstract String getResource(final Site site);
     
-    protected abstract InputStream getInputStreamFor(final URL resource);
+    protected abstract String getUrlFor(final String resource);
+    
+    protected abstract InputStream getInputStreamFor(final String resource);
 
     /** Get the SiteContext.
      [EMAIL PROTECTED] the SiteContext.
@@ -274,13 +275,13 @@
         }
     }
 
-    private boolean loadEmptyResource(final URL url) {
+    private boolean loadEmptyResource(final String resource) {
         
         LOG.debug("Loading empty resource for " + resource);
 
         switch(resourceType){
             case PROPERTIES:
-                props.put(context.getSite().getName(), url);
+                props.put(context.getSite().getName(), getUrlFor(resource));
                 break;
 
             case DOM_DOCUMENT:
@@ -297,13 +298,13 @@
     }
     
     
-    private boolean loadResource(final URL url) {
+    private boolean loadResource(final String resource) {
 
         boolean success = false;
 
-        if(urlExists(url)){
+        if(urlExists(resource)){
             
-            final InputStream is = getInputStreamFor(url);
+            final InputStream is = getInputStreamFor(resource);
 
             try {
 
@@ -316,7 +317,7 @@
                         final Properties newProps = new Properties();
                         newProps.load(is);
                         
-                        props.put(context.getSite().getName(), url);
+                        props.put(context.getSite().getName(), 
getUrlFor(resource));
                         
                         for(Object p : newProps.keySet()){
                             
@@ -342,28 +343,28 @@
                         break;
                 }
 
-                LOG.info(readResourceDebug(url));
+                LOG.info(readResourceDebug(resource));
                 success = true;
 
             } catch (NullPointerException e) {
-                LOG.warn(readResourceDebug(url), e);
+                LOG.warn(readResourceDebug(resource), e);
 
             } catch (IOException e) {
-                LOG.warn(readResourceDebug(url), e);
+                LOG.warn(readResourceDebug(resource), e);
                 
             } catch (SAXParseException e) {
                 throw new ResourceLoadException(
-                        readResourceDebug(url) + " at " + e.getLineNumber() + 
":" + e.getColumnNumber(), e);
+                        readResourceDebug(resource) + " at " + 
e.getLineNumber() + ":" + e.getColumnNumber(), e);
                 
             } catch (SAXException e) {
-                throw new ResourceLoadException(readResourceDebug(url), e);
+                throw new ResourceLoadException(readResourceDebug(resource), 
e);
                 
             }finally{
                 if( null != is ){
                     try{
                         is.close();
                     }catch(IOException ioe){
-                        LOG.warn(readResourceDebug(url), ioe);
+                        LOG.warn(readResourceDebug(resource), ioe);
                     }
                 }
             }
@@ -371,7 +372,7 @@
         return success;
     }
     
-    protected String readResourceDebug(final URL url){
+    protected String readResourceDebug(final String resource){
         
         return "Read Configuration from " + resource;
     }

Modified: 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/UrlResourceLoader.java
===================================================================
--- 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/UrlResourceLoader.java
        2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/UrlResourceLoader.java
        2007-06-09 16:16:40 UTC (rev 5296)
@@ -13,9 +13,9 @@
 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.HttpURLConnection;
 import java.net.SocketTimeoutException;
 import java.net.URL;
-import java.net.MalformedURLException;
 import java.util.Properties;
 import javax.xml.parsers.DocumentBuilder;
 import no.schibstedsok.searchportal.http.HTTPClient;
@@ -32,9 +32,9 @@
 
     // Constants -----------------------------------------------------
 
-    private static final GeneralCacheAdministrator PRESENCE_CACHE = new 
GeneralCacheAdministrator();
+    private static final GeneralCacheAdministrator PRESENCE_CACHE = new 
GeneralCacheAdministrator();  
     private static final int REFRESH_PERIOD = 60; // one minute
-
+    
     private static final Logger LOG = 
Logger.getLogger(UrlResourceLoader.class);
 
     private static final String DEBUG_CHECKING_EXISTANCE_OF = "Checking 
existance of ";
@@ -91,48 +91,72 @@
         return bcLoader;
     }
 
-    public static boolean doesUrlExist(final URL url){
+    public static boolean doesUrlExist(final String url, final String 
hostHeader){
 
         boolean success = false;
         
         try{
-            success = (Boolean)PRESENCE_CACHE.getFromCache(url.toString(), 
REFRESH_PERIOD);
+            success = (Boolean)PRESENCE_CACHE.getFromCache(url, 
REFRESH_PERIOD);
             
         }catch(NeedsRefreshException nre){
            
             boolean updatedCache = false;
+            HttpURLConnection con = null;
             try {
 
-                LOG.trace(DEBUG_CHECKING_EXISTANCE_OF + url + " is " + 
success);
+                final URL u = new URL(url);
 
-                success = HTTPClient.instance(url, "localhost").exists("");
-                PRESENCE_CACHE.putInCache(url.toString(), success);
-
+                con = (HttpURLConnection) u.openConnection();
+                con.setInstanceFollowRedirects(false);
+                con.setRequestMethod("HEAD");
+                con.addRequestProperty("host", hostHeader);
+                con.setConnectTimeout(1000);
+                con.setReadTimeout(1000);
+                
+                success = HttpURLConnection.HTTP_OK == con.getResponseCode();
+                PRESENCE_CACHE.putInCache(url, success);
                 updatedCache = true;
 
+                LOG.trace(DEBUG_CHECKING_EXISTANCE_OF + u + " is " + success);
+
             } catch (NullPointerException e) {
-                LOG.debug(url.toString(), e);
+                LOG.debug( '[' + hostHeader + "] " + url, e);
 
             } catch (SocketTimeoutException ste) {
-                LOG.debug(url.toString() + '\n' + ste);
+                LOG.debug( '[' + hostHeader + "] " + url + '\n' + ste);
 
             } catch (IOException e) {
-                LOG.warn(url.toString(), e);
+                LOG.warn( '[' + hostHeader + "] " + url, e);
 
             }  finally  {
                 
                 if(!updatedCache){ 
-                    PRESENCE_CACHE.cancelUpdate(url.toString());
+                    PRESENCE_CACHE.cancelUpdate(url);
                 }
+                if (con != null) {
+                    con.disconnect();
+                }
             }
         }
 
         return success;
     }
 
+    public static String getURL(final String resource){
+
+        return "http://localhost"+
+                resource.substring(resource.indexOf(':',8)>0 ? 
resource.indexOf(':',8) : resource.indexOf('/',8));
+    }
+
+    public static String getHostHeader(final String resource){
+
+        return resource.substring(7,resource.indexOf('/',8));
+    }
+
+
     // Constructors --------------------------------------------------
 
-    /**
+    /** [EMAIL PROTECTED]
      */
     protected UrlResourceLoader(final SiteContext cxt) {
         super(cxt);
@@ -143,9 +167,9 @@
 
 
     @Override
-    public final boolean urlExists(final URL url) {
+    public final boolean urlExists(final String url) {
 
-        return doesUrlExist(url);
+        return doesUrlExist(getUrlFor(url), getHostHeaderFor(url));
     }
 
 
@@ -158,44 +182,46 @@
     // Protected -----------------------------------------------------
 
     @Override
-    protected final URL getResource(final Site site) {
-        return getURL(getResource(), site);
+    protected final String getResource(final Site site) {
+
+        return "http://";
+                + site.getName()
+                + site.getConfigContext()
+                + (getResource().endsWith(".class") ? "classes/" : "conf/")
+                + getResource();
     }
 
-    public static URL getURL(final String resource, final Site site) {
-        try {
-            return new URL("http://";
-                        + site.getName()
-                        + site.getConfigContext()
-                        + (resource.endsWith(".class") ? "classes/" : "conf/")
-                        + resource);
-        } catch (MalformedURLException ex) {
-            throw new ResourceLoadException(ex.getMessage());
-        }
+    protected final String getHostHeaderFor(final String resource){
+
+        return getHostHeader(resource);
     }
 
     @Override
-    protected final InputStream getInputStreamFor(final URL url) {
+    protected String getUrlFor(final String resource){
 
+        return getURL(resource);
+    }
+
+    @Override
+    protected final InputStream getInputStreamFor(String resource) {
+
         HTTPClient client = null;
         try {
-            client = HTTPClient.instance(url, "localhost");
-            return client.getBufferedStream("");
+            final URL u = new URL(getUrlFor(resource));
+            client = HTTPClient.instance(u.getHost(), u.getPort(), 
getHostHeaderFor(resource));
+            
+            return client.getBufferedStream(u.getPath());
 
         }catch (IOException ex) {
-            throw new ResourceLoadException(ex.getMessage(), 
client.interceptIOException(ex));
+            throw new ResourceLoadException(ex.getMessage(), null != client ? 
client.interceptIOException(ex) : ex);
         }
 
 
     }
 
-    public static String getHostHeader(final String resource){
-        return resource.substring(7,resource.indexOf('/',8));
-    }
+    protected final String readResourceDebug(final String resource){
 
-    protected final String readResourceDebug(final URL url){
-
-        return "Read Configuration from " + url;
+        return "Read Configuration from " + getUrlFor(resource) + " [" + 
getHostHeaderFor(resource) + ']';
     }
 
     // Private -------------------------------------------------------

Modified: 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/SiteTestCase.java
===================================================================
--- 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/SiteTestCase.java
    2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/SiteTestCase.java
    2007-06-09 16:16:40 UTC (rev 5296)
@@ -12,10 +12,13 @@
 import java.util.Properties;
 import org.apache.log4j.Logger;
 import org.apache.log4j.MDC;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.AfterTest;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import no.schibstedsok.searchportal.site.Site;
 import no.schibstedsok.searchportal.site.Site.Context;
+import no.schibstedsok.searchportal.site.SiteContext;
 import no.schibstedsok.searchportal.site.config.FileResourceLoader;
 import no.schibstedsok.searchportal.site.config.PropertiesLoader;
 

Modified: 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/config/FileResourceLoader.java
===================================================================
--- 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/config/FileResourceLoader.java
       2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/config/FileResourceLoader.java
       2007-06-09 16:16:40 UTC (rev 5296)
@@ -12,7 +12,6 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
-import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -87,12 +86,19 @@
         super(cxt);
     }
 
-    public boolean urlExists(final URL url) {
+    public boolean urlExists(final String url) {
         
         try {
-            return new File(url.toURI()).exists();
+            final URL u = new URL(getUrlFor(url));
+
+            return new File(u.toURI()).exists();
+            
         }catch (URISyntaxException ex) {
             LOG.error(ex.getMessage(), ex);
+            
+        }catch (MalformedURLException ex) {
+            LOG.error(ex.getMessage(), ex);
+            
         }
 
         return false;
@@ -106,10 +112,9 @@
         if( projectName.indexOf(':') > 0 ){
             projectName = projectName.substring(0, projectName.indexOf(':'));
         }
-        if( projectName.endsWith("sesam/") && 
!"generic.sesam/".equals(projectName) ){
-            projectName = projectName.substring(0, projectName.length() - 1) + 
".no/";
+        if( projectName.endsWith("sesam") && 
!"generic.sesam".equals(projectName) ){
+            projectName = projectName + ".no";
         }
-
         if( "catalogue".equals(projectName)){
             projectName = "katalog.sesam.no";
         }
@@ -117,7 +122,7 @@
         return projectName;
     }
 
-    protected URL getResource(final Site site) {
+    protected String getResource(final Site site) {
         
         LOG.debug("getResource(" + site + ')');
         
@@ -128,18 +133,17 @@
 
             final File warFolder = new File(base + "/war");
 
-            URI uri = new URI("file://"
-                    + base
+            final String result = new URI("file://"
+                    + base 
                     + (warFolder.exists() && warFolder.isDirectory() ? 
"/war/target/classes/" : "/target/classes/")
-                    + getResource()).normalize();
+                    + getResource()).normalize().toString();
 
-            return uri.toURL();
+            LOG.debug("result: " + result);
+            return result;
          
         }catch (URISyntaxException ex) {
             throw new ResourceLoadException(ex.getMessage(), ex);
-        } catch (MalformedURLException ex) {
-            throw new ResourceLoadException(ex.getMessage(), ex);
-        }
+        } 
     }
 
     protected String getUrlFor(final String resource) {
@@ -158,11 +162,12 @@
         return "localhost";
     }
 
-    protected InputStream getInputStreamFor(URL url) {
+    protected InputStream getInputStreamFor(String resource) {
+
         try {
-            return url.openConnection().getInputStream();
-        } catch (IOException e) {
-            throw new ResourceLoadException(e.getMessage(), e);
+            return new FileInputStream(resource.replaceFirst("file:", ""));
+        }catch (FileNotFoundException ex) {
+            throw new ResourceLoadException(ex.getMessage(), ex);
         }
     }
 

Modified: trunk/site-spi/src/test/resources/log4j.xml
===================================================================
--- trunk/site-spi/src/test/resources/log4j.xml 2007-06-08 14:33:23 UTC (rev 
5295)
+++ trunk/site-spi/src/test/resources/log4j.xml 2007-06-09 16:16:40 UTC (rev 
5296)
@@ -19,7 +19,7 @@
     <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
         <param name="Target" value="System.err"/>
         <param name="encoding" value="UTF-8"/>
-        <param name="Threshold" value="DEBUG"/>
+        <param name="Threshold" value="INFO"/>
 
         <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%-5p %d{HH:mm:ss,SSS} 
(%F:%M)-%m%n"/>

Modified: 
trunk/war/src/main/java/no/schibstedsok/searchportal/http/filters/SiteLocatorFilter.java
===================================================================
--- 
trunk/war/src/main/java/no/schibstedsok/searchportal/http/filters/SiteLocatorFilter.java
    2007-06-08 14:33:23 UTC (rev 5295)
+++ 
trunk/war/src/main/java/no/schibstedsok/searchportal/http/filters/SiteLocatorFilter.java
    2007-06-09 16:16:40 UTC (rev 5296)
@@ -18,7 +18,6 @@
 import java.util.Properties;
 import java.util.UUID;
 import java.text.MessageFormat;
-import java.net.URL;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
@@ -37,7 +36,6 @@
 import no.schibstedsok.searchportal.site.config.UrlResourceLoader;
 import no.schibstedsok.searchportal.site.Site;
 import no.schibstedsok.searchportal.datamodel.DataModel;
-import no.schibstedsok.searchportal.http.HTTPClient;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.commons.lang.time.StopWatch;
 import org.apache.log4j.Logger;
@@ -376,7 +374,7 @@
         }
     }
 
-    private String recursivelyFindResource(final String resource, final Site 
site) throws IOException {
+    private String recursivelyFindResource(final String resource, final Site 
site) {
 
         // Problem with this approach is that skins can be updated without the 
server restarting (& updating START_TIME)
         // TODO an alternative approach would be to collect the lastModified 
timestamp of the resource and use it.
@@ -386,9 +384,7 @@
 
         final String url = HTTP + site.getName() + site.getConfigContext() + 
'/' + datedResource;
 
-        final URL u = HTTPClient.getURL(new URL(url), "localhost");
-
-        if (UrlResourceLoader.doesUrlExist(u)) {
+        if (UrlResourceLoader.doesUrlExist(UrlResourceLoader.getURL(url), 
UrlResourceLoader.getHostHeader(url))) {
             // return a relative url to ensure it can survice through an 
out-of-cluster server.
             return '/' + site.getConfigContext() + '/' + datedResource;
         } else if (site.getParent() != null) {

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

Reply via email to