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 * <h5 class='section'>See Also:</h5><ul>
031
032 * </ul>
033 *
034 * @serial exclude
035 */
036@Rest(
037   path="/jsonSchema",
038   messages="nls/JsonSchemaResource",
039   title="Sample JSON-Schema document",
040   description="Sample resource that shows how to generate JSON-Schema documents",
041   swagger=@Swagger(
042      contact=@Contact(name="Juneau Developer",email="dev@juneau.apache.org"),
043      license=@License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"),
044      version="2.0",
045      termsOfService="You are on your own.",
046      externalDocs=@ExternalDocs(description="Apache Juneau",url="http://juneau.apache.org")
047   )
048)
049@HtmlDocConfig(
050   widgets={
051      ContentTypeMenuItem.class
052   },
053   navlinks={
054      "up: request:/..",
055      "api: servlet:/api",
056      "stats: servlet:/stats",
057      "$W{ContentTypeMenuItem}",
058      "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/dto/JsonSchemaResource.java"
059   },
060   aside={
061      "<div style='min-width:200px' class='text'>",
062      "  <p>Shows how to produce JSON-Schema documents in a variety of languages using the JSON-Schema DTOs.</p>",
063      "</div>"
064   }
065)
066@Marshalled(on="Schema",example="$F{JsonSchemaResource_example.json}")
067public class JsonSchemaResource extends BasicRestServlet {
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 IllegalStateException(e);
091      }
092   }
093
094   /**
095    * [HTTP GET /dto/jsonSchema]
096    * Get the JSON-Schema document.
097    *
098    * @return The JSON-Schema document.
099    */
100   @RestGet(
101      summary="Get the JSON-Schema document"
102   )
103   public JsonSchema get() {
104      return schema;
105   }
106
107   /**
108    * [HTTP PUT /dto/jsonSchema]
109    * Overwrite the JSON-Schema document
110    *
111    * @param schema The new schema document.
112    * @return The updated schema document.
113    */
114   @RestPut(
115      summary="Overwrite the JSON-Schema document",
116      description="Replaces the schema document with the specified content, and then mirrors it as the response."
117   )
118   public JsonSchema put(@Content JsonSchema schema) {
119      this.schema = schema;
120      return schema;
121   }
122}