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