forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
92 lines (86 loc) · 2.97 KB
/
Copy pathApp.java
File metadata and controls
92 lines (86 loc) · 2.97 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
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.cloudrun;
import static spark.Spark.get;
import static spark.Spark.port;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) {
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
port(port);
// [START run_system_package_handler]
get(
"/diagram.png",
(req, res) -> {
InputStream image = null;
try {
String dot = req.queryParams("dot");
image = createDiagram(dot);
res.header("Content-Type", "image/png");
res.header("Content-Length", Integer.toString(image.available()));
res.header("Cache-Control", "public, max-age=86400");
} catch (Exception e) {
if (e.getMessage().contains("syntax")) {
res.status(400);
return String.format("Bad Request: %s", e.getMessage());
} else {
res.status(500);
return "Internal Server Error";
}
}
return image;
});
// [END run_system_package_handler]
}
// [START run_system_package_exec]
// Generate a diagram based on a graphviz DOT diagram description.
public static InputStream createDiagram(String dot) {
if (dot == null || dot.isEmpty()) {
throw new NullPointerException("syntax: no graphviz definition provided");
}
// Adds a watermark to the dot graphic.
List<String> args = new ArrayList<String>();
args.add("/usr/bin/dot");
args.add("-Glabel=\"Made on Cloud Run\"");
args.add("-Gfontsize=10");
args.add("-Glabeljust=right");
args.add("-Glabelloc=bottom");
args.add("-Gfontcolor=gray");
args.add("-Tpng");
StringBuilder output = new StringBuilder();
InputStream stdout = null;
try {
ProcessBuilder pb = new ProcessBuilder(args);
Process process = pb.start();
OutputStream stdin = process.getOutputStream();
stdout = process.getInputStream();
// The Graphviz dot program reads from stdin.
Writer writer = new OutputStreamWriter(stdin, "UTF-8");
writer.write(dot);
writer.close();
process.waitFor();
} catch (Exception e) {
System.out.println(e);
}
return stdout;
}
// [END run_system_package_exec]
}