1. Stephen McKamey
  2. duel

Commits

Stephen McKamey  committed 61574ec

renaming URLInterceptor to LinkInterceptor & adding implementation for CDN resource mapping

  • Participants
  • Parent commits c75d3b8
  • Branches default

Comments (0)

Files changed (4)

File duel/duel-runtime/src/main/java/org/duelengine/duel/CDNLinkInterceptor.java

View file
  • Ignore whitespace
+package org.duelengine.duel;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Map;
+
+public class CDNLinkInterceptor implements LinkInterceptor {
+
+	private final boolean isDevMode;
+	private final String cdnHost;
+	private final Map<String, String> cdnMap;
+
+	/**
+	 * @param cdnHost the CDN server hostname (e.g., "cdn.example.com")
+	 * @param cdnMap a mapping of paths to their compacted form (e.g., "/foo.js" => "/foo.min.js")
+	 * @param isDevMode uses normal paths unless compacted form has a dev form (e.g., "/foo.js" => "/foo.min.js" => "/foo.test.js", else "/foo.js")
+	 * @throws URISyntaxException
+	 */
+	public CDNLinkInterceptor(String cdnHost, Map<String, String> cdnMap, boolean isDevMode) throws URISyntaxException {
+
+		this.isDevMode = isDevMode;
+		this.cdnMap = cdnMap;
+
+		// use URI class to check for proper host syntax
+		this.cdnHost = (cdnHost == null || cdnHost.isEmpty()) ? "" : new URI("http", cdnHost, null, null).getRawSchemeSpecificPart();
+	}
+
+	public String transformURL(String url) {
+		if (!this.cdnMap.containsKey(url)) {
+			return url;
+		}
+
+		// lookup CDN resource
+		String cdnURL = this.cdnMap.get(url);
+
+		if (this.isDevMode) {
+			if (!this.cdnMap.containsKey(cdnURL)) {
+				// scripts & stylesheets served directly
+				return url;
+			}
+
+			// merge files serve generated placeholder
+			return this.cdnMap.get(cdnURL);
+		}
+
+		// CDN resources are compacted and optionally served from a differen host
+		return this.cdnHost + cdnURL;
+	}
+}

File duel/duel-runtime/src/main/java/org/duelengine/duel/DuelContext.java

View file
  • Ignore whitespace
 
 	private Appendable output;
 	private ClientIDStrategy clientID;
-	private URLInterceptor interceptor;
+	private LinkInterceptor interceptor;
 	private DataEncoder encoder;
 	private FormatPrefs format;
 
 		return this;
 	}
 
-	public DuelContext setURLInterceptor(URLInterceptor value) {
+	public DuelContext setLinkInterceptor(LinkInterceptor value) {
 		this.interceptor = value;
 
 		return this;

File duel/duel-runtime/src/main/java/org/duelengine/duel/LinkInterceptor.java

View file
  • Ignore whitespace
+package org.duelengine.duel;
+
+/**
+ * Allows links within view markup to be transformed at runtime.
+ * 
+ * Useful for dynamically resolving links from custom names, or transforming URLs per environment (DEV/TEST/PROD).
+ * 
+ * LinkInterceptors may be chained using a Decorator pattern.
+ */
+public interface LinkInterceptor {
+
+	public String transformURL(String url);
+}

File duel/duel-runtime/src/main/java/org/duelengine/duel/URLInterceptor.java

  • Ignore whitespace
-package org.duelengine.duel;
-
-public interface URLInterceptor {
-
-	public String transformURL(String url);
-}