Author: indika
Date: Mon Jul  7 07:46:09 2008
New Revision: 674511

URL: http://svn.apache.org/viewvc?rev=674511&view=rev
Log:
Add a method to create a URLConnection (It hides https,https,file,etc..). 

Change all locations that create a URLConnection  to use above method.


Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/definition/factory/KeyStoreInformationFactory.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java?rev=674511&r1=674510&r2=674511&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfigUtils.java
 Mon Jul  7 07:46:09 2008
@@ -169,10 +169,16 @@
             if (url == null) {
                 return null;
             }
-            URLConnection urlc = url.openConnection();
+            URLConnection connection = getURLConnection(url);
+            if (connection == null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Cannot create a URLConnection for given URL : " 
+ url);
+                }
+                return null;
+            }
             XMLToObjectMapper xmlToObject =
-                    getXmlToObjectMapper(urlc.getContentType());
-            InputStream inputStream = urlc.getInputStream();
+                    getXmlToObjectMapper(connection.getContentType());
+            InputStream inputStream = connection.getInputStream();
             try {
                 XMLStreamReader parser = XMLInputFactory.newInstance().
                         createXMLStreamReader(inputStream);
@@ -219,7 +225,13 @@
 
         try {
             // Open a new connection
-            URLConnection newConnection = url.openConnection();
+            URLConnection newConnection = getURLConnection(url);
+            if (newConnection == null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Cannot create a URLConnection for given URL : " 
+ url);
+                }
+                return null;
+            }
 
             BufferedInputStream newInputStream = new BufferedInputStream(
                     newConnection.getInputStream());
@@ -248,100 +260,16 @@
         if (url == null) {
             return null;
         }
