EmbedServlet2.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.io.PrintWriter;
  25. import java.text.DateFormat;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.HashSet;
  30. import java.util.Locale;
  31. import javax.servlet.ServletException;
  32. import javax.servlet.http.HttpServlet;
  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.servlet.http.HttpServletResponse;
  35. import com.google.appengine.api.utils.SystemProperty;
  36. /**
  37. * Servlet implementation class OpenServlet
  38. */
  39. public class EmbedServlet2 extends HttpServlet
  40. {
  41. /**
  42. *
  43. */
  44. private static final long serialVersionUID = 1L;
  45. /**
  46. *
  47. */
  48. protected static String lastModified = null;
  49. /**
  50. *
  51. */
  52. protected HashMap<String, String> stencils = new HashMap<String, String>();
  53. /**
  54. *
  55. */
  56. protected HashMap<String, String[]> libraries = new HashMap<String, String[]>();
  57. /**
  58. * @see HttpServlet#HttpServlet()
  59. */
  60. public EmbedServlet2()
  61. {
  62. if (lastModified == null)
  63. {
  64. // Uses deployment date as lastModified header
  65. String applicationVersion = SystemProperty.applicationVersion.get();
  66. Date uploadDate = new Date(Long
  67. .parseLong(applicationVersion
  68. .substring(applicationVersion.lastIndexOf(".") + 1))
  69. / (2 << 27) * 1000);
  70. DateFormat httpDateFormat = new SimpleDateFormat(
  71. "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
  72. lastModified = httpDateFormat.format(uploadDate);
  73. }
  74. initLibraries(libraries);
  75. }
  76. /**
  77. * @see HttpServlet#HttpServlet()
  78. */
  79. public static void initLibraries(HashMap<String, String[]> libraries)
  80. {
  81. libraries.put("bpmn", new String[] { "/shapes/bpmn/mxBpmnShape2.js",
  82. "/stencils/bpmn.xml" });
  83. libraries.put("er", new String[] { "/shapes/er/mxER.js" });
  84. libraries.put("ios", new String[] { "/shapes/mockup/mxMockupiOS.js" });
  85. libraries.put("ios7", new String[] { "/stencils/ios7.xml" });
  86. libraries.put("android", new String[] { "/shapes/mxAndroid.js",
  87. "/stencils/android/android.xml" });
  88. libraries.put("lean_mapping", new String[] { "/shapes/mxLeanMap.js",
  89. "/stencils/lean_mapping.xml" });
  90. // Required for anchor shape which follows non-standard naming scheme (see Sidebar.js)
  91. libraries.put("mockup",
  92. new String[] { "/shapes/mockup/mxMockupButtons.js" });
  93. libraries.put("mockup/buttons",
  94. new String[] { "/shapes/mockup/mxMockupButtons.js" });
  95. libraries.put("mockup/containers",
  96. new String[] { "/shapes/mockup/mxMockupContainers.js" });
  97. libraries.put("mockup/forms",
  98. new String[] { "/shapes/mockup/mxMockupForms.js" });
  99. libraries.put("mockup/graphics", new String[] {
  100. "/shapes/mockup/mxMockupGraphics.js",
  101. "/stencils/mockup/misc.xml" });
  102. libraries.put("mockup/markup",
  103. new String[] { "/shapes/mockup/mxMockupMarkup.js" });
  104. libraries
  105. .put("mockup/misc", new String[] {
  106. "/shapes/mockup/mxMockupMisc.js",
  107. "/stencils/mockup/misc.xml" });
  108. libraries.put("mockup/navigation", new String[] {
  109. "/shapes/mockup/mxMockupNavigation.js",
  110. "/stencils/mockup/misc.xml" });
  111. libraries.put("mockup/text",
  112. new String[] { "/shapes/mockup/mxMockupText.js" });
  113. libraries.put("pid2inst",
  114. new String[] { "/shapes/pid2/mxPidInstruments.js" });
  115. libraries.put("pid2misc", new String[] { "/shapes/pid2/mxPidMisc.js",
  116. "/stencils/pid/misc.xml" });
  117. libraries.put("pid2valves",
  118. new String[] { "/shapes/pid2/mxPidValves.js" });
  119. libraries.put("floorplan", new String[] { "/shapes/mxFloorplan.js",
  120. "/stencils/floorplan.xml" });
  121. libraries.put("archimate", new String[] { "/shapes/mxArchiMate.js" });
  122. libraries.put("azure", new String[] { "/stencils/azure.xml" });
  123. }
  124. /**
  125. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  126. */
  127. protected void doGet(HttpServletRequest request,
  128. HttpServletResponse response) throws ServletException, IOException
  129. {
  130. try
  131. {
  132. String qs = request.getQueryString();
  133. if (qs != null && qs.equals("stats"))
  134. {
  135. writeStats(response);
  136. }
  137. else
  138. {
  139. // Checks or sets last modified date of delivered content.
  140. // Date comparison not needed. Only return 304 if
  141. // delivered by this servlet instance.
  142. String modSince = request.getHeader("If-Modified-Since");
  143. if (modSince != null && modSince.equals(lastModified))
  144. {
  145. response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
  146. }
  147. else
  148. {
  149. writeEmbedResponse(request, response);
  150. }
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
  156. }
  157. }
  158. public void writeEmbedResponse(HttpServletRequest request,
  159. HttpServletResponse response) throws IOException
  160. {
  161. response.setCharacterEncoding("UTF-8");
  162. response.setContentType("application/javascript; charset=UTF-8");
  163. response.setHeader("Last-Modified", lastModified);
  164. OutputStream out = response.getOutputStream();
  165. // Creates XML for stencils
  166. PrintWriter writer = new PrintWriter(out);
  167. // Writes JavaScript and adds function call with
  168. // stylesheet and stencils as arguments
  169. writer.println(createEmbedJavaScript(request));
  170. response.setStatus(HttpServletResponse.SC_OK);
  171. writer.flush();
  172. writer.close();
  173. }
  174. public String createEmbedJavaScript(HttpServletRequest request)
  175. throws IOException
  176. {
  177. String sparam = request.getParameter("s");
  178. String dev = request.getParameter("dev");
  179. StringBuffer result = new StringBuffer("[");
  180. StringBuffer js = new StringBuffer("");
  181. // Processes each stencil only once
  182. HashSet<String> done = new HashSet<String>();
  183. // Processes each lib only once
  184. HashSet<String> libsLoaded = new HashSet<String>();
  185. if (sparam != null)
  186. {
  187. String[] names = sparam.split(";");
  188. for (int i = 0; i < names.length; i++)
  189. {
  190. if (names[i].indexOf("..") < 0 && !done.contains(names[i]))
  191. {
  192. if (names[i].equals("*"))
  193. {
  194. js.append(readXmlFile("/js/shapes.min.js", false));
  195. result.append(
  196. "'" + readXmlFile("/stencils.xml", true) + "'");
  197. }
  198. else
  199. {
  200. // Checks if any JS files are associated with the library
  201. // name and injects the JS into the page
  202. String[] libs = libraries.get(names[i]);
  203. if (libs != null)
  204. {
  205. for (int j = 0; j < libs.length; j++)
  206. {
  207. if (!libsLoaded.contains(libs[j]))
  208. {
  209. String tmp = stencils.get(libs[j]);
  210. libsLoaded.add(libs[j]);
  211. if (tmp == null)
  212. {
  213. try
  214. {
  215. tmp = readXmlFile(libs[j],
  216. !libs[j].toLowerCase()
  217. .endsWith(".js"));
  218. // Cache for later use
  219. if (tmp != null)
  220. {
  221. stencils.put(libs[j], tmp);
  222. }
  223. }
  224. catch (NullPointerException e)
  225. {
  226. // This seems possible according to access log so ignore stencil
  227. }
  228. }
  229. if (tmp != null)
  230. {
  231. // TODO: Add JS to Javascript code inline. This had to be done to quickly
  232. // add JS-based dynamic loading to the existing embed setup where everything
  233. // dynamic is passed via function call, so an indirection via eval must be
  234. // used even though the JS could be parsed directly by adding it to JS.
  235. if (libs[j].toLowerCase()
  236. .endsWith(".js"))
  237. {
  238. js.append(tmp);
  239. }
  240. else
  241. {
  242. if (result.length() > 1)
  243. {
  244. result.append(",");
  245. }
  246. result.append("'" + tmp + "'");
  247. }
  248. }
  249. }
  250. }
  251. }
  252. else
  253. {
  254. String tmp = stencils.get(names[i]);
  255. if (tmp == null)
  256. {
  257. try
  258. {
  259. tmp = readXmlFile(
  260. "/stencils/" + names[i] + ".xml",
  261. true);
  262. // Cache for later use
  263. if (tmp != null)
  264. {
  265. stencils.put(names[i], tmp);
  266. }
  267. }
  268. catch (NullPointerException e)
  269. {
  270. // This seems possible according to access log so ignore stencil
  271. }
  272. }
  273. if (tmp != null)
  274. {
  275. if (result.length() > 1)
  276. {
  277. result.append(",");
  278. }
  279. result.append("'" + tmp + "'");
  280. }
  281. }
  282. }
  283. done.add(names[i]);
  284. }
  285. }
  286. }
  287. result.append("]");
  288. // LATER: Detect protocol of request in dev
  289. // mode to avoid security errors
  290. String proto = "https://";
  291. // Installs a callback to load the stencils after the viewer was injected
  292. return "window.onDrawioViewerLoad = function() {"
  293. + "mxStencilRegistry.parseStencilSets(" + result.toString() + ");"
  294. + js
  295. + "GraphViewer.processElements(); };"
  296. + "var t = document.getElementsByTagName('script');"
  297. + "if (t != null && t.length > 0) {"
  298. + "var script = document.createElement('script');"
  299. + "script.type = 'text/javascript';" + "script.src = '" + proto
  300. + ((dev != null && dev.equals("1")) ? "test" : "www")
  301. + ".draw.io/js/viewer.min.js';"
  302. + "t[0].parentNode.appendChild(script);}";
  303. }
  304. public void writeStats(HttpServletResponse response) throws IOException
  305. {
  306. PrintWriter writer = new PrintWriter(response.getOutputStream());
  307. writer.println("<html>");
  308. writer.println("<body>");
  309. writer.println("Deployed: " + lastModified);
  310. writer.println("</body>");
  311. writer.println("</html>");
  312. writer.flush();
  313. }
  314. public String readXmlFile(String filename, boolean xmlContent)
  315. throws IOException
  316. {
  317. String result = readFile(filename);
  318. if (xmlContent)
  319. {
  320. result = result.replaceAll("'", "\\\\'").replaceAll("\t", "")
  321. .replaceAll("\n", "");
  322. }
  323. return result;
  324. }
  325. public String readFile(String filename) throws IOException
  326. {
  327. InputStream is = getServletContext().getResourceAsStream(filename);
  328. return Utils.readInputStream(is);
  329. }
  330. }