Versions Compared

Key

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

...

Code Block
languagejava
titleCode Sample - UserRestService
linenumberstrue
collapsetrue
package dbk.webservices;

/**
 *
 * @author Selvyn
 */
import com.celestial.dbankbusinessobjects.User;
import com.celestial.dbankbusinessobjects.UserMap;
import com.celestial.util.objects.SimpleErrorMessage;
import dbk.dbutils.DBConnector;
import dbk.dbutils.ResponseException;
import dbk.dbutils.UserHandler;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/user")
public class UserRestServices
{

    private String genPassword()
    {
        String result = "";
        Random gen = new Random();

        for (int x = 0; x < 8; x++)
        {
            result += (char) (gen.nextInt(26) + 'A');
        }

        return result;
    }

    @GET
    @Path("/sayhello")
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHelloTest()
    {
        return "<html> " + "<title>" + "Hello World" + "</title>"
                + "<body><h1>" + "Hello from the Deutsche Bank DB Tier" + "</h1></body>" + "</html> ";
    }

    @GET
    @Path("/cu/{user}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response createUser(@PathParam("user") String userId)
    {
        User usr = new User(userId, genPassword());
        
        return __createUser(usr);
    }

    @POST
    @Path("/cu")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createUserFromObject(User usr)
    {
        usr.setUserPwd(genPassword());
        return __createUser(usr);
    }
    
    private Response    __createUser( User usr )
    {
        Response resp;
        try
        {
            DBConnector connector = DBConnector.getConnector();
            connector.loadAndConnect();

            UserHandler.getLoader().addUser(connector.getConnection(), usr);
            
            resp = Response.status(201).entity(usr).build();
        } catch (IOException ex)
        {
            resp = Response.status(500).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(500).entity(ex.getResponse()).build();
        } catch (SQLException ex)
        {
            resp = Response.status(409).entity(new SimpleErrorMessage(usr.getUserID() + " CANNOT BE ADDED - ALREADY EXIST")).build();
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;
    }

    @DELETE
    @Path("/du")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteUser(User usr)
    {
        Response resp;
        try
        {
            DBConnector connector = DBConnector.getConnector();
            connector.loadAndConnect();

            UserHandler.getLoader().deleteUser(connector.getConnection(), usr);
            resp = Response.status(200).entity( usr ).build();
        } catch (IOException ex)
        {
            resp = Response.status(500).entity(new SimpleErrorMessage("CANNOT DELETE 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(409).entity(new SimpleErrorMessage(usr.getUserID() + " CANNOT BE DELETD - USER DOES NOT EXIST")).build();
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;
    }

    @PUT
    @Path("/uu")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateUserFromObject(UserMap umap)
    {
        return __updateUser( umap );
    }

    @GET
    @Path("/uu/{oldid}/{oldpwd}/{newid}/{newpwd}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response updateUser( @PathParam("oldid") String oldId, 
                                @PathParam("oldpwd") String oldpwd,
                                @PathParam("newid") String newId, 
                                @PathParam("newpwd") String newpwd)
    {
        User oldUser = new User( oldId, oldpwd );
        User newUser = new User( newId, newpwd );
        UserMap umap = new UserMap( oldUser, newUser );
        
        return __updateUser( umap );
    }
    
    private Response    __updateUser( UserMap umap )
    {
        Response resp;
        try
        {
            DBConnector connector = DBConnector.getConnector();
            connector.loadAndConnect();

            UserHandler.getLoader().updateUser(connector.getConnection(), umap.oldUser, umap.newUser);
            resp = Response.status(200).entity( umap ).build();
        } catch (IOException ex)
        {
            resp = Response.status(500).entity(new SimpleErrorMessage("CANNOT UPDATE 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(409).entity(new SimpleErrorMessage(umap.oldUser.getUserID() + " CANNOT BE CHANGED - USER DOES NOT EXIST")).build();
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;
    }

    @POST
    @Path("/fu")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response findUser( User usr )
    {
        return __findUser( usr );
    }
    
    @GET
    @Path("/fu/{user}/{pwd}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findUser(@PathParam("user") String userId, @PathParam("pwd") String pwd)
    {
        User usr = new User( userId, pwd );
        return __findUser( usr );
    }
    
    private Response    __findUser( User requsr )
    {
        User usr = null;
        try
        {
            DBConnector connector = DBConnector.getConnector();
            connector.loadAndConnect();

            usr = UserHandler.getLoader().loadFromDB(connector.getConnection(), requsr.getUserID(), requsr.getUserPwd());
            if (usr != null)
            {
                return Response.status(200).entity(usr).build();
            } else
            {
                SimpleErrorMessage msg = new SimpleErrorMessage("USER NOT FOUND");
                return Response.status(200).entity(msg).build();
            }
        } catch (IOException ex)
        {
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ResponseException ex)
        {
            Logger.getLogger(UserRestServices.class.getName()).log(Level.SEVERE, null, ex);
            return Response.status(200).entity(ex.getResponse()).build();
        }

        SimpleErrorMessage msg = new SimpleErrorMessage("ERROR IN DB TIER");
        return Response.status(200).entity(msg).build();
    }
}