-
-        InputStream urlInStream = null;
-
-        if (url.getProtocol().equalsIgnoreCase("https")) {
-            Properties synapseProperties = 
SynapsePropertiesLoader.loadSynapseProperties();
-            KeyManager[] keyManagers = null;
-            TrustManager[] trustManagers = null;
-
-            IdentityKeyStoreInformation identityInformation =
-                    
KeyStoreInformationFactory.createIdentityKeyStoreInformation(synapseProperties);
-
-            if (identityInformation != null) {
-                KeyManagerFactory keyManagerFactory =
-                        
identityInformation.getIdentityKeyManagerFactoryInstance();
-                if (keyManagerFactory != null) {
-                    keyManagers = keyManagerFactory.getKeyManagers();
-                }
-
-            }
-
-            TrustKeyStoreInformation trustInformation =
-                    
KeyStoreInformationFactory.createTrustKeyStoreInformation(synapseProperties);
-
-            if (trustInformation != null) {
-                TrustManagerFactory trustManagerFactory =
-                        trustInformation.getTrustManagerFactoryInstance();
-                if (trustManagerFactory != null) {
-                    trustManagers = trustManagerFactory.getTrustManagers();
-                }
-            }
-
-            HttpsURLConnectionImpl connection = (HttpsURLConnectionImpl) 
url.openConnection();
-            try {
-                SSLContext sslContext = SSLContext.getInstance("TLS");
-                sslContext.init(keyManagers,
-                        trustManagers, null);
-                connection.setSSLSocketFactory(sslContext.getSocketFactory());
-                if (trustInformation != null) {
-                    boolean enableHostnameVerifier = true;
-                    String value =
-                            trustInformation.getParameter(
-                                    
KeyStoreInformation.ENABLE_HOST_NAME_VERIFIER);
-                    if (value != null) {
-                        enableHostnameVerifier = Boolean.parseBoolean(value);
-                    }
-                    if (!enableHostnameVerifier) {
-                        connection.setHostnameVerifier(new 
javax.net.ssl.HostnameVerifier() {
-                            public boolean verify(String hostname, 
javax.net.ssl.SSLSession session) {
-                                if (log.isTraceEnabled()) {
-                                    log.trace("HostName verification 
disabled");
-                                    log.trace("host:   " + hostname);
-                                    log.trace("peer host:  " + 
session.getPeerHost());
-                                }
-                                return true;
-                            }
-
-                            public boolean verify(String hostname, String 
certHostname) {
-                                if (log.isTraceEnabled()) {
-                                    log.trace("Hostname verification 
disabled");
-                                    log.trace("host:   " + hostname);
-                                    log.trace("cert hostname:  " + 
certHostname);
-                                }
-                                return true;
-                            }
-                        });
-                    }
-                }
-
-            } catch (NoSuchAlgorithmException e) {
-                handleException("Error loading SSLContext ");
-            } catch (KeyManagementException e) {
-                handleException("Error initiation SSLContext with 
KeyManagers");
+        URLConnection connection = getURLConnection(url);
+        if (connection == null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Cannot create a URLConnection for given URL : " + 
urlStr);
             }
-
-            connection.setReadTimeout(getReadTimeout());
-            connection.setConnectTimeout(getConnectionTimeout());
-            connection.setRequestProperty("Connection", "close"); // if http 
is being used
-            urlInStream = connection.getInputStream();
-
-        } else {
-
-            URLConnection conn = url.openConnection();
-            conn.setReadTimeout(getReadTimeout());
-            conn.setConnectTimeout(getConnectionTimeout());
-            conn.setRequestProperty("Connection", "close"); // if http is 
being used
-            urlInStream = conn.getInputStream();
-        }
-
-        if (urlInStream == null) {
             return null;
         }
-
+        InputStream inStream = connection.getInputStream();
         try {
-            StAXOMBuilder builder = new StAXOMBuilder(urlInStream);
+            StAXOMBuilder builder = new StAXOMBuilder(inStream);
             OMElement doc = builder.getDocumentElement();
             doc.build();
             return doc;
@@ -356,7 +284,7 @@
             }
         } finally {
             try {
-                urlInStream.close();
+                inStream.close();
             } catch (IOException ignore) {
             }
         }
@@ -364,6 +292,7 @@
     }
 
     public static InputSource getInputSourceFormURI(URI uri) {
+
         if (uri == null) {
             if (log.isDebugEnabled()) {
                 log.debug("Can not create a URL from 'null' ");
@@ -377,17 +306,19 @@
             if (protocol == null || "".equals(protocol)) {
                 url = new URL("file:" + path);
             }
-            URLConnection conn = url.openConnection();
-            conn.setReadTimeout(getReadTimeout());
-            conn.setConnectTimeout(getConnectionTimeout());
-            conn.setRequestProperty("Connection", "close"); // if http is 
being used
-            BufferedInputStream urlInStream = new BufferedInputStream(
-                    conn.getInputStream());
+            URLConnection connection = getURLConnection(url);
+            if (connection == null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Cannot create a URLConnection for give URL : " 
+ uri);
+                }
+                return null;
+            }
+            BufferedInputStream urlInStream = new 
BufferedInputStream(connection.getInputStream());
             return new InputSource(urlInStream);
         } catch (MalformedURLException e) {
             handleException("Invalid URL ' " + uri + " '", e);
         } catch (IOException e) {
-            handleException("Error reading at URI ' " + uri + " ' ", e);
+            handleException("IOError when getting a stream from given url : " 
+ uri, e);
         }
         return null;
     }
@@ -405,6 +336,121 @@
         throw new SynapseException(msg, e);
     }
 
