EmbedServlet2.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * $Id: EmbedServlet.java,v 1.18 2014/01/31 22:27:07 gaudenz Exp $
  3. * Copyright (c) 2011-2012, JGraph Ltd
  4. *
  5. * TODO
  6. *
  7. * We could split the static part and the stencils into two separate requests
  8. * in order for multiple graphs in the pages to not load the static part
  9. * multiple times. This is only relevant if the embed arguments are different,
  10. * in which case there is a problem with parsin the graph model too soon, ie.
  11. * before certain stencils become available.
  12. *
  13. * Easier solution is for the user to move the embed script to after the last
  14. * graph in the page and merge the stencil arguments.
  15. *
  16. * Note: The static part is roundly 105K, the stencils are much smaller in size.
  17. * This means if the embed function is widely used, it will make sense to factor
  18. * out the static part because only stencils will change between pages.
  19. */
  20. package com.mxgraph.online;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.PrintWriter;
  26. import java.io.File;
  27. import java.net.HttpURLConnection;
  28. import java.net.URL;
  29. import java.net.URLConnection;
  30. import java.text.DateFormat;
  31. import java.text.SimpleDateFormat;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.HashSet;
  35. import java.util.Locale;
  36. import javax.servlet.ServletException;
  37. import javax.servlet.http.HttpServlet;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import org.apache.commons.text.StringEscapeUtils;
  41. import com.google.appengine.api.utils.SystemProperty;
  42. /**
  43. * Servlet implementation class OpenServlet
  44. */
  45. public class EmbedServlet2 extends HttpServlet
  46. {
  47. /**
  48. *
  49. */
  50. private static final long serialVersionUID = 1L;
  51. /**
  52. *
  53. */
  54. protected static String SHAPES_PATH = "/shapes";
  55. /**
  56. *
  57. */
  58. protected static String STENCIL_PATH = "/stencils";
  59. /**
  60. *
  61. */
  62. protected static String lastModified = null;
  63. /**
  64. * Max fetch size
  65. */
  66. protected static int MAX_FETCH_SIZE = 50 * 1024 * 1024; // 50 MB
  67. /**
  68. *
  69. */
  70. protected HashMap<String, String> stencils = new HashMap<String, String>();
  71. /**
  72. *
  73. */
  74. protected HashMap<String, String[]> libraries = new HashMap<String, String[]>();
  75. /**
  76. * @see HttpServlet#HttpServlet()
  77. */
  78. public EmbedServlet2()
  79. {
  80. if (lastModified == null)
  81. {
  82. // Uses deployment date as lastModified header
  83. String applicationVersion = SystemProperty.applicationVersion.get();
  84. Date uploadDate = new Date(Long
  85. .parseLong(applicationVersion
  86. .substring(applicationVersion.lastIndexOf(".") + 1))
  87. / (2 << 27) * 1000);
  88. DateFormat httpDateFormat = new SimpleDateFormat(
  89. "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
  90. lastModified = httpDateFormat.format(uploadDate);
  91. }
  92. initLibraries(libraries);
  93. }
  94. /**
  95. * Sets up collection of stencils
  96. */
  97. public static void initLibraries(HashMap<String, String[]> libraries)
  98. {
  99. libraries.put("mockup",
  100. new String[] { SHAPES_PATH + "/mockup/mxMockupButtons.js" });
  101. libraries.put("arrows2", new String[] { SHAPES_PATH + "/mxArrows.js" });
  102. libraries.put("bpmn",
  103. new String[] { SHAPES_PATH + "/bpmn/mxBpmnShape2.js",
  104. STENCIL_PATH + "/bpmn.xml" });
  105. libraries.put("er", new String[] { SHAPES_PATH + "/er/mxER.js" });
  106. libraries.put("ios",
  107. new String[] { SHAPES_PATH + "/mockup/mxMockupiOS.js" });
  108. libraries.put("rackGeneral",
  109. new String[] { SHAPES_PATH + "/rack/mxRack.js",
  110. STENCIL_PATH + "/rack/general.xml" });
  111. libraries.put("rackF5", new String[] { STENCIL_PATH + "/rack/f5.xml" });
  112. libraries.put("lean_mapping",
  113. new String[] { SHAPES_PATH + "/mxLeanMap.js",
  114. STENCIL_PATH + "/lean_mapping.xml" });
  115. libraries.put("basic", new String[] { SHAPES_PATH + "/mxBasic.js",
  116. STENCIL_PATH + "/basic.xml" });
  117. libraries.put("ios7icons",
  118. new String[] { STENCIL_PATH + "/ios7/icons.xml" });
  119. libraries.put("ios7ui",
  120. new String[] { SHAPES_PATH + "/ios7/mxIOS7Ui.js",
  121. STENCIL_PATH + "/ios7/misc.xml" });
  122. libraries.put("android", new String[] { SHAPES_PATH + "/mxAndroid.js",
  123. STENCIL_PATH + "electrical/transmission" });
  124. libraries.put("electrical/transmission",
  125. new String[] { SHAPES_PATH + "/mxElectrical.js",
  126. STENCIL_PATH + "/electrical/transmission.xml" });
  127. libraries.put("mockup/buttons",
  128. new String[] { SHAPES_PATH + "/mockup/mxMockupButtons.js" });
  129. libraries.put("mockup/containers",
  130. new String[] { SHAPES_PATH + "/mockup/mxMockupContainers.js" });
  131. libraries.put("mockup/forms",
  132. new String[] { SHAPES_PATH + "/mockup/mxMockupForms.js" });
  133. libraries.put("mockup/graphics",
  134. new String[] { SHAPES_PATH + "/mockup/mxMockupGraphics.js",
  135. STENCIL_PATH + "/mockup/misc.xml" });
  136. libraries.put("mockup/markup",
  137. new String[] { SHAPES_PATH + "/mockup/mxMockupMarkup.js" });
  138. libraries.put("mockup/misc",
  139. new String[] { SHAPES_PATH + "/mockup/mxMockupMisc.js",
  140. STENCIL_PATH + "/mockup/misc.xml" });
  141. libraries.put("mockup/navigation",
  142. new String[] { SHAPES_PATH + "/mockup/mxMockupNavigation.js",
  143. STENCIL_PATH + "/mockup/misc.xml" });
  144. libraries.put("mockup/text",
  145. new String[] { SHAPES_PATH + "/mockup/mxMockupText.js" });
  146. libraries.put("floorplan",
  147. new String[] { SHAPES_PATH + "/mxFloorplan.js",
  148. STENCIL_PATH + "/floorplan.xml" });
  149. libraries.put("bootstrap",
  150. new String[] { SHAPES_PATH + "/mxBootstrap.js",
  151. STENCIL_PATH + "/bootstrap.xml" });
  152. libraries.put("gmdl", new String[] { SHAPES_PATH + "/mxGmdl.js",
  153. STENCIL_PATH + "/gmdl.xml" });
  154. libraries.put("cabinets", new String[] { SHAPES_PATH + "/mxCabinets.js",
  155. STENCIL_PATH + "/cabinets.xml" });
  156. libraries.put("archimate",
  157. new String[] { SHAPES_PATH + "/mxArchiMate.js" });
  158. libraries.put("archimate3",
  159. new String[] { SHAPES_PATH + "/mxArchiMate3.js" });
  160. libraries.put("sysml", new String[] { SHAPES_PATH + "/mxSysML.js" });
  161. libraries.put("eip", new String[] { SHAPES_PATH + "/mxEip.js",
  162. STENCIL_PATH + "/eip.xml" });
  163. libraries.put("networks", new String[] { SHAPES_PATH + "/mxNetworks.js",
  164. STENCIL_PATH + "/networks.xml" });
  165. libraries.put("aws3d", new String[] { SHAPES_PATH + "/mxAWS3D.js",
  166. STENCIL_PATH + "/aws3d.xml" });
  167. libraries.put("pid2inst",
  168. new String[] { SHAPES_PATH + "/pid2/mxPidInstruments.js" });
  169. libraries.put("pid2misc",
  170. new String[] { SHAPES_PATH + "/pid2/mxPidMisc.js",
  171. STENCIL_PATH + "/pid/misc.xml" });
  172. libraries.put("pid2valves",
  173. new String[] { SHAPES_PATH + "/pid2/mxPidValves.js" });
  174. libraries.put("pidFlowSensors",
  175. new String[] { STENCIL_PATH + "/pid/flow_sensors.xml" });
  176. }
  177. /**
  178. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  179. */
  180. protected void doGet(HttpServletRequest request,
  181. HttpServletResponse response) throws ServletException, IOException
  182. {
  183. try
  184. {
  185. String qs = request.getQueryString();
  186. if (qs != null && qs.equals("stats"))
  187. {
  188. writeStats(response);
  189. }
  190. else
  191. {
  192. // Checks or sets last modified date of delivered content.
  193. // Date comparison not needed. Only return 304 if
  194. // delivered by this servlet instance.
  195. String modSince = request.getHeader("If-Modified-Since");
  196. if (modSince != null && modSince.equals(lastModified)
  197. && request.getParameter("fetch") == null)
  198. {
  199. response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
  200. }
  201. else
  202. {
  203. writeEmbedResponse(request, response);
  204. }
  205. }
  206. }
  207. catch (Exception e)
  208. {
  209. response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
  210. }
  211. }
  212. public void writeEmbedResponse(HttpServletRequest request,
  213. HttpServletResponse response) throws IOException
  214. {
  215. response.setCharacterEncoding("UTF-8");
  216. response.setContentType("application/javascript; charset=UTF-8");
  217. response.setHeader("Last-Modified", lastModified);
  218. if (request.getParameter("fetch") != null)
  219. {
  220. response.setHeader("Cache-Control", "no-store");
  221. }
  222. OutputStream out = response.getOutputStream();
  223. // Creates XML for stencils
  224. PrintWriter writer = new PrintWriter(out);
  225. // Writes JavaScript and adds function call with
  226. // stylesheet and stencils as arguments
  227. writer.println(createEmbedJavaScript(request));
  228. response.setStatus(HttpServletResponse.SC_OK);
  229. writer.flush();
  230. writer.close();
  231. }
  232. public String createEmbedJavaScript(HttpServletRequest request)
  233. throws IOException
  234. {
  235. String sparam = request.getParameter("s");
  236. String dev = request.getParameter("dev");
  237. StringBuffer result = new StringBuffer("[");
  238. StringBuffer js = new StringBuffer("");
  239. // Processes each stencil only once
  240. HashSet<String> done = new HashSet<String>();
  241. // Processes each lib only once
  242. HashSet<String> libsLoaded = new HashSet<String>();
  243. if (sparam != null)
  244. {
  245. String[] names = sparam.split(";");
  246. for (int i = 0; i < names.length; i++)
  247. {
  248. if (names[i].indexOf("..") < 0 && !done.contains(names[i]) && names[i].length() > 0)
  249. {
  250. if (names[i].equals("*"))
  251. {
  252. js.append(readXmlFile("/js/shapes-14-6-5.min.js", false));
  253. result.append(
  254. "'" + readXmlFile("/stencils.xml", true) + "'");
  255. }
  256. else
  257. {
  258. // Makes name canonical
  259. names[i] = new File("/" + names[i]).getCanonicalPath().substring(1);
  260. // Checks if any JS files are associated with the library
  261. // name and injects the JS into the page
  262. String[] libs = libraries.get(names[i]);
  263. if (libs != null)
  264. {
  265. for (int j = 0; j < libs.length; j++)
  266. {
  267. if (!libsLoaded.contains(libs[j]))
  268. {
  269. String tmp = stencils.get(libs[j]);
  270. libsLoaded.add(libs[j]);
  271. if (tmp == null)
  272. {
  273. try
  274. {
  275. tmp = readXmlFile(libs[j],
  276. !libs[j].toLowerCase()
  277. .endsWith(".js"));
  278. // Cache for later use
  279. if (tmp != null)
  280. {
  281. stencils.put(libs[j], tmp);
  282. }
  283. }
  284. catch (NullPointerException e)
  285. {
  286. // This seems possible according to access log so ignore stencil
  287. }
  288. }
  289. if (tmp != null)
  290. {
  291. // TODO: Add JS to Javascript code inline. This had to be done to quickly
  292. // add JS-based dynamic loading to the existing embed setup where everything
  293. // dynamic is passed via function call, so an indirection via eval must be
  294. // used even though the JS could be parsed directly by adding it to JS.
  295. if (libs[j].toLowerCase()
  296. .endsWith(".js"))
  297. {
  298. js.append(tmp);
  299. }
  300. else
  301. {
  302. if (result.length() > 1)
  303. {
  304. result.append(",");
  305. }
  306. result.append("'" + tmp + "'");
  307. }
  308. }
  309. }
  310. }
  311. }
  312. else
  313. {
  314. String tmp = stencils.get(names[i]);
  315. if (tmp == null)
  316. {
  317. try
  318. {
  319. tmp = readXmlFile(
  320. "/stencils/" + names[i] + ".xml",
  321. true);
  322. // Cache for later use
  323. if (tmp != null)
  324. {
  325. stencils.put(names[i], tmp);
  326. }
  327. }
  328. catch (NullPointerException e)
  329. {
  330. // This seems possible according to access log so ignore stencil
  331. }
  332. }
  333. if (tmp != null)
  334. {
  335. if (result.length() > 1)
  336. {
  337. result.append(",");
  338. }
  339. result.append("'" + tmp + "'");
  340. }
  341. }
  342. }
  343. done.add(names[i]);
  344. }
  345. }
  346. }
  347. result.append("]");
  348. // LATER: Detect protocol of request in dev
  349. // mode to avoid security errors
  350. String proto = "https://";
  351. String setCachedUrls = "";
  352. String[] urls = request.getParameterValues("fetch");
  353. if (urls != null)
  354. {
  355. HashSet<String> completed = new HashSet<String>();
  356. int sizeLimit = MAX_FETCH_SIZE;
  357. for (int i = 0; i < urls.length; i++)
  358. {
  359. try
  360. {
  361. // Checks if URL already fetched to avoid duplicates
  362. if (!completed.contains(urls[i]) && Utils.sanitizeUrl(urls[i]))
  363. {
  364. completed.add(urls[i]);
  365. URL url = new URL(urls[i]);
  366. URLConnection connection = url.openConnection();
  367. ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
  368. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  369. String contentLength = connection.getHeaderField("Content-Length");
  370. // If content length is available, use it to enforce maximum size
  371. if (contentLength != null && Long.parseLong(contentLength) > sizeLimit)
  372. {
  373. break;
  374. }
  375. sizeLimit -= Utils.copyRestricted(connection.getInputStream(), stream, sizeLimit);
  376. setCachedUrls += "GraphViewer.cachedUrls['"
  377. + StringEscapeUtils.escapeEcmaScript(urls[i])
  378. + "'] = decodeURIComponent('"
  379. + StringEscapeUtils.escapeEcmaScript(
  380. Utils.encodeURIComponent(
  381. stream.toString("UTF-8"),
  382. Utils.CHARSET_FOR_URL_ENCODING))
  383. + "');";
  384. }
  385. }
  386. catch (Exception e)
  387. {
  388. // ignore
  389. }
  390. }
  391. }
  392. // Installs a callback to load the stencils after the viewer was injected
  393. return "window.onDrawioViewerLoad = function() {" + setCachedUrls
  394. + "mxStencilRegistry.parseStencilSets(" + result.toString()
  395. + ");" + js + "GraphViewer.processElements(); };"
  396. + "var t = document.getElementsByTagName('script');"
  397. + "if (t != null && t.length > 0) {"
  398. + "var script = document.createElement('script');"
  399. + "script.type = 'text/javascript';" + "script.src = '" + proto
  400. + ((dev != null && dev.equals("1")) ? "test" : "www")
  401. + ".draw.io/js/viewer-static.min.js';"
  402. + "t[0].parentNode.appendChild(script);}";
  403. }
  404. public void writeStats(HttpServletResponse response) throws IOException
  405. {
  406. PrintWriter writer = new PrintWriter(response.getOutputStream());
  407. writer.println("<html>");
  408. writer.println("<body>");
  409. writer.println("Deployed: " + lastModified);
  410. writer.println("</body>");
  411. writer.println("</html>");
  412. writer.flush();
  413. }
  414. public String readXmlFile(String filename, boolean xmlContent)
  415. throws IOException
  416. {
  417. String result = readFile(filename);
  418. if (xmlContent)
  419. {
  420. result = result.replaceAll("'", "\\\\'").replaceAll("\t", "")
  421. .replaceAll("\n", "");
  422. }
  423. return result;
  424. }
  425. public String readFile(String filename) throws IOException
  426. {
  427. InputStream is = getServletContext().getResourceAsStream(filename);
  428. return Utils.readInputStream(is);
  429. }
  430. }