Skip to content

draw.io is a JavaScript, client-side editor for general diagramming.

Replaces gae-stub-1.0.9.jar with source for CacheFacade and SecretFacade

David Benson24afc3bad9bf
diff --git a/src/main/server/java/com/mxgraph/online/CacheFacade.java b/src/main/server/java/com/mxgraph/online/CacheFacade.java
new file mode 100644
index 000000000..b97eafad0
--- /dev/null
+++ b/src/main/server/java/com/mxgraph/online/CacheFacade.java
@@ -0,0 +1,82 @@
+/**
+ * Copyright (c) 2020-2026, JGraph Holdings Ltd
+ * Copyright (c) 2020-2026, draw.io AG
+ */
+package com.mxgraph.online;
+
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import javax.cache.Cache;
+import javax.cache.CacheManager;
+import javax.cache.Caching;
+import javax.cache.configuration.MutableConfiguration;
+import javax.cache.expiry.CreatedExpiryPolicy;
+import javax.cache.expiry.Duration;
+import javax.cache.spi.CachingProvider;
+
+import org.ehcache.jsr107.EhcacheCachingProvider;
+
+/**
+ * Public build version of src/main/server/java/com/mxgraph/online/CacheFacade.java,
+ * which uses App Engine memcache and is excluded from the public repository.
+ * Keep the public methods of both in step.
+ */
+public class CacheFacade {
+
+ /**
+ * Not shipped in the public build, used if DRAWIO_MEMCACHED_ENDPOINT is
+ * set and its jar was added to WEB-INF/lib.
+ */
+ private static final String MEMCACHED_PROVIDER = "org.memcached.jcache.MemcachedCachingProvider";
+
+ private CacheFacade() {}
+
+ public static Cache<String, String> createCache()
+ {
+ return createCache("cache", 300); // default values (one cache and 5 min)
+ }
+
+ public static Cache<String, String> createCache(String name, int expirationDelta)
+ {
+ String memcachedEndpoint = System.getenv("DRAWIO_MEMCACHED_ENDPOINT");
+ CachingProvider provider;
+ CacheManager cacheManager;
+
+ if (memcachedEndpoint != null && memcachedEndpoint.length() > 0)
+ {
+ provider = Caching.getCachingProvider(MEMCACHED_PROVIDER);
+ Properties properties = provider.getDefaultProperties();
+ properties.setProperty("servers", memcachedEndpoint);
+ properties.setProperty(name + ".useSharedClientConnection", "true");
+ cacheManager = provider.getCacheManager(
+ provider.getDefaultURI(), null, properties);
+ }
+ else
+ {
+ provider = Caching.getCachingProvider(EhcacheCachingProvider.class.getName());
+ cacheManager = provider.getCacheManager();
+ }
+
+ MutableConfiguration<String, String> configuration =
+ new MutableConfiguration<String, String>()
+ .setStoreByValue(false) //Memcached does not support store by value
+ .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, expirationDelta)));
+ return cacheManager.createCache(name, configuration);
+ }
+
+ /**
+ * Stores the value if the key is absent and returns true if it was stored.
+ */
+ @SuppressWarnings("unchecked")
+ public static boolean putIfAbsent(Cache cache, Object key, Object value)
+ {
+ return cache.putIfAbsent(key, value);
+ }
+
+ public static String getStatistics()
+ {
+ //TODO implement this for memcached
+ return "NYI";
+ }
+}
diff --git a/src/main/server/java/com/mxgraph/online/EmbedServlet2.java b/src/main/server/java/com/mxgraph/online/EmbedServlet2.java
index f87ecbf72..3918d72b4 100644
--- a/src/main/server/java/com/mxgraph/online/EmbedServlet2.java
+++ b/src/main/server/java/com/mxgraph/online/EmbedServlet2.java
@@ -43,8 +43,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.text.StringEscapeUtils;
-import com.google.appengine.api.utils.SystemProperty;
-
import com.mxgraph.online.Utils.SizeLimitExceededException;
/**
@@ -82,6 +80,17 @@ public class EmbedServlet2 extends HttpServlet
*/
protected HashMap<String, String[]> libraries = new HashMap<String, String[]>();
+ /**
+ * Returns the deployed App Engine version, which App Engine sets as a
+ * system property (the value of SystemProperty.applicationVersion), or
+ * "1.0.0" outside App Engine.
+ */
+ protected static String getApplicationVersion()
+ {
+ return System.getProperty("com.google.appengine.application.version",
+ "1.0.0");
+ }
+
/**
* @see HttpServlet#HttpServlet()
*/
@@ -90,7 +99,7 @@ public class EmbedServlet2 extends HttpServlet
if (lastModified == null)
{
// Uses deployment date as lastModified header
- String applicationVersion = SystemProperty.applicationVersion.get();
+ String applicationVersion = getApplicationVersion();
Date uploadDate = new Date(Long
.parseLong(applicationVersion
.substring(applicationVersion.lastIndexOf(".") + 1))
@@ -493,7 +502,7 @@ public class EmbedServlet2 extends HttpServlet
// is a release version - see EmbedServlet.writeStats (the
// release-monitor watchdog reads /embed.js?stats, this servlet
// mirrors it for /embed2.js)
- String applicationVersion = SystemProperty.applicationVersion.get();
+ String applicationVersion = getApplicationVersion();
int dot = applicationVersion.lastIndexOf(".");
String versionId = dot > 0 ? applicationVersion.substring(0, dot)
: applicationVersion;
diff --git a/src/main/server/java/com/mxgraph/online/ProxyServlet.java b/src/main/server/java/com/mxgraph/online/ProxyServlet.java
index 65ae17c91..61164b839 100644
--- a/src/main/server/java/com/mxgraph/online/ProxyServlet.java
+++ b/src/main/server/java/com/mxgraph/online/ProxyServlet.java
@@ -22,7 +22,6 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import com.google.apphosting.api.DeadlineExceededException;
import com.mxgraph.online.Utils.UnsupportedContentException;
import com.mxgraph.online.Utils.SizeLimitExceededException;
@@ -49,6 +48,12 @@ public class ProxyServlet extends HttpServlet
public static boolean IS_GAE = (System.getProperty("com.google.appengine.runtime.version") == null) ? false : true;
+ /**
+ * Thrown by App Engine when a request passes its deadline. Matched by
+ * name since the public build has no App Engine SDK.
+ */
+ private static final String DEADLINE_EXCEEDED_EXCEPTION = "com.google.apphosting.api.DeadlineExceededException";
+
/**
* @see HttpServlet#HttpServlet()
*/
@@ -180,10 +185,6 @@ public class ProxyServlet extends HttpServlet
+ ", referer=" + ((ref != null) ? ref : "[null]")
+ ", user agent=" + ((ua != null) ? ua : "[null]"));
}
- catch (DeadlineExceededException e)
- {
- response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
- }
catch (UnknownHostException | FileNotFoundException e)
{
// do not log 404 and DNS errors
@@ -207,13 +208,18 @@ public class ProxyServlet extends HttpServlet
{
response.setStatus(
HttpServletResponse.SC_BAD_REQUEST);
- log.log(Level.FINE, "proxy request failed: url="
- + ((urlParam != null) ? urlParam : "[null]")
- + ", referer=" + ((ref != null) ? ref : "[null]")
- + ", user agent=" + ((ua != null) ? ua : "[null]"));
- e.printStackTrace();
- throw e;
+ // Requests that run past the App Engine deadline end quietly
+ if (!e.getClass().getName().equals(DEADLINE_EXCEEDED_EXCEPTION))
+ {
+ log.log(Level.FINE, "proxy request failed: url="
+ + ((urlParam != null) ? urlParam : "[null]")
+ + ", referer=" + ((ref != null) ? ref : "[null]")
+ + ", user agent=" + ((ua != null) ? ua : "[null]"));
+ e.printStackTrace();
+
+ throw e;
+ }
}
}
else
diff --git a/src/main/server/java/com/mxgraph/online/SecretFacade.java b/src/main/server/java/com/mxgraph/online/SecretFacade.java
new file mode 100644
index 000000000..5ecd023d1
--- /dev/null
+++ b/src/main/server/java/com/mxgraph/online/SecretFacade.java
@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2020-2026, JGraph Holdings Ltd
+ * Copyright (c) 2020-2026, draw.io AG
+ */
+package com.mxgraph.online;
+
+import javax.servlet.ServletContext;
+
+/**
+ * Public build version of src/main/server/java/com/mxgraph/online/SecretFacade.java,
+ * which uses Secret Manager and is excluded from the public repository. Reads
+ * secrets from WEB-INF/<key>, where the Docker image writes them.
+ */
+public class SecretFacade
+{
+ private SecretFacade() { }
+
+ public static String getSecret(String key, ServletContext servletContext)
+ {
+ try
+ {
+ return Utils.readInputStream(servletContext
+ .getResourceAsStream("/WEB-INF/" + key))
+ .replaceAll("\n", "");
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Reading secret " + key + " failed.");
+ }
+ }
+}
diff --git a/src/main/webapp/WEB-INF/lib/gae-stub-1.0.9.jar b/src/main/webapp/WEB-INF/lib/gae-stub-1.0.9.jar
deleted file mode 100644
index fc8609962..000000000
Binary files a/src/main/webapp/WEB-INF/lib/gae-stub-1.0.9.jar and /dev/null differ