Share the Knowledge
RSS icon Home icon
  • How to print the values of all the fields of an object in Java

    Posted on August 17th, 2011 webmaster No comments         Print Print

    I was just doing some Java coding and I needed to check the values of a bunch of fields of an object (mostly numbers).  Normally I’d just call and print each get() method if I just need to check a few fields, but the class I’m dealing with has over 30 fields and I need to check them all and there’s no way I’m doing over 30 print statements for each get() method!

    A quick Google search led me to this StackOverflow post which has exactly what I was looking for:

    ClassABC abc = new ClassABC();
    for (Field field : abc.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(abc);
        System.out.printf("%s: %s%n", name, value);
    }
    
  • GWT, Django, and JSON (with padding)

    Posted on August 8th, 2011 webmaster No comments         Print Print

    A couple of months ago I was working on a project where I needed to retrieve data in JSON format from a web service written in Python (using the Django web framework) under GWT.  The data is then loaded into GXT/Ext GWT grids and charts.

    After looking at a GXT example of loading JSON data to a grid, I thought this would be pretty straightforward, but of course I was wrong.  Since the Django app is on a different server and domain/subdomain, I learned something called “same origin policy“, preventing our two applications to communicate with each other.

    Fortunately, there’s an easy workaround to this using “JSON with padding” or JSONP.  Here’s one way to get this working:

    GWT side:

    1.  The JsonpRequestBuilder object has been added in GWT 2.0 making these types of cross site requests easier to implement.  Here’s a very well written post showing how to do this (yes, I’m lazy and he obviously knows a lot more about these than I do :D ).

    Now, when you make a request to the web service on the Django side, it will automatically append the callback function in the request and the request may look something like this:

    GET /get_gpu_stats/?callback=__gwt__jsonp__.I18.onSuccess HTTP/1.1

    Django side:

    1.  In your Django view, one way to handle this is to inspect the request and see if there’s a parameter called “callback” and change the response so it looks like a function (this is the “padding” part) instead of a regular JSON response.  You can do something like this (forgot where I found this example from):

    def get_gpu_stats(request):
        .
        .
        .
        gpu_stats = {'gpu_stats': data}
        response = simplejson.dumps(gpu_stats)
    
        # Return as JSONP if the callback parameter is specified.
        callback = request.GET.get('callback')
        if callback:
            response = '%s(%s)' % (callback, response)
        return HttpResponse(response, mimetype='application/json')
    

    Yup, that’s it, they should now be able to exchange data!

  • How to convert a Java keystore (JKS) to PEM format

    Posted on May 8th, 2011 webmaster No comments         Print Print

    I’m currently working on a new project where I need to write a service to serve static files to users.  Our web application currently runs on Tomcat alone as we don’t have many users (internal users only) and most of the content is dynamic.  But with this new project it just seems to make sense to put an Apache web server in front of Tomcat and use the mod_xsendfile module to serve the files, which will also allow us to control which users can access which files.

    So I started playing with Apache, mod_xsendfile, and mod_proxy and finally got things working.  The last step is to add encryption.  The Java keystore format won’t work with Apache, however, so I needed a way to export the certificate and private key from the Java keystore we used for Tomcat and import it to a new PEM file so I can use it with Apache.

    If we’re controlling the Certification Authority (CA) I probably would’ve just generated a new certificate, but this is unfortunately not the case in our environment (big company thing).  I would need to generate a certificate signing request, open a ticket with corporate IT, wait for approval, wait for someone to do it and send it to me, etc.  The process could take a few days so I decided to just do some Googling on how to extract the keys/certificates from the keystore and convert it to PEM which Apache web server will accept.

    There doesn’t seem to be a quick way to directly convert from JKS to PEM so I had to convert from JKS to PKCS#12 first, then to PEM.

    Here are the steps I took to do the conversion:

    1.  Export certificate from the Java keystore and import it to a new PKCS#12 keystore format using the Java keytool (C:\Program Files\Java\jre6\bin\keytool.exe by default on Windows).

    keytool -importkeystore -srckeystore myapp.jks -destkeystore myapp.p12 -srcalias myapp-dev -srcstoretype jks -deststoretype pkcs12
    

    2. Convert the new PKCS#12 file (myapp.p12) to PEM using openssl (openssl.exe is in the bin directory of the Apache installation on Windows).

    openssl pkcs12 -in myapp.p12 -out myapp.pem
    

    If you’re running Apache on *nix, you’re all set! But if you’re running on Windows (I know, I know), you will need to remove the passphrase from the PEM file.

    3. (Optional depending on enviroment) Create a version of the PEM file with the passphrase removed.

    You may get this message when using the certificate in Apache running on Windows:

    SSLPassPhraseDialog builtin is not supported on Win32.

    The solution is to remove the password/passphrase from the PEM file, so let’s create a version of the PEM file without the passphrase.

    openssl rsa -in myapp.pem -out myapp_nopassphrase.pem
    openssl x509 -in myapp.pem >>myapp_nopassphrase.pem
    

    Reference the myapp_newpassphrase.pem in your httpd.conf, start the Apache service, and you’re good to go!

    Sources: