Using scaffolding with REST in grails

You are developing an app and using Grails as its backend, now days REST is the new buzzword and you’ll probably want to create a REST api via Grails. Fortunately, Grails support REST api out of the box (version 2.3 and above), but what happens if you want to support scaffolding + REST api in the same controller.

Well, it seems that Grails supports it, although not in the most elegant way, by using content negotiation.
You have an ability to tell Grails in what format you want your response either by supplying a suffix to the request (.json) or by specifying it in the header. Either way it gives you the following ability:

  1. Define a regular Grails controller.
    class FamilyController {
     static scaffold = true
    }
    
  2. When calling the controller you can add a suffix to you call
    http://localhost:8080/app/family/index.json
  3. You should be able to get back results as json, same applies to xml.
  4. As you can see the default content being served is html so you can also access the scaffolding page.

The annoying part here is the fact that you cannot access your resource in a “by the book” manner, .e.g. you need to use an action like show, edit/${id} to access your resource and you can not do it directly via family/1

Advertisements

Leave a comment

Filed under Uncategorized

ConcurrentModificationException when using unmodifiableCollection

Learning something new everyday.

One of the best ways to avoid data manipulation problems in concurrent environments is to use read only data sets, one of the best way way to create a read only collection in Java is to use unmodifiableCollection method, however this method is not as innocent as you may think.

Behind the scenes unmodifiable collections are just a stub that redirect all methods to the original collections, except for methods that actually change the collection like add etc, in which case it throws an exception.

Test yourself, what happens when trying to iterate over an unmodifiable collection, when in a different thread new elements are added to the original collection… ConcurrentModificationException .

The best way to avoid it is to actually create a copy, e.g. new LinkedList(…)

To sum it up:

  1. Use unmodifiable collections whenever you want to make sure that in an external usage of the collection nothing will be modified, for example use it as a return value for your methods.
  2. Use different mechanisms in case of a multi-threaded environment, for example creating a real copy.

Leave a comment

Filed under API, Uncategorized

Instagram API wrapper for Android

Synopsis

Instagram(IG) API wrapper for Android for handling an Instagram app lifecycle. Split into 2 sub-projects:

  • InstagramAuth Authentication library, based uppon Facebook for Android library.
  • restig A wrapper arround IG rest API calls. Based upon restFB.

Documentation

Currently short on documentation, will add more as time permits.

Examples

InstagramAuth

/**
 * Authorize a user in Instagram. Will show up the auth dialog and will
 * allow the user to enter his/her credentials.
*

* <strong> Pay attention</strong> Use the listener afterwards to save the
 * authentication token.
 *
 * @param listener
 * - A dialog listener that can react to various authentication
 * events. Cannot be null.
 * @param permissions
 * - Use various permissions. Can be empty but not null.
 * @param activity
 * - An activity that will be the parent of the dialog box.
 */
 public void authorize(final DialogListener listener,
 final Activity activity, final String... permissions) {
 InstagramModule module = new InstagramModule();
 final InstagramAuthDialog dialog = new InstagramAuthDialog(activity,
 listener, mAppId, permissions);
 dialog.setCancelable(false);
 dialog.show();
 }

public class InstagramModule implements
 DialogListener {
 @Override
 public void onComplete(final Bundle values) {
 String accessToken = values.getString("access_token");
 }

@Override
 public void onError(final DialogError error) {
 // Log error
 }

@Override
 public void onCancel() {
 // User canceled.
 }
 }
 ...
 authorize(new InstagramModule(), activity, "likes", "comments");

restig

InstagramClient userClient = new DefaultInstagramClient(appId, mAccessToken);
final String endPoint = format("media/%s/comments",
"1234");
final InstagramCollection comments = client
.fetchCollection(endPoint, Comment.class);

You can download the source from github

Leave a comment

Filed under API

Android 101

Gave a lecture today on Android development for ILTechTalks, mainly first steps.

Talked about the following topics:

  1. Prerequisites
  2. Activities
  3. Widgets and layouts
  4. Saving content
  5. Publishing to the market

