Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Essentially I have created two entry points to the same service __createUser() on the same interface class UserRestService.  I am going to repeat this pattern for all the other services I want to expose.  This is a simple case of refactoring the interfaces methods.

The __createUser() method looks like this

Code Block
languagejava
titleCode Sample
linenumberstrue
    private Response    __createUser( User usr )
    {
        Response resp;
        try
        {
            DBConnector connector = DBConnector.getConnector();
            connector.loadAndConnect();

            UserHandler.getLoader().addUser(connector.getConnection(), usr);
            
            resp = Response.status(200).entity(usr).build();
        } catch (IOException ex)
        {
            resp = Response.status(200).entity(new SimpleErrorMessage("CANNOT ADD USER - SERVER PROBLEM")).build();
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ResponseException ex)
        {
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
            resp = Response.status(200).entity(ex.getResponse()).build();
        } catch (SQLException ex)
        {
            resp = Response.status(200).entity(new SimpleErrorMessage(usr.getUserID() + " CANNOT BE ADDED - ALREADY EXIST")).build();
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;
    }

I didn't use Java 8 exception multi catch statements as I wanted to report different messages back to the client.