Leave a comment

Jafkopf II

A couple of months after publishing the first post about Jafkopf – my first open source project – I finally managed to upload first classes in SVN!
The reason for this lag was my own disability to install the corresponding plugin to eclipse 3.7.1 because of a recurring error message. By updating to 3.7.2 I finally wangled to install this plugin, so I could first upload some code.

I hope it did’t take too long by the end …

Leave a comment

Command Pattern

Managing commands belongs to the group of behavioral patterns.

Imagine a switch for turning the light on and off again, or the remote control of your tv at home. These are two examples out of our daily lives for illustration of the command pattern.

Let’s start with a simple example.
At first we need a class that represents a certain behaviour. In my example it’s the light …

public class Light {
        public void turnOn() {
                // do something
                System.out.println("Light is on");
        }

        public void turnOff() {
                // do something
                System.out.println("Light is off");
        }
}

Now we have to define an interface for the commands we want to implement …

public interface Command {
        public void execute();
}

Next we use this interface for writing the two classes that turn the light on and off by calling the methods of the class Light.

public class LightOnCommand implements Command {

        private Light l;

        public LightOnCommand (Light l) {
                this.l = l;
        }

        public void execute() {
                l.turnOn();
        }
}

public class LightOffCommand implements Command {

        private Light l;

        public LightOffCommand (Light l) {
                this.l = l;
        }

        public void execute() {
                l.turnOff();
        }
}

At last we probably want to have a class that contains and calls both commands we’ve just written: the switch!

public class Switch {

        private Command on, off;

        public Switch(Command on, Command off) {
                this.on = on;
                this.off = off;
        }

        public void activate() { 
                on.execute();                           
        }

        public void deactivate() {
                off.execute();
        }
}

If you implement a graphical user interface by using Java Swing, the following points you have to adjust:

  • command interface -> ActionListener, MouseListener, etc.
  • commands -> classes that implement a certain Listener
  • Switch -> the class creating and managing the GUI that contains JButtons, JMenuItems, etc. instead of the methods activate() and deactivate()

Finally the methods of the class Light probably have some return value, that the (Action-)Listener can transmit to the gui class by calling one of its methods that changes the look of a view element to show the result of the action to the user.

Leave a comment

My lovely friend called Twitter API Policy

I was writing a client for Twitter. I’ve chosen the xAuth login mode, because I thought it would be the easiest way to log in (further possibilities you find here). After I’ve done many attempts to create a http request, that orders a request token I need to authorize the further requests, e.g. for writing a tweet, I read by combing throught the API documentation of Twitter, that I had to send a mail to the Twitter support to ask their permission for using xAuth …
Rats! If I had read it earlier, i would have been able to save much time. At least I thought so …

Perhaps I should list all the steps I’ve done before:
I had to …

  1. … read many articles about what a RESTful webservice actually is
  2. … teach myself how to use HTTP requests in Java to communicate with the webservers of Twitter
  3. … discover, that I have to register my client to get a consumer key and consumer secret I need for requests to the Twitter servers
  4. … read technical documentation over and over again
  5. … make many attemps of logging in that failed until I figured, that I had to ask their permission in terms of xAuth

So I wrote a mail to api@twitter.com regarding xAuth. In this mail I had to describe which data and resources the client would use and some other informations like the consumer key, the client name, etc..
The first response of them was a confirmation of receipt:

01/05/2012 12:06:48 AM CET

Hello,

Thank you for your interest in xAuth access.

This email is an automated response to acknowledge receipt of your email regarding xAuth. A member of our team will review your request as soon as possible.

Our review will proceed much more quickly if you were to reply to this ticket with the following required information:

• Your App ID number (or consumer key) from https://dev.twitter.com/apps/
• A link to your application’s website
• Links to screenshots of your application’s user-facing Twitter functionality
• Links to screenshots of your application in use

Please note that our ticketing system does not accept attachments, so you will need to upload your screenshots to your server or an image host and link to them in this ticket with full URLs (HTML is not supported).

If you have already provided the required information in your initial request, please respond to this ticket to affirm that the required information is present.

For your application to qualify for xAuth access, it needs to be production-ready and only accessible via standalone desktop application or via mobile.

We are not granting xAuth for test purposes, single-user applications, or for applications that are in the early stages of development.

If you are not requesting access to XAuth and have received this response in error, please let us know by responding to this ticket.

If you have a technical question about the API, please visit our developer site, where you will find extensive documentation about the Twitter API: http://dev.twitter.com/doc

This ticket will be closed automatically if we do not receive a response from you within one week.

Thanks,
Twitter API Policy

The next day, at 01/06/2012 08:38:51 PM CET, they wrote me:

Hello,

We’ve reviewed your request. Unfortunately, we do not grant specific authentication privileges for testing purposes.

Instead, we recommend that you set up a one-time OAuth workflow to obtain the access token for a single account, and then store that token to authorize your API calls during this initial development stage.

You’ll find resources that will guide you through this process on our developer site: https://dev.twitter.com/docs/auth/oauth/single-user-with-examples.

Twitter does not expire access tokens unless the user revokes access to the application, or the application is suspended for violating policy.

We apologize for any inconvenience this policy may create for you and your project. Please let us know when you have a more production-ready version of your application, or if you have any other questions.

Thanks,
Twitter API Policy

One day later I answered, that I managed to implement the core functionality of posting tweets and displaying home timelines. I hoped they would accept my request this time. So I was waiting …
At 01/10/2012 01:43 PM CET, I still got no answer yet … *rmpf*. So I did’t know what to do! I seemed to be forced to be satisfied with a single-user application for now, but I’ll try to get the permission of using xAuth a second time soon! I hope I’ve got greater success the next time.

… to be continued

[update 11.01.2012]
It’s 10.01.2012 09:46:30 PM CET and I received a new mail sent by notifications-support@twitter.zendesk.com!!! I was full of hope of getting the permission I begged for.

But the reality was disillusioning:

Hello,

Thank you for writing in. We have reviewed the information you have provided and believe that a different authentication method may better suit your application’s needs.

Please visit this page on our developer site, where you will find comprehensive documentation on all of the Twitter API’s authentication and authorization paths: https://dev.twitter.com/docs/auth.

Regards,
Twitter API Policy

… Ok, maybe I should rearrange my initial development plan and deal with these other methods – even though I already put some efforts in realizing the login this way!
I’ll post my results as soon as the work is done.