Search in sources :

Example 1 with Diagram

use of net.sourceforge.plantuml.core.Diagram in project plantuml-server by plantuml.

the class ProxyServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    final String fmt = request.getParameter("fmt");
    final String source = request.getParameter("src");
    final String index = request.getParameter("idx");
    final URL srcUrl;
    // Check if the src URL is valid
    try {
        srcUrl = new URL(source);
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
        return;
    }
    // generate the response
    String diagmarkup = getSource(srcUrl);
    SourceStringReader reader = new SourceStringReader(diagmarkup);
    int n = index == null ? 0 : Integer.parseInt(index);
    List<BlockUml> blocks = reader.getBlocks();
    BlockUml block = blocks.get(n);
    Diagram diagram = block.getDiagram();
    UmlSource umlSrc = diagram.getSource();
    String uml = umlSrc.getPlainString();
    // System.out.println("uml=" + uml);
    // generate the response
    DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
    try {
        dr.sendDiagram(uml, 0);
    } catch (IIOException iioe) {
    // Browser has closed the connection, so the HTTP OutputStream is closed
    // Silently catch the exception to avoid annoying log
    }
    dr = null;
}
Also used : UmlSource(net.sourceforge.plantuml.core.UmlSource) MalformedURLException(java.net.MalformedURLException) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) BlockUml(net.sourceforge.plantuml.BlockUml) IIOException(javax.imageio.IIOException) URL(java.net.URL) Diagram(net.sourceforge.plantuml.core.Diagram)

Example 2 with Diagram

use of net.sourceforge.plantuml.core.Diagram in project plantuml-server by plantuml.

the class DiagramResponse method sendDiagram.

void sendDiagram(String uml, int idx) throws IOException {
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.setContentType(getContentType());
    SourceStringReader reader = new SourceStringReader(uml);
    if (format == FileFormat.BASE64) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DiagramDescription result = reader.outputImage(baos, idx, new FileFormatOption(FileFormat.PNG));
        baos.close();
        final String encodedBytes = "data:image/png;base64," + Base64Coder.encodeLines(baos.toByteArray()).replaceAll("\\s", "");
        response.getOutputStream().write(encodedBytes.getBytes());
        return;
    }
    final BlockUml blockUml = reader.getBlocks().get(0);
    if (notModified(blockUml)) {
        addHeaderForCache(blockUml);
        response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    if (StringUtils.isDiagramCacheable(uml)) {
        addHeaderForCache(blockUml);
    }
    final Diagram diagram = blockUml.getDiagram();
    final ImageData result = diagram.exportDiagram(response.getOutputStream(), idx, new FileFormatOption(format));
}
Also used : FileFormatOption(net.sourceforge.plantuml.FileFormatOption) ImageData(net.sourceforge.plantuml.core.ImageData) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) BlockUml(net.sourceforge.plantuml.BlockUml) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DiagramDescription(net.sourceforge.plantuml.core.DiagramDescription) Diagram(net.sourceforge.plantuml.core.Diagram)

Example 3 with Diagram

use of net.sourceforge.plantuml.core.Diagram in project plantuml-server by plantuml.

the class DiagramResponse method addHeaderForCache.

private void addHeaderForCache(BlockUml blockUml) {
    long today = System.currentTimeMillis();
    // Add http headers to force the browser to cache the image
    final int maxAge = 3600 * 24 * 5;
    response.addDateHeader("Expires", today + 1000L * maxAge);
    response.addDateHeader("Date", today);
    response.addDateHeader("Last-Modified", blockUml.lastModified());
    response.addHeader("Cache-Control", "public, max-age=" + maxAge);
    // response.addHeader("Cache-Control", "max-age=864000");
    response.addHeader("Etag", "\"" + blockUml.etag() + "\"");
    final Diagram diagram = blockUml.getDiagram();
    response.addHeader("X-PlantUML-Diagram-Description", diagram.getDescription().getDescription());
    if (diagram instanceof PSystemError) {
        final PSystemError error = (PSystemError) diagram;
        for (ErrorUml err : error.getErrorsUml()) {
            response.addHeader("X-PlantUML-Diagram-Error", err.getError());
            response.addHeader("X-PlantUML-Diagram-Error-Line", "" + err.getLineLocation().getPosition());
        }
    }
    addHeaders(response);
}
Also used : PSystemError(net.sourceforge.plantuml.PSystemError) ErrorUml(net.sourceforge.plantuml.ErrorUml) Diagram(net.sourceforge.plantuml.core.Diagram)

Example 4 with Diagram

use of net.sourceforge.plantuml.core.Diagram in project plantuml-server by plantuml.

the class DiagramResponse method sendMap.

void sendMap(String uml) throws IOException {
    response.setContentType(getContentType());
    SourceStringReader reader = new SourceStringReader(uml);
    final BlockUml blockUml = reader.getBlocks().get(0);
    if (StringUtils.isDiagramCacheable(uml)) {
        addHeaderForCache(blockUml);
    }
    final Diagram diagram = blockUml.getDiagram();
    ImageData map = diagram.exportDiagram(new NullOutputStream(), 0, new FileFormatOption(FileFormat.PNG, false));
    if (map.containsCMapData()) {
        PrintWriter httpOut = response.getWriter();
        final String cmap = map.getCMapData("plantuml");
        httpOut.print(cmap);
    }
}
Also used : FileFormatOption(net.sourceforge.plantuml.FileFormatOption) ImageData(net.sourceforge.plantuml.core.ImageData) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) BlockUml(net.sourceforge.plantuml.BlockUml) Diagram(net.sourceforge.plantuml.core.Diagram) NullOutputStream(net.sourceforge.plantuml.NullOutputStream) PrintWriter(java.io.PrintWriter)

Aggregations

Diagram (net.sourceforge.plantuml.core.Diagram)4 BlockUml (net.sourceforge.plantuml.BlockUml)3 SourceStringReader (net.sourceforge.plantuml.SourceStringReader)3 FileFormatOption (net.sourceforge.plantuml.FileFormatOption)2 ImageData (net.sourceforge.plantuml.core.ImageData)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 IIOException (javax.imageio.IIOException)1 ErrorUml (net.sourceforge.plantuml.ErrorUml)1 NullOutputStream (net.sourceforge.plantuml.NullOutputStream)1 PSystemError (net.sourceforge.plantuml.PSystemError)1 DiagramDescription (net.sourceforge.plantuml.core.DiagramDescription)1 UmlSource (net.sourceforge.plantuml.core.UmlSource)1