use of io.kroki.server.error.BadRequestException in project kroki by yuzutech.
the class Plantuml method convert.
@Override
public void convert(String sourceDecoded, String serviceName, FileFormat fileFormat, JsonObject options, Handler<AsyncResult<Buffer>> handler) {
String source;
try {
source = sanitize(sourceDecoded, this.safeMode, this.includeWhitelist);
source = source.trim();
source = withDelimiter(source);
} catch (IOException e) {
if (e instanceof UnsupportedEncodingException) {
handler.handle(new Delegator.Failure(new BadRequestException("Characters must be encoded in UTF-8.", e)));
} else {
handler.handle(new Delegator.Failure(e));
}
return;
}
final String primeSource = source;
DitaaContext ditaaContext = findDitaaContext(source);
if (ditaaContext != null) {
logging.reroute("plantuml", "ditaa", ditaaContext.getSource(), fileFormat);
// found a ditaa context, delegate to the optimized ditaa service
vertx.executeBlocking(future -> {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// REMIND: options are unsupported for now.
Ditaa.convert(fileFormat, new ByteArrayInputStream(ditaaContext.getSource().getBytes()), outputStream);
future.complete(outputStream.toByteArray());
} catch (IllegalStateException e) {
future.fail(e);
}
}, res -> handler.handle(res.map(o -> Buffer.buffer((byte[]) o))));
} else {
// ...otherwise, continue with PlantUML
vertx.executeBlocking(future -> {
try {
byte[] data = convert(primeSource, fileFormat, options);
future.complete(data);
} catch (IllegalStateException e) {
future.fail(e);
}
}, res -> handler.handle(res.map(o -> Buffer.buffer((byte[]) o))));
}
}
use of io.kroki.server.error.BadRequestException in project kroki by yuzutech.
the class Structurizr method convert.
static byte[] convert(String source, FileFormat fileFormat, StructurizrPlantUMLExporter structurizrPlantUMLExporter, JsonObject options) {
StructurizrDslParser parser = new StructurizrDslParser();
try {
parser.parse(source);
Collection<View> views = parser.getWorkspace().getViews().getViews();
if (views.isEmpty()) {
throw new BadRequestException("Empty diagram, does not have any view.");
}
View selectedView;
String viewKey = options.getString("view-key");
if (viewKey != null && !viewKey.trim().isEmpty()) {
Optional<View> viewFound = views.stream().filter(view -> Objects.equals(view.getKey(), viewKey)).findFirst();
if (!viewFound.isPresent()) {
throw new BadRequestException("Unable to find view for key: " + viewKey + ".");
}
selectedView = viewFound.get();
} else {
// take the first view if not specified
selectedView = views.iterator().next();
}
final Diagram diagram;
if (selectedView instanceof DynamicView) {
diagram = structurizrPlantUMLExporter.export((DynamicView) selectedView);
} else if (selectedView instanceof DeploymentView) {
diagram = structurizrPlantUMLExporter.export((DeploymentView) selectedView);
} else if (selectedView instanceof ComponentView) {
diagram = structurizrPlantUMLExporter.export((ComponentView) selectedView);
} else if (selectedView instanceof ContainerView) {
diagram = structurizrPlantUMLExporter.export((ContainerView) selectedView);
} else if (selectedView instanceof SystemContextView) {
diagram = structurizrPlantUMLExporter.export((SystemContextView) selectedView);
} else if (selectedView instanceof SystemLandscapeView) {
diagram = structurizrPlantUMLExporter.export((SystemLandscapeView) selectedView);
} else {
throw new BadRequestException("View type is not supported: " + selectedView.getClass().getSimpleName() + ", must be a DynamicView, DeploymentView, ComponentView, ContainerView, SystemContextView or SystemLandscapeView.");
}
return Plantuml.convert(diagram.getDefinition(), fileFormat, new JsonObject());
} catch (StructurizrDslParserException e) {
String cause = e.getMessage();
final String message;
if (cause != null && !cause.trim().isEmpty()) {
message = "Unable to parse the Structurizr DSL. " + cause + ".";
} else {
message = "Unable to parse the Structurizr DSL.";
}
throw new BadRequestException(message, e);
}
}
use of io.kroki.server.error.BadRequestException in project kroki by yuzutech.
the class Plantuml method convert.
static byte[] convert(String source, FileFormat format, JsonObject options) {
try {
String theme = options.getString("theme");
if (theme != null && !theme.trim().isEmpty()) {
// add !theme directive just after the @start directive
source = START_BLOCK_RX.matcher(source).replaceAll("$1!theme " + theme + "\n");
}
SourceStringReader reader = new SourceStringReader(source);
if (format == FileFormat.BASE64) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
reader.outputImage(baos, 0, new FileFormatOption(FileFormat.PNG.toPlantumlFileFormat()));
baos.close();
final String encodedBytes = "data:image/png;base64," + Base64Coder.encodeLines(baos.toByteArray()).replaceAll("\\s", "");
return encodedBytes.getBytes();
}
if (reader.getBlocks().isEmpty()) {
throw new BadRequestException("Empty diagram, missing delimiters?");
}
final BlockUml blockUml = reader.getBlocks().get(0);
final Diagram diagram = blockUml.getDiagram();
if (diagram instanceof PSystemError) {
Collection<ErrorUml> errors = ((PSystemError) diagram).getErrorsUml();
if (!errors.isEmpty()) {
ErrorUml errorUml = errors.iterator().next();
LineLocation lineLocation = errorUml.getLineLocation();
String lineLocationPosition = "";
if (lineLocation != null) {
lineLocationPosition = " (line: " + lineLocation.getPosition() + ")";
}
throw new BadRequestException(errorUml.getError() + lineLocationPosition);
}
throw new BadRequestException("Unable to convert the diagram");
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
diagram.exportDiagram(byteArrayOutputStream, 0, new FileFormatOption(format.toPlantumlFileFormat()));
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Bad request", e);
}
}
Aggregations