Mercurial Hosting > nabble
diff src/nabble/view/web/embed/JsEmbed.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/web/embed/JsEmbed.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,107 @@ + +package nabble.view.web.embed; + +import fschmidt.util.java.HtmlUtils; +import fschmidt.util.servlet.JtpContext; +import nabble.model.ModelHome; +import nabble.model.Node; +import nabble.model.Site; +import nabble.view.lib.Jtp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + + +public final class JsEmbed extends HttpServlet { + + private static final Logger logger = LoggerFactory.getLogger(JsEmbed.class); + + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + response.setHeader("Content-Type","application/x-javascript"); + PrintWriter out = response.getWriter(); + + long siteId = Jtp.getLong(request, "site"); + Site site = ModelHome.getSite(siteId); + if (site == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, "Site not found"); + return; + } + + long nodeId = Jtp.getLong(request, "node"); + Node node = site.getNode(nodeId); + + // cache the page + List<String> events = new ArrayList<String>(); + Jtp.addBreadCrumbEvents( events, site.getRootNode() ); + JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName); + jtpContext.setEtag(request,response, events.toArray( new String[events.size()] ) ); + + String baseUrl = Jtp.getBaseUrl(request); + + String embedUrl = request.getParameter("url"); + logger.info("URL=" + embedUrl); + + embedUrl = HtmlUtils.urlDecode(embedUrl); + int pos = embedUrl.indexOf("#nabble-"); + String defaultUrl = node == null ? null : baseUrl+Jtp.path(node); + if (pos > 0) { + String sufix = embedUrl.substring(pos+7); + // if sufix contains #a (with post id) + int posHash = sufix.indexOf('|'); + if (posHash > 0) { + sufix = sufix.substring(0, posHash); + } + defaultUrl = baseUrl + '/' + sufix + ".html"; + } + + out.print( "\r\nvar Nabble = new Object();\r\nNabble.defaultHeight = 700;\r\nNabble.currentHeight = 0;\r\nNabble.counter = 0;\r\nNabble.title = document.title == \"\"? \"\" : document.title + \" - \";\r\nNabble.resizeTimeoutID;\r\nNabble.context = '" ); + out.print( (baseUrl) ); + out.print( "';\r\nNabble.defaultUrl = '" ); + out.print( (defaultUrl) ); + out.print( "';\r\n\r\nNabble.get = function(id) { return document.getElementById(id); };\r\n\r\nNabble.resizeTimeout = function() {\r\n Nabble.resizeTimeoutID = setTimeout(Nabble.showMoreLink, 6000);\r\n};\r\n\r\nNabble.cancelTimeout = function() {\r\n if (Nabble.resizeTimeoutID) {\r\n clearInterval(Nabble.resizeTimeoutID);\r\n Nabble.get('nabblemore').style.display = 'none';\r\n Nabble.resizeTimeoutID = null;\r\n }\r\n};\r\n\r\nNabble.showMoreLink = function() {\r\n if (Nabble.resizeTimeoutID) {\r\n Nabble.get('nabblemore').style.display = 'block';\r\n }\r\n};\r\n\r\n Nabble.showMore = function() {\r\n if (Nabble.currentHeight == 0)\r\n Nabble.currentHeight = Nabble.defaultHeight;\r\n Nabble.currentHeight += 300;\r\n Nabble.get('nabbleiframe').style.height = Nabble.currentHeight + 'px';\r\n};\r\n\r\nNabble.escape = function(value) {\r\n if (typeof value == 'string') {\r\n var hasSpace = value.indexOf(' ') >= 0;\r\n var hasQuote = value.indexOf('\"') >= 0;\r\n\r\n value = value.replace(/\\;/g, '%3B');\r\n value = value.replace(/\"/g, '\\\\\"');\r\n\r\n if (hasSpace || hasQuote)\r\n value = '\"' + value + '\"';\r\n }\r\n return value;\r\n};\r\n\r\nNabble.unescape = function(value) {\r\n if (value.charAt(0) == '\"' && value.charAt(value.length-1) == '\"')\r\n value = value.substring(1, value.length-1);\r\n\r\n value = value.replace(/\\\\\"/g, '\"');\r\n value = value.replace(/%3B/g, ';');\r\n return value;\r\n};\r\n\r\n Nabble.setCookie = function(name, value) {\r\n name = name+'" ); + out.print( (siteId) ); + out.print( "';\r\n document.cookie = name + \"=\" + Nabble.escape(value) + \"; path=/\";\r\n};\r\n\r\nNabble.getCookie = function(name) {\r\n name = name+'" ); + out.print( (siteId) ); + out.print( "';\r\n var dc = document.cookie;\r\n var prefix = name + \"=\";\r\n var begin = dc.indexOf(\"; \" + prefix);\r\n if (begin == -1) {\r\n begin = dc.indexOf(prefix);\r\n if (begin != 0) return null;\r\n } else\r\n begin += 2;\r\n var end = document.cookie.indexOf(\";\", begin);\r\n if (end == -1)\r\n end = dc.length;\r\n return Nabble.unescape(dc.substring(begin + prefix.length, end));\r\n};\r\n\r\nNabble.setPersistentCookie = function(name, value) {\r\n name = name+'" ); + out.print( (siteId) ); + out.print( "';\r\n var expires = new Date();\r\n expires.setFullYear(expires.getFullYear()+10);\r\n var curCookie = name + \"=\" + Nabble.escape(value) + \"; expires=\" + expires.toGMTString() + \"; path=/\";\r\n document.cookie = curCookie;\r\n};\r\n\r\n Nabble.deleteCookie = function(name) {\r\n name = name+'" ); + out.print( (siteId) ); + out.print( "';\r\n document.cookie = name + \"=\" +\r\n \"; path=/\" +\r\n \"; expires=Thu, 01-Jan-1970 00:00:01 GMT\";\r\n};\r\n\r\nNabble.noHash = function(url) {\r\n var pos = url.indexOf('#');\r\n return (pos>-1)?url.substring(0, pos):url;\r\n};\r\n\r\nNabble.debug = function(s) {\r\n if (Nabble.debugElement == 0)\r\n return;\r\n if (!Nabble.debugElement) {\r\n Nabble.debugElement = Nabble.get('debug');\r\n if (!Nabble.debugElement) {\r\n Nabble.debugElement = 0;\r\n return;\r\n }\r\n }\r\n Nabble.debugElement.innerHTML = Nabble.debugElement.innerHTML+s+'<br/>';\r\n};\r\n\r\nNabble.loadScript = function(url) {\r\n Nabble.debug('Loading script: ' + url);\r\n var e = document.createElement(\"script\");\r\n e.src = url;\r\n e.type=\"text/javascript\";\r\n document.getElementsByTagName(\"head\")[0].appendChild(e);\r\n};\r\n\r\nNabble.getJs = function(keys) {\r\n if (!window.clientID)\r\n return;\r\n var p = '';\r\n for (var i=0;i<keys.length;i++) {\r\n p += '&key=' + keys[i];\r\n }\r\n var url = Nabble.context+\"/util/SessionService.jtp?action=get\" + p + \"&cid=\" + window.clientID + \"&_=\" + new Date().getTime();\r\n Nabble.loadScript(url);\r\n};\r\n\r\nNabble.scroll = function(y) {\r\n Nabble.debug('[scroll] y=' + y);\r\n if (y == 1 && window.nabble_scroll_top) {\r\n scrollTo(0, 0);\r\n } else if (y > 0 && !window.nabble_ignore_scroll) {\r\n var obj = Nabble.get('nabbleiframe');\r\n do {\r\n y += obj.offsetTop;\r\n } while (obj = obj.offsetParent);\r\n scrollTo(0, y);\r\n }\r\n};\r\n\r\nNabble.resizeFrames = function(height,title,validHeight) {\r\n if (document.title != title && !window.nabble_ignore_title)\r\n document.title = title;\r\n Nabble.debug('[resizeFrames] Counter = ' + (Nabble.counter++) + ' Height = ' + height + ' Title=[' + title + '] History=' + history.length + ' -- cid=' + window.clientID);\r\n\r\n if (height != Nabble.currentHeight) {\r\n Nabble.currentHeight = height;\r\n var f = Nabble.get('nabbleiframe');\r\n if (f) {\r\n f.scrolling = validHeight? 'no' : 'auto';\r\n Nabble.debug('Scrolling=' + f.scrolling);\r\n f.style.height = height + 'px';\r\n Nabble.cancelTimeout();\r\n }\r\n }\r\n};\r\n\r\nNabble.getCurrentUrl = function() {\r\n var currentUrl = Nabble.defaultUrl;\r\n if (Nabble.hash.indexOf('#nabble+') == 0) {\r\n var path = Nabble.hash.substring(8);\r\n path = decodeURIComponent(path);\r\n path = path\r\n .replace(/</g,'%3C')\r\n .replace(/>/g,'%3E')\r\n .replace(/\"/g,'%22')\r\n .replace(/'/g,'%27');\r\n currentUrl = Nabble.context+\"/\" + path;\r\n }\r\n currentUrl += Nabble.realHash == ''? '' : '#' + Nabble.realHash;\r\n return currentUrl;\r\n};\r\n\r\nNabble.getClientID = function() {\r\n var clientID = Nabble.getCookie('clientID');\r\n if (!clientID) {\r\n clientID = new Date().getTime() + '-' + Math.ceil(Math.random() * 1000);\r\n Nabble.setCookie('clientID', clientID);\r\n }\r\n return clientID;\r\n};\r\n\r\nNabble.restart = function(nodeId, baseUrl) {\r\n Nabble.debug('Restart -- baseUrl=' + baseUrl);\r\n Nabble.context = baseUrl;\r\n Nabble.defaultUrl = baseUrl+'/';\r\n Nabble.start();\r\n};\r\n\r\nNabble.getConf = function() {\r\n return window.nabble_ignore_scroll? 'noscroll;':'';\r\n};\r\n\r\nNabble.start = function() {\r\n Nabble.infoLoaded = false;\r\n window.clientID = Nabble.getClientID();\r\n\r\n " ); + /* Hash processing */ + out.print( "\r\n var hash = location.hash;\r\n var pipe = hash.indexOf('|');\r\n var realHash = '';\r\n if (pipe > 0) {\r\n " ); +/* + If there is a pipe in the hash, the value after it is the + hash of the original URL, which must be restored. + For Example: + #nabble-t100|a123 + */ + out.print( "\r\n realHash = hash.substring(pipe);\r\n hash = hash.replace(realHash, '');\r\n realHash = realHash.substring(1);\r\n }\r\n " ); +/* save the hash and realHash variables because the getCurrentUrl() method will need them.. */ + out.print( "\r\n Nabble.hash = hash;\r\n Nabble.realHash = realHash;\r\n Nabble.infoUrl = Nabble.context+\"/embed/EmbedInfo.jtp?node=" ); + out.print( (nodeId) ); + out.print( "&cid=\" + window.clientID + \"&hash=\" + realHash + '&conf=' + Nabble.getConf() + \"&_=\" + new Date().getTime() + \"#\" + Nabble.noHash(location.href);\r\n var emptyUrl = Nabble.context+\"/util/Empty.jtp\";\r\n var html = \"<div id='nabblemain'><div style='height:700px'><img src='" ); + out.print( (baseUrl) ); + out.print( "/images/loading.png' width=94 height=33 alt='Loading...'></div></div>\";\r\n html += \"<div id='nabblemore' style='display:none'><a href=\\\"javascript: void Nabble.showMore()\\\">view more</a></div>\";\r\n html += \"<iframe name='nabbleinfo' id='nabbleinfo' width='1' height='1' style='display:none' src=''></iframe>\";\r\n html += \"<iframe name='nabbleresize' onload='Nabble.getJs([\\\"resizejs\\\", \\\"scrolljs\\\", \\\"others\\\"])' width='1' height='1' style='display:none' src='\" + emptyUrl + \"'></iframe>\";\r\n html += \"<iframe name='nabbleready' onload='Nabble.loadMain()' width='1' height='1' style='display:none' src='\" + emptyUrl + \"'></iframe>\";\r\n var div = Nabble.get('nabbleforum');\r\n div.innerHTML = html;\r\n};\r\n\r\n" ); +/* + 1 - Loads the main page as the last page in order to allow the + embedding code to run before it. + 2 - We must load the iframe using a string because IE (and some + other browsers) have problems to load an iframe through the DOM. + 3 - The loadMain() function is called when the 'nabbleready' iframe is loaded. + Note that this iframe doesn't have an initial URL. The first URL is provided in EmbedInfo.jtp. + */ + out.print( "\r\nNabble.loadMain = function() {\r\n Nabble.debug('Loading main page...infoLoaded='+Nabble.infoLoaded);\r\n if (!Nabble.infoLoaded) {\r\n Nabble.debug('InfoUrl='+Nabble.infoUrl);\r\n Nabble.get('nabbleinfo').setAttribute('src',Nabble.infoUrl);\r\n Nabble.infoLoaded = true;\r\n return;\r\n }\r\n var width = window.nabble_width? window.nabble_width : '100%';\r\n var currentUrl = Nabble.getCurrentUrl();\r\n Nabble.debug('CurrentUrl='+currentUrl);\r\n var m = '<iframe name=\"nabbleiframe\" id=\"nabbleiframe\" src=\"' + currentUrl + '\" width=\"' + width + '\" height=\"' + Nabble.defaultHeight + '\" frameBorder=\"0\" scrolling=\"no\" allowtransparency=\"true\"></iframe>';\r\n Nabble.get('nabblemain').innerHTML = m;\r\n Nabble.resizeTimeout();\r\n}\r\n\r\nNabble.start();\r\n" ); + + } +} +