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;
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations