implemented display of document positions
This commit is contained in:
@@ -32,10 +32,6 @@ public abstract class BaseHandler extends PathHandler {
|
||||
return ex;
|
||||
}
|
||||
|
||||
public boolean forbidden(HttpExchange ex) throws IOException {
|
||||
return sendEmptyResponse(HTTP_FORBIDDEN,ex);
|
||||
}
|
||||
|
||||
public record Page(String mime, byte[] bytes){}
|
||||
|
||||
public boolean load(Path path, HttpExchange ex) throws IOException {
|
||||
@@ -73,7 +69,7 @@ public abstract class BaseHandler extends PathHandler {
|
||||
}
|
||||
|
||||
public boolean unauthorized(HttpExchange ex) throws IOException {
|
||||
return sendEmptyResponse(HTTP_FORBIDDEN,ex);
|
||||
return sendEmptyResponse(HTTP_UNAUTHORIZED,ex);
|
||||
}
|
||||
|
||||
public boolean notImplemented(HttpExchange ex,String message,Object clazz) throws IOException{
|
||||
|
||||
@@ -8,18 +8,23 @@ import static java.lang.System.Logger.Level.*;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.xrbpowered.jparsedown.JParsedown;
|
||||
import de.srsoftware.tools.Query;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Util {
|
||||
public static final System.Logger LOG = System.getLogger("Util");
|
||||
private static final Pattern UML_PATTERN = Pattern.compile("@start(\\w+)(.*)@end(\\1)",Pattern.DOTALL);
|
||||
private static File plantumlJar = null;
|
||||
private static final JParsedown MARKDOWN = new JParsedown();
|
||||
|
||||
private Util(){}
|
||||
|
||||
public static System.Logger.Level mapLogLevel(String lbl) {
|
||||
@@ -32,9 +37,38 @@ public class Util {
|
||||
};
|
||||
}
|
||||
|
||||
public static String markdown(String code){
|
||||
LOG.log(ERROR,"{0}.markdown(…) not implemented",Util.class.getCanonicalName());
|
||||
return code;
|
||||
public static String markdown(String source){
|
||||
try {
|
||||
if (plantumlJar.exists()) {
|
||||
var matcher = UML_PATTERN.matcher(source);
|
||||
if (matcher.find()) {
|
||||
var uml = matcher.group(0).trim();
|
||||
var start = matcher.start(0);
|
||||
var end = matcher.end(0);
|
||||
|
||||
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", plantumlJar.getAbsolutePath(), "-tsvg", "-pipe");
|
||||
var ignored = processBuilder.redirectErrorStream();
|
||||
var process = processBuilder.start();
|
||||
try (OutputStream os = process.getOutputStream()) {
|
||||
os.write(uml.getBytes(UTF_8));
|
||||
os.flush();
|
||||
}
|
||||
|
||||
try (InputStream is = process.getInputStream()) {
|
||||
byte[] out = is.readAllBytes();
|
||||
var svg = new String(out, UTF_8);
|
||||
source = source.substring(0, start) + svg + source.substring(end);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return MARKDOWN.text(source);
|
||||
} catch (Exception e){
|
||||
if (LOG.isLoggable(TRACE)){
|
||||
LOG.log(TRACE,"Failed to render markdown, input was: \n{0}",source);
|
||||
} else LOG.log(WARNING,"Failed to render markdown. Enable TRACE log level for details.");
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpURLConnection open(URL url) throws IOException {
|
||||
@@ -101,4 +135,9 @@ public class Util {
|
||||
throw new UmbrellaException(500,"Request to {0} failed!",target).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setPlantUmlJar(File file){
|
||||
LOG.log(INFO,"Using plantuml @ {0}",file.getAbsolutePath());
|
||||
plantumlJar = file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
|
||||
@@ -25,10 +27,15 @@ public class UmbrellaException extends Exception{
|
||||
return this;
|
||||
}
|
||||
|
||||
public static UmbrellaException databaseException(String message, Object fills) {
|
||||
public static UmbrellaException databaseException(String message, Object... fills) {
|
||||
System.getLogger("Configuration").log(WARNING,message,fills);
|
||||
return new UmbrellaException(HTTP_SERVER_ERROR,message,fills);
|
||||
}
|
||||
|
||||
public static UmbrellaException forbidden(String message, Object... fills) {
|
||||
return new UmbrellaException(HTTP_FORBIDDEN,message,fills);
|
||||
}
|
||||
|
||||
public static UmbrellaException invalidFieldException(String field,String expected){
|
||||
return new UmbrellaException(HTTP_UNPROCESSABLE, ERROR_INVALID_FIELD, field, expected);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user