@@ -119,14 +119,20 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
119119 try {
120120 JsonObject requestBody = readJsonRequest (request );
121121
122- if (pathInfo .equals ("/render" )) {
122+ if (pathInfo .equals ("/check" )) {
123+ handleCheck (requestBody , response );
124+ } else if (pathInfo .equals ("/render" )) {
123125 handleRender (requestBody , response );
126+ } else if (pathInfo .equals ("/metadata" )) {
127+ handleMetadata (requestBody , response );
124128 } else if (pathInfo .equals ("/render-url" )) {
125129 handleRenderUrl (requestBody , response );
126130 } else if (pathInfo .equals ("/analyze" )) {
127131 handleAnalyze (requestBody , response );
128132 } else if (pathInfo .equals ("/workspace/create" )) {
129133 handleWorkspaceCreate (requestBody , response );
134+ } else if (pathInfo .equals ("/workspace/put" )) {
135+ handleWorkspaceUpdate (requestBody , response );
130136 } else if (pathInfo .equals ("/workspace/update" )) {
131137 handleWorkspaceUpdate (requestBody , response );
132138 } else if (pathInfo .equals ("/workspace/get" )) {
@@ -138,6 +144,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
138144 } else {
139145 sendError (response , HttpServletResponse .SC_NOT_FOUND , "Endpoint not found" );
140146 }
147+ } catch (com .google .gson .JsonSyntaxException e ) {
148+ // Handle JSON parsing errors
149+ sendError (response , HttpServletResponse .SC_BAD_REQUEST ,
150+ "Invalid JSON: " + e .getMessage ());
141151 } catch (Exception e ) {
142152 // Log error (servlet container will handle logging)
143153 sendError (response , HttpServletResponse .SC_INTERNAL_SERVER_ERROR ,
@@ -240,6 +250,99 @@ private void handleExamplesGet(HttpServletRequest request, HttpServletResponse r
240250 sendJson (response , result );
241251 }
242252
253+ private void handleCheck (JsonObject requestBody , HttpServletResponse response )
254+ throws IOException {
255+ String source = getJsonString (requestBody , "source" , null );
256+ if (source == null || source .isEmpty ()) {
257+ sendError (response , HttpServletResponse .SC_BAD_REQUEST , "Missing 'source' field" );
258+ return ;
259+ }
260+
261+ try {
262+ // Try to parse the diagram to check for syntax errors
263+ SourceStringReader reader = new SourceStringReader (source );
264+ // Use a null output stream to just validate
265+ reader .outputImage (new ByteArrayOutputStream (), 0 , new FileFormatOption (FileFormat .PNG ));
266+
267+ Map <String , Object > result = new HashMap <>();
268+ result .put ("ok" , true );
269+ result .put ("errors" , new Object [0 ]);
270+ sendJson (response , result );
271+ } catch (Exception e ) {
272+ Map <String , Object > result = new HashMap <>();
273+ result .put ("ok" , false );
274+ Map <String , Object > error = new HashMap <>();
275+ error .put ("line" , 0 );
276+ error .put ("message" , e .getMessage () != null ? e .getMessage () : "Syntax error" );
277+ result .put ("errors" , new Object []{error });
278+ sendJson (response , result );
279+ }
280+ }
281+
282+ private void handleMetadata (JsonObject requestBody , HttpServletResponse response )
283+ throws IOException {
284+ String source = getJsonString (requestBody , "source" , null );
285+ if (source == null || source .isEmpty ()) {
286+ sendError (response , HttpServletResponse .SC_BAD_REQUEST , "Missing 'source' field" );
287+ return ;
288+ }
289+
290+ try {
291+ // Extract basic metadata from the source
292+ Map <String , Object > result = new HashMap <>();
293+
294+ // Parse participants/entities from the source
295+ java .util .List <String > participants = new java .util .ArrayList <>();
296+ String [] lines = source .split ("\n " );
297+ for (String line : lines ) {
298+ // Simple parsing for common diagram elements
299+ line = line .trim ();
300+ if (line .matches ("^[a-zA-Z0-9_]+\\ s*->.*" ) || line .matches (".*->\\ s*[a-zA-Z0-9_]+.*" )) {
301+ // Extract participant names from arrow notations
302+ String [] parts = line .split ("->" );
303+ for (String part : parts ) {
304+ String name = part .trim ().split ("\\ s" )[0 ].replaceAll ("[^a-zA-Z0-9_]" , "" );
305+ if (!name .isEmpty () && !participants .contains (name )) {
306+ participants .add (name );
307+ }
308+ }
309+ } else if (line .matches ("^(class|interface|entity|participant)\\ s+[a-zA-Z0-9_]+.*" )) {
310+ String [] parts = line .split ("\\ s+" );
311+ if (parts .length >= 2 ) {
312+ String name = parts [1 ].replaceAll ("[^a-zA-Z0-9_]" , "" );
313+ if (!participants .contains (name )) {
314+ participants .add (name );
315+ }
316+ }
317+ }
318+ }
319+
320+ result .put ("participants" , participants .toArray (new String [0 ]));
321+ result .put ("directives" , new String [0 ]);
322+
323+ // Detect diagram type
324+ String diagramType = "unknown" ;
325+ if (source .contains ("@startuml" )) {
326+ if (source .contains ("->" ) || source .contains ("participant" )) {
327+ diagramType = "sequence" ;
328+ } else if (source .contains ("class" ) || source .contains ("interface" )) {
329+ diagramType = "class" ;
330+ } else if (source .contains ("state" )) {
331+ diagramType = "state" ;
332+ } else if (source .contains ("usecase" ) || source .contains ("actor" )) {
333+ diagramType = "usecase" ;
334+ }
335+ }
336+ result .put ("diagramType" , diagramType );
337+ result .put ("warnings" , new String [0 ]);
338+
339+ sendJson (response , result );
340+ } catch (Exception e ) {
341+ sendError (response , HttpServletResponse .SC_BAD_REQUEST ,
342+ "Metadata extraction failed: " + e .getMessage ());
343+ }
344+ }
345+
243346 private void handleRender (JsonObject requestBody , HttpServletResponse response )
244347 throws IOException {
245348 String source = getJsonString (requestBody , "source" , null );
@@ -258,20 +361,22 @@ private void handleRender(JsonObject requestBody, HttpServletResponse response)
258361 reader .outputImage (outputStream , 0 , new FileFormatOption (fileFormat ));
259362
260363 byte [] imageBytes = outputStream .toByteArray ();
261- String dataUrl = formatDataUrl (imageBytes , fileFormat );
262- String sha256 = computeSha256 (imageBytes );
364+ String dataBase64 = Base64 .getEncoder ().encodeToString (imageBytes );
263365
264366 Map <String , Object > result = new HashMap <>();
265- result .put ("status " , "ok" );
367+ result .put ("ok " , true );
266368 result .put ("format" , format );
267- result .put ("dataUrl" , dataUrl );
268- result .put ("renderTimeMs" , System .currentTimeMillis () - startTime );
269- result .put ("sha256" , sha256 );
369+ result .put ("dataBase64" , dataBase64 );
270370
271371 sendJson (response , result );
272372 } catch (Exception e ) {
273- sendError (response , HttpServletResponse .SC_BAD_REQUEST ,
274- "Rendering failed: " + e .getMessage ());
373+ Map <String , Object > errorResult = new HashMap <>();
374+ errorResult .put ("ok" , false );
375+ errorResult .put ("errors" , new Object []{
376+ java .util .Collections .singletonMap ("message" , "Rendering failed: " + e .getMessage ())
377+ });
378+ response .setStatus (HttpServletResponse .SC_OK );
379+ sendJson (response , errorResult );
275380 }
276381 }
277382
0 commit comments