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