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