94
94
* This resource is registered with the Jersey servlet.
95
95
*
96
96
* @author Laurent Garnier - Initial contribution
97
+ * @author Laurent Garnier - Add YAML output for things
97
98
*/
98
99
@ Component
99
100
@ JaxrsResource
@@ -108,6 +109,15 @@ public class FileFormatResource implements RESTResource {
108
109
/** The URI path to this resource */
109
110
public static final String PATH_FILE_FORMAT = "file-format" ;
110
111
112
+ private static final String DSL_THINGS_EXAMPLE = "Bridge binding:typeBridge:idBridge \" Label brigde\" @ \" Location bridge\" [stringParam=\" my value\" ] {" //
113
+ + "\n Thing type id \" Label thing\" @ \" Location thing\" [booleanParam=true, decimalParam=2.5]\n }" ;
114
+ private static final String DSL_THING_EXAMPLE = "Thing binding:type:idBridge:id \" Label thing\" @ \" Location thing\" (binding:typeBridge:idBridge) [stringParam=\" my value\" , booleanParam=true, decimalParam=2.5]" ;
115
+ private static final String YAML_THINGS_EXAMPLE = "version: 2\n things:\n " //
116
+ + " binding:typeBridge:idBridge:\n isBridge: true\n label: Label bridge\n location: Location bridge\n config:\n stringParam: my value\n "
117
+ + " binding:type:idBridge:id:\n bridge: binding:typeBridge:idBridge\n label: Label thing\n location: Location thing\n config:\n booleanParam: true\n decimalParam: 2.5" ;
118
+ private static final String YAML_THING_EXAMPLE = "version: 2\n things:\n " //
119
+ + " binding:type:idBridge:id:\n bridge: binding:typeBridge:idBridge\n label: Label thing\n location: Location thing\n config:\n stringParam: my value\n booleanParam: true\n decimalParam: 2.5" ;
120
+
111
121
private final Logger logger = LoggerFactory .getLogger (FileFormatResource .class );
112
122
113
123
private final ItemRegistry itemRegistry ;
@@ -216,16 +226,28 @@ public Response createFileFormatForItem(final @Context HttpHeaders httpHeaders,
216
226
@ GET
217
227
@ RolesAllowed ({ Role .ADMIN })
218
228
@ Path ("/things" )
219
- @ Produces ("text/vnd.openhab.dsl.thing" )
229
+ @ Produces ({ "text/vnd.openhab.dsl.thing" , "application/yaml" } )
220
230
@ Operation (operationId = "createFileFormatForAllThings" , summary = "Create file format for all existing things in registry." , security = {
221
231
@ SecurityRequirement (name = "oauth2" , scopes = { "admin" }) }, responses = {
222
- @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = "Bridge binding:typeBridge:idBridge \" Label\" @ \" Location\" [stringParam=\" my value\" ] {\n Thing type id \" Label\" @ \" Location\" [booleanParam=true, decimalParam=2.5]\n }" ))),
232
+ @ ApiResponse (responseCode = "200" , description = "OK" , content = {
233
+ @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = DSL_THINGS_EXAMPLE )),
234
+ @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_THINGS_EXAMPLE )) }),
223
235
@ ApiResponse (responseCode = "415" , description = "Unsupported media type." ) })
224
236
public Response createFileFormatForAllThings (final @ Context HttpHeaders httpHeaders ,
225
237
@ DefaultValue ("true" ) @ QueryParam ("hideDefaultParameters" ) @ Parameter (description = "hide the configuration parameters having the default value" ) boolean hideDefaultParameters ) {
226
238
String acceptHeader = httpHeaders .getHeaderString (HttpHeaders .ACCEPT );
227
- String format = "text/vnd.openhab.dsl.thing" .equals (acceptHeader ) ? "DSL" : null ;
228
- ThingFileGenerator generator = format == null ? null : thingFileGenerators .get (format );
239
+ ThingFileGenerator generator ;
240
+ switch (acceptHeader ) {
241
+ case "text/vnd.openhab.dsl.thing" :
242
+ generator = thingFileGenerators .get ("DSL" );
243
+ break ;
244
+ case "application/yaml" :
245
+ generator = thingFileGenerators .get ("YAML" );
246
+ break ;
247
+ default :
248
+ generator = null ;
249
+ break ;
250
+ }
229
251
if (generator == null ) {
230
252
return Response .status (Response .Status .UNSUPPORTED_MEDIA_TYPE )
231
253
.entity ("Unsupported media type '" + acceptHeader + "'!" ).build ();
@@ -238,18 +260,30 @@ public Response createFileFormatForAllThings(final @Context HttpHeaders httpHead
238
260
@ GET
239
261
@ RolesAllowed ({ Role .ADMIN })
240
262
@ Path ("/things/{thingUID}" )
241
- @ Produces ("text/vnd.openhab.dsl.thing" )
263
+ @ Produces ({ "text/vnd.openhab.dsl.thing" , "application/yaml" } )
242
264
@ Operation (operationId = "createFileFormatForThing" , summary = "Create file format for an existing thing in things or discovery registry." , security = {
243
265
@ SecurityRequirement (name = "oauth2" , scopes = { "admin" }) }, responses = {
244
- @ ApiResponse (responseCode = "200" , description = "OK" , content = @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = "Thing binding:type:idBridge:id \" Label\" @ \" Location\" (binding:typeBridge:idBridge) [stringParam=\" my value\" , booleanParam=true, decimalParam=2.5]" ))),
266
+ @ ApiResponse (responseCode = "200" , description = "OK" , content = {
267
+ @ Content (mediaType = "text/vnd.openhab.dsl.thing" , schema = @ Schema (example = DSL_THING_EXAMPLE )),
268
+ @ Content (mediaType = "application/yaml" , schema = @ Schema (example = YAML_THING_EXAMPLE )) }),
245
269
@ ApiResponse (responseCode = "404" , description = "Thing not found in things or discovery registry or thing type not found." ),
246
270
@ ApiResponse (responseCode = "415" , description = "Unsupported media type." ) })
247
271
public Response createFileFormatForThing (final @ Context HttpHeaders httpHeaders ,
248
272
@ DefaultValue ("true" ) @ QueryParam ("hideDefaultParameters" ) @ Parameter (description = "hide the configuration parameters having the default value" ) boolean hideDefaultParameters ,
249
273
@ PathParam ("thingUID" ) @ Parameter (description = "thingUID" ) String thingUID ) {
250
274
String acceptHeader = httpHeaders .getHeaderString (HttpHeaders .ACCEPT );
251
- String format = "text/vnd.openhab.dsl.thing" .equals (acceptHeader ) ? "DSL" : null ;
252
- ThingFileGenerator generator = format == null ? null : thingFileGenerators .get (format );
275
+ ThingFileGenerator generator ;
276
+ switch (acceptHeader ) {
277
+ case "text/vnd.openhab.dsl.thing" :
278
+ generator = thingFileGenerators .get ("DSL" );
279
+ break ;
280
+ case "application/yaml" :
281
+ generator = thingFileGenerators .get ("YAML" );
282
+ break ;
283
+ default :
284
+ generator = null ;
285
+ break ;
286
+ }
253
287
if (generator == null ) {
254
288
return Response .status (Response .Status .UNSUPPORTED_MEDIA_TYPE )
255
289
.entity ("Unsupported media type '" + acceptHeader + "'!" ).build ();
0 commit comments