use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class AccessTokenFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String requestUri = request.getRequestURI();
String userAgent = request.getHeader("user-agent");
String accessTokenId = request.getParameter(RequestParameter.ACCESS_TOKEN.getName());
accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
AccessToken accessToken;
if (requestUri.equals("/remote_api") || (userAgent != null && userAgent.equals("Amazon CloudFront"))) {
accessToken = null;
} else if (autoGenerate) {
// Used by gamma, default & api modules.
String accessTokenCookie = getCookieValue(RequestCookie.ACCESS_TOKEN.getName(), request);
if ((accessTokenId == null || accessTokenId.isEmpty()) && (accessTokenCookie == null || accessTokenCookie.isEmpty())) {
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
} else {
accessToken = accessTokenId != null && !accessTokenId.isEmpty() ? dataAccessor.getAccessToken(accessTokenId) : dataAccessor.getAccessToken(accessTokenCookie);
if (accessToken == null || accessToken.isExpired()) {
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
} else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
}
}
if (!accessToken.getId().equals(accessTokenCookie)) {
accessTokenId = accessToken.getId();
setCookieValue(RequestCookie.ACCESS_TOKEN.getName(), accessTokenId, 30, response);
}
} else if (isWorker) {
// Used by worker module.
accessTokenId = dataAccessor.getAppProperty(AppProperty.WORKER_ACCESS_TOKEN_ID).getValue();
accessToken = dataAccessor.getAccessToken(accessTokenId);
} else if (requestUri.equals("/user/accesstoken")) {
// Used by gamma-android & android module.
accessToken = AccessTokenDataUtil.newUserAccessToken(request);
dispatchResponse(response, new UserAccessTokenApi.Response(accessToken.getId(), accessToken.getExpiry()));
return;
} else {
// TODO: Consider only header. Ignore accessToken from request param.
if (accessTokenId == null || accessTokenId.isEmpty()) {
accessTokenId = request.getHeader(RequestHeader.ACCESS_TOKEN.getName());
accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
}
if (accessTokenId == null || accessTokenId.isEmpty()) {
dispatchResponse(response, new InvalidArgumentException("Access Token is missing."));
return;
} else if ((accessToken = dataAccessor.getAccessToken(accessTokenId)) == null) {
dispatchResponse(response, new InvalidArgumentException("Access Token is invalid."));
return;
} else if (accessToken.isExpired()) {
dispatchResponse(response, new InsufficientAccessException("Access Token is expired."));
return;
} else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
}
}
threadLocalAccessToken.set(accessToken);
chain.doFilter(request, response);
threadLocalAccessToken.remove();
}
use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class UserDataUtil method sendPasswordResetMail.
public static void sendPasswordResetMail(String email, Language language) throws InvalidArgumentException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null)
throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_NOT_REGISTERED);
String verificationToken = _getNextToken(user.getVerificationToken());
if (!verificationToken.equals(user.getVerificationToken())) {
user.setVerificationToken(verificationToken);
user = dataAccessor.createOrUpdateUser(user);
}
Map<String, String> dataModel = new HashMap<>();
String passwordResetUrl = "/" + "?" + "email=" + user.getEmail() + "&" + "token=" + verificationToken.substring(0, verificationToken.indexOf("|")) + "&" + "passwordReset=" + Boolean.TRUE;
dataModel.put("passwordResetUrl", passwordResetUrl);
EmailUtil.sendMail(createUserData(user).getDisplayName(), user.getEmail(), "password-reset", language, dataModel);
}
use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class UserDataUtil method registerUser.
public static UserData registerUser(String firstName, String lastName, String email, String password, UserSignUpSource signUpSource) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() == UserState.DELETED) {
user = dataAccessor.newUser();
} else if (user.getState() != UserState.REFERRAL) {
JsonObject errorMessages = new JsonObject();
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
throw new InvalidArgumentException(errorMessages);
}
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
user.setPassword(PasswordUtil.getSaltedHash(password));
user.setEmail(email.toLowerCase());
user.setState(UserState.REGISTERED);
user.setSignUpDate(new Date());
user.setSignUpSource(signUpSource);
user = dataAccessor.createOrUpdateUser(user, auditLog);
UserData userData = createUserData(user);
userData.setFirstName(firstName);
userData.setLastName(lastName);
return userData;
}
use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class UserDataUtil method verifyUserEmail.
public static void verifyUserEmail(String email, String verificationToken) throws InvalidArgumentException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
if (user == null || user.getState() != UserState.REGISTERED)
return;
if (!_verifyToken(user, verificationToken))
throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
user.setState(UserState.ACTIVE);
// Note: Do not clear VerificationToken
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.
the class PratilipiDocUtil method _setPage.
private static void _setPage(Long pratilipiId, PratilipiContentDoc.Page page, String html) throws InvalidArgumentException, UnexpectedServerException {
// Deleting Existing Pagelets
page.deleteAllPagelets();
// Adding Pagelets from HTML
if (html != null && !html.trim().isEmpty()) {
Node body = Jsoup.parse(html).body();
Node badNode = _validateContent(body);
if (badNode != null) {
String errMsg = "";
while (badNode != body) {
errMsg = " > " + badNode.nodeName() + errMsg;
badNode = badNode.parent();
}
errMsg = "Invalid node " + errMsg;
throw new InvalidArgumentException(errMsg);
}
for (Node node : body.childNodes()) {
if (node.nodeName().equals("p")) {
if (node.childNodeSize() == 1 && node.childNode(0).equals("img")) {
JsonObject imgData = _createImageData(pratilipiId, node.childNode(0));
if (imgData != null)
page.addPagelet(PageletType.IMAGE, imgData);
} else {
page.addPagelet(PageletType.HTML, ((Element) node).html(), _getAlignment(node));
}
} else if (node.nodeName().equals("img")) {
JsonObject imgData = _createImageData(pratilipiId, node);
if (imgData != null)
page.addPagelet(PageletType.IMAGE, imgData);
} else if (node.nodeName().equals("blockquote")) {
page.addPagelet(PageletType.BLOCK_QUOTE, ((Element) node).html());
} else if (node.nodeName().equals("ol")) {
page.addPagelet(PageletType.LIST_ORDERED, ((Element) node).html());
} else if (node.nodeName().equals("ul")) {
page.addPagelet(PageletType.LIST_UNORDERED, ((Element) node).html());
}
}
}
}
Aggregations