+    private static HttpsURLConnection getHttpsURLConnection(URL url) {
+
+        if (log.isDebugEnabled()) {
+            log.debug("Creating a HttpsURL Connection from given URL : " + 
url);
+        }
+
+        KeyManager[] keyManagers = null;
+        TrustManager[] trustManagers = null;
+
+        Properties synapseProperties = 
SynapsePropertiesLoader.loadSynapseProperties();
+
+        IdentityKeyStoreInformation identityInformation =
+                
KeyStoreInformationFactory.createIdentityKeyStoreInformation(synapseProperties);
+
+        if (identityInformation != null) {
+            KeyManagerFactory keyManagerFactory =
+                    identityInformation.getIdentityKeyManagerFactoryInstance();
+            if (keyManagerFactory != null) {
+                keyManagers = keyManagerFactory.getKeyManagers();
+            }
+
+        }
+
+        TrustKeyStoreInformation trustInformation =
+                
KeyStoreInformationFactory.createTrustKeyStoreInformation(synapseProperties);
+
+        if (trustInformation != null) {
+            TrustManagerFactory trustManagerFactory =
+                    trustInformation.getTrustManagerFactoryInstance();
+            if (trustManagerFactory != null) {
+                trustManagers = trustManagerFactory.getTrustManagers();
+            }
+        }
+
+        try {
+            HttpsURLConnectionImpl connection = (HttpsURLConnectionImpl) 
url.openConnection();
+            //Create a SSLContext
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            sslContext.init(keyManagers,
+                    trustManagers, null);
+            connection.setSSLSocketFactory(sslContext.getSocketFactory());
+
+            if (trustInformation != null) {
+                // Determine is it need to overwrite default Host Name verifier
+                boolean enableHostnameVerifier = true;
+                String value =
+                        trustInformation.getParameter(
+                                KeyStoreInformation.ENABLE_HOST_NAME_VERIFIER);
+                if (value != null) {
+                    enableHostnameVerifier = Boolean.parseBoolean(value);
+                }
+
+                if (!enableHostnameVerifier) {
+
+                    if (log.isDebugEnabled()) {
+                        log.debug("Overriding default HostName Verifier." +
+                                "HostName verification disabled");
+                    }
+
+                    connection.setHostnameVerifier(new 
javax.net.ssl.HostnameVerifier() {
+                        public boolean verify(String hostname, 
javax.net.ssl.SSLSession session) {
+                            if (log.isTraceEnabled()) {
+                                log.trace("HostName verification disabled");
+                                log.trace("Host:   " + hostname);
+                                log.trace("Peer Host:  " + 
session.getPeerHost());
+                            }
+                            return true;
+                        }
+
+                        public boolean verify(String hostname, String 
certHostname) {
+                            if (log.isTraceEnabled()) {
+                                log.trace("HostName verification disabled");
+                                log.trace("Host:   " + hostname);
+                                log.trace("Cert HostName:  " + certHostname);
+                            }
+                            return true;
+                        }
+                    });
+                } else {
+                    if (log.isDebugEnabled()) {
+                        log.debug("Using default HostName verifier...");
+                    }
+                }
+            }
+            return connection;
+
+        } catch (NoSuchAlgorithmException e) {
+            handleException("Error loading SSLContext ", e);
+        } catch (KeyManagementException e) {
+            handleException("Error initiation SSLContext with KeyManagers", e);
+        } catch (IOException e) {
+            handleException("Error opening a https connection from URL : " + 
url, e);
+        }
+        return null;
+    }
+
+    public static URLConnection getURLConnection(URL url) {
+
+        try {
+            URLConnection connection;
+            if (url.getProtocol().equalsIgnoreCase("https")) {
+                connection = getHttpsURLConnection(url);
+            } else {
+                connection = url.openConnection();
+            }
+            connection.setReadTimeout(getReadTimeout());
+            connection.setConnectTimeout(getConnectionTimeout());
+            connection.setRequestProperty("Connection", "close"); // if http 
is being used
+            return connection;
+        } catch (IOException e) {
+            handleException("Error reading at URI ' " + url + " ' ", e);
+        }
+        return null;
+    }
+
     private static void handleException(String msg) {
         log.warn(msg);
         throw new SynapseException(msg);
@@ -503,7 +549,7 @@
         if (parentLocation == null) {
             return importUri.toString();
         } else {
-            // if the importuri is absolute
+            // if the import-uri is absolute
             if (relativeLocation.startsWith("/") || 
relativeLocation.startsWith("\\")) {
                 if (importUri != null && !importUri.isAbsolute()) {
                     try {

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java?rev=674511&r1=674510&r2=674511&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/registry/url/SimpleURLRegistry.java
 Mon Jul  7 07:46:09 2008
@@ -65,7 +65,13 @@
 
         BufferedInputStream inputStream;
         try {
-            URLConnection connection = url.openConnection();
+            URLConnection connection = 
SynapseConfigUtils.getURLConnection(url);
+            if (connection == null) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Cannot create a URLConnection for given URL : " 
+ url);
+                }
+                return null;
+            }
             connection.connect();
             inputStream = new BufferedInputStream(connection.getInputStream());
         } catch (IOException e) {
@@ -126,39 +132,36 @@
     }
 
     public RegistryEntry getRegistryEntry(String key) {
+        
         if (log.isDebugEnabled()) {
             log.debug("Perform RegistryEntry lookup for key : " + key);
         }
-        try {
-            URL url = SynapseConfigUtils.getURLFromPath(root + key);
-            if (url == null) {
-                return null;
-            }
-            URLConnection urlc = url.openConnection();
-            urlc.setReadTimeout(30000);
-            urlc.setRequestProperty("Connection", "Close");
-
-            RegistryEntryImpl wre = new RegistryEntryImpl();
-            wre.setKey(key);
-            wre.setName(url.getFile());
-            wre.setType(urlc.getContentType());
-            wre.setDescription("Resource at : " + url.toString());
-            wre.setLastModified(urlc.getLastModified());
-            wre.setVersion(urlc.getLastModified());
-            if (urlc.getExpiration() > 0) {
-                wre.setCachableDuration(
-                        urlc.getExpiration() - System.currentTimeMillis());
-            } else {
-                wre.setCachableDuration(getCachableDuration(key));
+        URL url = SynapseConfigUtils.getURLFromPath(root + key);
+        if (url == null) {
+            return null;
+        }
+        URLConnection connection = SynapseConfigUtils.getURLConnection(url);
+        if (connection == null) {
+            if (log.isDebugEnabled()) {
+                log.debug("Cannot create a URLConnection for given URL : " + 
url);
             }
-            return wre;
+            return null;
+        }
 
-        } catch (MalformedURLException e) {
-            handleException("Invalid URL reference " + root + key, e);
-        } catch (IOException e) {
-            handleException("IO Error reading from URL " + root + key, e);
+        RegistryEntryImpl wre = new RegistryEntryImpl();
+        wre.setKey(key);
+        wre.setName(url.getFile());
+        wre.setType(connection.getContentType());
+        wre.setDescription("Resource at : " + url.toString());
+        wre.setLastModified(connection.getLastModified());
+        wre.setVersion(connection.getLastModified());
+        if (connection.getExpiration() > 0) {
+            wre.setCachableDuration(
+                    connection.getExpiration() - System.currentTimeMillis());
+        } else {
+            wre.setCachableDuration(getCachableDuration(key));
         }
-        return null;
+        return wre;
     }
 
     public void init(Properties properties) {

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/definition/factory/KeyStoreInformationFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/definition/factory/KeyStoreInformationFactory.java?rev=674511&r1=674510&r2=674511&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/definition/factory/KeyStoreInformationFactory.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/security/definition/factory/KeyStoreInformationFactory.java
 Mon Jul  7 07:46:09 2008
@@ -71,7 +71,7 @@
                 properties, IDENTITY_KEY_STORE, null);
         if (keyStoreLocation == null || "".equals(keyStoreLocation)) {
             if (log.isDebugEnabled()) {
-                log.debug("Cannot find a KeyStoreLocation");
+                log.debug("Cannot find a KeyStoreLocation for private key 
store");
             }
             return null;
         }
@@ -109,7 +109,7 @@
                         TRUST_STORE, null);
         if (keyStoreLocation == null || "".equals(keyStoreLocation)) {
             if (log.isDebugEnabled()) {
-                log.debug("Cannot find a KeyStoreLocation");
+                log.debug("Cannot find a KeyStoreLocation for trust store");
             }
             return null;
         }
@@ -132,17 +132,26 @@
     private static void parseParameter(String parameterString, 
KeyStoreInformation information) {
 
         if (parameterString == null || "".equals(parameterString)) {
+            if (log.isDebugEnabled()) {
+                log.debug("No additional parameter for KeyStore");
+            }
             return;
         }
 
         String[] parameterPairs = parameterString.split(";");
         if (parameterPairs == null) {
+            if (log.isDebugEnabled()) {
+                log.debug("No additional parameter for KeyStore");
+            }
             return;
         }
 
         for (String parameterPairString : parameterPairs) {
             String[] parametersPair = parameterPairString.split("=");
-            if (parametersPair == null) {
+            if (parametersPair == null || parameterPairs.length != 2) {
+                if (log.isDebugEnabled()) {
+                    log.debug("A parameter with no (name,value) pair has been 
found ");
+                }
                 return;
             }
             information.addParameter(parametersPair[0], parametersPair[1]);


Reply via email to