Search in sources :

Example 41 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class PratilipiDocUtil method getContentIndex.

// Content doc
public static JsonArray getContentIndex(Long pratilipiId) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToReadPratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    PratilipiContentDoc pcDoc = _validateChapterIdInContent(pratilipiId);
    return pcDoc == null ? null : pcDoc.getIndex();
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 42 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentImage.

public static String saveContentImage(Long pratilipiId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    String contentImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName(_createImageFullName(pratilipiId, contentImageName));
    blobAccessor.createOrUpdateBlob(blobEntry);
    return contentImageName;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date)

Example 43 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentPageBatch.

public static PratilipiContentDoc saveContentPageBatch(Long pratilipiId, JsonObject chapters) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    for (Map.Entry<String, JsonElement> chapterEntry : chapters.entrySet()) {
        String chapterId = chapterEntry.getKey();
        // chapterTitle
        String chapterTitle = null;
        if (chapterEntry.getValue().getAsJsonObject().has("chapterTitle"))
            chapterTitle = chapterEntry.getValue().getAsJsonObject().get("chapterTitle").getAsString();
        // content
        String content = null;
        if (chapterEntry.getValue().getAsJsonObject().has("content"))
            content = chapterEntry.getValue().getAsJsonObject().get("content").getAsString();
        // pageNo
        if (!chapterEntry.getValue().getAsJsonObject().has("pageNo"))
            throw new InvalidArgumentException(GenericRequest.ERR_PRATILIPI_PAGE_NO_REQUIRED);
        Integer pageNo = chapterEntry.getValue().getAsJsonObject().get("pageNo").getAsInt();
        // Chapter
        Chapter chapter = pcDoc.getChapter(chapterId);
        if (chapter == null) {
            chapter = pcDoc.addChapter(pcDoc.getChapterCount() + 1, chapterTitle);
            chapter.setId(chapterId);
        } else if (chapterTitle != null) {
            chapter.setTitle(chapterTitle);
        }
        chapter.setLastUpdated(UxModeFilter.getClientType());
        // Page
        PratilipiContentDoc.Page page = chapter.getPage(pageNo);
        if (page == null)
            page = chapter.addPage(pageNo);
        _setPage(pratilipiId, page, content);
    }
    // Save
    docAccessor.save(pratilipiId, pcDoc);
    // AuditLog
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE_CONTENT, null);
    auditLog.setEventDataNew(chapters);
    auditLog.setPrimaryContentId(pratilipiId);
    auditLog.setCreationDate(new Date());
    dataAccessor.createOrUpdateAuditLog(auditLog);
    return pcDoc;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonElement(com.google.gson.JsonElement) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Map(java.util.Map) HashMap(java.util.HashMap)

Example 44 with Pratilipi

use of com.pratilipi.data.type.Pratilipi 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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DataAccessor(com.pratilipi.data.DataAccessor) HttpServletResponse(javax.servlet.http.HttpServletResponse) Website(com.pratilipi.common.type.Website) Author(com.pratilipi.data.type.Author) Event(com.pratilipi.data.type.Event) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi) BlogPost(com.pratilipi.data.type.BlogPost) URL(java.net.URL)

Example 45 with Pratilipi

use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.

the class EcsContentApi method get.

@Get
public GenericResponse get(GetRequest request) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
    if (pratilipi != null && pratilipi.getState() == PratilipiState.DELETED)
        throw new InvalidArgumentException("Deleted pratilipiId");
    return new GetResponse(request.pratilipiId, PratilipiDocUtil._validateChapterIdInContent(request.pratilipiId));
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) Get(com.pratilipi.api.annotation.Get)

Aggregations

Pratilipi (com.pratilipi.data.type.Pratilipi)45 DataAccessor (com.pratilipi.data.DataAccessor)36 UserPratilipi (com.pratilipi.data.type.UserPratilipi)33 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)14 DocAccessor (com.pratilipi.data.DocAccessor)13 AuditLog (com.pratilipi.data.type.AuditLog)10 Author (com.pratilipi.data.type.Author)9 Page (com.pratilipi.data.type.Page)9 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)9 Date (java.util.Date)9 BlobEntry (com.pratilipi.data.type.BlobEntry)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)6 BlobAccessor (com.pratilipi.data.BlobAccessor)6 Get (com.pratilipi.api.annotation.Get)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 PratilipiData (com.pratilipi.data.client.PratilipiData)5 PratilipiEntity (com.pratilipi.data.type.gae.PratilipiEntity)4