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.jsonschema.annotation.ExternalDocs; 017import org.apache.juneau.dto.jsonschema.*; 018import org.apache.juneau.html.annotation.*; 019import org.apache.juneau.http.annotation.*; 020import org.apache.juneau.http.annotation.Body; 021import org.apache.juneau.rest.*; 022import org.apache.juneau.rest.annotation.*; 023import org.apache.juneau.rest.widget.*; 024 025/** 026 * Sample resource that shows how to serialize JSON-Schema documents. 027 * 028 * <ul class='seealso'> 029 * <li class='extlink'>{@source} 030 * </ul> 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=@ResourceSwagger( 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 ThemeMenuItem.class 049 }, 050 navlinks={ 051 "up: request:/..", 052 "options: servlet:/?method=OPTIONS", 053 "$W{ContentTypeMenuItem}", 054 "$W{ThemeMenuItem}", 055 "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/dto/$R{servletClassSimple}.java" 056 }, 057 aside={ 058 "<div style='min-width:200px' class='text'>", 059 " <p>Shows how to produce JSON-Schema documents in a variety of languages using the JSON-Schema DTOs.</p>", 060 "</div>" 061 } 062) 063@BeanConfig( 064 examples="Schema: $F{JsonSchemaResource_example.json}" 065) 066public class JsonSchemaResource extends BasicRestServletJena { 067 private static final long serialVersionUID = 1L; 068 069 private JsonSchema schema; // The schema document 070 071 @Override /* Servlet */ 072 public void init() { 073 074 try { 075 schema = new JsonSchema() 076 .setId("http://example.com/sample-schema#") 077 .setSchemaVersionUri("http://json-schema.org/draft-04/schema#") 078 .setTitle("Example Schema") 079 .setType(JsonType.OBJECT) 080 .addProperties( 081 new JsonSchemaProperty("firstName", JsonType.STRING), 082 new JsonSchemaProperty("lastName", JsonType.STRING), 083 new JsonSchemaProperty("age", JsonType.INTEGER) 084 .setDescription("Age in years") 085 .setMinimum(0) 086 ) 087 .addRequired("firstName", "lastName"); 088 } catch (Exception e) { 089 throw new RuntimeException(e); 090 } 091 } 092 093 /** 094 * Get the JSON-Schema document. 095 * 096 * @return The JSON-Schema document. 097 */ 098 @RestMethod( 099 summary="Get the JSON-Schema document" 100 ) 101 public JsonSchema get() { 102 return schema; 103 } 104 105 /** 106 * Overwrite the JSON-Schema document 107 * 108 * @param schema The new schema document. 109 * @return The updated schema document. 110 */ 111 @RestMethod( 112 summary="Overwrite the JSON-Schema document", 113 description="Replaces the schema document with the specified content, and then mirrors it as the response." 114 ) 115 public JsonSchema put(@Body JsonSchema schema) { 116 this.schema = schema; 117 return schema; 118 } 119}