Search in sources :

Example 11 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserProcessApi method post.

@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, UnexpectedServerException {
    if (request.validateData != null && request.validateData) {
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        User user = dataAccessor.getUser(request.userId);
        // DELETED User entity can not have a non-DELETED Author entity linked.
        if (user.getState() == UserState.DELETED) {
            Author author = dataAccessor.getAuthorByUserId(user.getId());
            if (author != null && author.getState() != AuthorState.DELETED)
                throw new InvalidArgumentException("DELETED User entity has a non-DELETED Author entity linked.");
            // TODO: DELETED User entity can not have non-DELETED UserAuthor entities.
            return new GenericResponse();
        }
        // Either of two - email and facebook id - must be present.
        if (user.getEmail() == null && user.getFacebookId() == null)
            throw new InvalidArgumentException("Neither email nor facebook id is set.");
        // Email, if present, must not be empty.
        if (user.getEmail() != null && user.getEmail().trim().isEmpty())
            throw new InvalidArgumentException("Email is empty.");
        // Facebook id, if present, must not be empty.
        if (user.getFacebookId() != null && user.getFacebookId().trim().isEmpty())
            throw new InvalidArgumentException("Facebook Id is empty.");
        // Email, if present, must be trimmed and converted to lower case.
        if (user.getEmail() != null && !user.getEmail().equals(user.getEmail().trim().toLowerCase()))
            throw new InvalidArgumentException("Email is either not trimmed or not converted to lower case.");
        // Only one non-DELETED User entity can exist per email id.
        if (user.getEmail() != null) {
            Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("EMAIL", user.getEmail()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
            List<UserEntity> list = query.list();
            if (list.size() != 1)
                throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for email " + user.getEmail() + " .");
        }
        // Only one non-DELETED User entity can exist per facebook id.
        if (user.getFacebookId() != null) {
            Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("FACEBOOK_ID", user.getFacebookId()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
            List<UserEntity> list = query.list();
            if (list.size() != 1)
                throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for Facebook Id " + user.getFacebookId() + " .");
        }
        // Author profile for the user.
        Query<AuthorEntity> query = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).filter("STATE !=", AuthorState.DELETED).order("STATE").order("REGISTRATION_DATE");
        List<AuthorEntity> authorList = query.list();
        if (authorList.size() == 0) {
            if (user.getState() != UserState.REFERRAL || user.getSignUpSource() != UserSignUpSource.PRE_LAUNCH_WEBSITE)
                throw new InvalidArgumentException("Could not find an Author entity linked.");
        } else if (authorList.size() == 1) {
        // Do Nothing.
        } else {
            throw new InvalidArgumentException("User has " + authorList.size() + " non-DELETED Author entities linked.");
        }
    }
    if (request.updateFollowsDoc != null && request.updateFollowsDoc) {
        UserDocUtil.updateUserFollows(request.userId);
    }
    if (request.updateUserAuthorStats != null && request.updateUserAuthorStats) {
        UserDataUtil.updateUserAuthorStats(request.userId);
    }
    return new GenericResponse();
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) UserEntity(com.pratilipi.data.type.gae.UserEntity) AuthorEntity(com.pratilipi.data.type.gae.AuthorEntity) Post(com.pratilipi.api.annotation.Post)

Example 12 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserEmailApi method post.

@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    JsonObject errorMessages = new JsonObject();
    if ((request.sendWelcomeMail() || request.sendBirthdayMail()) && request.getUserId() == null)
        errorMessages.addProperty("userId", GenericRequest.ERR_USER_ID_REQUIRED);
    if (request.sendEmailVerificationMail() && request.getUserId() == null && (request.getEmail() == null || request.getEmail().trim().isEmpty()))
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    if (request.sendPasswordResetMail() && (request.getEmail() == null || request.getEmail().trim().isEmpty()))
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
    Language language = request.getLanguage() == null ? // User facing requests
    UxModeFilter.getDisplayLanguage() : // Offline requests
    request.getLanguage();
    if (request.sendWelcomeMail())
        UserDataUtil.sendWelcomeMail(request.getUserId(), language);
    if (request.sendEmailVerificationMail()) {
        if (request.getUserId() != null)
            UserDataUtil.sendEmailVerificationMail(request.getUserId(), language);
        else
            UserDataUtil.sendEmailVerificationMail(request.getEmail(), language);
    }
    if (request.sendPasswordResetMail())
        UserDataUtil.sendPasswordResetMail(request.getEmail(), language);
    if (request.sendBirthdayMail())
        throw new InvalidArgumentException("Feature not yet supported !");
    return new GenericResponse();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) Language(com.pratilipi.common.type.Language) GenericResponse(com.pratilipi.api.shared.GenericResponse) JsonObject(com.google.gson.JsonObject) Post(com.pratilipi.api.annotation.Post)

Example 13 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserPasswordUpdateApi method post.

@Post
public UserV1Api.Response post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException {
    UserData userData;
    if (request.getEmail() != null && request.getVerificationToken() != null) {
        // Verifying email
        UserDataUtil.verifyUserEmail(request.getEmail(), request.getVerificationToken());
        // Updating password
        UserDataUtil.updateUserPassword(request.getEmail(), request.getVerificationToken(), request.getNewPassword());
        // Logging-in the user
        userData = UserDataUtil.loginUser(request.getEmail(), request.getVerificationToken());
    } else if (request.getPassword() != null) {
        // Updating password
        UserDataUtil.updateUserPassword(request.getPassword(), request.getNewPassword());
        userData = UserDataUtil.getCurrentUser();
    } else {
        throw new InvalidArgumentException(GenericRequest.ERR_INSUFFICIENT_ARGS);
    }
    return new UserV1Api.Response(userData, UserPasswordUpdateApi.class);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UserData(com.pratilipi.data.client.UserData) Post(com.pratilipi.api.annotation.Post)

Example 14 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class AccessTokenFilter method dispatchResponse.

// Ref: GenericApi.dispatchApiResponse
private void dispatchResponse(HttpServletResponse response, Throwable ex) throws IOException {
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    if (ex instanceof InvalidArgumentException)
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    else if (ex instanceof InsufficientAccessException)
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    else if (ex instanceof UnexpectedServerException)
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    else
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    writer.println(ex.getMessage());
    writer.close();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PrintWriter(java.io.PrintWriter)

Example 15 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserDataUtil method sendEmailVerificationMail.

public static void sendEmailVerificationMail(String emailId, Language language) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    Long userId = AccessTokenFilter.getAccessToken().getUserId();
    if (userId.equals(0L))
        throw new InsufficientAccessException();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(emailId.trim().toLowerCase());
    if (user == null || !user.getId().equals(userId))
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_NOT_REGISTERED);
    _sendEmailVerificationMail(user, language);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)37 JsonObject (com.google.gson.JsonObject)21 DataAccessor (com.pratilipi.data.DataAccessor)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)11 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)9 User (com.pratilipi.data.type.User)9 Gson (com.google.gson.Gson)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)7 AuditLog (com.pratilipi.data.type.AuditLog)7 Author (com.pratilipi.data.type.Author)6 JsonElement (com.google.gson.JsonElement)5 Post (com.pratilipi.api.annotation.Post)5 UserData (com.pratilipi.data.client.UserData)5 Page (com.pratilipi.data.type.Page)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Language (com.pratilipi.common.type.Language)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 PrintWriter (java.io.PrintWriter)3