[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-27
7
Medium Priority
?
231 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:richardsimnett
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24247676
Use SSLServerSocketFactory to create your ServerSocket

http://helpdesk.objects.com.au/java/how-to-create-an-ssl-server-socket
0
 

Author Comment

by:richardsimnett
ID: 24247775
objects,
do you also have an example of how to set the keystore up and link it to the socket?
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24247795
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 24248031
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
 

Author Comment

by:richardsimnett
ID: 24254071
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
 
LVL 92

Expert Comment

by:objects
ID: 24256151
which part of the code is causing the load?

0
 

Author Comment

by:richardsimnett
ID: 24256605
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question