Class NetHelper


  • public class NetHelper
    extends Object
    • Constructor Detail

      • NetHelper

        public NetHelper()
    • Method Detail

      • followURL

        public static URL followURL​(URL url)
                             throws Exception
        follow redirection and return final url
        Parameters:
        url -
        Returns:
        Throws:
        Exception
      • readJson

        public static com.google.gson.JsonElement readJson​(URL url)
                                                    throws Exception
        read a page a put content in a String.
        Parameters:
        url - a valid URL
        Returns:
        code returned by the http request on the URL.
        Throws:
        IOException
        Exception
      • setJsonContentType

        public static void setJsonContentType​(javax.servlet.http.HttpServletResponse response)
      • readPage

        public static void readPage​(URL url,
                                    OutputStream out)
                             throws Exception
        read a page a put content in a Stream.
        Parameters:
        out - the output stream, it receive the url inputstream
        Throws:
        IOException
        Exception
      • readPageWithGet

        public static String readPageWithGet​(URL url)
        read a page a put content in a Stream.
        Parameters:
        out - the output stream, it receive the url inputstream
        Returns:
        code returned by the http request on the URL.
        Throws:
        IOException
      • getPageTitle

        public static String getPageTitle​(URL url)
                                   throws Exception
        extract the title of a web page.
        Parameters:
        url -
        Returns:
        the title of the page.
        Throws:
        Exception
      • getPageTitle

        public static String getPageTitle​(String content)
        extract the title of a web page.
        Parameters:
        URL -
        Returns:
        the title of the page.
      • getPageDescription

        public static String getPageDescription​(String content)
        extract the title of a web page.
        Parameters:
        URL -
        Returns:
        the title of the page.
      • isUserAgentRobot

        public static boolean isUserAgentRobot​(String userAgent)
      • extractExternalURL

        public static List<String> extractExternalURL​(URL inURL,
                                                      String content)
      • getLocalCopyOfPageImage

        public static String getLocalCopyOfPageImage​(ContentContext ctx,
                                                     URL inURL,
                                                     String content,
                                                     CRC32 crc32,
                                                     boolean preferVertical,
                                                     boolean needVertical)
        analyse a page and retreive a image
        Parameters:
        URL - the url of the page
        content - the content
        Returns:
        the uri to the local file
      • getLocalCopyOfPageImage

        public static String getLocalCopyOfPageImage​(String cacheFolder,
                                                     String dataFolder,
                                                     URL pageURL,
                                                     URL imageURL,
                                                     String content,
                                                     CRC32 crc32,
                                                     boolean preferVertical,
                                                     boolean needVertical)
        analyse a page and retreive a image
        Parameters:
        URL - the url of the page
        content - the content
        Returns:
        the uri to the local file
      • isURLValid

        public static boolean isURLValid​(URL url)
      • isURLValid

        public static boolean isURLValid​(URL url,
                                         boolean only404)
      • getExternalLinks

        public static List<URL> getExternalLinks​(String content)
      • getLinksFromText

        public static List<URL> getLinksFromText​(String content)
      • isConnected

        public static boolean isConnected()
        check internet connection with stable server.
        Returns:
      • canReach

        public static boolean canReach​(String url)
        check if the given url can be reached.
        Parameters:
        url -
        Returns:
      • sendMailToAdministrator

        public static void sendMailToAdministrator​(GlobalContext globalContext,
                                                   String subject,
                                                   String content)
                                            throws javax.mail.internet.AddressException
        Throws:
        javax.mail.internet.AddressException
      • sendMailToAdministrator

        public static void sendMailToAdministrator​(GlobalContext globalContext,
                                                   javax.mail.internet.InternetAddress from,
                                                   String subject,
                                                   String content)
      • sendXHTMLMail

        public static void sendXHTMLMail​(ContentContext ctx,
                                         javax.mail.internet.InternetAddress from,
                                         javax.mail.internet.InternetAddress to,
                                         javax.mail.internet.InternetAddress cc,
                                         javax.mail.internet.InternetAddress bcc,
                                         String subject,
                                         String content,
                                         String templateName)
                                  throws Exception
        Throws:
        Exception
      • sendMail

        public static boolean sendMail​(GlobalContext globalContext,
                                       javax.mail.internet.InternetAddress from,
                                       javax.mail.internet.InternetAddress to,
                                       javax.mail.internet.InternetAddress cc,
                                       javax.mail.internet.InternetAddress bcc,
                                       String subject,
                                       String content)
      • sendMail

        public static boolean sendMail​(GlobalContext globalContext,
                                       javax.mail.internet.InternetAddress from,
                                       javax.mail.internet.InternetAddress to,
                                       javax.mail.internet.InternetAddress cc,
                                       javax.mail.internet.InternetAddress bcc,
                                       String subject,
                                       String content,
                                       String contentTxt,
                                       boolean isHTML)
      • sendRedirectPermanently

        public static void sendRedirectPermanently​(javax.servlet.http.HttpServletResponse response,
                                                   String url)
      • sendRedirectTemporarily

        public static void sendRedirectTemporarily​(javax.servlet.http.HttpServletResponse response,
                                                   String url)
      • getCookie

        public static javax.servlet.http.Cookie getCookie​(javax.servlet.http.HttpServletRequest request,
                                                          String name)
      • isRobot

        public static boolean isRobot​(String userAgent)
      • isMobile

        public static boolean isMobile​(String userAgent)
      • removeParams

        public static URL removeParams​(URL url)
        remove params of a url
      • getIpAsInt

        public static int getIpAsInt​(String ip)
        convert a ip in string (192.168.0.1) to a integer value.
        Parameters:
        ip -
        Returns:
      • ipInRange

        public static boolean ipInRange​(String ip,
                                        String range)
        check if a ip is in a specific range
        Parameters:
        ip -
        range -
        Returns:
      • isIPAccepted

        public static boolean isIPAccepted​(ContentContext ctx)
      • insertEtag

        public static boolean insertEtag​(ContentContext ctx,
                                         File file,
                                         String secondaryHash)
                                  throws IOException
        insert and check ETag
        Parameters:
        ctx -
        file -
        secondaryHash -
        Returns:
        true if content in cache (>> stop rendering)
        Throws:
        IOException
      • testPort

        public static boolean testPort​(String host,
                                       int port)
      • getIp

        public static String getIp​(javax.servlet.http.HttpServletRequest request)