use of com.pratilipi.data.type.BlogPost in project pratilipi by Pratilipi.
the class BlogPostDataUtil method createBlogPostDataList.
public static List<BlogPostData> createBlogPostDataList(List<BlogPost> blogPostList) {
List<Long> blogIdList = new ArrayList<>();
for (BlogPost blogPost : blogPostList) blogIdList.add(blogPost.getId());
Map<Long, Page> blogPostPages = DataAccessorFactory.getDataAccessor().getPages(PageType.BLOG_POST, blogIdList);
List<BlogPostData> blogPostDataList = new ArrayList<>();
for (BlogPost blogPost : blogPostList) blogPostDataList.add(createBlogPostData(blogPost, blogPostPages.get(blogPost.getId())));
return blogPostDataList;
}
use of com.pratilipi.data.type.BlogPost in project pratilipi by Pratilipi.
the class PageDataUtil method _getSitemapForTypePage.
private static String _getSitemapForTypePage(Long cursor, String hostName, Language language) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
List<Page> pageList = dataAccessor.getPageList(cursor, cursor + SITEMAP_PAGE_COUNT);
Map<Long, String> pratilipiPageMap = new HashMap<>();
Map<Long, String> authorPageMap = new HashMap<>();
Map<Long, String> blogPageMap = new HashMap<>();
Map<Long, String> blogPostPageMap = new HashMap<>();
Map<Long, String> eventPageMap = new HashMap<>();
for (Page page : pageList) {
String pageUri = page.getUriAlias() != null ? page.getUriAlias() : page.getUri();
switch(page.getType()) {
case PRATILIPI:
pratilipiPageMap.put(page.getPrimaryContentId(), pageUri);
break;
case AUTHOR:
authorPageMap.put(page.getPrimaryContentId(), pageUri);
break;
case BLOG:
blogPageMap.put(page.getPrimaryContentId(), pageUri);
break;
case BLOG_POST:
blogPostPageMap.put(page.getPrimaryContentId(), pageUri);
break;
case EVENT:
eventPageMap.put(page.getPrimaryContentId(), pageUri);
break;
default:
break;
}
}
Map<Long, Pratilipi> pratilipis = dataAccessor.getPratilipis(pratilipiPageMap.keySet());
Map<Long, Author> authors = dataAccessor.getAuthors(authorPageMap.keySet());
Map<Long, BlogPost> blogPosts = dataAccessor.getBlogPosts(blogPostPageMap.keySet());
Map<Long, Event> events = dataAccessor.getEvents(eventPageMap.keySet());
StringBuilder sitemap = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR);
sitemap.append("<urlset xmlns=\"" + SITEMAP_NAMESPACE + "\">" + LINE_SEPARATOR);
// For a few cases when sitemap is empty, add an entry to avoid error on crawler
if (pratilipiPageMap.isEmpty() && authorPageMap.isEmpty() && blogPageMap.isEmpty() && blogPostPageMap.isEmpty() && eventPageMap.isEmpty()) {
sitemap.append(_getSitemapEntry(hostName, "/", null, "hourly", "0.9"));
}
for (Long pratilipiId : pratilipiPageMap.keySet()) {
Pratilipi pratilipi = pratilipis.get(pratilipiId);
if (pratilipi.getLanguage() != language)
continue;
if (pratilipi.getState() != PratilipiState.PUBLISHED)
continue;
sitemap.append(_getSitemapEntry(hostName, pratilipiPageMap.get(pratilipiId), pratilipi.getLastUpdated(), "daily", "0.7"));
sitemap.append(_getSitemapEntry(hostName, "/read?" + RequestParameter.CONTENT_ID.getName() + "=" + pratilipiId, pratilipi.getLastUpdated(), "daily", "0.7"));
}
for (Long authorId : authorPageMap.keySet()) {
Author author = authors.get(authorId);
if (author.getLanguage() != language)
continue;
if (author.getState() != AuthorState.ACTIVE)
continue;
sitemap.append(_getSitemapEntry(hostName, authorPageMap.get(authorId), author.getLastUpdated(), "daily", "0.6"));
}
for (Long blogId : blogPageMap.keySet()) sitemap.append(_getSitemapEntry(hostName, blogPageMap.get(blogId), null, "weekly", null));
for (Long blogPostId : blogPostPageMap.keySet()) {
BlogPost blogPost = blogPosts.get(blogPostId);
if (blogPost.getLanguage() != language)
continue;
if (blogPost.getState() != BlogPostState.PUBLISHED)
continue;
sitemap.append(_getSitemapEntry(hostName, blogPostPageMap.get(blogPostId), blogPost.getLastUpdated(), "weekly", "0.6"));
}
for (Long eventId : eventPageMap.keySet()) {
Event event = events.get(eventId);
if (event.getLanguage() != language)
continue;
sitemap.append(_getSitemapEntry(hostName, eventPageMap.get(eventId), event.getLastUpdated(), "weekly", "0.6"));
}
sitemap.append("</urlset>");
return sitemap.toString();
}
use of com.pratilipi.data.type.BlogPost in project pratilipi by Pratilipi.
the class BlogPostApi method get.
@Get
public Response get(GetRequest request) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlogPost blogPost = dataAccessor.getBlogPost(request.blogPostId);
BlogPostData blogPostData = BlogPostDataUtil.createBlogPostData(blogPost);
return new Response(blogPostData);
}
use of com.pratilipi.data.type.BlogPost in project pratilipi by Pratilipi.
the class BlogPostDataUtil method saveBlogPostData.
public static BlogPostData saveBlogPostData(BlogPostData blogPostData) throws InvalidArgumentException, InsufficientAccessException {
_validateBlogPostDataForSave(blogPostData);
boolean isNew = blogPostData.getId() == null;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlogPost blogPost = isNew ? dataAccessor.newBlogPost() : dataAccessor.getBlogPost(blogPostData.getId());
if (isNew && !hasAccessToAddBlogPostData(blogPostData))
throw new InsufficientAccessException();
if (!isNew && !hasAccessToUpdateBlogPostData(blogPost, blogPostData))
throw new InsufficientAccessException();
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.BLOG_POST_ADD : AccessType.BLOG_POST_UPDATE, blogPost);
if (// Changing blog id is not allowed
isNew && blogPostData.hasBlogId())
blogPost.setBlogId(blogPostData.getBlogId());
if (blogPostData.hasTitle())
blogPost.setTitle(blogPostData.getTitle());
if (blogPostData.hasTitleEn())
blogPost.setTitleEn(blogPostData.getTitleEn());
if (blogPostData.hasContent())
blogPost.setContent(blogPostData.getContent());
if (blogPostData.hasLanguage())
blogPost.setLanguage(blogPostData.getLanguage());
if (blogPostData.hasState())
blogPost.setState(blogPostData.getState());
if (isNew) {
blogPost.setCreatedBy(AccessTokenFilter.getAccessToken().getUserId());
blogPost.setCreationDate(new Date());
} else if (blogPost.getCreatedBy() == null) {
blogPost.setCreatedBy(AccessTokenFilter.getAccessToken().getUserId());
}
blogPost.setLastUpdated(new Date());
blogPost = dataAccessor.createOrUpdateBlogPost(blogPost, auditLog);
_updateBlogPostPageUrl(blogPost);
return createBlogPostData(blogPost);
}
use of com.pratilipi.data.type.BlogPost 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);
threadLocalClientType.set(ClientType.ANDROID);
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if (request.getHeader(RequestHeader.ANDROID_VERSION_NAME.getName()) != null && !request.getHeader(RequestHeader.ANDROID_VERSION_NAME.getName()).trim().isEmpty())
threadLocalClientVersion.set(request.getHeader(RequestHeader.ANDROID_VERSION_NAME.getName()));
else if (// Backward Compatibility
request.getHeader(RequestHeader.ANDROID_VERSION.getName()) != null && !request.getHeader(RequestHeader.ANDROID_VERSION.getName()).trim().isEmpty())
threadLocalClientVersion.set(request.getHeader(RequestHeader.ANDROID_VERSION.getName()));
else if (request.getHeader(RequestHeader.PAG_CLIENT_VERSION.getName()) != null && !request.getHeader(RequestHeader.PAG_CLIENT_VERSION.getName()).trim().isEmpty())
threadLocalClientVersion.set(request.getHeader(RequestHeader.PAG_CLIENT_VERSION.getName()));
} else {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// ECS-HostName -> From PAG
// Client-Version -> From User service through PAG
boolean requestFromEcs = request.getHeader("ECS-HostName") != null || request.getHeader("Client-Version") != null;
String ECSHostName = requestFromEcs && request.getHeader("ECS-HostName") != null ? request.getHeader("ECS-HostName") : request.getHeader("Client-Version");
String hostName = requestFromEcs ? ECSHostName : 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;
}
}
// Forcing to https://
if (!requestFromEcs && website != null && website != Website.ALPHA && !request.isSecure()) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocation = "https://" + (basicMode ? website.getMobileHostName() : website.getHostName()) + requestUri;
if (request.getQueryString() != null && !request.getQueryString().isEmpty())
newLocation += "?" + request.getQueryString();
response.setHeader("Location", newLocation);
return;
}
// NOTE: DO NOT redirect Facebook Scraping requests
if (!requestFromEcs && 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 && !requestFromEcs) {
if (userAgent == null || userAgent.trim().isEmpty()) {
basicBrowser = true;
} else if (userAgent.contains("UCBrowser") || userAgent.contains("UCWEB")) {
// 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"
*/
// Not sure whether Polymer 1.0 is supported or not
basicBrowser = true;
} 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;
basicBrowser = false;
} 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;
// }
basicBrowser = false;
} 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;
basicBrowser = false;
} 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;
} else {
basicBrowser = true;
logger.log(Level.INFO, "UNKNOWN_USER_AGENT: " + userAgent);
}
}
// 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(requestFromEcs ? ECSHostName : new URL(referer).getHost());
// Refer api-web.xml and worker-web.xml
// Requests coming from api module
threadLocalClientType.set(ClientType.WEB);
if (website != null)
threadLocalClientVersion.set(basicMode ? website.getMobileHostName() : website.getHostName());
}
chain.doFilter(req, resp);
threadLocalBasicMode.remove();
threadLocalWebsite.remove();
threadLocalIsCrawler.remove();
threadLocalRefererHost.remove();
threadLocalClientType.remove();
threadLocalClientVersion.remove();
}
Aggregations