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.core.dto;
014
015import org.apache.juneau.dto.atom.Feed;
016import org.apache.juneau.dto.atom.Person;
017import org.apache.juneau.dto.swagger.Swagger;
018import org.apache.juneau.html.HtmlSerializer;
019import org.apache.juneau.http.MediaType;
020import org.apache.juneau.json.JsonSerializer;
021
022import static org.apache.juneau.dto.atom.AtomBuilder.*;
023import static org.apache.juneau.dto.html5.HtmlBuilder.*;
024import static org.apache.juneau.dto.swagger.SwaggerBuilder.*;
025
026/**
027 * Sample class which shows the usage of DTO module which is a
028 * Sub module of the core.
029 *
030 * <ul class='seealso'>
031 *    <li class='extlink'>{@source}
032 * </ul>
033 */
034public class DtoExample {
035
036   /**
037    * DTO Samples
038    *
039    * @param args Unused.
040    * @throws Exception Unused.
041    */
042   @SuppressWarnings("unused")
043   public static void main(String[] args) throws Exception {
044
045      //Produces
046      /**
047       * <table>
048       * <tr>
049       * <th>c1</th>
050       * <th>c2</th>
051       * </tr>
052       * <tr>
053       * <td>v1</td>
054       * <td>v2</td>
055       * </tr>
056       * </table>
057       */
058      Object mytable =
059         table(
060            tr(
061               th("c1"),
062               th("c2")
063            ),
064            tr(
065               td("v1"),
066               td("v2")
067            )
068         );
069
070      String html = HtmlSerializer.DEFAULT.serialize(mytable);
071
072      Object mainJsp =
073         form().action("main.jsp").method("GET")
074         .children(
075            input("text").name("first_name").value("apache"), br(),
076            input("text").name("last_name").value("juneau"), br(),
077            button("submit", "Submit"),
078            button("reset", "Reset")
079         );
080
081      /**
082       * <form action='main.jsp' method='POST'>
083       * Position (1-10000): <input name='pos' type='number'
084       * value='1'/><br/>
085       * Limit (1-10000): <input name='pos' type='number'
086       * value='100'/><br/>
087       * <button type='submit'>Submit</button>
088       * <button type='reset'>Reset</button>
089       * </form>
090       */
091      html = HtmlSerializer.DEFAULT.serialize(mainJsp);
092
093      /**
094       * Produces
095       * {
096       *    a:{action:'main.jsp',method:'GET'},
097       *    c:[
098       *    {a:{type:'text',name:'first_name',value:'apache'}},{},
099       *    {a:{type:'text',name:'last_name',value:'juneau'}},{},
100       *    {a:{type:'submit'},c:['Submit']},
101       *    {a:{type:'reset'},c:['Reset']}
102       *    ]
103       * }
104       */
105      html =  JsonSerializer.create().simple().sq().build().serialize(mainJsp);
106
107      Feed feed =
108         feed("tag:juneau.apache.org", "Juneau ATOM specification", "2018-12-15T08:52:05Z")
109         .title("Example apache Juneau feed")
110         .subtitle(text("html").text("Describes <em>stuff</em> about Juneau"))
111         .links(
112            link("alternate", "text/html", "http://juneau.apache.org/").hreflang("en"),
113            link("self", "application/atom+xml", "http://juneau.apache.org/feed.atom")
114         )
115         .rights("Copyright (c) 2016, Apache Foundation")
116         .authors(new Person("Juneau_Commiter"))
117         .updated("2018-12-15T08:52:05Z")
118         .entries(
119            entry("tag:juneau.sample.com,2013:1.2345", "Juneau ATOM specification snapshot", "2016-01-02T03:04:05Z")
120            .published("2016-01-02T03:04:05Z")
121            .content(
122               content("xhtml")
123               .lang("en")
124               .base("http://www.apache.org/")
125               .text("<div><p><i>[Update: Juneau supports ATOM.]</i></p></div>")
126            )
127         );
128
129      Swagger swagger = swagger()
130         .swagger("2.0")
131         .info(
132            info("Swagger Petstore", "1.0.0")
133            .description("This is a sample server Petstore server.")
134            .termsOfService("http://swagger.io/terms/")
135            .contact(
136               contact().email("apiteam@swagger.io")
137            )
138            .license(
139               license("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0.html")
140            )
141         )
142         .path("/pet", "post",
143            operation()
144            .tags("pet")
145            .summary("Add a new pet to the store")
146            .description("")
147            .operationId("addPet")
148            .consumes(MediaType.JSON, MediaType.XML)
149            .produces(MediaType.JSON, MediaType.XML)
150            .parameters(
151               parameterInfo("body", "body")
152               .description("Pet object that needs to be added to the store")
153               .required(true)
154            )
155            .response("405", responseInfo("Invalid input"))
156         );
157
158      // Serialize to Swagger/JSON
159      /**
160       * Produces
161       * {
162       *  "swagger": "2.0",
163       *  "info": {
164       *      "title": "Swagger Petstore",
165       *      "description": "This is a sample server Petstore server.",
166       *      "version": "1.0.0",
167       *      "termsOfService": "http://swagger.io/terms/",
168       *      "contact": {
169       *          "email": "apiteam@swagger.io"
170       *      },
171       *      "license": {
172       *          "name": "Apache 2.0",
173       *          "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
174       *      }
175       *  },
176       * "paths": {
177       *      "/pet": {
178       *          "post": {
179       *              "tags": [
180       *                  "pet"
181       *               ],
182       *              "summary": "Add a new pet to the store",
183       *              "description": "",
184       *              "operationId": "addPet",
185       *              "consumes": [
186       *                  "application/json",
187       *                  "text/xml"
188       *              ],
189       *              "produces": [
190       *                  "application/json",
191       *                  "text/xml"
192       *              ],
193       *              "parameters": [
194       *                  {
195       *                      "in": "body",
196       *                      "name": "body",
197       *                      "description": "Pet object that needs to be added to the store",
198       *                      "required": true
199       *                  }
200       *              ],
201       *              "responses": {
202       *                  "405": {
203       *                      "description": "Invalid input"
204       *                  }
205       *              }
206       *         }
207       *      }
208       *  },
209       *  }
210       */
211      String swaggerJson = JsonSerializer.DEFAULT_READABLE.serialize(swagger);
212
213   }
214}