Author: tyrell
Date: Tue May 13 09:46:45 2008
New Revision: 16951

Log:
Initial commit towards implementing mashup sharing and service invocation 
within mashups by adding trusted certs to keystore.

Added:
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/CustomProtocolSocketFactory.java
Modified:
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupArchiveManupulator.java

Added: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/CustomProtocolSocketFactory.java
==============================================================================
--- (empty file)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/CustomProtocolSocketFactory.java
   Tue May 13 09:46:45 2008
@@ -0,0 +1,165 @@
+package org.wso2.mashup.utils;
+
+import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
+import org.apache.commons.httpclient.params.HttpConnectionParams;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.utils.ServerConfiguration;
+import org.wso2.mashup.MashupConstants;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.TrustManagerFactory;
+import javax.net.SocketFactory;
+import java.net.Socket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.security.KeyStore;
+import java.security.NoSuchAlgorithmException;
+import java.security.KeyStoreException;
+import java.security.UnrecoverableKeyException;
+import java.security.KeyManagementException;
+import java.security.cert.CertificateException;
+
+
+public class CustomProtocolSocketFactory implements 
SecureProtocolSocketFactory {
+
+    /**
+     * Log object for this class.
+     */
+    private static final Log LOG = 
LogFactory.getLog(CustomProtocolSocketFactory.class);
+
+    private SSLContext sslcontext = null;
+
+    public CustomProtocolSocketFactory() {
+        super();
+    }
+
+    private static SSLContext createSSLContext() {
+        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
+
+        String keyStoreLocation =
+                serverConfig.getFirstProperty(MashupConstants.SECURITY_CONFIG 
+ "." +
+                        MashupConstants.SECURITY_CONFIG_KEYSTORE + "." + 
MashupConstants
+                        .SECURITY_CONFIG_KEYSTORE_LOCATION);
+        
+        String keyStorePass = 
serverConfig.getFirstProperty(MashupConstants.SECURITY_CONFIG + "." +
+                MashupConstants.SECURITY_CONFIG_KEYSTORE + "." + 
MashupConstants
+                .SECURITY_CONFIG_KEYSTORE_PASSWORD);
+
+        try {
+            System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+
+            KeyManagerFactory keyManagerFactory = 
KeyManagerFactory.getInstance("SunX509");
+
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            char[] keyPassphrase = keyStorePass.toCharArray();
+            keyStore.load(new FileInputStream(keyStoreLocation), 
keyPassphrase);
+            keyManagerFactory.init(keyStore, keyPassphrase);
+
+            TrustManagerFactory trustManagerFactory = 
TrustManagerFactory.getInstance("SunX509");
+            KeyStore trustStore = KeyStore.getInstance("JKS");
+            char[] trustPassphrase = keyStorePass.toCharArray();
+            trustStore.load(new FileInputStream(keyStoreLocation), 
trustPassphrase);
+            trustManagerFactory.init(trustStore);
+
+            sslContext.init(keyManagerFactory.getKeyManagers(),
+                            trustManagerFactory.getTrustManagers(),
+                            null);
+
+            return sslContext;
+
+        } catch (NoSuchAlgorithmException e) {
+            LOG.error(e);
+        } catch (KeyStoreException e) {
+            LOG.error(e);
+        } catch (IOException e) {
+            LOG.error(e);
+        } catch (CertificateException e) {
+            LOG.error(e);
+        } catch (UnrecoverableKeyException e) {
+            LOG.error(e);
+        } catch (KeyManagementException e) {
+            LOG.error(e);
+        }
+
+        return null;
+    }
+
+    private SSLContext getSSLContext() {
+        if (this.sslcontext == null) {
+            this.sslcontext = createSSLContext();
+        }
+        return this.sslcontext;
+    }
+
+
+    public Socket createSocket(
+            Socket socket,
+            String host,
+            int port,
+            boolean autoClose)
+            throws IOException {
+        return getSSLContext().getSocketFactory().createSocket(
+                socket,
+                host,
+                port,
+                autoClose
+        );
+    }
+
+
+    public Socket createSocket(
+            String host,
+            int port,
+            InetAddress clientHost,
+            int clientPort)
+            throws IOException {
+
+        return getSSLContext().getSocketFactory().createSocket(
+                host,
+                port,
+                clientHost,
+                clientPort
+        );
+    }
+
+
+    public Socket createSocket(
+            final String host,
+            final int port,
+            final InetAddress localAddress,
+            final int localPort,
+            final HttpConnectionParams params
+    ) throws IOException {
+        if (params == null) {
+            throw new IllegalArgumentException("Parameters may not be null");
+        }
+        int timeout = params.getConnectionTimeout();
+        SocketFactory socketfactory = getSSLContext().getSocketFactory();
+        if (timeout == 0) {
+            return socketfactory.createSocket(host, port, localAddress, 
localPort);
+        } else {
+            Socket socket = socketfactory.createSocket();
+            SocketAddress localaddr = new InetSocketAddress(localAddress, 
localPort);
+            SocketAddress remoteaddr = new InetSocketAddress(host, port);
+            socket.bind(localaddr);
+            socket.connect(remoteaddr, timeout);
+            return socket;
+        }
+    }
+
+
+    public Socket createSocket(String host, int port)
+            throws IOException {
+        return getSSLContext().getSocketFactory().createSocket(
+                host,
+                port
+        );
+    }
+
+}

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupArchiveManupulator.java
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupArchiveManupulator.java
      (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupArchiveManupulator.java
      Tue May 13 09:46:45 2008
@@ -32,6 +32,8 @@
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.axis2.rpc.client.RPCServiceClient;
 import org.apache.axis2.transport.http.HTTPConstants;
+import org.apache.commons.httpclient.protocol.Protocol;
+import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
 import org.wso2.authenticator.AuthenticatorException;
 import org.wso2.javascript.rhino.JavaScriptEngineConstants;
 import org.wso2.mashup.MashupConstants;
@@ -141,7 +143,7 @@
         }
 
         Parameter myRegistryPath =
