/** * @return if CDN is globally enabled. */ public boolean isCdnEnabled() { return null != integration.getCDNStrategy() && integration.getCDNStrategy().supportsCdn(); }
@Override public void addToUrl(UrlBuilder urlBuilder) { if (webResourceIntegration.getCDNStrategy() != null && webResourceIntegration.getCDNStrategy().supportsCdn()) { addRelativeUrlQueryKey(urlBuilder); } }
/** * @return url prefixed with CDN prefix. */ public String getResourceCdnPrefix(final String url) { return getWebResourceMapper().mapSingle(url).orElseGet(() -> integration.getCDNStrategy().transformRelativeUrl(url)); }
final String hash = config.computeGlobalStateHash(); final WebResourceMapper wrm = config.getWebResourceMapper(); final Optional<PrebakeConfig> prebakeConfig = webResourceIntegration.getCDNStrategy() != null ? webResourceIntegration.getCDNStrategy().getPrebakeConfig() : Optional.empty();
@Override protected WebResourceMapper create() throws Exception { if (integration.isCtCdnMappingEnabled()) { final Optional<PrebakeConfig> prebakeConfig = integration.getCDNStrategy() != null ? integration.getCDNStrategy().getPrebakeConfig() : Optional.empty(); if (prebakeConfig.isPresent()) { try { log.info("Creating DefaultWebResourceMapper"); return new DefaultWebResourceMapper(integration, new MappingParser(), prebakeConfig.get(), computeGlobalStateHash(), getCtCdnBaseUrl(), integration.getBaseUrl(UrlMode.RELATIVE)); } catch (FileNotFoundException e) { //no additional logging, it isn't an error return new NoOpWebResourceMapper(e); } catch (Exception e) { log.info("DefaultWebResourceMapper was not created properly. Pre-baked CT-CDN is disabled but will fall back to product's CDN Strategy, if any.", e); return new NoOpWebResourceMapper(e); } } else { log.info("Creating NoOpWebResourceMapper since PrebakeConfig is not present. Pre-baked CT-CDN is disabled but will fall back to product's CDN Strategy, if any."); return new NoOpWebResourceMapper(new Exception("PrebakeConfig not present")); } } else { log.info("Creating NoOpWebResourceMapper since WebResourceIntegration#isCtCdnMappingEnabled flag is disabled. Pre-baked CT-CDN is disabled but will fall back to product's CDN Strategy, if any."); return new NoOpWebResourceMapper(new Exception("CT-CDN is explicitly disabled by the product.")); } } };
@Override protected String create() { // This transform's only "variable" is the context path, and hence is not even variable since the // context path is already in the url path. Therefore we don't need to contribute anything additional // to the hash - this transform is a "constant" and other transformers / conditions don't apply to data // uri's or images. final String version = Config.getPluginVersionOrInstallTime(webResourceIntegration.getPluginAccessor().getPlugin(parameters.getPluginKey()), usePluginInstallTimeInsteadOfTheVersionForSnapshotPlugins); String resourcePrefix = PATH_SEPARATOR + AbstractFileServerServlet.SERVLET_PATH + PATH_SEPARATOR + AbstractFileServerServlet.RESOURCE_URL_PREFIX; String localRelativeUrl = webResourceUrlProvider.getStaticResourcePrefix(version, UrlMode.RELATIVE) + resourcePrefix + PATH_SEPARATOR + parameters.getPluginKey() + ":" + parameters.getModuleKey() + PATH_SEPARATOR; if (requestCdnUrl) { final CDNStrategy cdnStrategy = webResourceIntegration.getCDNStrategy(); if (cdnStrategy == null || !cdnStrategy.supportsCdn()) { // This will throw an exception if we have the CDN parameter in the URL, but // webResourceIntegration.getCdnStrategy() returns null. This situation will occur // if the cdn strategy is changed (eg via dark feature) between url-generation time and // resource-fetch time. In this situation, it is correct to throw an exception - the alternative // is to poison the CDN-side cache with a resource that has not been transformed to CDN. throw new CdnStrategyChangedException("CDN strategy has changed between url generation time and resource fetch time"); } return cdnResourceUrlTransformer.getResourceCdnPrefix(localRelativeUrl); } else { return localRelativeUrl; } } };