示例#1
0
  @POST
  @Path("register")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public AuthenticationResponse register(UserRegistrationRequest request) {
    AuthenticationResponse response = new AuthenticationResponse();

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException ee) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/register", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    }

    if (StringUtils.isBlank(request.getEmail())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "email"));
      return response;
    } else if (StringUtils.isBlank(request.getPassword())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "password"));
      return response;
    } else if (StringUtils.isBlank(request.getFirstName())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "firstName"));
      return response;
    } else if (StringUtils.isBlank(request.getLastName())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "lastName"));
      return response;
    }

    if (!EmailValidator.getInstance().isValid(request.getEmail())) {
      response.setInvalidEmailJSONException(
          new InvalidEmailJSONException("user/register", request.getEmail()));
      return response;
    }

    User user = new User();
    user.setEmail(request.getEmail().toLowerCase());
    user.setFirstName(request.getFirstName());
    user.setLastName(request.getLastName());

    try {
      String sessionKey = userService.register(user, request.getPassword(), Platform.OTHER);
      response.setSessionKey(sessionKey);
      return response;
    } catch (EmailAlreadyInUseException e) {
      response.setRegisterEmailAlreadyInUseJSONException(
          new RegisterEmailAlreadyInUseJSONException("user/register", request.getEmail()));
      return response;
    } catch (PasswordLenghtInvalidException e) {
      response.setPasswordLengthInvalidJSONException(
          new PasswordLengthInvalidJSONException("user/register"));
      return response;
    }
  }
示例#2
0
  @POST
  @Path("login")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public AuthenticationResponse login(UserLoginRequest request) {
    AuthenticationResponse response = new AuthenticationResponse();

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException e1) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/login", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    }

    try {
      String sessionKey =
          userService.login(request.getEmail(), request.getPassword(), Platform.OTHER);
      response.setSessionKey(sessionKey);
      return response;
    } catch (UserNotFoundException e) {
      response.setEmailOrPasswordIncorrectJSONException(
          new EmailOrPasswordIncorrectJSONException("user/login"));
      return response;
    } catch (PasswordIncorrectException e) {
      response.setEmailOrPasswordIncorrectJSONException(
          new EmailOrPasswordIncorrectJSONException("user/login"));
      return response;
    }
  }