use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method updateUserPassword.
public static void updateUserPassword(String email, String verificationToken, String newPassword) throws InvalidArgumentException, InsufficientAccessException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUserByEmail(email.toLowerCase());
JsonObject errorMessages = new JsonObject();
if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
throw new InvalidArgumentException(errorMessages);
} else if (user.getState() == UserState.BLOCKED) {
throw new InvalidArgumentException(GenericRequest.ERR_ACCOUNT_BLOCKED);
}
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.setPassword(PasswordUtil.getSaltedHash(newPassword));
// Note: Do not clear VerificationToken
user = dataAccessor.createOrUpdateUser(user, auditLog);
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserAuthorDataUtil method getUserFollowList.
public static DataListCursorTuple<AuthorData> getUserFollowList(Long userId, String cursor, Integer offset, Integer resultCount) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUser(userId);
if (user.getFollowCount() == 0L)
return new DataListCursorTuple<>(new ArrayList<AuthorData>(0), null, 0L);
DataListCursorTuple<Long> authorIdListCursorTuple = dataAccessor.getUserAuthorFollowList(userId, null, cursor, offset, resultCount);
List<Long> authorIdList = authorIdListCursorTuple.getDataList();
List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, true);
// Setting AuthorData.isFollowing flag
if (userId.equals(AccessTokenFilter.getAccessToken().getUserId())) {
for (AuthorData authorData : authorDataList) authorData.setFollowing(true);
} else {
List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
if (userAuthorList.size() != 0)
for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
authorDataList.get(i).setFollowing(true);
}
// Setting UserData.isFollowing flag
Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
if (author != null) {
List<Long> userIdList = new ArrayList<>(authorDataList.size());
List<UserData> userDataList = new ArrayList<>(authorDataList.size());
for (AuthorData authorData : authorDataList) {
if (authorData.getUser().getId() != null) {
userIdList.add(authorData.getUser().getId());
userDataList.add(authorData.getUser());
}
}
List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, author.getId());
if (userAuthorList != null) {
for (UserAuthor userAuthor : userAuthorList) if (userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING)
userDataList.get(userIdList.indexOf(userAuthor.getUserId())).setFollowing(true);
}
}
return new DataListCursorTuple<>(authorDataList, authorIdListCursorTuple.getCursor(), user.getFollowCount());
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UxModeFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
if (isAndroidApp) {
threadLocalBasicMode.set(false);
threadLocalWebsite.set(null);
threadLocalRefererHost.set(null);
} else {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String hostName = request.getServerName();
String requestUri = request.getRequestURI();
String userAgent = request.getHeader("user-agent");
// Defaults - for all test environments
boolean basicMode = false;
Website website = null;
// Figuring out Mode and Languages from a pre-configured list
for (Website web : Website.values()) {
if (hostName.equals(web.getHostName())) {
basicMode = false;
website = web;
break;
} else if (hostName.equals(web.getMobileHostName())) {
basicMode = true;
website = web;
break;
}
}
// NOTE: DO NOT redirect Facebook Scraping requests
if (isWebApp && website == Website.ALL_LANGUAGE && (userAgent == null || userAgent.isEmpty() || !userAgent.startsWith("facebookexternalhit/1.1"))) {
String destHostName = null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Page page = dataAccessor.getPage(requestUri);
if (page == null) {
// Do Nothing
} else if (page.getType() == PageType.PRATILIPI) {
Pratilipi pratilipi = dataAccessor.getPratilipi(page.getPrimaryContentId());
for (Website web : Website.values()) {
if (web.getFilterLanguage() == pratilipi.getLanguage()) {
destHostName = basicMode ? web.getMobileHostName() : web.getHostName();
break;
}
}
} else if (page.getType() == PageType.AUTHOR) {
Author author = dataAccessor.getAuthor(page.getPrimaryContentId());
for (Website web : Website.values()) {
if (web.getFilterLanguage() == author.getLanguage()) {
destHostName = basicMode ? web.getMobileHostName() : web.getHostName();
break;
}
}
} else if (page.getType() == PageType.EVENT) {
Event event = dataAccessor.getEvent(page.getPrimaryContentId());
for (Website web : Website.values()) {
if (web.getFilterLanguage() == event.getLanguage()) {
destHostName = basicMode ? web.getMobileHostName() : web.getHostName();
break;
}
}
} else if (page.getType() == PageType.BLOG_POST) {
BlogPost blogPost = dataAccessor.getBlogPost(page.getPrimaryContentId());
for (Website web : Website.values()) {
if (web.getFilterLanguage() == blogPost.getLanguage()) {
destHostName = basicMode ? web.getMobileHostName() : web.getHostName();
break;
}
}
}
if (destHostName != null) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", (request.isSecure() ? "https://" : "http://") + destHostName + requestUri);
return;
}
}
// NOTE: DO NOT redirect Facebook Scraping requests
if (isWebApp && (userAgent == null || userAgent.isEmpty() || !userAgent.startsWith("facebookexternalhit/1.1"))) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Page page = dataAccessor.getPage(requestUri);
if (page != null && page.getUriAlias() != null && requestUri.equals(page.getUri())) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", page.getUriAlias());
return;
}
}
// Figuring out Browser capability
boolean basicBrowser = false;
boolean isCrawler = false;
if (isWebApp) {
if (userAgent == null || userAgent.trim().isEmpty()) {
basicBrowser = true;
} else if (userAgent.contains("UCBrowser")) {
// UCBrowser
/*
* UCBrowser on Android 4.3
* "Mozilla/5.0 (Linux; U; Android 4.3; en-US; GT-I9300 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/10.0.1.512 U3/0.8.0 Mobile Safari/533.1"
*/
// Extreme mode
basicBrowser = true;
// } else if( userAgent.contains( "Opera Mobi" ) ) { // Opera Classic
// /*
// * Opera Classic on Android 4.3
// * "Opera/9.80 (Android 4.3; Linux; Opera Mobi/ADR-1411061201) Presto/2.11.355 Version/12.10"
// */
// basicBrowser = true; // Not sure whether Polymer 1.0 is supported or not
} else if (userAgent.contains("Opera Mini")) {
// Opera Mini
/*
* Opera Mini on Android 4.3
* "Opera/9.80 (Android; Opera Mini/7.6.40077/35.5706; U; en) Presto/2.8.119 Version/11.10"
*/
// Extreme mode
basicBrowser = true;
// } else if( userAgent.contains( "Trident/7" ) && userAgent.contains( "rv:11" ) ) { // Microsoft Internet Explorer 11
// /*
// * Microsoft Internet Explorer 11 on Microsoft Windows 8.1
// * "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; LCJB; rv:11.0) like Gecko"
// */
// basicBrowser = true;
//
// } else if( userAgent.contains( "OPR" ) ) { // Opera
// /*
// * Opera on Microsoft Windows 8.1
// * "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 OPR/26.0.1656.24"
// * Opera on Android 4.3
// * "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.102 Mobile Safari/537.36 OPR/25.0.1619.84037"
// */
// String userAgentSubStr = userAgent.substring( userAgent.indexOf( "OPR" ) + 4 );
// int version = Integer.parseInt( userAgentSubStr.substring( 0, userAgentSubStr.indexOf( "." ) ) );
// basicBrowser = version < 20;
//
// } else if( userAgent.contains( "Edge" ) ) {
// /*
// * Microsoft Edge browser on Windows 10
// * Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393
// */
// basicBrowser = false;
//
// } else if( userAgent.contains( "Chrome" ) && ! userAgent.contains( "(Chrome)" ) ) { // Google Chrome
// /*
// * Google Chrome on Microsoft Windows 8.1
// * "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36"
// * Google Chrome on Android 4.3
// * "Mozilla/5.0 (Linux; Android 4.3; GT-I9300 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.59 Mobile Safari/537.36"
// */
// String userAgentSubStr = userAgent.substring( userAgent.indexOf( "Chrome" ) + 7 );
// int version = Integer.parseInt( userAgentSubStr.substring( 0, userAgentSubStr.indexOf( "." ) ) );
// basicBrowser = version < 35;
//
// } else if( userAgent.contains( "Safari" ) ) { // Apple Safari
// /*
// * Apple Safari on Microsoft Windows 8.1
// * Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
// */
// if( userAgent.contains( "Version" ) ) {
// String userAgentSubStr = userAgent.substring( userAgent.indexOf( "Version" ) + 8 );
// int version = Integer.parseInt( userAgentSubStr.substring( 0, userAgentSubStr.indexOf( "." ) ) );
// basicBrowser = version < 8;
// } else {
// String userAgentSubStr = userAgent.substring( userAgent.indexOf( "Safari" ) + 7 );
// int version = Integer.parseInt( userAgentSubStr.substring( 0, userAgentSubStr.indexOf( "." ) ) );
// basicBrowser = version < 538 || version > 620;
// }
//
// } else if( userAgent.contains( "Firefox" ) ) { // Mozilla Firefox
// /*
// * Mozilla Firefox on Microsoft 8.1
// * "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0 AlexaToolbar/alxf-2.21"
// * Mozilla Firefox on Android 4.3
// * "Mozilla/5.0 (Android; Mobile; rv:33.0) Gecko/33.0 Firefox/33.0"
// * Mozilla Firefox on Linux
// * "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0 (Chrome)"
// */
// String userAgentSubStr = userAgent.substring( userAgent.indexOf( "Firefox" ) + 8 );
// int version = Integer.parseInt( userAgentSubStr.substring( 0, userAgentSubStr.indexOf( "." ) ) );
// basicBrowser = version < 28;
} else if (userAgent.contains("Googlebot")) {
// Googlebot/2.1; || Googlebot-News || Googlebot-Image/1.0 || Googlebot-Video/1.0
isCrawler = true;
} else if (userAgent.equals("Google (+https://developers.google.com/+/web/snippet/)")) {
// Google+
isCrawler = true;
} else if (userAgent.contains("Bingbot")) {
// Microsoft Bing
isCrawler = true;
} else if (userAgent.contains("Slurp")) {
// Yahoo
isCrawler = true;
} else if (userAgent.contains("DuckDuckBot")) {
// DuckDuckGo
isCrawler = true;
} else if (userAgent.contains("Baiduspider")) {
// Baidu - China
isCrawler = true;
} else if (userAgent.contains("YandexBot")) {
// Yandex - Russia
isCrawler = true;
} else if (userAgent.contains("Exabot")) {
// ExaLead - France
isCrawler = true;
} else if (userAgent.equals("facebot") || userAgent.startsWith("facebookexternalhit/1.0") || userAgent.startsWith("facebookexternalhit/1.1")) {
// Facebook Scraping requests
isCrawler = true;
} else if (userAgent.startsWith("WhatsApp")) {
// Whatsapp
isCrawler = true;
} else if (userAgent.startsWith("ia_archiver")) {
// Alexa Crawler
isCrawler = true;
}
}
// Redirecting requests coming from basic browsers to BasicMode
if (basicBrowser && !basicMode && website != null && website.getMobileHostName() != null) {
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
String queryString = request.getQueryString();
if (queryString == null || queryString.isEmpty())
response.setHeader("Location", (request.isSecure() ? "https://" : "http://") + website.getMobileHostName() + requestUri);
else
response.setHeader("Location", (request.isSecure() ? "https://" : "http://") + website.getMobileHostName() + requestUri + "?" + request.getQueryString());
return;
}
threadLocalBasicMode.set(basicMode);
threadLocalWebsite.set(website);
threadLocalIsCrawler.set(isCrawler);
String referer = request.getHeader("referer");
if (// Eg: referer = android-app
referer == null || !referer.startsWith("http"))
threadLocalRefererHost.set(hostName);
else
threadLocalRefererHost.set(new URL(referer).getHost());
}
chain.doFilter(req, resp);
threadLocalBasicMode.remove();
threadLocalWebsite.remove();
threadLocalIsCrawler.remove();
threadLocalRefererHost.remove();
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class PratilipiSite method createDataModelForFollowingPage.
public Map<String, Object> createDataModelForFollowingPage(Long userId, Integer currPage, Language language, Boolean basicMode) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Long authorId = dataAccessor.getAuthorByUserId(userId).getId();
AuthorApi.GetRequest authorApiGetRequest = new AuthorApi.GetRequest();
authorApiGetRequest.setAuthorId(authorId);
AuthorApi.Response authorResponse = ApiRegistry.getApi(AuthorApi.class).get(authorApiGetRequest);
Integer resultCount = 10;
UserAuthorFollowListApi.GetRequest followingListRequest = new UserAuthorFollowListApi.GetRequest();
followingListRequest.setUserId(userId);
followingListRequest.setResultCount(resultCount);
followingListRequest.setOffset((currPage - 1) * resultCount);
UserAuthorFollowListApi.Response followingList = ApiRegistry.getApi(UserAuthorFollowListApi.class).get(followingListRequest);
Map<String, Object> dataModel = new HashMap<String, Object>();
Gson gson = new Gson();
dataModel.put("title", SEOTitleUtil.getFollowersPageTitle(userId, language));
if (basicMode) {
dataModel.put("author", authorResponse);
dataModel.put("followingList", followingList);
dataModel.put("currPage", currPage);
dataModel.put("maxPage", followingList.getNumberFound() % resultCount == 0 ? followingList.getNumberFound() / resultCount : followingList.getNumberFound() / resultCount + 1);
} else {
dataModel.put("authorJson", gson.toJson(authorResponse));
dataModel.put("followingObjectJson", gson.toJson(followingList));
}
return dataModel;
}
use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.
the class UserDataUtil method sendWelcomeMail.
public static void sendWelcomeMail(Long userId, Language language) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUser(userId);
EmailUtil.sendMail(createUserData(user).getDisplayName(), user.getEmail(), "welcome", language);
}
Aggregations