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