Wednesday, May 14, 2014

Software licenses in plain english

Often in projects, especially private hobby projects, I find myself getting a headache when trying to understand the software license for some library or graphics that I want to use. I have know found two sites that makes this a bit easier.

http://choosealicense.com/
https://tldrlegal.com/

Both sites lists popular licenses and simple tables showing what you have to, can and can not do with the licensed object. Very helpful.

Exception: "Apache xmlsec IdResolver could not resolve the Element for id reference" while decrypting

org.opensaml.xml.validation.ValidationException: Apache xmlsec IdResolver could not resolve the Element for id reference:

This is a common exception that can be thrown when verifying a signature after decryption an object.

To avoid this, it is often enough to configure your Decryptor using the following setting before decrypting.

decrypter.setRootInNewDocument(true);

Monday, May 5, 2014

Nullpointer exception in OpenSAML

This is a common exception to beginners in using OpenSAML. A common mistake when starting to use the OpenSAML library is to not initialise the library.
OpenSAML needs a couple of configuration files in order to work. The library is provided with a default set of these files that is sufficient for mot uses. Before starting the use the library the configurations must be loaded. This is done using the bootstrap function.

try {
   DefaultBootstrap.bootstrap();
} catch (ConfigurationException e) {
   throw new RuntimeException("Bootstrapping failed");
}      

If you do not do this before you start using the library, you might run into exceptions like.

Exception in thread "main" java.lang.NullPointerException
 at no.steras.opensaml.Main.main(Main.java:25)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Tuesday, March 18, 2014

Authenticating to Google Fusion Tables from Server

A couple of weeks ago I started a project where I needed to save big amounts of data for later statistical analysis. The first data store I looked at was Google Fusion Tabels (because it was free...). The reason I didn't go for it was that there were quota limitation that didn't allow that much data.

But before I got to the point that I discarded it wrote a working backend on Google app engine that could write to a table. Here is the code. The hard thing here was authenticating to the Fusion tables. 

Working with Google APIs from Google App Engine should be very easy thanks to Google App engines support for service account, unfortunately Fusion tables API does not support this yet. So we need to use the harder way to use Application Accounts. Here is the code for authenticating and making request to Fusion tables. Please ignore bad exception handling.
private Fusiontables fusiontables = null;
   
    public void initiateFusionTablesAPI() {
        Collection scopes =
                Collections.singleton("https://www.googleapis.com/auth/fusiontables");

        String serviceAccountId = "768188911902@developer.gserviceaccount.com";

        String p12FileName = "/privatekey.p12";
        InputStream p12Stream = context.getResourceAsStream(p12FileName);
        PrivateKey serviceAccountPK = null;

        try {
            serviceAccountPK = SecurityUtils.loadPrivateKeyFromKeyStore(
                    SecurityUtils.getPkcs12KeyStore(), p12Stream,
                    "notasecret", "privatekey", "notasecret");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(new NetHttpTransport())
                .setJsonFactory(new JacksonFactory())
                .setServiceAccountId(serviceAccountId)
                .setServiceAccountScopes(scopes)
                .setServiceAccountPrivateKey(serviceAccountPK)
                .build();



        fusiontables = new Fusiontables.Builder(
                new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("Bysykkel-stats")
                .build();

    }

   
    public void storeInformation(){

    String sql = "INSERT INTO ...";
   
    try {
        Sqlresponse sqlresponse = fusiontables.query().sql(sql).execute();
        if (sqlresponse.getRows().size() != 1) {
            throw new RuntimeException("Error in sql response " + sqlresponse.toPrettyString());
        }
    }  catch (Exception e) {
            e.printStackTrace();

    }

    }


Further reading

This book covers, among other things, google fusion tables

Wednesday, March 12, 2014

Tool to test queries on Google Fusion tables

A couple of weeks ago I started a project where I needed to save big amounts of data for later statistical analysis. The first data store I looked at was Google Fusion Tabels (because it was free...). The reason I didn't go for it was that there were quota limitation that didn't allow that much data.

But another problem I found was that there is no good way to simply test out queries on a table. So I built a tool for it. So for those of you using Google Fusion Tables or thinking about it, go have a look.

The tool is here, https://rasmusson.github.io/fusion-tables-tool
Any ideas can be submitted here, https://github.com/rasmusson/fusion-tables-tool/issues


Further reading

This book covers, among other things, google fusion tables

Saturday, March 8, 2014

A simple way of doing pagination with Angular UI

In the last few days I have been developing a tool to work with Google fusion tables. In this I display results for a search query in a table with pagination. I found a lot of different ways to do this but I finally ended up with this.
{{column}}
{{cell}}
The relevant part in this is row in queryResult.rows.slice(((currentPage-1)*10), ((currentPage)*10)) track by $index. 

I using the currentPage variable from the Angular UI pagination component in combination with the javascript array slice method to split the result up in pages. Simple and efficient.

Here is a plunker with an example

The limitation of this being that you have to load the entire result at once. Pagination is entirely on the client side and not in the query.

Further reading

This is the book I'm reading to learn AngularJS. Very good and very well formatted for kindle.

Friday, January 17, 2014

Using CDI in external JAR module

I recently had a task to create a REST API in a jar module separate from the main application. The JAR will be a dependency to the main application.
In this REST resource class I which to inject the som session scoped data about the user.

Problem is that CDI does not inspect external JAR files by default. This off cource resulted in the classic NullPointerException.

After some research  found that adding an empty beans.xml to the resources/META-INF/ tells CDI to inspect the classes in the JAR for injection points.