Author: ssmaeklu
Date: 2007-06-08 16:33:23 +0200 (Fri, 08 Jun 2007)
New Revision: 5295

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:
Some refactoring of HTTPClient and resource loading. Preparation for SERCH-1743.

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 13:40:03 UTC (rev 5294)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLResourceLoader.java
      2007-06-08 14:33:23 UTC (rev 5295)
@@ -3,6 +3,7 @@
 
 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;
@@ -35,9 +36,8 @@
 public class URLResourceLoader extends ResourceLoader {
     
     public interface Context{
-        boolean doesUrlExist(final String url, final String hostHeader);
-        String getURL(final String resource);
-        String getHostHeader(final String resource);
+        boolean doesUrlExist(final URL url);
+        URL getURL(final String resource, final Site site);
     }
 
     // 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,31 +113,17 @@
     /** [EMAIL PROTECTED]
      */
     public long getLastModified(Resource resource){
-
         try{
+            final URL url = findUrl(resource.getName(), site);
+            final HTTPClient client = HTTPClient.instance(url, "localhost");
 
-            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());
-
+            return client.getLastModified("");
         }catch( ResourceNotFoundException e ){
             LOG.error( ERR_RESOURCE_NOT_FOUND + resource.getName() );
         }catch( IOException e ){
             LOG.error( ERR_RESOURCE_NOT_FOUND + resource.getName() );
         }
         return 0;
-
     }
 
     
@@ -147,7 +133,7 @@
 
     // Private -------------------------------------------------------
     
-    private static String findUrl(final String url, final Site currentSite) 
throws ResourceNotFoundException{
+    private static URL findUrl(final String url, final Site currentSite) 
throws ResourceNotFoundException{
 
         try{
             LOG.trace(DEBUG_LOOKING_FOR + url );
@@ -159,11 +145,13 @@
         }
     }
 
-    private static String findUrlImpl(final String url, final Site currentSite)
+    private static URL findUrlImpl(final String url, final Site currentSite)
             throws IOException, ResourceNotFoundException {
 
-        if 
(context.doesUrlExist(context.getURL(url),context.getHostHeader(url))) {
-            return url;
+        final URL u = context.getURL(url, currentSite);
+
+        if (context.doesUrlExist(u)) {
+            return u;
         } else {
             final Site parent = currentSite.getParent();
 
@@ -189,24 +177,11 @@
     }
 
 
-    private static InputStream getStream(final String url) throws IOException{
+    private static InputStream getStream(final URL url) throws IOException{
 
-        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));
-        
+        final HTTPClient client = HTTPClient.instance(url, "localhost");
         try{
-            return client.getBufferedStream(u.getPath());
-            
+            return client.getBufferedStream("");
         }catch(IOException ioe){
             throw client.interceptIOException(ioe);
         }
@@ -214,20 +189,19 @@
     
     
     // Inner classes -------------------------------------------------
-    
+
     private static final class DefaultContext implements Context{
-        
-        public boolean doesUrlExist(final String url, final String hostHeader) 
{
-            return UrlResourceLoader.doesUrlExist(url, hostHeader);
+        public boolean doesUrlExist(final URL url) {
+            return UrlResourceLoader.doesUrlExist(url);
         }
 
-        public String getURL(final String resource) {
-            return UrlResourceLoader.getURL(resource);
+        public URL getURL(final String resource, final Site site) {
+            try {
+                return new URL(resource);
+            } catch (MalformedURLException e) {
+                throw new ResourceNotFoundException(e); 
+            }
         }
-
-        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 13:40:03 UTC (rev 5294)
+++ 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/mode/config/UrlResourcesSiteConfigurationtest.java
        2007-06-08 14:33:23 UTC (rev 5295)
@@ -23,6 +23,7 @@
  * @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 13:40:03 UTC (rev 5294)
+++ 
trunk/core-api/src/test/java/no/schibstedsok/searchportal/view/velocity/VelocityTemplateTest.java
   2007-06-08 14:33:23 UTC (rev 5295)
@@ -14,6 +14,8 @@
 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;
@@ -134,18 +136,27 @@
             });
         }
 
-        public boolean doesUrlExist(final String url, final String hostHeader) 
{
-            return urlExists(url);
+        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 String getURL(final String resource) {
-
+        public URL getURL(final String resource, final Site site) {
             LOG.trace("getURL(" + resource + ')');
 
             try{
 
-                String siteFolder = resource.substring(resource.indexOf("//") 
+ 2);
-                siteFolder = siteFolder.substring(0, siteFolder.indexOf('/'));
+                final String siteFolder = site.getConfigContext();
 
                 final String base = System.getProperty("basedir") // test jvm 
sets this property
                         + (System.getProperty("basedir").endsWith("war") ? 
"/../../" : "/../")
@@ -160,15 +171,13 @@
                 return new URI("file://"
                         + base
                         + (wf.exists() && wf.isDirectory() ? 
"/war/src/main/templates/" : "/src/main/templates/")
-                        + rsc).normalize().toString();
+                        + rsc).normalize().toURL();
 
             }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 13:40:03 UTC (rev 5294)
+++ 
trunk/httpclient-api/src/main/java/no/schibstedsok/searchportal/http/HTTPClient.java
        2007-06-08 14:33:23 UTC (rev 5295)
@@ -19,7 +19,10 @@
 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;
@@ -44,70 +47,115 @@
     // Constants -----------------------------------------------------
 
     private static final Logger LOG = Logger.getLogger(HTTPClient.class);
-    private static final String DEBUG_USING_URL = "Using url ";
-    private static final String DEBUG_USING_HOSTHEADER = "Using host header: ";
+    private static final String DEBUG_USING_URL = "Using url {0} and 
Host-header {1} ";
 
     private final String id;
-    private final String host, hostHeader;
-    private final int port;
-    private volatile URLConnection urlConn;
+    private URLConnection urlConn;
+    private final URL u;
 
-    private HTTPClient(final String host, final int port, final String 
hostHeader) {
+    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);
+        }
+    }
 
-        this.host = host;
-        this.port = port;
-        this.hostHeader = hostHeader;
-
-        String id = host + ':' + port;
-        if (id.contains("://")) {
-            id = id.substring(id.indexOf("://") + 3);
+    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.id = id;
-
     }
 
+
     /**
-     * @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
+     * 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.
      */
     public static HTTPClient instance(final String host, final int port) {
-
-        return new HTTPClient(host, port, host);
+        return instance(ensureProtocol(host), port, null);
     }
 
     /**
-     * @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
+     * 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.
      */
     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);
+        }
+    }
 
