Java SSL: How to accept a self-signed certificate

I’ve been working on a RESTful API for use on computers and mobile devices. I have a JUnit test case that connects to the URLs and reads the responses. Given the nature of the data being transferred back and forth, I enabled SSL on the web server using a self-signed certificate I generated using Java’s keytool.

keytool -genkey -alias <hostname> -keyalg RSA

This is where my problems began. My test case is using HTTP, so I needed to refactor it to use the HTTPS protocol. Unbeknownst to me, accepting SSL connections from self-signed certificates is non-trivial, especially if you want to do it right. There is copious advice on the inter-web about how to accept any certificate. I’m not keen on that approach; therefore, I set out to find the correct way.

Without a decent Java security reference handy, I surfed the internet for answers. I found partial code snippets on stackoverflow and Example Depot. In a nutshell, I found that you need an instance of a SSLSocketFactory to set in a HttpsURLConnection. Here’s how:

// Load the keystore in the user's home directory
File file = new File(System.getProperty("user.home") + File.separatorChar + ".keystore");
FileInputStream fis = null;
KeyStore keyStore = null;

fis = new FileInputStream(file);
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(fis, keystorePassword.toCharArray());

TrustManagerFactory tmf;

tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);

//Get an instance of the socket factory
SSLSocketFactory sslFactory = ctx.getSocketFactory();

HttpsURLConnection connection = null;
url = new URL(newURLString);

connection = (HttpsURLConnection)url.openConnection();

//set the socket factory in the connection
connection.setSSLSocketFactory(sslFactory);

//...

Tip: Remember the alias created in the keytool must be the hostname of the server. Otherwise, an exception will be thrown on the client:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching <alias> found
Java SSL: How to accept a self-signed certificate

Is the hosted version of ScrumDesk truly hosted in the age of the cloud? No.

I’ve been looking for a free or inexpensive Scrum hosted tool for my micro development shop. I poked around some blog reviews and came across a recommendation for ScrumDesk by Torin on Mike Cohn’s userStories website.

ScrumDesk comes in a hosted offering, however, it’s for Windows only. This isn’t so great for us because we develop on Mac OS X, deploy server components to Ubuntu and distribute clients on Mac OS X and iPhone OS. Nonetheless, I decided to give it a shot using VMWare Fusion. I found the setup to be cumbersome. First you have to install the .NET Framework 3.5, ScrumDesk and configure a database connection to a Microsoft SQL Server.

Once I had everything setup, I started adding users in the administration screen. It doesn’t send emails automatically to new users, you have to remember to send the database connection info to them by clicking a button in the menu bar. Once they receive the message, they’ll have no idea what do if they haven’t previously been told to install the .NET Framework 3.5 and ScrumDesk because the email contains only the steps on how to load the attached file in the login screen.

After setting up a few users, I transcribed my user stories from sticky notes in the backlog view, which is a nice emulation of notes on a wall. However, after using it awhile, I found the application to be sluggish in my 768 MB VMWare image. There also was a temporary outage of service on the second day of use, due to a problem with their ISP.

After my experience, I decided against the product and exported my user stories. Too my surprise, you can only export in two formats: Open XML Paper Specification (.XPS), which requires a 3rd party viewer on Mac OS X or Microsoft Excel XML. Why not CSV?

At the end of the day the hosted version of ScrumDesk is nothing more than a Windows only client-server application. If you’re looking for cloud-based Scrum tool check out the offerings from Rally Software or VersionOne.

Is the hosted version of ScrumDesk truly hosted in the age of the cloud? No.

Troubleshooting remote connections to MySQL on Linux

If you encounter a problem when trying to remotely connect to MySQL on Linux, make sure that the bind-address property in the configuration file (/etc/mysql/my.cnf) is set to the machine’s host name. I wasted a considerable amount of time before resolving this issue.

“If the server was started with –bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections.”

source: MySQL documentation

I hope this saves somebody some time.

Troubleshooting remote connections to MySQL on Linux

A few subtle differences between UIKit and AppKit

If you cut your Objective-C teeth on the iPhone, like me, then you’ll run across these subtle differences between text fields and table views.

//** Text fields **/
//Mac OS X

NSString* someText = [someTextField stringValue];

//iPhone OS
NSString* someText = someTextField.text;

Table view data source delegate methods are different. On the iPhone, cell data is encapsulated in UITableViewCell and accessed by the framework by calling cellForRowAtIndexPath. On the Mac, cell data is accessed by the framework by calling objectValueForTableColumn. Note: This method is not required with Cocoa bindings.

/** Table view delegates **/

//Mac OS X 
-(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { /*...*/ }

//iPhone OS
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /*...*/ }

Please share your notable differences.

A few subtle differences between UIKit and AppKit

How to remove the UITableViewCell selection color

If you want to remove the pesky blue highlighting when a a row is selected in your UITableView, make sure to the add the following line in your cellForRowAtIndexPath delegate method.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   // Set up the cell...

    return cell;

Happy iPhone development!

How to remove the UITableViewCell selection color

It’s the Garage Phase Stupid

I’ve taken an extended break from blogging. I have a myriad of reasons and excuses, but I’ll refrain from boring you with all that. The reality, I’m immersed in the garage phase of a startup company. The company will be a cloud-based services company and I’m juggling a number priorities such as prototype development and funding. Anyway, a great presentation, which I came across while considering funding options, is the following slideshow from Mint.com. It outlines their journey through the stages of funding.

Mint.coms Accounting for Startups

Some additional information about Mint.com’s funding strategy can found here.


It’s the Garage Phase Stupid

Get a Startup Engineering Department Up & Running in a Flash With Cloud Source Control

Have little time or interest in installing and administering a software configuration management system (SCM) for a small team.? Me too! I suggest taking a look at Unfuddle. It allows you and your team to track bugs, manage projects and control source code in a secure hosted environment.

They have freemium pricing model, which ranges from free for two users to $99/month for enterprises.

I’ve been using their Subversion hosting with Xcode and Eclipse. If you are using Eclipse, I recommend installing the Subclipse plugin for Subversion.



Get a Startup Engineering Department Up & Running in a Flash With Cloud Source Control