如何通过Web Service Rest通过ID获取用户信息

时间:2019-05-12 06:52:37

标签: java json rest web-services jboss

所以我正在为移动应用程序登录,但是我需要一个Rest Web服务,这样我才能获得用户的帐户,所以我在想,也许我应该创建一个Web服务,在其中我通过id和然后在登录时用它来获取他的帐户,所以唯一可以做的就是让所有客户都可以帮助我,我已经为此奋斗了一个多月。

@Stateless
public class PersonneService implements PersonneServiceLocal {

    @PersistenceContext(unitName="bankingApp-ejb")
    EntityManager em;

    @Override
    public Employee addPeronne(Employee employee) {
        try {
            employee.setPassword(Utils.toMD5(employee.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            return null;

        }
        employee.setToken(Utils.tokenGenerator());
        em.persist(employee);
        return employee;
    }

    @Override
    public Client addClient(Client client) {
        try {
            client.setPassword(Utils.toMD5(client.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            return null;

        }
        client.setToken(Utils.tokenGenerator());
        em.persist(client);
        return client;
    }


    public List<Client> getListClients() {
        TypedQuery<Client> query = em.createQuery("SELECT c FROM Client c ", Client.class);
        return query.getResultList();
    }




    @Override
    public List<Employee> getListEmployees() {
        TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e ", Employee.class);
        return query.getResultList();
    }

    @Override
    public Personne SignIn(String email, String password) {
        System.out.println("login");
        try {
            password = Utils.toMD5(password);
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        if (isExistEmail(email)) {
            Personne user = loginEmail(email, password);
            if (user != null) {
                return user;
            } else {
                return null;
            }
        }
        return null;
    }

    /**************** verifier l'existance d'un email  *************************/
    public boolean isExistEmail(String email) {
        long result = (long) em.createQuery("SELECT count(u) from Personne u WHERE u.email = :email")
                .setParameter("email", email).getSingleResult();

        if (result == 0)
            return false;
        else
            return true;
    }



    /**************** verifier l'existance d'un utilisateur  *************************/
    public boolean isExistUsername(String username) {
        long result = (long) em.createQuery("SELECT count(u) from User u WHERE u.username = :username")
                .setParameter("username", username).getSingleResult();
        if (result == 0)
            return false;
        else
            return true;
    }

    /*************************************  find Personne By Email and Password *****************************/
    public Personne loginEmail(String email, String password) {
        try {
            Personne result = (Personne) em
                    .createQuery("SELECT u from Personne u WHERE u.email = :email and u.password = :password")
                    .setParameter("email", email).setParameter("password", password).getSingleResult();
            return result;
        } catch (NoResultException e) {
            return null;
        }
    }

    @Override
    public int updateClient(Client client) {
        return em.createQuery("update Client c SET c.nom = "+client.getNom()+"SET c.email ="+client.getEmail()+"SET c.telephone ="+client.getTelephone()+" WHERE c.id = "+client.getId()).executeUpdate();
    }

    @Override
    public int updateEmployee(Employee employee) {
        return em.createQuery("update Employee c SET c.nom = "+employee.getNom()+"SET c.email ="+employee.getEmail()+"SET c.telephone ="+employee.getTelephone()+" WHERE c.id = "+employee.getId()).executeUpdate();
    }

    @Override
    public int suspendClient(Client client) {
        return em.createQuery("update Client c SET c.is_suspended = false WHERE c.id = "+client.getId()).executeUpdate();
    }




}`

    @Path("/personne")
@RequestScoped
public class PersoneWS {

    @EJB
    PersonneServiceLocal personneManager;

    @Path("/client")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addClient(Client client){
        client.setDateInscription(new Date());
        System.out.println(client);
        return Response.ok(personneManager.addClient(client)).build();
    }

    @Path("/client")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getClient(){
        return Response.ok(personneManager.getListClients()).build();

    }


    @Path("/login")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response signIn(Personne user) {
        GenerateToken generateToken = new GenerateToken();
        JsonObjectBuilder jsonObject = Json.createObjectBuilder();
        if (user.getEmail() != null) {
            Personne personne = personneManager.SignIn(user.getEmail(), user.getPassword());
            if(personne != null){
                jsonObject.add("token", generateToken.issueToken(personne.getEmail()));
                jsonObject.add("username", personne.getNom());
                if(personne instanceof Client)
                    jsonObject.add("type", "client");
                if(personne instanceof Employee)
                    jsonObject.add("type", "employee");
                jsonObject.add("email", personne.getEmail());
                System.out.println(jsonObject);
                return Response.ok(jsonObject.build()).build();
            }

        }
        return Response.status(Status.UNAUTHORIZED).build();
    }

}



       @Local
    public interface PersonneServiceLocal {

        public Employee addPeronne(Employee employee);
        public Client addClient(Client client );
        public List<Client> getListClients();
        public List<Employee> getListEmployees();
        public Personne SignIn (String usernameOrEmail , String password);
        public int updateClient(Client client);
        public int updateEmployee(Employee employee);
        public int suspendClient(Client client);


    }

0 个答案:

没有答案