001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.examples.rest.dto; 014 015import org.apache.juneau.annotation.*; 016import org.apache.juneau.dto.jsonschema.*; 017import org.apache.juneau.html.annotation.*; 018import org.apache.juneau.http.annotation.*; 019import org.apache.juneau.rest.annotation.*; 020import org.apache.juneau.rest.servlet.*; 021import org.apache.juneau.rest.widget.*; 022 023/** 024 * Sample resource that shows how to serialize JSON-Schema documents. 025 * 026 * <h5 class='section'>See Also:</h5><ul> 027 028 * </ul> 029 * 030 * @serial exclude 031 */ 032@Rest( 033 path="/jsonSchema", 034 messages="nls/JsonSchemaResource", 035 title="Sample JSON-Schema document", 036 description="Sample resource that shows how to generate JSON-Schema documents", 037 swagger=@Swagger( 038 contact=@Contact(name="Juneau Developer",email="dev@juneau.apache.org"), 039 license=@License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"), 040 version="2.0", 041 termsOfService="You are on your own.", 042 externalDocs=@ExternalDocs(description="Apache Juneau",url="http://juneau.apache.org") 043 ) 044) 045@HtmlDocConfig( 046 widgets={ 047 ContentTypeMenuItem.class 048 }, 049 navlinks={ 050 "up: request:/..", 051 "api: servlet:/api", 052 "stats: servlet:/stats", 053 "$W{ContentTypeMenuItem}", 054 "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/dto/JsonSchemaResource.java" 055 }, 056 aside={ 057 "<div style='min-width:200px' class='text'>", 058 " <p>Shows how to produce JSON-Schema documents in a variety of languages using the JSON-Schema DTOs.</p>", 059 "</div>" 060 } 061) 062@Marshalled(on="Schema",example="$F{JsonSchemaResource_example.json}") 063public class JsonSchemaResource extends BasicRestServlet { 064 private static final long serialVersionUID = 1L; 065 066 private JsonSchema schema; // The schema document 067 068 @Override /* Servlet */ 069 public void init() { 070 071 try { 072 schema = new JsonSchema() 073 .setId("http://example.com/sample-schema#") 074 .setSchemaVersionUri("http://json-schema.org/draft-04/schema#") 075 .setTitle("Example Schema") 076 .setType(JsonType.OBJECT) 077 .addProperties( 078 new JsonSchemaProperty("firstName", JsonType.STRING), 079 new JsonSchemaProperty("lastName", JsonType.STRING), 080 new JsonSchemaProperty("age", JsonType.INTEGER) 081 .setDescription("Age in years") 082 .setMinimum(0) 083 ) 084 .addRequired("firstName", "lastName"); 085 } catch (Exception e) { 086 throw new RuntimeException(e); 087 } 088 } 089 090 /** 091 * [HTTP GET /dto/jsonSchema] 092 * Get the JSON-Schema document. 093 * 094 * @return The JSON-Schema document. 095 */ 096 @RestGet( 097 summary="Get the JSON-Schema document" 098 ) 099 public JsonSchema get() { 100 return schema; 101 } 102 103 /** 104 * [HTTP PUT /dto/jsonSchema] 105 * Overwrite the JSON-Schema document 106 * 107 * @param schema The new schema document. 108 * @return The updated schema document. 109 */ 110 @RestPut( 111 summary="Overwrite the JSON-Schema document", 112 description="Replaces the schema document with the specified content, and then mirrors it as the response." 113 ) 114 public JsonSchema put(@Content JsonSchema schema) { 115 this.schema = schema; 116 return schema; 117 } 118}