@@ -206,7 +206,7 @@ public class FileFormatResource implements RESTResource {
206206 param: my param value
207207 """ ;
208208
209- private static final String YAML_ITEMS_AND_THINGS_EXAMPLE = """
209+ private static final String YAML_FULL_EXAMPLE = """
210210 version: 1
211211 things:
212212 binding:typeBridge:idBridge:
@@ -251,6 +251,15 @@ public class FileFormatResource implements RESTResource {
251251 value: my value
252252 config:
253253 param: my param value
254+ sitemaps:
255+ MySitemap:
256+ label: My Sitemap
257+ widgets:
258+ - type: Frame
259+ widgets:
260+ - type: Input
261+ item: MyItem
262+ label: My Input
254263 """ ;
255264
256265 private static final String DSL_SITEMAPS_EXAMPLE = """
@@ -261,6 +270,19 @@ public class FileFormatResource implements RESTResource {
261270 }
262271 """ ;
263272
273+ private static final String YAML_SITEMAPS_EXAMPLE = """
274+ version: 1
275+ sitemaps:
276+ MySitemap:
277+ label: My Sitemap
278+ widgets:
279+ - type: Frame
280+ widgets:
281+ - type: Input
282+ item: MyItem
283+ label: My Input
284+ """ ;
285+
264286 private static final String GEN_ID_PATTERN = "gen_file_format_%d" ;
265287
266288 private final Logger logger = LoggerFactory .getLogger (FileFormatResource .class );
@@ -463,11 +485,12 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
463485 @ RolesAllowed ({ Role .ADMIN })
464486 @ Path ("/sitemaps" )
465487 @ Consumes (MediaType .APPLICATION_JSON )
466- @ Produces ({ "text/vnd.openhab.dsl.sitemap" })
488+ @ Produces ({ "text/vnd.openhab.dsl.sitemap" , "application/yaml" })
467489 @ Operation (operationId = "createFileFormatForSitemaps" , summary = "Create file format for a list of sitemaps in registry." , security = {
468490 @ SecurityRequirement (name = "oauth2" , scopes = { "admin" }) }, responses = {
469491 @ ApiResponse (responseCode = "200" , description = "OK" , content = {
470- @ Content (mediaType = "text/vnd.openhab.dsl.sitemap" , schema = @ Schema (example = DSL_SITEMAPS_EXAMPLE )) }),
492+ @ Content (mediaType = "text/vnd.openhab.dsl.sitemap" , schema = @ Schema (example = DSL_SITEMAPS_EXAMPLE )),
493+ @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_SITEMAPS_EXAMPLE )) }),
471494 @ ApiResponse (responseCode = "400" , description = "Payload invalid." ),
472495 @ ApiResponse (responseCode = "404" , description = "One or more sitemaps not found in registry." ),
473496 @ ApiResponse (responseCode = "415" , description = "Unsupported media type." ) })
@@ -523,7 +546,7 @@ public Response createFileFormatForSitemaps(final @Context HttpHeaders httpHeade
523546 @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = DSL_THINGS_EXAMPLE )),
524547 @ Content (mediaType = "text/vnd.openhab.dsl.item" , schema = @ Schema (example = DSL_ITEMS_EXAMPLE )),
525548 @ Content (mediaType = "text/vnd.openhab.dsl.sitemap" , schema = @ Schema (example = DSL_SITEMAPS_EXAMPLE )),
526- @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_ITEMS_AND_THINGS_EXAMPLE )) }),
549+ @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_FULL_EXAMPLE )) }),
527550 @ ApiResponse (responseCode = "400" , description = "Invalid JSON data." ),
528551 @ ApiResponse (responseCode = "415" , description = "Unsupported media type." ) })
529552 public Response create (final @ Context HttpHeaders httpHeaders ,
@@ -594,10 +617,15 @@ public Response create(final @Context HttpHeaders httpHeaders,
594617 itemSerializer .setItemsToBeSerialized (genId , items ,
595618 hideChannelLinksAndMetadata ? List .of () : metadata , stateFormatters , hideDefaultParameters );
596619 }
620+ if (sitemapSerializer != null ) {
621+ sitemapSerializer .setSitemapsToBeSerialized (genId , sitemaps );
622+ }
597623 if (thingSerializer != null ) {
598624 thingSerializer .generateFormat (genId , outputStream );
599625 } else if (itemSerializer != null ) {
600626 itemSerializer .generateFormat (genId , outputStream );
627+ } else if (sitemapSerializer != null ) {
628+ sitemapSerializer .generateFormat (genId , outputStream );
601629 }
602630 break ;
603631 default :
@@ -623,7 +651,7 @@ public Response parse(final @Context HttpHeaders httpHeaders,
623651 @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = DSL_THINGS_EXAMPLE )),
624652 @ Content (mediaType = "text/vnd.openhab.dsl.item" , schema = @ Schema (example = DSL_ITEMS_EXAMPLE )),
625653 @ Content (mediaType = "text/vnd.openhab.dsl.sitemap" , schema = @ Schema (example = DSL_SITEMAPS_EXAMPLE )),
626- @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_ITEMS_AND_THINGS_EXAMPLE )) }) String input ) {
654+ @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_FULL_EXAMPLE )) }) String input ) {
627655 String contentTypeHeader = httpHeaders .getHeaderString (HttpHeaders .CONTENT_TYPE );
628656 logger .debug ("parse: contentType = {}" , contentTypeHeader );
629657
@@ -641,6 +669,7 @@ public Response parse(final @Context HttpHeaders httpHeaders,
641669 SitemapParser sitemapParser = getSitemapParser (contentTypeHeader );
642670 String modelName = null ;
643671 String modelName2 = null ;
672+ String modelName3 = null ;
644673 switch (contentTypeHeader ) {
645674 case "text/vnd.openhab.dsl.thing" :
646675 if (thingParser == null ) {
@@ -684,13 +713,13 @@ public Response parse(final @Context HttpHeaders httpHeaders,
684713 return Response .status (Response .Status .UNSUPPORTED_MEDIA_TYPE )
685714 .entity ("Unsupported content type '" + contentTypeHeader + "'!" ).build ();
686715 }
687- modelName2 = sitemapParser .startParsingFormat (input , errors , warnings );
688- if (modelName2 == null ) {
716+ modelName3 = sitemapParser .startParsingFormat (input , errors , warnings );
717+ if (modelName3 == null ) {
689718 return Response .status (Response .Status .BAD_REQUEST ).entity (String .join ("\n " , errors )).build ();
690719 }
691- sitemaps = sitemapParser .getParsedObjects (modelName2 );
720+ sitemaps = sitemapParser .getParsedObjects (modelName3 );
692721 if (sitemaps .isEmpty ()) {
693- sitemapParser .finishParsingFormat (modelName2 );
722+ sitemapParser .finishParsingFormat (modelName3 );
694723 return Response .status (Response .Status .BAD_REQUEST ).entity ("No sitemap loaded from input" ).build ();
695724 }
696725 break ;
@@ -717,6 +746,19 @@ public Response parse(final @Context HttpHeaders httpHeaders,
717746 metadata = itemParser .getParsedMetadata (modelNameToUse );
718747 stateFormatters = itemParser .getParsedStateFormatters (modelNameToUse );
719748 }
749+ if (sitemapParser != null ) {
750+ // Avoid parsing the input a second time
751+ if (modelName == null && modelName2 == null ) {
752+ modelName3 = sitemapParser .startParsingFormat (input , errors , warnings );
753+ if (modelName3 == null ) {
754+ return Response .status (Response .Status .BAD_REQUEST ).entity (String .join ("\n " , errors ))
755+ .build ();
756+ }
757+ }
758+ String modelNameToUse = modelName != null ? modelName
759+ : (modelName2 != null ? modelName2 : Objects .requireNonNull (modelName3 ));
760+ sitemaps = sitemapParser .getParsedObjects (modelNameToUse );
761+ }
720762 break ;
721763 default :
722764 return Response .status (Response .Status .UNSUPPORTED_MEDIA_TYPE )
@@ -730,8 +772,8 @@ public Response parse(final @Context HttpHeaders httpHeaders,
730772 if (modelName2 != null && itemParser != null ) {
731773 itemParser .finishParsingFormat (modelName2 );
732774 }
733- if (modelName2 != null && sitemapParser != null ) {
734- sitemapParser .finishParsingFormat (modelName2 );
775+ if (modelName3 != null && sitemapParser != null ) {
776+ sitemapParser .finishParsingFormat (modelName3 );
735777 }
736778 return Response .ok (result ).build ();
737779 }
@@ -903,6 +945,7 @@ private Thing simulateThing(DiscoveryResult result, ThingType thingType) {
903945 private @ Nullable SitemapSerializer getSitemapSerializer (String mediaType ) {
904946 return switch (mediaType ) {
905947 case "text/vnd.openhab.dsl.sitemap" -> sitemapSerializers .get ("DSL" );
948+ case "application/yaml" -> sitemapSerializers .get ("YAML" );
906949 default -> null ;
907950 };
908951 }
@@ -927,6 +970,7 @@ private Thing simulateThing(DiscoveryResult result, ThingType thingType) {
927970 private @ Nullable SitemapParser getSitemapParser (String contentType ) {
928971 return switch (contentType ) {
929972 case "text/vnd.openhab.dsl.sitemap" -> sitemapParsers .get ("DSL" );
973+ case "application/yaml" -> sitemapParsers .get ("YAML" );
930974 default -> null ;
931975 };
932976 }
0 commit comments