Search in sources :

Example 11 with InputValidationException

use of com.willshex.gson.web.service.server.InputValidationException in project blogwt by billy1380.

the class SessionValidator method lookup.

/**
 * @param session
 * @param name
 * @return
 * @throws InputValidationException
 */
public static Session lookup(Session session, String name) throws InputValidationException {
    if (session == null)
        throwServiceError(InputValidationException.class, ApiError.InvalidValueNull, TYPE + ": " + name);
    boolean isIdLookup = false;
    if (session.id != null) {
        isIdLookup = true;
    }
    if (!isIdLookup)
        throwServiceError(InputValidationException.class, ApiError.DataTypeNoLookup, TYPE + ": " + name);
    Session lookupSession = null;
    if (isIdLookup) {
        lookupSession = SessionServiceProvider.provide().getSession(session.id);
    }
    if (lookupSession == null)
        throwServiceError(InputValidationException.class, ApiError.DataTypeNotFound, TYPE + ": " + name);
    return lookupSession;
}
Also used : InputValidationException(com.willshex.gson.web.service.server.InputValidationException) Session(com.willshex.blogwt.shared.api.datatype.Session)

Example 12 with InputValidationException

use of com.willshex.gson.web.service.server.InputValidationException in project blogwt by billy1380.

the class UserValidator method lookup.

public static User lookup(User user, String name) throws InputValidationException {
    notNull(user, CLASS, name);
    boolean isIdLookup = false, isNameLookup = false;
    if (user.id != null) {
        isIdLookup = true;
    } else if (user.username != null) {
        isNameLookup = true;
    }
    if (!(isIdLookup || isNameLookup))
        throwServiceError(InputValidationException.class, ApiError.DataTypeNoLookup, TYPE + ": " + name);
    User lookupUser = null;
    if (isIdLookup) {
        lookupUser = UserServiceProvider.provide().getUser(user.id);
    } else if (isNameLookup) {
        lookupUser = UserServiceProvider.provide().getUsernameUser(user.username);
    }
    if (lookupUser == null)
        throwServiceError(InputValidationException.class, ApiError.DataTypeNotFound, TYPE + ": " + name);
    return lookupUser;
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) InputValidationException(com.willshex.gson.web.service.server.InputValidationException)

Example 13 with InputValidationException

use of com.willshex.gson.web.service.server.InputValidationException in project blogwt by billy1380.

