use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiSiteFilter method doFilter.
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String host = request.getServerName();
String requestUri = request.getRequestURI();
String userAgent = request.getHeader("user-agent");
if (requestUri.equals("/_ah/warmup") || requestUri.equals("/_ah/start") || requestUri.equals("/_ah/stop")) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else if (userAgent != null && (userAgent.startsWith("libwww-perl") || userAgent.startsWith("WhatsApp/"))) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else if (validHostSubdomainRegEx.matcher(host).matches()) {
// Redirecting to <lang>.pratilipi.com
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String queryString = request.getQueryString();
if (queryString == null || queryString.isEmpty())
response.setHeader("Location", (request.isSecure() ? "https:" : "http:") + "//" + host.substring(4) + requestUri);
else
response.setHeader("Location", (request.isSecure() ? "https:" : "http:") + "//" + host.substring(4) + requestUri + "?" + request.getQueryString());
} else if (!validHostRegEx.matcher(host).matches()) {
// Redirecting to www.pratilipi.com
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String queryString = request.getQueryString();
if (queryString == null || queryString.isEmpty())
response.setHeader("Location", (request.isSecure() ? "https:" : "http:") + "//www.pratilipi.com" + requestUri + "?redirect=gcp");
else
response.setHeader("Location", (request.isSecure() ? "https:" : "http:") + "//www.pratilipi.com" + requestUri + "?" + request.getQueryString() + "&redirect=gcp");
} else if (requestUri.length() > 1 && requestUri.endsWith("/")) {
// Removing trailing "/"
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", requestUri.substring(0, requestUri.length() - 1));
} else if (redirections.get(requestUri) != null) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", redirections.get(requestUri));
} else if ("/api.pratilipi/pratilipi/resource".equals(requestUri)) {
// Redirecting to new Pratilipi content image url
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String queryString = request.getQueryString();
if (queryString == null || queryString.isEmpty())
response.setHeader("Location", "/api/pratilipi/content/image");
else
response.setHeader("Location", "/api/pratilipi/content/image" + "?" + request.getQueryString());
} else if (oldPratilipiCoverUrlRegEx.matcher(requestUri).matches()) {
// Redirecting to new Pratilipi cover url
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String pratilipiIdStr = requestUri.substring(requestUri.lastIndexOf('/') + 1);
Long pratilipiId = Long.parseLong(pratilipiIdStr);
Pratilipi pratilipi = DataAccessorFactory.getDataAccessor().getPratilipi(pratilipiId);
response.setHeader("Location", PratilipiDataUtil.createPratilipiCoverUrl(pratilipi, 150));
} else if (oldAuthorImageUrlRegEx.matcher(requestUri).matches()) {
// Redirecting to new Author image url
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String authorIdStr = requestUri.substring(requestUri.lastIndexOf('/') + 1);
Long authorId = Long.parseLong(authorIdStr);
Author author = DataAccessorFactory.getDataAccessor().getAuthor(authorId);
response.setHeader("Location", AuthorDataUtil.createAuthorProfileImageUrl(author, 150));
} else if (oldPratilipiReaderUrlRegEx.matcher(requestUri).matches()) {
// Redirecting to new Pratilipi reader url
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", requestUri.replaceFirst("/(book|poem|story|article|pratilipi)/", "?id="));
} else {
DataAccessorFactory.getL1CacheAccessor().flush();
chain.doFilter(request, response);
}
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class UserPratilipiDataUtil method hasAccessToUpdateUserPratilipiData.
public static boolean hasAccessToUpdateUserPratilipiData(UserPratilipi userPratilipi, AccessType accessType) {
AccessToken accessToken = AccessTokenFilter.getAccessToken();
if (!userPratilipi.getUserId().equals(accessToken.getUserId()))
return false;
if (!UserAccessUtil.hasUserAccess(accessToken.getUserId(), null, accessType))
return false;
// Review can not be created for content pieces created by the user
if (accessType == AccessType.USER_PRATILIPI_REVIEW) {
if (userPratilipi.getReviewState() == UserReviewState.BLOCKED)
return false;
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(userPratilipi.getPratilipiId());
Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
if (author != null && userPratilipi.getUserId().equals(author.getUserId()))
return false;
}
return true;
}
use of com.pratilipi.data.type.Pratilipi 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.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiDocUtil method deleteContentChapter.
public static JsonArray deleteContentChapter(Long pratilipiId, Integer chapterNo) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
return new JsonArray();
pcDoc.deleteChapter(chapterNo);
docAccessor.save(pratilipiId, pcDoc);
return pcDoc.getIndex();
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updateMeta.
public static boolean updateMeta(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
PratilipiMetaDoc pmDoc = docAccessor.getPratilipiMetaDoc(pratilipiId);
if (pmDoc == null)
pmDoc = docAccessor.newPratilipiMetaDoc();
int wordCount = 0;
int imageCount = 0;
int chapterCount = 0;
Map<String, Integer> wordCounts = new HashMap<>();
for (PratilipiContentDoc.Chapter chapter : pcDoc.getChapterList()) {
String content = chapter.getTitle() == null ? "" : chapter.getTitle();
for (PratilipiContentDoc.Page page : chapter.getPageList()) {
for (PratilipiContentDoc.Pagelet pagelet : page.getPageletList()) {
if (pagelet.getType() == PratilipiContentDoc.PageletType.IMAGE)
imageCount++;
else
content += " " + pagelet.getDataAsString();
}
}
content = content.trim();
if (content.length() > 1) {
String[] words = content.replaceAll(nonKeywordsPattern, " ").split("[\\s]+");
for (String word : words) {
Integer count = wordCounts.get(word);
count = count == null ? 1 : count++;
wordCounts.put(word, count);
}
wordCount += words.length;
}
chapterCount++;
}
boolean isChanged = wordCounts.size() != pmDoc.getWordCounts().size();
if (!isChanged) {
for (Entry<String, Integer> entry : pmDoc.getWordCounts().entrySet()) {
if (!entry.getValue().equals(wordCounts.get(entry.getKey()))) {
isChanged = true;
break;
}
}
}
if (isChanged) {
// Update & Save Pratilipi Meta Doc
pmDoc.setWordCounts(wordCounts);
docAccessor.save(pratilipiId, pmDoc);
// Update counts in Pratilipi Entity
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
pratilipi.setWordCount(wordCount);
pratilipi.setImageCount(imageCount);
pratilipi.setChapterCount(chapterCount);
pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
}
return isChanged;
}
Aggregations