-                        
mashupService.getParameter(MashupConstants.REGISTRY_MASHUP_PATH);
+                
mashupService.getParameter(MashupConstants.REGISTRY_MASHUP_PATH);
         ByteArrayInputStream byteArrayInputStream = null;
         if ("true".equals(migrateTags) && myRegistryPath != null) {
             String mashupPath = (String) myRegistryPath.getValue();
@@ -207,7 +209,7 @@
                                     ConfigurationContext configCtx, String 
serviceJsFileName,
                                     String mashupServiceName, String username, 
String password,
                                     String overwriteExisting)
-            throws AxisFault {
+            throws IOException{
 
         if (destinationServerAddress.startsWith("http://";)) {
             // We should call this service in https mode as we are sending the 
users username
@@ -232,6 +234,12 @@
         options.setAction("urn:shareMashup");
         options.setProperty(HTTPConstants.CHUNKED, "false");
         options.setProperty(Constants.Configuration.ENABLE_MTOM, 
Constants.VALUE_TRUE);
+
+        // Creating a custom protocol based on the user's keystores and 
trusted certs within
+        ProtocolSocketFactory psf = new CustomProtocolSocketFactory();
+        Protocol protocol = new Protocol("https", psf, 443);
+        options.setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,  protocol);
+
         QName opAddEntry = new 
QName("http://service.share.mashup.wso2.org/xsd";, "shareMashup");
 
         // parameters to the service ServiceName string, service file name,
@@ -323,10 +331,11 @@
      * @param zos          - The outputStrean for the zip
      * @param jsFile       - The handle to the JS File
      * @param resourcesDir - A handle to the resources directory
-     * @throws IOException           - Thrown in case the js file cannot be 
read
+     * @throws IOException                   - Thrown in case the js file 
cannot be read
      * @throws java.io.FileNotFoundException - Thrown in case the js file 
cannot be found
      */
-    private void createMashupArchive(ZipOutputStream zos, File jsFile, File 
resourcesDir, ByteArrayInputStream byteArrayInputStream)
+    private void createMashupArchive(ZipOutputStream zos, File jsFile, File 
resourcesDir,
+                                     ByteArrayInputStream byteArrayInputStream)
             throws IOException {
         if (!resourcesDir.isDirectory()) {
             throw new MashupFault(resourcesDir.getPath() + " is not a 
directory");
@@ -350,7 +359,8 @@
         // We are adding in the tags file here
         if (byteArrayInputStream != null) {
             String shortFileName = 
DescriptionBuilder.getShortFileName(jsFile.getName());
-            File tagsFile = new File(resourcesDir.getParentFile(), 
shortFileName + MashupConstants.TAGS_File);
+            File tagsFile = new File(resourcesDir.getParentFile(),
+                                     shortFileName + 
MashupConstants.TAGS_File);
             zos.putNextEntry(new ZipEntry(getZipEntryPath(tagsFile)));
             // now write the content of the file to the ZipOutputStream
             readBuffer = new byte[40960];

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to