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 static org.apache.juneau.BeanContext.*; 016 017import org.apache.juneau.jsonschema.annotation.ExternalDocs; 018import org.apache.juneau.dto.jsonschema.*; 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@RestResource( 029 path="/jsonSchema", 030 messages="nls/JsonSchemaResource", 031 title="Sample JSON-Schema document", 032 description="Sample resource that shows how to generate JSON-Schema documents", 033 properties={ 034 @Property(name=BEAN_examples, value="{'org.apache.juneau.dto.jsonschema.Schema': $F{JsonSchemaResource_example.json}}") 035 }, 036 htmldoc=@HtmlDoc( 037 widgets={ 038 ContentTypeMenuItem.class, 039 ThemeMenuItem.class 040 }, 041 navlinks={ 042 "up: request:/..", 043 "options: servlet:/?method=OPTIONS", 044 "$W{ContentTypeMenuItem}", 045 "$W{ThemeMenuItem}", 046 "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/dto/$R{servletClassSimple}.java" 047 }, 048 aside={ 049 "<div style='min-width:200px' class='text'>", 050 " <p>Shows how to produce JSON-Schema documents in a variety of languages using the JSON-Schema DTOs.</p>", 051 "</div>" 052 } 053 ), 054 swagger=@ResourceSwagger( 055 contact=@Contact(name="Juneau Developer",email="dev@juneau.apache.org"), 056 license=@License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"), 057 version="2.0", 058 termsOfService="You are on your own.", 059 externalDocs=@ExternalDocs(description="Apache Juneau",url="http://juneau.apache.org") 060 ) 061) 062public class JsonSchemaResource extends BasicRestServletJena { 063 private static final long serialVersionUID = 1L; 064 065 private JsonSchema schema; // The schema document 066 067 @Override /* Servlet */ 068 public void init() { 069 070 try { 071 schema = new JsonSchema() 072 .setId("http://example.com/sample-schema#") 073 .setSchemaVersionUri("http://json-schema.org/draft-04/schema#") 074 .setTitle("Example Schema") 075 .setType(JsonType.OBJECT) 076 .addProperties( 077 new JsonSchemaProperty("firstName", JsonType.STRING), 078 new JsonSchemaProperty("lastName", JsonType.STRING), 079 new JsonSchemaProperty("age", JsonType.INTEGER) 080 .setDescription("Age in years") 081 .setMinimum(0) 082 ) 083 .addRequired("firstName", "lastName"); 084 } catch (Exception e) { 085 throw new RuntimeException(e); 086 } 087 } 088 089 @RestMethod( 090 summary="Get the JSON-Schema document" 091 ) 092 public JsonSchema get() throws Exception { 093 return schema; 094 } 095 096 @RestMethod( 097 summary="Overwrite the JSON-Schema document", 098 description="Replaces the schema document with the specified content, and then mirrors it as the response." 099 ) 100 public JsonSchema put(@Body JsonSchema schema) throws Exception { 101 this.schema = schema; 102 return schema; 103 } 104}