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.bean.openapi3; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021import static org.apache.juneau.marshall.internal.ConverterUtils.*; 022 023import java.net.*; 024import java.util.*; 025 026import org.apache.juneau.commons.collections.*; 027import org.apache.juneau.marshall.*; 028 029/** 030 * License information for the exposed API. 031 * 032 * <p> 033 * The License Object provides license information for the exposed API. This information helps clients understand 034 * the terms under which the API can be used, including any restrictions or requirements. 035 * 036 * <h5 class='section'>OpenAPI Specification:</h5> 037 * <p> 038 * The License Object is composed of the following fields: 039 * <ul class='spaced-list'> 040 * <li><c>name</c> (string, REQUIRED) - The license name used for the API 041 * <li><c>url</c> (string) - A URL to the license used for the API 042 * </ul> 043 * 044 * <h5 class='section'>Example:</h5> 045 * <p class='bcode'> 046 * <jc>// Construct using SwaggerBuilder.</jc> 047 * License <jv>x</jv> = <jsm>license</jsm>(<js>"Apache 2.0"</js>, <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>); 048 * 049 * <jc>// Serialize using JsonSerializer.</jc> 050 * String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>); 051 * 052 * <jc>// Or just use toString() which does the same as above.</jc> 053 * String <jv>json</jv> = <jv>x</jv>.toString(); 054 * </p> 055 * <p class='bcode'> 056 * <jc>// Output</jc> 057 * { 058 * <js>"name"</js>: <js>"Apache 2.0"</js>, 059 * <js>"url"</js>: <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js> 060 * } 061 * </p> 062 * 063 * <h5 class='section'>See Also:</h5><ul> 064 * <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#license-object">OpenAPI Specification > License Object</a> 065 * <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/api-general-info/">OpenAPI API General Info</a> 066 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a> 067 * </ul> 068 */ 069@SuppressWarnings({ 070 "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants 071}) 072public class License extends OpenApiElement { 073 074 private String name; 075 private URI url; 076 077 /** 078 * Default constructor. 079 */ 080 public License() {} 081 082 /** 083 * Copy constructor. 084 * 085 * @param copyFrom The object to copy. 086 */ 087 public License(License copyFrom) { 088 super(copyFrom); 089 090 this.name = copyFrom.name; 091 this.url = copyFrom.url; 092 } 093 094 /** 095 * Make a deep copy of this object. 096 * 097 * @return A deep copy of this object. 098 */ 099 public License copy() { 100 return new License(this); 101 } 102 103 @Override /* Overridden from OpenApiElement */ 104 public <T> T get(String property, Class<T> type) { 105 reqnn("property", property); 106 return switch (property) { 107 case "name" -> toType(getName(), type); 108 case "url" -> toType(getUrl(), type); 109 default -> super.get(property, type); 110 }; 111 } 112 113 /** 114 * Bean property getter: <property>name</property>. 115 * 116 * <p> 117 * The license name used for the API. 118 * 119 * @return The property value, or <jk>null</jk> if it is not set. 120 */ 121 public String getName() { return name; } 122 123 /** 124 * Bean property getter: <property>url</property>. 125 * 126 * <p> 127 * A URL to the license used for the API. 128 * 129 * @return The property value, or <jk>null</jk> if it is not set. 130 */ 131 public URI getUrl() { return url; } 132 133 @Override /* Overridden from OpenApiElement */ 134 public Set<String> keySet() { 135 // @formatter:off 136 var s = stb(String.class) 137 .addIf(nn(name), "name") 138 .addIf(nn(url), "url") 139 .build(); 140 // @formatter:on 141 return new MultiSet<>(s, super.keySet()); 142 } 143 144 @Override /* Overridden from OpenApiElement */ 145 public License set(String property, Object value) { 146 reqnn("property", property); 147 return switch (property) { 148 case "name" -> setName(s(value)); 149 case "url" -> setUrl(toUri(value)); 150 default -> { 151 super.set(property, value); 152 yield this; 153 } 154 }; 155 } 156 157 /** 158 * Bean property setter: <property>name</property>. 159 * 160 * <p> 161 * The license name used for the API. 162 * 163 * @param value 164 * The new value for this property. 165 * <br>Property value is required. 166 * <br>Can be <jk>null</jk> to unset the property. 167 * @return This object 168 */ 169 public License setName(String value) { 170 name = value; 171 return this; 172 } 173 174 /** 175 * Bean property setter: <property>url</property>. 176 * 177 * <p> 178 * A URL to the license used for the API. 179 * 180 * @param value 181 * The new value for this property. 182 * <br>URIs defined by {@link UriResolver} can be used for values. 183 * <br>Can be <jk>null</jk> to unset the property. 184 * @return This object 185 */ 186 public License setUrl(URI value) { 187 url = value; 188 return this; 189 } 190 191 @Override /* Overridden from OpenApiElement */ 192 public License strict() { 193 super.strict(); 194 return this; 195 } 196 197 @Override /* Overridden from OpenApiElement */ 198 public License strict(Object value) { 199 super.strict(value); 200 return this; 201 } 202}