-        return new HTTPClient(host, port, hostHeader);
+    /**
+     * 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);
     }
 
-    private URL getURL(final String path) throws MalformedURLException {
+    /**
+     * 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);
+    }
 
-        if (port == 0) {
-            throw new MalformedURLException("Null port");
-        }
+    /**
+     * 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);
+    }
 
-        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;
+    /**
+     * 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));
     }
 
     /**
@@ -120,12 +168,6 @@
 
         loadUrlConnection(path);
 
-        if (!hostHeader.equals(host)) {
-
-            LOG.debug(DEBUG_USING_HOSTHEADER + hostHeader);
-            urlConn.addRequestProperty("host", hostHeader);
-        }
-
         final DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
         final DocumentBuilder builder;
 
@@ -234,29 +276,27 @@
         boolean success = false;
         loadUrlConnection(path);
 
-        if (urlConn instanceof HttpURLConnection) {
+        if (urlConn instanceof HttpURLConnection  || urlConn instanceof 
JarURLConnection) {
             try {
 
-                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();
-
+                if (urlConn instanceof HttpURLConnection) {
+                    
((HttpURLConnection)urlConn).setInstanceFollowRedirects(false);
+                    ((HttpURLConnection)urlConn).setRequestMethod("HEAD");
+                    success = HttpURLConnection.HTTP_OK == 
((HttpURLConnection)urlConn).getResponseCode();
+                } else {
+                    success = urlConn.getContentLength() > 0;
+                }
             } catch (IOException e) {
                 throw interceptIOException(e);
 
             } finally {
                 urlConn = null;
             }
-
         } else {
-
             final File file = new File(path);
             success = file.exists();
         }
+
         return success;
     }
 
@@ -327,18 +367,16 @@
     }
 
     private URLConnection loadUrlConnection(final String path) throws 
IOException {
-
         if (null == urlConn) {
-            urlConn = getURL(path).openConnection();
-
-            if (!hostHeader.equals(host)) {
-                LOG.trace(DEBUG_USING_HOSTHEADER + hostHeader);
-                urlConn.addRequestProperty("host", hostHeader);
-            }
+            urlConn = new URL(u, path).openConnection();
         }
         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()) {
@@ -358,64 +396,63 @@
         }
     }
 
-    // --HTTPClient implementation to allow keepalive or pipelining.
-    // --  see revision 3596 for original implementation
+    private static class PhysicalHostStreamHandler extends URLStreamHandler {
 
-//    private static final String DEBUG_ADDING_CONF = "Adding configuration ";
+        private final String physicalHost;
 
-//    private static final Map<String,HostConfiguration> hostConfigurations = 
new HashMap<String,HostConfiguration>();
-//
-//    private static final HttpConnectionManager cMgr = new 
ConnectionManagerWithoutKeepAlive();
-//    private HttpClient commonsHttpClient;
+        public PhysicalHostStreamHandler(final String physicalHost) {
+            this.physicalHost = physicalHost;
+        }
 
-//    private static final HTTPClient client = new HTTPClient();
+        protected URLConnection openConnection(final URL u) throws IOException 
{
+            final URL url = new URL(u.getProtocol(), physicalHost, 
u.getPort(), u.getFile());
 
-//    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);
-//    }
+            final URLConnection connection = url.openConnection();
 
-//    public static HTTPClient instance(final String id, final String host, 
final int port) {
-//
-//        if (!hostConfigurations.containsKey(id)) {
-//            addHostConfiguration(id, host, port);
-//        }
-//    }
+            connection.addRequestProperty("host", u.getHost());
+            connection.setConnectTimeout(1000);
+            connection.setReadTimeout(1000);
 
-//    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;
-//    }
+            if (LOG.isTraceEnabled()) {
+                LOG.trace(MessageFormat.format(DEBUG_USING_URL, url, 
u.getHost()));
+            }
 
-//    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);
-//        }
-//    }
+            return connection;
+        }
+    }
 
+    private static class HostHeaderStreamHandler extends URLStreamHandler {
 
-    private static class Statistic implements Comparable<Statistic> {
+        private final String hostHeader;
 
+        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 13:40:03 UTC (rev 5294)
+++ 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/AbstractResourceLoader.java
   2007-06-08 14:33:23 UTC (rev 5295)
@@ -20,6 +20,7 @@
 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;
@@ -106,13 +107,11 @@
         context = cxt;
     }
     
-    public abstract boolean urlExists(String url);
+    public abstract boolean urlExists(URL url);
     
-    protected abstract String getResource(final Site site);
+    protected abstract URL getResource(final Site site);
     
-    protected abstract String getUrlFor(final String resource);
-    
-    protected abstract InputStream getInputStreamFor(final String resource);
+    protected abstract InputStream getInputStreamFor(final URL resource);
 
     /** Get the SiteContext.
      [EMAIL PROTECTED] the SiteContext.
@@ -275,13 +274,13 @@
         }
     }
 
-    private boolean loadEmptyResource(final String resource) {
+    private boolean loadEmptyResource(final URL url) {
         
         LOG.debug("Loading empty resource for " + resource);
 
         switch(resourceType){
             case PROPERTIES:
-                props.put(context.getSite().getName(), getUrlFor(resource));
+                props.put(context.getSite().getName(), url);
                 break;
 
             case DOM_DOCUMENT:
@@ -298,13 +297,13 @@
     }
     
     
-    private boolean loadResource(final String resource) {
+    private boolean loadResource(final URL url) {
 
         boolean success = false;
 
-        if(urlExists(resource)){
+        if(urlExists(url)){
             
-            final InputStream is = getInputStreamFor(resource);
+            final InputStream is = getInputStreamFor(url);
 
             try {
 
@@ -317,7 +316,7 @@
                         final Properties newProps = new Properties();
                         newProps.load(is);
                         
-                        props.put(context.getSite().getName(), 
getUrlFor(resource));
+                        props.put(context.getSite().getName(), url);
                         
                         for(Object p : newProps.keySet()){
                             
@@ -343,28 +342,28 @@
                         break;
                 }
 
-                LOG.info(readResourceDebug(resource));
+                LOG.info(readResourceDebug(url));
                 success = true;
 
             } catch (NullPointerException e) {
-                LOG.warn(readResourceDebug(resource), e);
+                LOG.warn(readResourceDebug(url), e);
 
             } catch (IOException e) {
-                LOG.warn(readResourceDebug(resource), e);
+                LOG.warn(readResourceDebug(url), e);
                 
             } catch (SAXParseException e) {
                 throw new ResourceLoadException(
-                        readResourceDebug(resource) + " at " + 
e.getLineNumber() + ":" + e.getColumnNumber(), e);
+                        readResourceDebug(url) + " at " + e.getLineNumber() + 
":" + e.getColumnNumber(), e);
                 
             } catch (SAXException e) {
-                throw new ResourceLoadException(readResourceDebug(resource), 
e);
+                throw new ResourceLoadException(readResourceDebug(url), e);
                 
             }finally{
                 if( null != is ){
                     try{
                         is.close();
                     }catch(IOException ioe){
-                        LOG.warn(readResourceDebug(resource), ioe);
+                        LOG.warn(readResourceDebug(url), ioe);
                     }
                 }
             }
@@ -372,7 +371,7 @@
         return success;
     }
     
-    protected String readResourceDebug(final String resource){
+    protected String readResourceDebug(final URL url){
         
         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 13:40:03 UTC (rev 5294)
+++ 
trunk/site-spi/src/main/java/no/schibstedsok/searchportal/site/config/UrlResourceLoader.java
        2007-06-08 14:33:23 UTC (rev 5295)
@@ -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,72 +91,48 @@
         return bcLoader;
     }
 
-    public static boolean doesUrlExist(final String url, final String 
hostHeader){
+    public static boolean doesUrlExist(final URL url){
 
         boolean success = false;
         
         try{
-            success = (Boolean)PRESENCE_CACHE.getFromCache(url, 
REFRESH_PERIOD);
+            success = (Boolean)PRESENCE_CACHE.getFromCache(url.toString(), 
REFRESH_PERIOD);
             
         }catch(NeedsRefreshException nre){
            
             boolean updatedCache = false;
-            HttpURLConnection con = null;
             try {
 
-                final URL u = new URL(url);
+                LOG.trace(DEBUG_CHECKING_EXISTANCE_OF + url + " is " + 
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);
+                success = HTTPClient.instance(url, "localhost").exists("");
+                PRESENCE_CACHE.putInCache(url.toString(), success);
+
                 updatedCache = true;
 
-                LOG.trace(DEBUG_CHECKING_EXISTANCE_OF + u + " is " + success);
-
             } catch (NullPointerException e) {
-                LOG.debug( '[' + hostHeader + "] " + url, e);
+                LOG.debug(url.toString(), e);
 
             } catch (SocketTimeoutException ste) {
-                LOG.debug( '[' + hostHeader + "] " + url + '\n' + ste);
+                LOG.debug(url.toString() + '\n' + ste);
 
             } catch (IOException e) {
-                LOG.warn( '[' + hostHeader + "] " + url, e);
+                LOG.warn(url.toString(), e);
 
             }  finally  {
                 
                 if(!updatedCache){ 
-                    PRESENCE_CACHE.cancelUpdate(url);
+                    PRESENCE_CACHE.cancelUpdate(url.toString());
                 }
-                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);
@@ -167,9 +143,9 @@
 
 
     @Override
-    public final boolean urlExists(final String url) {
+    public final boolean urlExists(final URL url) {
 
-        return doesUrlExist(getUrlFor(url), getHostHeaderFor(url));
+        return doesUrlExist(url);
     }
 
 
@@ -182,46 +158,44 @@
     // Protected -----------------------------------------------------
 
     @Override
-    protected final String getResource(final Site site) {
-
-        return "http://";
-                + site.getName()
-                + site.getConfigContext()
-                + (getResource().endsWith(".class") ? "classes/" : "conf/")
-                + getResource();
+    protected final URL getResource(final Site site) {
+        return getURL(getResource(), site);
     }
 
-    protected final String getHostHeaderFor(final String resource){
-
-        return getHostHeader(resource);
+    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());
+        }
     }
 
     @Override
-    protected String getUrlFor(final String resource){
+    protected final InputStream getInputStreamFor(final URL url) {
 
-        return getURL(resource);
-    }
-
-    @Override
-    protected final InputStream getInputStreamFor(String resource) {
-
         HTTPClient client = null;
         try {
-            final URL u = new URL(getUrlFor(resource));
-            client = HTTPClient.instance(u.getHost(), u.getPort(), 
getHostHeaderFor(resource));
-            
-            return client.getBufferedStream(u.getPath());
+            client = HTTPClient.instance(url, "localhost");
+            return client.getBufferedStream("");
 
         }catch (IOException ex) {
-            throw new ResourceLoadException(ex.getMessage(), null != client ? 
client.interceptIOException(ex) : ex);
+            throw new ResourceLoadException(ex.getMessage(), 
client.interceptIOException(ex));
         }
 
 
     }
 
-    protected final String readResourceDebug(final String resource){
+    public static String getHostHeader(final String resource){
+        return resource.substring(7,resource.indexOf('/',8));
+    }
 
-        return "Read Configuration from " + getUrlFor(resource) + " [" + 
getHostHeaderFor(resource) + ']';
+    protected final String readResourceDebug(final URL url){
+
+        return "Read Configuration from " + url;
     }
 
     // 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 13:40:03 UTC (rev 5294)
+++ 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/SiteTestCase.java
    2007-06-08 14:33:23 UTC (rev 5295)
@@ -12,13 +12,10 @@
 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 13:40:03 UTC (rev 5294)
+++ 
trunk/site-spi/src/test/java/no/schibstedsok/searchportal/site/config/FileResourceLoader.java
       2007-06-08 14:33:23 UTC (rev 5295)
@@ -12,6 +12,7 @@
 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;
@@ -86,19 +87,12 @@
         super(cxt);
     }
 
-    public boolean urlExists(final String url) {
+    public boolean urlExists(final URL url) {
         
         try {
-            final URL u = new URL(getUrlFor(url));
-
-            return new File(u.toURI()).exists();
-            
+            return new File(url.toURI()).exists();
         }catch (URISyntaxException ex) {
             LOG.error(ex.getMessage(), ex);
-            
-        }catch (MalformedURLException ex) {
-            LOG.error(ex.getMessage(), ex);
-            
         }
 
         return false;
@@ -112,9 +106,10 @@
         if( projectName.indexOf(':') > 0 ){
             projectName = projectName.substring(0, projectName.indexOf(':'));
         }
-        if( projectName.endsWith("sesam") && 
!"generic.sesam".equals(projectName) ){
-            projectName = projectName + ".no";
+        if( projectName.endsWith("sesam/") && 
!"generic.sesam/".equals(projectName) ){
+            projectName = projectName.substring(0, projectName.length() - 1) + 
".no/";
         }
+
         if( "catalogue".equals(projectName)){
             projectName = "katalog.sesam.no";
         }
@@ -122,7 +117,7 @@
         return projectName;
     }
 
-    protected String getResource(final Site site) {
+    protected URL getResource(final Site site) {
         
         LOG.debug("getResource(" + site + ')');
         
@@ -133,17 +128,18 @@
 
             final File warFolder = new File(base + "/war");
 
-            final String result = new URI("file://"
-                    + base 
+            URI uri = new URI("file://"
+                    + base
                     + (warFolder.exists() && warFolder.isDirectory() ? 
"/war/target/classes/" : "/target/classes/")
-                    + getResource()).normalize().toString();
+                    + getResource()).normalize();
 
-            LOG.debug("result: " + result);
-            return result;
+            return uri.toURL();
          
         }catch (URISyntaxException ex) {
             throw new ResourceLoadException(ex.getMessage(), ex);
-        } 
+        } catch (MalformedURLException ex) {
+            throw new ResourceLoadException(ex.getMessage(), ex);
+        }
     }
 
     protected String getUrlFor(final String resource) {
@@ -162,12 +158,11 @@
         return "localhost";
     }
 
-    protected InputStream getInputStreamFor(String resource) {
-
+    protected InputStream getInputStreamFor(URL url) {
         try {
-            return new FileInputStream(resource.replaceFirst("file:", ""));
-        }catch (FileNotFoundException ex) {
-            throw new ResourceLoadException(ex.getMessage(), ex);
+            return url.openConnection().getInputStream();
+        } catch (IOException e) {
+            throw new ResourceLoadException(e.getMessage(), e);
         }
     }
 

Modified: trunk/site-spi/src/test/resources/log4j.xml
===================================================================
--- trunk/site-spi/src/test/resources/log4j.xml 2007-06-08 13:40:03 UTC (rev 
5294)
+++ trunk/site-spi/src/test/resources/log4j.xml 2007-06-08 14:33:23 UTC (rev 
5295)
@@ -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="INFO"/>
+        <param name="Threshold" value="DEBUG"/>
 
         <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 13:40:03 UTC (rev 5294)
+++ 
trunk/war/src/main/java/no/schibstedsok/searchportal/http/filters/SiteLocatorFilter.java
    2007-06-08 14:33:23 UTC (rev 5295)
@@ -18,6 +18,7 @@
 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;
@@ -36,6 +37,7 @@
 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;
@@ -374,7 +376,7 @@
         }
     }
 
-    private String recursivelyFindResource(final String resource, final Site 
site) {
+    private String recursivelyFindResource(final String resource, final Site 
site) throws IOException {
 
         // 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.
@@ -384,7 +386,9 @@
 
         final String url = HTTP + site.getName() + site.getConfigContext() + 
'/' + datedResource;
 
-        if (UrlResourceLoader.doesUrlExist(UrlResourceLoader.getURL(url), 
UrlResourceLoader.getHostHeader(url))) {
+        final URL u = HTTPClient.getURL(new URL(url), "localhost");
+
+        if (UrlResourceLoader.doesUrlExist(u)) {
             // 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