Last active
February 9, 2018 11:40
-
-
Save OlivierJaquemet/30b64026828732b4967a9bb4bda0c8cd to your computer and use it in GitHub Desktop.
JPlatform URL Media template JSP allowing any URL with a valid oEmbed provider to be inserted and displayed in Jalios JPlatform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Copy JSP in your JPlatform webapp | |
# 2. Declare this JPlatform URL media template with the following properties (adapte th JSP path) : | |
# oEmbed | |
media.template.default.URL.zzz-oEmbed.enabled: true | |
media.template.default.URL.zzz-oEmbed.regex: .* | |
media.template.default.URL.zzz-oEmbed.jsp: /jcore/media/url/mediaTemplateOEmbed.jsp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-- | |
@Summary: Very naive implementation to render URL using oEmbed endpoints | |
see https://oembed.com/ | |
--%><%@ page import="com.fasterxml.jackson.databind.JsonNode" %><% | |
%><%@ page import="com.fasterxml.jackson.databind.ObjectMapper" %><% | |
%><%@ page import="java.util.regex.Matcher" %><% | |
%><%@ page import="java.util.regex.Pattern" %><% | |
%><%@ include file='/jcore/doInitPage.jspf' %><% | |
%><%@ include file='/jcore/media/mediaTemplateInit.jspf' %><%! | |
final String OEMBED_PROVIDERS_JSON_URL = "https://oembed.com/providers.json"; | |
final ObjectMapper oEmbedMapper = new ObjectMapper(); | |
JsonNode oEmbedProviders = null; | |
%><% | |
if (Util.isEmpty(url)){ | |
return; | |
} | |
// Lazy initialization of providers list | |
if (oEmbedProviders == null) { | |
try { | |
oEmbedProviders = oEmbedMapper.readTree(new URL(OEMBED_PROVIDERS_JSON_URL)); | |
} catch (Exception ex) { | |
logger.warn("Could not retrieve oEmbed providers list", ex); | |
} | |
} | |
// | |
if (oEmbedProviders == null) { | |
return; | |
} | |
String html = null; | |
for (JsonNode provider : oEmbedProviders) { | |
final String providerName = provider.get("provider_name").asText(); | |
final String providerUrl = provider.get("provider_url").asText(); | |
final JsonNode providerEndpoints = provider.get("endpoints"); | |
if (providerEndpoints != null) { | |
for (JsonNode providerEndpoint : providerEndpoints) { | |
final String endpointUrl = providerEndpoint.get("url").asText(); | |
final JsonNode endpointSchemes = providerEndpoint.get("schemes"); | |
boolean match = false; | |
if (endpointSchemes != null) { | |
for (JsonNode endpointScheme : endpointSchemes) { | |
final String scheme = Util.getString(endpointScheme.asText(), ""); | |
final String schemeJavaRegex = "^" + scheme.replace(".", "\\.").replace("*", ".*") + "$"; // very naive impl | |
if (url.matches(schemeJavaRegex)) { | |
if (logger.isDebugEnabled()) { | |
logger.debug("URL matches provider " + providerName + " at " + providerUrl + ". Use endpoint " + endpointUrl); | |
} | |
match = true; | |
break; | |
} | |
} | |
} | |
if (match) { | |
try { | |
URL oembedURL = new URL(endpointUrl+"?url="+ServletUtil.encodeURL(url)); | |
JsonNode oembed = oEmbedMapper.readTree(oembedURL); | |
html = oembed.get("html").asText(); | |
} catch (Exception ex) { | |
logger.warn("Could not contact/parse oEmbed endpoint " + endpointUrl + " for URL" +url, ex); | |
} | |
break; | |
} | |
} | |
} | |
if (html != null) { | |
break; | |
} | |
} | |
if (html == null) { | |
%><a href="<%= encodeForHTMLAttribute(url) %>"><%= encodeForHTML(url) %></a><% | |
return; | |
} | |
%><%= html %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment