Performing API calls from a mobile client, reauthorizing the session if necessary I'm working on mobile app which doing requests to a server's API. I want to develop module that doing following:


get authorization key already exists in previous session
connect to server api and read server StatusLine answer code
if answer code is 403 then try to re authorize
when authorization is passed process server answer body


I implement this with state-machine:

enum ConnectionState {
    checkAnswer, reAuth, processLinks, endProcess
}


And made process in while-loop:

        int authCount = 0;
        HttpResponse httpResponse = null;
        ConnectionState state = ConnectionState.checkAnswer;
        boolean processConnect = true;
        while (processConnect) {
            switch (state) {
                case checkAnswer:
                    httpResponse = client.execute(httpGet);
                    final int serverAnswer = httpResponse.getStatusLine().getStatusCode();
                    if (403 == serverAnswer) {
                        state = ConnectionState.reAuth;
                    } else {
                        state = ConnectionState.processLinks;
                    }
                    break;
                case reAuth:
                    if (++authCount &gt; MAX_AUTH_ATTEMPTS) {
                        state = ConnectionState.endProcess;
                        break;
                    }
                    auth.invalidateAuth();
                    if (!auth.doAuth()) {
                        Utils.log("could not authorize api");
                    } else {
                        state = ConnectionState.checkAnswer;
                    }
                    break;
                case processLinks:
                    if (null == httpResponse)
                        throw new RuntimeException("HttpResponse is null");
                    processLinks(httpResponse, imgLinks);
                    state = ConnectionState.endProcess;
                    break;
                case endProcess:
                    processConnect = false;
                    break;
                default:
                    throw new RuntimeException("Finded impossible state: " + state.toString());
            }
        }


What do you think about such code organization for this task? How to write it more elegantly?
I agree with Ibalazscs that StatePattern makes sense - using a switch to drive changes in behavior is a code smell.

Given that this state machine has only one end state, you could ditch the processConnect boolean, and just check for end state...

ConnectionState state = ConnectionState.checkAnswer;
while(! ConnectionState.endProcess.equals(state) {
  ...
}


You also might want to consider whether ConnectionState should implement an interface to make it extensible.  See Item #34 "Emulate extensible enums with interfaces", Effective Java Second Edition, by Joshua Bloch