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.*;
016import org.apache.juneau.dto.atom.Feed;
017import org.apache.juneau.dto.swagger.Swagger;
018import org.apache.juneau.html.HtmlSerializer;
019import org.apache.juneau.json.*;
020
021import static org.apache.juneau.dto.atom.AtomBuilder.*;
022import static org.apache.juneau.dto.atom.AtomBuilder.link;
023import static org.apache.juneau.dto.html5.HtmlBuilder.*;
024import static org.apache.juneau.dto.swagger.SwaggerBuilder.*;
025
026import java.net.*;
027
028/**
029 * Sample class which shows the usage of DTO module which is a
030 * Sub module of the core.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 * </ul>
034 */
035public class DtoExample {
036
037   /**
038    * DTO Samples
039    *
040    * @param args Unused.
041    * @throws Exception Unused.
042    */
043   @SuppressWarnings("unused")
044   public static void main(String[] args) throws Exception {
045
046      //Produces
047      /**
048       * <table>
049       * <tr>
050       * <th>c1</th>
051       * <th>c2</th>
052       * </tr>
053       * <tr>
054       * <td>v1</td>
055       * <td>v2</td>
056       * </tr>
057       * </table>
058       */
059      Object mytable =
060         table(
061            tr(
062               th("c1"),
063               th("c2")
064            ),
065            tr(
066               td("v1"),
067               td("v2")
068            )
069         );
070
071      String html = HtmlSerializer.DEFAULT.serialize(mytable);
072
073      Object mainJsp =
074         form().action("main.jsp").method("GET")
075         .children(
076            input("text").name("first_name").value("apache"), br(),
077            input("text").name("last_name").value("juneau"), br(),
078            button("submit", "Submit"),
079            button("reset", "Reset")
080         );
081
082      /**
083       * <form action='main.jsp' method='POST'>
084       * Position (1-10000): <input name='pos' type='number'
085       * value='1'/><br/>
086       * Limit (1-10000): <input name='pos' type='number'
087       * value='100'/><br/>
088       * <button type='submit'>Submit</button>
089       * <button type='reset'>Reset</button>
090       * </form>
091       */
092      html = HtmlSerializer.DEFAULT.serialize(mainJsp);
093
094      /**
095       * Produces
096       * {
097       *    a:{action:'main.jsp',method:'GET'},
098       *    c:[
099       *    {a:{type:'text',name:'first_name',value:'apache'}},{},
100       *    {a:{type:'text',name:'last_name',value:'juneau'}},{},
101       *    {a:{type:'submit'},c:['Submit']},
102       *    {a:{type:'reset'},c:['Reset']}
103       *    ]
104       * }
105       */
106      html =  Json5Serializer.DEFAULT.serialize(mainJsp);
107
108      Feed feed =
109         feed("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
110         .setSubtitle(text("html").setText("Subtitle"))
111         .setLinks(
112            link("alternate", "text/html", "http://foo.org/").setHreflang("en"),
113            link("self", "application/atom+xml", "http://foo.org/feed.atom")
114         )
115         .setGenerator(
116            generator("Example Toolkit").setUri("http://www.foo.org/").setVersion("1.0")
117         )
118         .setEntries(
119            entry("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
120            .setLinks(
121               link("alternate", "text/html", "http://foo.org/2005/04/02/atom"),
122               link("enclosure", "audio/mpeg", "http://foo.org/audio/foobar.mp3").setLength(1337)
123            )
124            .setPublished("2016-12-31T05:02:03Z")
125            .setAuthors(
126               person("John Smith").setUri(new URI("http://foo.org/")).setEmail("foo@foo.org")
127            )
128            .setContributors(
129               person("John Smith"),
130               person("Jane Smith")
131            )
132            .setContent(
133               content("xhtml")
134               .setLang("en")
135               .setBase("http://foo.org/")
136               .setText("<div><p><i>[Sample content]</i></p></div>")
137            )
138         );
139
140      Swagger swagger = swagger()
141         .setSwagger("2.0")
142         .setInfo(
143            info("Swagger Petstore", "1.0.0")
144            .setDescription("This is a sample server Petstore server.")
145            .setTermsOfService("http://swagger.io/terms/")
146            .setContact(
147               contact().setEmail("apiteam@swagger.io")
148            )
149            .setLicense(
150               license("Apache 2.0").setUrl(URI.create("http://www.apache.org/licenses/LICENSE-2.0.html"))
151            )
152         )
153         .addPath("/pet", "post",
154            operation()
155            .setTags("pet")
156            .setSummary("Add a new pet to the store")
157            .setDescription("")
158            .setOperationId("addPet")
159            .setConsumes(MediaType.JSON, MediaType.XML)
160            .setProduces(MediaType.JSON, MediaType.XML)
161            .setParameters(
162               parameterInfo("body", "body")
163               .setDescription("Pet object that needs to be added to the store")
164               .setRequired(true)
165            )
166            .addResponse("405", responseInfo("Invalid input"))
167         );
168
169      // Serialize to Swagger/JSON
170      /**
171       * Produces
172       * {
173       *  "swagger": "2.0",
174       *  "info": {
175       *      "title": "Swagger Petstore",
176       *      "description": "This is a sample server Petstore server.",
177       *      "version": "1.0.0",
178       *      "termsOfService": "http://swagger.io/terms/",
179       *      "contact": {
180       *          "email": "apiteam@swagger.io"
181       *      },
182       *      "license": {
183       *          "name": "Apache 2.0",
184       *          "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
185       *      }
186       *  },
187       * "paths": {
188       *      "/pet": {
189       *          "post": {
190       *              "tags": [
191       *                  "pet"
192       *               ],
193       *              "summary": "Add a new pet to the store",
194       *              "description": "",
195       *              "operationId": "addPet",
196       *              "consumes": [
197       *                  "application/json",
198       *                  "text/xml"
199       *              ],
200       *              "produces": [
201       *                  "application/json",
202       *                  "text/xml"
203       *              ],
204       *              "parameters": [
205       *                  {
206       *                      "in": "body",
207       *                      "name": "body",
208       *                      "description": "Pet object that needs to be added to the store",
209       *                      "required": true
210       *                  }
211       *              ],
212       *              "responses": {
213       *                  "405": {
214       *                      "description": "Invalid input"
215       *                  }
216       *              }
217       *         }
218       *      }
219       *  },
220       *  }
221       */
222      String swaggerJson = JsonSerializer.DEFAULT_READABLE.serialize(swagger);
223
224   }
225}