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