You can download the presentation and related code from here. You are more than welcomed to use this content under the creative common license.

Leave a comment

Filed under Uncategorized

Facebook Mobile HACK – Tel Aviv

Once in while I have a chance to attend an event that actually leaves an impression, “Facebook Mobile Hack” was one of those.

The event lasted for 12 hours ! it had technical lectures from Facebook R&D and what I think was the cream the la creme, the hackaton.

If you had a chance to read Mark’s manifest then you understand that intense hack days like these are rooted deep in the Facebook culture and all I can say that it was magnificent.

Beer in one end, coffee on the other I sat in one of the corners of the hanger and started coding for about 5 hours, sitting on my chair and feeling how the adrenaline is pumping. At the end of the hackaton all of the contenders went to stage, pitched the idea to all of us and to the panel of judges (Yossi Vardi,  influential VCs, Angels and techies).

The winner of the event was a cool app called Instapeace which connects FB and Instagram. Unfortunately I didn’t win, but you can see the end result on the Android Market – I Want To Buy

For those of you interested in the technical stuff you can download the presentations from here.

Leave a comment

Filed under Building

AOP on Android – Duplicate files at the same path inside the APK

This post won’t deal with Android/aop configuration, for more information on the subject  I would recommend reading this one.

I’ve been struggling all day to resolve an annoying issue I had with aspectJ, Android and Maven.

When compiling java class files to dex the dex compiler packs everything under the same file, classes.dex. During this process if for any reason you have files with the same name that exist under the same path then you’ll have the dreadful exception “Duplicate files at  the same path inside the APK”.

Whenever using the aspectj maven plugin a configuration file named builddef.lst is created and is placed in the project build output directory. It seems that this is the culprit for duplicate file exception. One way to solve it is to use argumentFileName parameter under the plugin configuration:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>    <!-- use this goal to weave all your main classes -->
            </goals>
        </execution>
    </executions>
    <configuration>
        <argumentFileName>trace.lst</argumentFileName> <!-- Set the name here -->
        <complianceLevel>1.6</complianceLevel>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>com.blinxbox</groupId>
                <artifactId>aop</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
</plugin>

The problem is the need to give a different name for each library that uses aop, annoying to the say the least.
If only we could remove the configuration file from the final jar/apk, guess what…you can. The jar plugin comes to the rescue:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <excludes>
            <exclude>**/trace.lst</exclude>
        </excludes>
    </configuration>
</plugin>

We are excluding trace.lst and avoiding the problem from the start. Make sure to add this plugin under the build section of your pom file for each jar/apk that uses aop and you are done.

Leave a comment

Filed under Building

Which is less expensive: Amazon or self-hosted? The start-up angle

I read today a wonderful article about AWS (iaas) vs self hosted solution , the pros and cons money wise.

Couple of points regarding the discussion that relates directly to startups:

  1. The author recommends for spiky/unknown/unproven loads to stay with the public cloud,later in the comments he adds: “ …let’s the the situation of an early-stage business. While I’ve referred to “spiky” loads, there’s another way to say that which is “variable”, “unknown” or “unproven” loads which describes the situation that a startup usually finds itself in. In those cases, the fact that you cannot forecast very well is the reason why it’s unlikely you’ll save money by self-hosting…because you’re very unlikely to buy the right amount of capacity.
  2. He is talking here mainly about infrastructure as a service (iaas), one major point that I think he overlooked here is that many startups don’t want to deal with the provisioning and machine preparations (you still have to do that under iaas), we want to be lean and quick to market, I would prefer to pay more money for platform as a service (paas like app engine) and deal with writing the actual business logic.
  3. Disaster recovery is also important, the ability to spread your machine across several geographical areas is costly.
  4. No matter how you’ll look at it, there is a good chance that as a startup you’ll use cloud api for your business, there are so many services today that cuts down your dev time and are only available as public api on the cloud. A good example might be google analytics. So you can’t really escape it.

3 Comments

Filed under Uncategorized