Is there an easy way to convert this http listener to support https?

Hello,
I have a listener class I wrote awhile back, that I would like to adapt to accept SSL connections. I understand that you have to create a secured listener, but I cant figure out how to do it. Can someone show me how to modify my existing class to support https?

Worth 500 points.

Thanks,
Rick
/*
 * httpListener.java
 *
 * Created on January 13, 2007, 1:07 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
package tracking_server;
 
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
 
public class conversionListener extends Thread{
 
    config cfg;
    String ip;
    int port;
    campaignTracking campaigns;
    ExecutorService dataPool;
    boolean exit = false;
    
    public conversionListener(config c)
    {
        cfg = c;
        this.campaigns = c.campaigns;
        this.ip = "0.0.0.0";
        this.port = cfg.pixel_listener_port;
        dataPool = Executors.newFixedThreadPool(cfg.max_pixel_threads);
        
        cfg.logger.addToLog("Conversion Listener Initialized.");
    }
    
    synchronized public void run()
    {
         ServerSocket servSock;
            try
            {
                //start a server socket on port root/httpserver/listener_port
                servSock = new ServerSocket(port,0,InetAddress.getByName(ip));
            
                cfg.logger.addToLog("Conversion Listener started on port " + port + ".");
        
                //listen for incoming connections - when found start a new httpServerThread
                while (exit == false) 
                {
                    try
                    {
                        dataPool.execute(new conversionThread(servSock.accept(),campaigns));
                    }
                    catch(Exception e)
                    {
                        if (cfg.logExceptions)
                        {
                            cfg.logger.addToLog("Exception in Conversion Listener: " + e.getMessage() + "\n\nStack Trace:\n");
                            cfg.logger.addToLog(cfg.logger.getStackTrace(e));
                        }
            
                    }
                }    
                servSock.close();
                
            } 
            catch (IOException ex)
            {   
                if (cfg.logExceptions)
                {//output the error
                    cfg.logger.addToLog("Exception in Conversion Listener: " + ex.getMessage() + "\n\nStack Trace:\n");
                    cfg.logger.addToLog(cfg.logger.getStackTrace(ex));
                }
            }
    }
 
    public void shutdown()
    {
        dataPool.shutdown();
        cfg.logger.addToLog("Conversion Service: Shutdown.");
        exit = true;
    }
}

Open in new window

richardsimnettAsked:
Who is Participating?
 
objectsCommented:
Use SSLServerSocketFactory to create your ServerSocket

http://helpdesk.objects.com.au/java/how-to-create-an-ssl-server-socket
0
 
richardsimnettAuthor Commented:
objects,
do you also have an example of how to set the keystore up and link it to the socket?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
objectsCommented:
If you need to create a test certificate then use keytool
http://helpdesk.objects.com.au/java/how-to-create-a-keystore-with-a-test-certificate

0
 
richardsimnettAuthor Commented:
objects,
Ok I have tried making https work using the SSLServerSocketFactory. I have a problem however, since adding the ssl class, the cpu usage is constantly at 100%. LIke its in an endless loop. I've been toying around with this all day, I have a properly working certificate, and as far as I can tell the class structure and initialization is right.

Can you shed some light on this?




/*
 * httpListener.java
 *
 * Created on January 13, 2007, 1:07 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
package tracking_server;
 
import java.net.*;
import javax.net.ssl.*;
import javax.net.*;
import java.io.*;
import java.util.concurrent.*;
 
public class SSLConversionListener extends Thread{
 
    config cfg;
    String ip;
    int port;
    campaignTracking campaigns;
    ExecutorService sslPool;
    boolean exit = false;
    
    public SSLConversionListener(config c)
    {
        cfg = c;
        this.campaigns = c.campaigns;
        this.ip = "0.0.0.0";
        this.port = cfg.https_pixel_listener_port;
        sslPool = Executors.newFixedThreadPool(cfg.max_pixel_threads/2);
        
        cfg.logger.addToLog("SSL Conversion Listener Initialized.");
    }
    
    synchronized public void run()
    {
         ServerSocket servSock;
            try
            {
                ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
                
                //start a new ssl server socket on port
                servSock = serverSocketFactory.createServerSocket(cfg.https_pixel_listener_port);
                
                cfg.logger.addToLog("SSL Conversion Listener started on port " + port + ".");
        
                //listen for incoming connections - when found start a new httpServerThread
                while (exit == false) 
                {
                    try
                    {
                        sslPool.execute(new conversionThread(servSock.accept(),campaigns));
                    }
                    catch(Exception e)
                    {
                        if (cfg.logExceptions)
                        {
                            cfg.logger.addToLog("Exception in SSL Conversion Listener: " + e.getMessage() + "\n\nStack Trace:\n");
                            cfg.logger.addToLog(cfg.logger.getStackTrace(e));
                        }
            
                    }
                }    
                servSock.close();
                
            } 
            catch (IOException ex)
            {   
                if (cfg.logExceptions)
                {//output the error
                    cfg.logger.addToLog("Exception in SSL Conversion Listener: " + ex.getMessage() + "\n\nStack Trace:\n");
                    cfg.logger.addToLog(cfg.logger.getStackTrace(ex));
                }
            }
    }
 
    public void shutdown()
    {
        sslPool.shutdown();
        cfg.logger.addToLog("SSL Conversion Service: Shutdown.");
        exit = true;
    }
}

Open in new window

0
 
objectsCommented:
which part of the code is causing the load?

0
 
richardsimnettAuthor Commented:
I havent pinpointed the exact line yet. But its within the new class. It seems to full initialize so my guess is its happening at the while loop. Either the servSock.accpet() isnt blocking until there is an inbound connection. Or something else is going haywire. I will experiment with it more tomorrow, and see exactly what the problematic line(s) are.

Thanks,
Rick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.