Skip to content

Commit

Permalink
ConsoleEngineImpl doc command: check that the page exists before laun…
Browse files Browse the repository at this point in the history
…ching browser
  • Loading branch information
mattirn committed Oct 22, 2020
1 parent 5d4d46b commit ea98b90
Showing 1 changed file with 60 additions and 34 deletions.
94 changes: 60 additions & 34 deletions console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
Expand Down Expand Up @@ -739,7 +741,7 @@ private boolean consoleOption(String option) {

@Override
public ExecutionResult postProcess(String line, Object result, String output) {
ExecutionResult out = new ExecutionResult(1, null);
ExecutionResult out;
Object _output = output != null && !output.trim().isEmpty() && !consoleOption("no-splittedOutput")
? output.split("\\r?\\n") : output;
String consoleVar = parser().getVariable(line);
Expand All @@ -758,7 +760,7 @@ public ExecutionResult postProcess(String line, Object result, String output) {

private ExecutionResult postProcess(String line, Object result) {
int status = 0;
Object out = result != null && result instanceof String && ((String)result).trim().isEmpty() ? null : result;
Object out = result instanceof String && ((String)result).trim().isEmpty() ? null : result;
String consoleVar = parser().getVariable(line);
if (consoleVar != null) {
status = saveResult(consoleVar, result);
Expand Down Expand Up @@ -1048,54 +1050,78 @@ private Object doc(CommandInput input) {
if (input.xargs().length == 0) {
return null;
}
if (Desktop.isDesktopSupported()) {
Map<String,Object> docs = consoleOption("docs", null);
boolean done = false;
Object arg = input.xargs()[0];
if (arg instanceof String) {
String address = docs != null ? (String)docs.get(input.args()[0]) : null;
if (address != null) {
done = true;
if (!Desktop.isDesktopSupported()) {
throw new IllegalStateException("Desktop is not supported!");
}
Map<String,Object> docs = consoleOption("docs", null);
boolean done = false;
Object arg = input.xargs()[0];
if (arg instanceof String) {
String address = docs != null ? (String)docs.get(input.args()[0]) : null;
if (address != null) {
done = true;
if (urlExists(address)) {
Desktop.getDesktop().browse(new URI(address));
}
}
if (!done) {
String name;
if (arg instanceof String && ((String)arg).matches("([a-z]+\\.)+[A-Z][a-zA-Z]+")) {
name = (String)arg;
} else {
name = arg.getClass().getCanonicalName();
throw new IllegalArgumentException("Document not found: " + address);
}
name = name.replaceAll("\\.", "/") + ".html";
Object doc = null;
assert docs != null;
for (Map.Entry<String,Object> entry : docs.entrySet()) {
if (name.matches(entry.getKey())) {
doc = entry.getValue();
break;
}
}
}
if (!done) {
String name;
if (arg instanceof String && ((String)arg).matches("([a-z]+\\.)+[A-Z][a-zA-Z]+")) {
name = (String)arg;
} else {
name = arg.getClass().getCanonicalName();
}
name = name.replaceAll("\\.", "/") + ".html";
Object doc = null;
assert docs != null;
for (Map.Entry<String,Object> entry : docs.entrySet()) {
if (name.matches(entry.getKey())) {
doc = entry.getValue();
break;
}
if (doc != null) {
if (doc instanceof Collection) {
for (Object o : (Collection<?>)doc) {
Desktop.getDesktop().browse(new URI(o + name));
}
boolean docFound = false;
if (doc != null) {
if (doc instanceof Collection) {
for (Object o : (Collection<?>) doc) {
String url = o + name;
if (urlExists(url)) {
Desktop.getDesktop().browse(new URI(url));
docFound = true;
}
} else {
Desktop.getDesktop().browse(new URI(doc + name));
}
} else {
throw new IllegalArgumentException("Document not found: " + name);
String url = doc + name;
if (urlExists(url)) {
Desktop.getDesktop().browse(new URI(url));
docFound = true;
}
}
}
} else {
throw new IllegalStateException("Desktop is not supported!");
if (!docFound) {
throw new IllegalArgumentException("Document not found: " + name);
}
}
} catch (Exception e) {
exception = e;
}
return null;
}

private boolean urlExists(String weburl) {
try {
URL url = new URL(weburl);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("HEAD");
return huc.getResponseCode() == HttpURLConnection.HTTP_OK;
} catch (Exception e) {
return false;
}
}

private List<Completer> slurpCompleter(String command) {
List<Completer> completers = new ArrayList<>();
List<OptDesc> optDescs = commandOptions("slurp");
Expand Down

0 comments on commit ea98b90

Please sign in to comment.