Teneo !!!

Aaron’s blog on Networking, and Enterprise Technology

Category Archives: Java

Liferay 6+ and Blog Portlet Ideas

So, I’m in the process to looking at other vendor’s for my Blog.  I really like WordPress for a number of reasons, but I want my website to be fully integrated with my blog.  I don’t want to have 2 seperate websites.

Liferay is my choice (mostly because I love java, it’s very expandable, and I want to implement Liferay at my company for our intranet portal.)

However, there are a few things the Blog Portlet is lacking.  The largest among those, is “Anonymous Comments”.  See, not everyone wants to “Create an account”, for just one comment.  If I were building a community, that would be different, but I’m not.  The purpose of a blog is not one-way communications.  If that were true, then we would just create static web pages.  We need two-way communications, and the harder we make it for our visitors, the less likely we will have them.

The first thing that we should implement for Liferay Blog, would be to “easily allow” guest comments.  Right now, we have to modify an embedded text file called “blogs.xml” and add it to our ext-environment.  Next, we can add captcha to guest comments, which will prevent bots from spamming our blogs.  Liferay already implements Captcha, so it should not be too difficult.

Those two things will get everything started.  Now, to get Liferay Blog “up-to-standards” with the other vendors, the next milestone would be comment workflow.  This is to allow the moderating of comments, i.e. to not publish comments until an approval is made.  Again, not too difficult, as Liferay already uses workflow.

For example, with WordPress, you can add comment moderation, which sends out an email when a comment is waiting to be approved.  You can also add, “approve comment, if you have previously approved comments from the same person”.  So, if Liferay sees a comment from someone that you have previously approved, then auto-approve it.

So, to sum up:

  1. Add 3 fields to anonymous comments
    1. Name
    2. Email Address
    3. Website
  2. Easily configure to allow Anonymous comments in the config pane of the blog portlet
  3. Option to add “Captcha” or “Re-Captcha” for comment authentication
  4. Comment moderation (i.e. Workflow)
Advertisements

Java – IP Address to Integer and back

Many applications store the IP Address as an integer.  This makes it easy to perform mathematical operations on it (say, adding 1024 hosts to a subnet to get min/max addresses, etc).

Currently, I am implementing IPPlan to my toolset, to help me manage all my IP’s and subnets, and make it easy for others to request IP’s.  However, I didn’t want to duplicate the IP’s in use.  I wanted my current network management system to FEED the hosts it already knows into IPPlan.

Easy, I’ll just run a daily SQL to update the IPPlan database.  The issue is, my NMS stores the IP Address as a String, and IPPlan stores it as an integer.

So, taking bits and pieces off the internet, here are a couple of cool Java methods that helped me accomplish this:

From Limewire’s IP.java, here is converting an existing Integer to String:

public static String intToIp(int i) {
        return ((i >> 24 ) & 0xFF) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >>  8 ) & 0xFF) + "." +
               ( i        & 0xFF);
    }

From Justin-Cook.com, here is converting an existing String to Integer:

public static Long ipToInt(String addr) {
        String[] addrArray = addr.split("\\.");

        long num = 0;
        for (int i=0;i<addrArray.length;i++) {
            int power = 3-i;

            num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
        }
        return num;
    }

Cisco Timestamps – Converting

This was so freaking confusing for me! In Java, I would convert my timestamps one way, but then, if I needed to convert them in Excel, I was 70 years off?!? WTF.

Well, here’s how you convert those timestamps to something meaningful, and why you are getting different results with different systems.

First, some terms:

Unix Epoch: number of seconds elapsed since Jan 1, 1970

NTP Epoch: number of seconds elapsed since Jan 1, 1900

I have no idea why the different Epochs, but did you notice that the difference is 70 years? YEP!!!

Okay, so Microsoft Products use the NTP Epoch (i.e. SQL Server, Excel, Access, etc) as a reference to build it’s Date objects. Unix, Macintosh, Java, and C / C++ uses the Unix Epoch as a reference for it’s date objects. Cisco uses Unix Epoch to export its timestamps.

So, basically, we have to add 25,569 days (70 years, approx) to the NTP Epoch, in order to get valid results in NTP Epoch-type systems (Excel, SQL Server, etc).

Convert a timestamp in JAVA:

public static void main(String[] args){

// declare our timestamp in seconds

long timestamp = 1198167416;

// Since timestamp is in seconds, but Java Date works in milliseconds,

// convert to milliseconds
Date mydate = new Date(timestamp*1000);

// Format how the date is displayed to us

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");

// Print the date to standard out.

System.out.println(formatter.format(mydate));
}

Convert a timestamp in Excel (Assuming your timestamp to convert is in column A1):

A1/86400+25569

Then, just format the cells as “Date”. So, to explain the Excel formula:

1). Divide the timestamp by the number of seconds in a day. This will give you the number of days

2). Add 25569 days to the already converted days, to take into account the difference between Unix Epoch and NTP Epoch shift.