コード例 #1
0
  /*
   * curl --data "name=Test&lastname=Tester&[email protected]&password=test" http://localhost:8080/prostalytics/rest/auth/register
   *
   */
  @Path("/register")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.TEXT_HTML)
  public Response register(
      @FormParam("email") String email,
      @FormParam("name") String name,
      @FormParam("lastname") String lastName,
      @FormParam("password") String password)
      throws URISyntaxException {

    try {
      User user = new User();
      user.setName(name);
      user.setLastname(lastName);
      user.setEmail(email);
      user.setPassword(hashPassword(password));
      validate(user);

      userDao.saveUser(user);

      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromAuthRegister(Navigation.OK)))
              .build();
      return Response.seeOther(uri).build();
    } catch (Throwable e) {
      e.printStackTrace(); // TODO :)
      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromAuthRegister(Navigation.ERROR, e)))
              .build();
      return Response.seeOther(uri).build();
    }
  }
コード例 #2
0
  /*
   * curl --data '{"name":"Test","lastname":"Tester","email":"*****@*****.**","password":"******"}' http://localhost:8080/prostalytics/rest/auth/register --header "Content-Type:application/json"
   *
   */
  @Path("/register")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response register(User user) throws URISyntaxException {

    try {
      user.setPassword(hashPassword(user.getPassword()));
      validate(user);

      long id = userDao.saveUser(user);

      user.setId(id);
      user.setPassword(null); // we don't send around password
      return Response.status(Response.Status.OK).entity(user).build();
    } catch (Throwable e) {
      e.printStackTrace(); // TODO :)
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()).build();
    }
  }