Package org.javlo.helper
Class NetHelper
- java.lang.Object
-
- org.javlo.helper.NetHelper
-
public class NetHelper extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static String
HEADER_DATE
static String
HEADER_ETAG
static String
HEADER_IF_MODIFIED_SINCE
static String
HEADER_IF_MODIFIED_SINCE_ETAG
static String
HEADER_LAST_MODIFIED
static String
JAVLO_USER_AGENT
protected static Logger
logger
create a static logger.static String
MOZILLA_USER_AGENT
-
Constructor Summary
Constructors Constructor Description NetHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
canReach(String url)
check if the given url can be reached.static List<String>
extractExternalURL(URL inURL, String content)
static List<VisualResource>
extractImage(URL inURL, String content, boolean needSize)
static List<URL>
extractMostSimilarLinks(URL url)
static List<String>
extractURL(URL inURL, String content)
static URL
followURL(URL url)
follow redirection and return final urlstatic String
getContentType(URL url)
static javax.servlet.http.Cookie
getCookie(javax.servlet.http.HttpServletRequest request, String name)
static List<URL>
getExternalLinks(String content)
static String
getIp(javax.servlet.http.HttpServletRequest request)
static int
getIpAsInt(String ip)
convert a ip in string (192.168.0.1) to a integer value.static List<URL>
getLinks(String content, String inURL)
static List<URL>
getLinksFromText(String content)
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 imagestatic String
getLocalCopyOfPageImage(ContentContext ctx, URL inURL, String content, CRC32 crc32, boolean preferVertical, boolean needVertical)
analyse a page and retreive a imagestatic String
getPageDescription(String content)
extract the title of a web page.static PageMeta
getPageMeta(URL url)
static String
getPageTitle(String content)
extract the title of a web page.static String
getPageTitle(URL url)
extract the title of a web page.static boolean
insertEtag(ContentContext ctx, File file, String secondaryHash)
insert and check ETagstatic boolean
insertEtag(ContentContext ctx, MenuElement page)
static boolean
ipInRange(String ip, String range)
check if a ip is in a specific rangestatic boolean
isConnected()
check internet connection with stable server.static boolean
isIPAccepted(ContentContext ctx)
static boolean
isMobile(String userAgent)
static boolean
isRobot(String userAgent)
static boolean
isURLValid(URL url)
static boolean
isURLValid(URL url, boolean only404)
static boolean
isUserAgentRobot(String userAgent)
static void
main(String[] args)
static void
nocheckCertificatHttps()
static String
postJsonRequest(URL url, String userAgent, Map<String,String> header, String json)
static Long
readDate(URL url)
static com.google.gson.JsonElement
readJson(URL url)
read a page a put content in a String.static String
readPage(String inURL, boolean cssInline)
static String
readPage(URL url)
static String
readPage(URL url, boolean cssInline, String userAgent)
static void
readPage(URL url, OutputStream out)
read a page a put content in a Stream.static String
readPage(URL url, String userName, String password)
static String
readPageForMailing(URL url)
static String
readPageForMailing(URL url, String token)
static String
readPageForMailing(URL url, String login, String pwd)
static String
readPageGet(URL url)
static String
readPageGet(URLConnection conn, boolean checkReturnCode)
static String
readPageGet(URL url, boolean checkReturnCode)
static String
readPageGetFollowRedirect(URL url)
static String
readPageNoError(URL url)
static String
readPageWithGet(URL url)
read a page a put content in a Stream.static URL
removeParams(URL url)
remove params of a urlstatic 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)
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)
static void
sendMailToAdministrator(GlobalContext globalContext, String subject, String content)
static void
sendMailToAdministrator(GlobalContext globalContext, javax.mail.internet.InternetAddress from, String subject, String content)
static void
sendPageByMailing(ContentContext ctx, MenuElement page, String sender, String recipient, Map<String,Object> params)
static void
sendRedirectPermanently(javax.servlet.http.HttpServletResponse response, String url)
static void
sendRedirectTemporarily(javax.servlet.http.HttpServletResponse response, String url)
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)
static void
setJsonContentType(javax.servlet.http.HttpServletResponse response)
static boolean
testPort(String host, int port)
static Company
validVATEuroparlEU(ContentContext ctx, String vat)
static void
writeURLToStream(URL url, OutputStream out)
-
-
-
Field Detail
-
JAVLO_USER_AGENT
public static final String JAVLO_USER_AGENT
- See Also:
- Constant Field Values
-
MOZILLA_USER_AGENT
public static final String MOZILLA_USER_AGENT
- See Also:
- Constant Field Values
-
logger
protected static Logger logger
create a static logger.
-
HEADER_DATE
public static final String HEADER_DATE
- See Also:
- Constant Field Values
-
HEADER_LAST_MODIFIED
public static final String HEADER_LAST_MODIFIED
- See Also:
- Constant Field Values
-
HEADER_IF_MODIFIED_SINCE
public static final String HEADER_IF_MODIFIED_SINCE
- See Also:
- Constant Field Values
-
HEADER_ETAG
public static final String HEADER_ETAG
- See Also:
- Constant Field Values
-
HEADER_IF_MODIFIED_SINCE_ETAG
public static final String HEADER_IF_MODIFIED_SINCE_ETAG
- See Also:
- Constant Field Values
-
-
Method Detail
-
readPageForMailing
public static String readPageForMailing(URL url) throws Exception
- Throws:
Exception
-
readPageForMailing
public static String readPageForMailing(URL url, String login, String pwd) throws Exception
- Throws:
Exception
-
readPageForMailing
public static String readPageForMailing(URL url, String token) throws Exception
- Throws:
Exception
-
readPageGet
public static String readPageGet(URLConnection conn, boolean checkReturnCode) throws Exception
- Throws:
Exception
-
followURL
public static URL followURL(URL url) throws Exception
follow redirection and return final url- Parameters:
url
-- Returns:
- Throws:
Exception
-
readPageGetFollowRedirect
public static String readPageGetFollowRedirect(URL url) throws Exception
- Throws:
Exception
-
readPageGet
public static String readPageGet(URL url, boolean checkReturnCode) throws Exception
- Throws:
Exception
-
readPage
public static String readPage(URL url, String userName, String password) throws Exception
- Throws:
Exception
-
readPage
public static String readPage(String inURL, boolean cssInline) throws Exception
- Throws:
Exception
-
readPage
public static String readPage(URL url, boolean cssInline, String userAgent) throws Exception
- Throws:
Exception
-
postJsonRequest
public static String postJsonRequest(URL url, String userAgent, Map<String,String> header, String json) throws Exception
- Throws:
Exception
-
nocheckCertificatHttps
public static void nocheckCertificatHttps() throws NoSuchAlgorithmException, KeyManagementException
-
writeURLToStream
public static void writeURLToStream(URL url, OutputStream out) throws Exception
- 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)
-
extractImage
public static List<VisualResource> extractImage(URL inURL, String content, boolean needSize)
-
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 pagecontent
- the content- Returns:
- the uri to the local file
-
extractMostSimilarLinks
public static List<URL> extractMostSimilarLinks(URL url) throws Exception
- Throws:
Exception
-
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 pagecontent
- 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)
-
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)
-
sendPageByMailing
public static void sendPageByMailing(ContentContext ctx, MenuElement page, String sender, String recipient, Map<String,Object> params) throws Exception
- Throws:
Exception
-
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)
-
validVATEuroparlEU
public static Company validVATEuroparlEU(ContentContext ctx, String vat) throws MalformedURLException, Exception
- Throws:
MalformedURLException
Exception
-
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)
-
insertEtag
public static boolean insertEtag(ContentContext ctx, MenuElement page) throws Exception
- Throws:
Exception
-
getIp
public static String getIp(javax.servlet.http.HttpServletRequest request)
-
-