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