Skip to content

Commit bfe63de

Browse files
committed
Move ShowCFGCommand to magpiebridge package
1 parent aea07c9 commit bfe63de

File tree

2 files changed

+98
-98
lines changed

2 files changed

+98
-98
lines changed

src/main/java/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import abstractdebugging.AbstractDebuggingServerLauncher;
33
import abstractdebugging.ResultsService;
44
import analysis.GoblintAnalysis;
5-
import analysis.ShowCFGCommand;
5+
import magpiebridge.ShowCFGCommand;
66
import api.GoblintService;
77
import api.GoblintServiceLauncher;
88
import api.messages.params.Params;
Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
1-
package analysis;
2-
3-
import com.google.common.io.CharStreams;
4-
import com.google.gson.JsonPrimitive;
5-
import magpiebridge.core.MagpieClient;
6-
import magpiebridge.core.MagpieServer;
7-
import magpiebridge.core.WorkspaceCommand;
8-
import org.apache.http.HttpResponse;
9-
import org.apache.http.client.methods.HttpPost;
10-
import org.apache.http.entity.ContentType;
11-
import org.apache.http.entity.StringEntity;
12-
import org.apache.http.impl.client.CloseableHttpClient;
13-
import org.apache.http.impl.client.HttpClients;
14-
import org.apache.logging.log4j.LogManager;
15-
import org.apache.logging.log4j.Logger;
16-
import org.eclipse.lsp4j.ExecuteCommandParams;
17-
import org.eclipse.lsp4j.services.LanguageClient;
18-
19-
import java.io.IOException;
20-
import java.io.InputStreamReader;
21-
import java.nio.charset.StandardCharsets;
22-
23-
public class ShowCFGCommand implements WorkspaceCommand {
24-
25-
private final String httpServerAddress;
26-
private final Logger log = LogManager.getLogger(ShowCFGCommand.class);
27-
28-
public ShowCFGCommand(String httpServerAddress) {
29-
this.httpServerAddress = httpServerAddress;
30-
}
31-
32-
@Override
33-
public void execute(ExecuteCommandParams params, MagpieServer server, LanguageClient client) {
34-
try {
35-
String funName;
36-
Object uriJson = params.getArguments().get(0);
37-
if (uriJson instanceof JsonPrimitive) {
38-
funName = ((JsonPrimitive) uriJson).getAsString();
39-
} else {
40-
funName = (String) uriJson;
41-
}
42-
showHTMLinClientOrBrowser(server, client, funName);
43-
} catch (IOException e) {
44-
// Note: Logging the exception using log.error("Error:", e) causes JSON-RPC communication with VS Code to break.
45-
// This is caused by a warning printed to stdout by log4j. See https://github.com/apache/logging-log4j2/issues/1484 for more info.
46-
log.error("Error running showcfg command:");
47-
e.printStackTrace();
48-
}
49-
}
50-
51-
52-
/**
53-
* Show A HTML page with the given CFG in the client, or in a browser if the client doesn't support this.
54-
*
55-
* @param server The MagpieServer
56-
* @param client The IDE/Editor
57-
* @param funName The name of the function to show the CFG for
58-
* @throws IOException IO exception
59-
*/
60-
61-
public void showHTMLinClientOrBrowser(MagpieServer server, LanguageClient client, String funName) throws IOException {
62-
if (server.clientSupportShowHTML()) {
63-
if (client instanceof MagpieClient) {
64-
String json = "{\"funName\": \"" + funName + "\"}";
65-
String content = httpPostJson(httpServerAddress + "cfg/", json);
66-
((MagpieClient) client).showHTML(content);
67-
}
68-
} /*else {
69-
// TODO: Not tested if this works, probably not?
70-
if (Desktop.isDesktopSupported())
71-
Desktop.getDesktop().browse(new URI(URIUtils.checkURI(cfg)));
72-
}*/
73-
}
74-
75-
76-
/**
77-
* Sends the request to get the HTML for visualizing the CFG.
78-
*
79-
* @param url The HTTPServer url to post the request to.
80-
* @param json The request body as a JSON.
81-
* @return The response body as a string.
82-
* @throws IOException in case of a problem or the connection was aborted.
83-
*/
84-
85-
public static String httpPostJson(String url, String json) throws IOException {
86-
CloseableHttpClient httpClient = HttpClients.createDefault();
87-
HttpPost httpPost = new HttpPost(url);
88-
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
89-
HttpResponse response = httpClient.execute(httpPost);
90-
String responseBody = CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
91-
httpClient.close();
92-
return responseBody;
93-
}
94-
95-
96-
}
97-
1+
package magpiebridge;
2+
3+
import com.google.common.io.CharStreams;
4+
import com.google.gson.JsonPrimitive;
5+
import magpiebridge.core.MagpieClient;
6+
import magpiebridge.core.MagpieServer;
7+
import magpiebridge.core.WorkspaceCommand;
8+
import org.apache.http.HttpResponse;
9+
import org.apache.http.client.methods.HttpPost;
10+
import org.apache.http.entity.ContentType;
11+
import org.apache.http.entity.StringEntity;
12+
import org.apache.http.impl.client.CloseableHttpClient;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.logging.log4j.LogManager;
15+
import org.apache.logging.log4j.Logger;
16+
import org.eclipse.lsp4j.ExecuteCommandParams;
17+
import org.eclipse.lsp4j.services.LanguageClient;
18+
19+
import java.io.IOException;
20+
import java.io.InputStreamReader;
21+
import java.nio.charset.StandardCharsets;
22+
23+
public class ShowCFGCommand implements WorkspaceCommand {
24+
25+
private final String httpServerAddress;
26+
private final Logger log = LogManager.getLogger(ShowCFGCommand.class);
27+
28+
public ShowCFGCommand(String httpServerAddress) {
29+
this.httpServerAddress = httpServerAddress;
30+
}
31+
32+
@Override
33+
public void execute(ExecuteCommandParams params, MagpieServer server, LanguageClient client) {
34+
try {
35+
String funName;
36+
Object uriJson = params.getArguments().get(0);
37+
if (uriJson instanceof JsonPrimitive) {
38+
funName = ((JsonPrimitive) uriJson).getAsString();
39+
} else {
40+
funName = (String) uriJson;
41+
}
42+
showHTMLinClientOrBrowser(server, client, funName);
43+
} catch (IOException e) {
44+
// Note: Logging the exception using log.error("Error:", e) causes JSON-RPC communication with VS Code to break.
45+
// This is caused by a warning printed to stdout by log4j. See https://github.com/apache/logging-log4j2/issues/1484 for more info.
46+
log.error("Error running showcfg command:");
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
52+
/**
53+
* Show A HTML page with the given CFG in the client, or in a browser if the client doesn't support this.
54+
*
55+
* @param server The MagpieServer
56+
* @param client The IDE/Editor
57+
* @param funName The name of the function to show the CFG for
58+
* @throws IOException IO exception
59+
*/
60+
61+
public void showHTMLinClientOrBrowser(MagpieServer server, LanguageClient client, String funName) throws IOException {
62+
if (server.clientSupportShowHTML()) {
63+
if (client instanceof MagpieClient) {
64+
String json = "{\"funName\": \"" + funName + "\"}";
65+
String content = httpPostJson(httpServerAddress + "cfg/", json);
66+
((MagpieClient) client).showHTML(content);
67+
}
68+
} /*else {
69+
// TODO: Not tested if this works, probably not?
70+
if (Desktop.isDesktopSupported())
71+
Desktop.getDesktop().browse(new URI(URIUtils.checkURI(cfg)));
72+
}*/
73+
}
74+
75+
76+
/**
77+
* Sends the request to get the HTML for visualizing the CFG.
78+
*
79+
* @param url The HTTPServer url to post the request to.
80+
* @param json The request body as a JSON.
81+
* @return The response body as a string.
82+
* @throws IOException in case of a problem or the connection was aborted.
83+
*/
84+
85+
public static String httpPostJson(String url, String json) throws IOException {
86+
CloseableHttpClient httpClient = HttpClients.createDefault();
87+
HttpPost httpPost = new HttpPost(url);
88+
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
89+
HttpResponse response = httpClient.execute(httpPost);
90+
String responseBody = CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
91+
httpClient.close();
92+
return responseBody;
93+
}
94+
95+
96+
}
97+

0 commit comments

Comments
 (0)