JodaTime setCurrentMillisSystem()

When programs have logic based on the current time, it’s important to be able to programmatically set the current time for testing purposes. Other not so great options we have to change the computer clock, or change the logic for testing, or change the date in the database. Using JodaTime, it’s easy to programatically change the system time. Here are the things you need to do.

1. Make sure you use JodaTime’s DateTime class instead of java.util.Date when working with dates. Only convert DateTime to java.util.Date or java.sql.Date during persistence or when you are done with all date calculations and logic.

2. Call JodaTime’s DateTimeUtils.setCurrentMillisFixed(int millis) to set the system time. Here’s an example of setting the system time to 2013-09-23 10:30 AM.

DateTimeUtils.setCurrentMillisFixed(new DateTime(2013, 9, 23, 10, 30).getMillis());
DateTime now = new DateTime();
System.out.println(now);

This code prints

2013-09-23T10:30:00.000-04:00

3. To undo setCurrentMillisFixed, call DateTimeUtils.setCurrentMillisSystem(). This should return the real system time again.

DateTimeUtils.setCurrentMillisSystem();

In your related JUnit tests, you can put setCurrentMillisSystem() in the @After function

@After
public void after() {
DateTimeUtils.setCurrentMillisSystem();
}

Date Binding in Spring MVC

Here’s how to accept java.util.Date parameters in your Spring MVC Controller. This has been tested on Spring 3.1.

1. In your Spring MVC Controller, add a request parameter with a DateTimeFormat annotation. Something like:

public String foo(@RequestParam @DateTimeFormat(pattern="MM/dd/yyyy") Date date) {
    System.out.println(date);
    return "foo"
}

2. Add Joda-Time to your web application. If you’re using Maven, you can add the following dependency to your pom.xml.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.2</version>
</dependency>

3. In your web page, pass the date to the server in the format MM/dd/yyyy. We are using the jQuery UI Datepicker in our web application, so we have something like this:

$("#datepicker").datepicker({dateFormat: 'mm/dd/yy'});

Where #datepicker is the selector, which looks for a text input with the id=”datepicker”.

4. Submit the request with the date parameter in the format MM/dd/yyyy to your Spring MVC controller and you should see the date passed printed in the server console.


How to Add Cipher Suites Support in Openfire and Smack

Creating the Openfire Patch

This is how to patch Openfire to add cipher suite support.

1. Download the Openfire source distribution from http://www.igniterealtime.org/downloads/source.jsp.

2. Edit the class org.jivesoftware.openfire.nio.NIOConnection.

3. Look for the following code, around line 355.

filter.setUseClientMode(clientMode);

4. Insert the following code after.

String cipherSuites = JiveGlobals.getProperty("xmpp.socket.ssl.cipherSuites");
if (cipherSuites != null && !cipherSuites.isEmpty()) {
      filter.setEnabledCipherSuites(cipherSuites.split(","));
}

5. Read the official instructions on how to build Openfire from source. You will need to build openfire.jar from the source distribution.

Creating the Smack Patch

1. Get the Smack code from http://www.igniterealtime.org/downloads/source.jsp.

2. Edit org.jivesoftware.smack.XMPPConnection.

3. Look for the following code, around line 817.

// Proceed to do the handshake
((SSLSocket) socket).startHandshake();

4. Insert the following lines before.

String cipherSuites = System.getProperty("https.cipherSuites");
if (cipherSuites != null && !cipherSuites.isEmpty()) {
   ((SSLSocket) socket).setEnabledCipherSuites(cipherSuites.split(","));
}

5. Read the official instructions on how to build Smack from source. You will need to build smack.jar from the source distribution.