forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeploy.java
More file actions
executable file
·106 lines (93 loc) · 3.53 KB
/
Deploy.java
File metadata and controls
executable file
·106 lines (93 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* $Id: Deploy.java,v 1.7 2012/01/13 12:31:21 david Exp $
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.examples.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mxgraph.io.mxCodec;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
/**
* Demonstrates the deployment of a graph which is created on the server side
* and then deployed with the client library in a single response. This is done
* by replacing the %graph% placeholder in the javascript/example/template.html
* file with the XML representation of the graph that was created on the server.
*
* Point your browser to http://localhost:8080/graph to fetch the HTML file.
*
* This example returns an HTML page when the client issues a get request. The
* readme in the java directory explains how to run this example.
*
* The /javascript/examples/template.html file is used by this example. In
* doGet a graph is created and the XML of the graph obtained by:
*
* mxCodec codec = new mxCodec();
* String xml = mxUtils.getXml(codec.encode(graph.getModel()));
*
* The template.html is then loaded as a string and instances of %graph% are
* replaced with the XML of the graph. In the template.html the following line
* defines the page body:
*
* <body onload="main(document.getElementById('graphContainer'), '%graph%');">
*
* So the XML string of the graph becomes the second parameter of the main
* function. When the template.html page is loaded in the browser, the main
* function is called and within that function these lines:
*
* var doc = mxUtils.parseXml(xml);
* var codec = new mxCodec(doc);
* codec.decode(doc.documentElement, graph.getModel());
*
* insert the XML into the graph model and that graph will then display.
*/
public class Deploy extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = -1046635303777463670L;
protected String createGraph(HttpServletRequest request)
{
// Creates the graph on the server-side
mxCodec codec = new mxCodec();
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30);
Object v2 = graph.insertVertex(parent, null, "World", 200, 150, 80,
30);
graph.insertEdge(parent, null, "", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
// Turns the graph into XML data
return mxXmlUtils.getXml(codec.encode(graph.getModel()));
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// Loads the static HTML page with the placeholder from the template
String template = mxUtils.readFile("javascript/examples/template.html");
String xml = createGraph(request);
// Replaces the placeholder in the template with the XML data
// which is then parsed into the graph model on the client.
// In a production environment you should use a template engine.
String page = template.replaceAll("%graph%", mxUtils.htmlEntities(xml));
// Makes sure there is no caching on the client side
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setHeader("Cache-control", "private, no-cache, no-store");
response.setHeader("Expires", "0");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(page);
}
}