the class GetPostActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(GetPostRequest input, GetPostResponse output) throws Exception {
    ApiValidator.request(input, GetPostRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    if (input.session != null) {
        try {
            output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
        } catch (InputValidationException ex) {
            output.session = input.session = null;
        }
    }
    Post post = PostValidator.lookup(input.post, "input.post");
    if (post != null) {
        output.post = PostValidator.viewable(post, output.session, "input.post");
        output.post.author = UserServiceProvider.provide().getUser(keyToId(output.post.authorKey));
        UserHelper.stripSensitive(output.post.author);
        output.post.content = PostServiceProvider.provide().getPostContent(output.post);
    }
}
Also used : Post(com.willshex.blogwt.shared.api.datatype.Post) InputValidationException(com.willshex.gson.web.service.server.InputValidationException)

Example 14 with InputValidationException

use of com.willshex.gson.web.service.server.InputValidationException in project blogwt by billy1380.

the class GetPostsActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(GetPostsRequest input, GetPostsResponse output) throws Exception {
    ApiValidator.request(input, GetPostsRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    Boolean showAll = Boolean.TRUE.equals(input.showAll) ? Boolean.TRUE : Boolean.FALSE;
    if (input.session != null) {
        try {
            output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
            List<Permission> permissions = new ArrayList<Permission>();
            Permission postPermission = PermissionServiceProvider.provide().getCodePermission(PermissionHelper.MANAGE_POSTS);
            permissions.add(postPermission);
            try {
                UserValidator.authorisation(input.session.user, permissions, "input.session.user");
            } catch (AuthorisationException aEx) {
                showAll = Boolean.FALSE;
            }
        } catch (InputValidationException ex) {
            output.session = input.session = null;
            showAll = Boolean.FALSE;
        }
    } else {
        showAll = Boolean.FALSE;
    }
    if (!showAll) {
        input.pager.sortBy = PostSortType.PostSortTypePublished.toString();
    }
    if (input.includePostContents == null) {
        input.includePostContents = Boolean.FALSE;
    }
    boolean postsForTag = false, postsForArchiveEntry = false, postsForQuery = false;
    if (input.tag != null && input.tag.length() > 0) {
        postsForTag = true;
        Tag tag = TagServiceProvider.provide().getSlugTag(input.tag);
        if (tag != null) {
            output.posts = PersistenceHelper.batchLookup(PostServiceProvider.provide(), tag.postKeys);
        }
    }
    if (!postsForTag && input.archiveEntry != null) {
        postsForTag = true;
        if (input.archiveEntry.posts != null) {
            output.posts = input.archiveEntry.posts = PostValidator.lookupAll(input.archiveEntry.posts, "input.archiveEntry.posts");
        } else {
            input.archiveEntry = ArchiveEntryValidator.lookup(input.archiveEntry, "input.archiveEntry");
            output.posts = PersistenceHelper.batchLookup(PostServiceProvider.provide(), input.archiveEntry.postKeys);
        }
    }
    if (!postsForTag && !postsForArchiveEntry && input.query != null) {
        postsForQuery = true;
        if (input.session != null && input.session.user != null) {
            output.posts = PostServiceProvider.provide().getUserViewablePartialSlugPosts(input.query, input.session.user, showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
        } else {
            output.posts = PostServiceProvider.provide().getPartialSlugPosts(input.query, showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.PostSortTypePublished, SortDirectionType.SortDirectionTypeDescending);
        }
    }
    if (!postsForTag && !postsForArchiveEntry && !postsForQuery) {
        output.posts = PostServiceProvider.provide().getPosts(showAll, input.includePostContents, input.pager.start, input.pager.count, PostSortType.fromString(input.pager.sortBy), input.pager.sortDirection);
    }
    if (output.posts != null) {
        Map<Key<User>, User> users = new HashMap<Key<User>, User>();
        for (Post post : output.posts) {
            if (users.get(post.authorKey) == null) {
                users.put(post.authorKey, UserHelper.stripSensitive(UserServiceProvider.provide().getUser(keyToId(post.authorKey))));
            }
            post.author = users.get(post.authorKey);
        }
    }
    output.pager = PagerHelper.moveForward(input.pager);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) HashMap(java.util.HashMap) Post(com.willshex.blogwt.shared.api.datatype.Post) ArrayList(java.util.ArrayList) Permission(com.willshex.blogwt.shared.api.datatype.Permission) InputValidationException(com.willshex.gson.web.service.server.InputValidationException) Tag(com.willshex.blogwt.shared.api.datatype.Tag) AuthorisationException(com.willshex.blogwt.server.api.exception.AuthorisationException) Key(com.googlecode.objectify.Key)

Example 15 with InputValidationException

use of com.willshex.gson.web.service.server.InputValidationException in project blogwt by billy1380.

the class ChangePasswordActionHandler method handle.

/* (non-Javadoc)
	 * 
	 * @see
	 * com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
	 * gson.web.service.shared.Request,
	 * com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(ChangePasswordRequest input, ChangePasswordResponse output) throws Exception {
    ApiValidator.request(input, ChangePasswordRequest.class);
    ApiValidator.accessCode(input.accessCode, "input.accessCode");
    ApiValidator.notNull(input.changedPassword, String.class, "input.changedPassword");
    if (input.session != null) {
        try {
            output.session = input.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
        } catch (InputValidationException ex) {
            output.session = input.session = null;
        }
    }
    // // if not the logged in user
    // if (!DataTypeHelper.<User> same(input.user, input.session.user)) {
    // List<Role> roles = new ArrayList<Role>();
    // roles.add(RoleHelper.createAdmin());
    // 
    // List<Permission> permissions = new ArrayList<Permission>();
    // Permission postPermission = PermissionServiceProvider.provide()
    // .getCodePermission(PermissionHelper.MANAGE_USERS);
    // permissions.add(postPermission);
    // 
    // UserValidator.authorisation(input.session.user, roles,
    // permissions, "input.session.user");
    // }
    boolean isExistingPassword = false, isActionCode = false;
    if (input.resetCode != null && input.resetCode.length() > 0) {
        isActionCode = true;
    }
    if (input.password != null && input.password.length() > 0) {
        isExistingPassword = true;
    }
    if (!(isActionCode || isExistingPassword))
        ApiValidator.throwServiceError(InputValidationException.class, ApiError.InvalidValueNull, "String: input.password or input.resetCode");
    User user = null;
    if (isActionCode) {
        input.resetCode = UserValidator.validateToken(input.resetCode, "input.resetCode");
        user = UserServiceProvider.provide().getActionCodeUser(input.resetCode);
        if (user == null)
            ApiValidator.throwServiceError(InputValidationException.class, ApiError.DataTypeNotFound, "String: input.resetToken");
        user.actionCode = null;
    }
    if (isExistingPassword && !isActionCode) {
        user = input.session.user;
        if (!UserServiceProvider.provide().verifyPassword(user, input.password))
            ApiValidator.throwServiceError(InputValidationException.class, ApiError.AuthenticationFailedBadPassword, "String: input.password");
    }
    user.password = UserServiceProvider.provide().generatePassword(input.changedPassword);
    UserServiceProvider.provide().updateUser(user);
}
Also used : User(com.willshex.blogwt.shared.api.datatype.User) InputValidationException(com.willshex.gson.web.service.server.InputValidationException)

Aggregations

InputValidationException (com.willshex.gson.web.service.server.InputValidationException)25 User (com.willshex.blogwt.shared.api.datatype.User)9 Post (com.willshex.blogwt.shared.api.datatype.Post)5 Permission (com.willshex.blogwt.shared.api.datatype.Permission)3 Property (com.willshex.blogwt.shared.api.datatype.Property)3 Relationship (com.willshex.blogwt.shared.api.datatype.Relationship)3 Key (com.googlecode.objectify.Key)2 Page (com.willshex.blogwt.shared.api.datatype.Page)2 Role (com.willshex.blogwt.shared.api.datatype.Role)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AuthorisationException (com.willshex.blogwt.server.api.exception.AuthorisationException)1 ArchiveEntry (com.willshex.blogwt.shared.api.datatype.ArchiveEntry)1 GeneratedDownload (com.willshex.blogwt.shared.api.datatype.GeneratedDownload)1 MetaNotification (com.willshex.blogwt.shared.api.datatype.MetaNotification)1 NotificationSetting (com.willshex.blogwt.shared.api.datatype.NotificationSetting)1 Rating (com.willshex.blogwt.shared.api.datatype.Rating)1 Resource (com.willshex.blogwt.shared.api.datatype.Resource)1 Session (com.willshex.blogwt.shared.api.datatype.Session)1 Tag (com.willshex.blogwt.shared.api.datatype.Tag)1