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.dto.openapi3;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017import static org.apache.juneau.internal.ConverterUtils.*;
018
019import org.apache.juneau.annotation.Bean;
020import org.apache.juneau.internal.*;
021
022import java.util.LinkedHashMap;
023import java.util.Map;
024import java.util.Set;
025
026/**
027 * information for Link object.
028 *
029 * <h5 class='section'>Example:</h5>
030 * <p class='bcode'>
031 *    <jc>// Construct using SwaggerBuilder.</jc>
032 *    Contact x = <jsm>contact</jsm>(<js>"API Support"</js>, <js>"http://www.swagger.io/support"</js>, <js>"support@swagger.io"</js>);
033 *
034 *    <jc>// Serialize using JsonSerializer.</jc>
035 *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.toString(x);
036 *
037 *    <jc>// Or just use toString() which does the same as above.</jc>
038 *    String json = x.toString();
039 * </p>
040 * <p class='bcode'>
041 *    <jc>// Output</jc>
042 *    {
043 *       <js>"name"</js>: <js>"API Support"</js>,
044 *       <js>"url"</js>: <js>"http://www.swagger.io/support"</js>,
045 *       <js>"email"</js>: <js>"support@swagger.io"</js>
046 *    }
047 * </p>
048 */
049@Bean(properties="authorizationUrl,tokenUrl,refreshUrl,scopes,*")
050@FluentSetters
051public class OAuthFlow extends OpenApiElement {
052
053   private String authorizationUrl;
054   private String tokenUrl;
055   private String refreshUrl;
056   private Map<String,String> scopes;
057
058
059   /**
060    * Default constructor.
061    */
062   public OAuthFlow() {}
063
064   /**
065    * Copy constructor.
066    *
067    * @param copyFrom The object to copy.
068    */
069   public OAuthFlow(OAuthFlow copyFrom) {
070      super(copyFrom);
071
072      this.authorizationUrl = copyFrom.authorizationUrl;
073      this.tokenUrl = copyFrom.tokenUrl;
074      this.refreshUrl = copyFrom.refreshUrl;
075
076      if (copyFrom.scopes == null)
077         this.scopes = null;
078      else
079         this.scopes = new LinkedHashMap<>(copyFrom.scopes);
080   }
081
082   /**
083    * Make a deep copy of this object.
084    *
085    * @return A deep copy of this object.
086    */
087   public OAuthFlow copy() {
088      return new OAuthFlow(this);
089   }
090
091   /**
092    * Bean property getter:  <property>operationRef</property>.
093    *
094    * <p>
095    * The identifying name of the contact person/organization.
096    *
097    * @return The property value, or <jk>null</jk> if it is not set.
098    */
099   public String getAuthorizationUrl() {
100      return authorizationUrl;
101   }
102
103   /**
104    * Bean property setter:  <property>operationRef</property>.
105    *
106    * <p>
107    * The identifying name of the contact person/organization.
108    *
109    * @param value
110    *    The new value for this property.
111    *    <br>Can be <jk>null</jk> to unset the property.
112    * @return This object
113    */
114   public OAuthFlow setAuthorizationUrl(String value) {
115      authorizationUrl = value;
116      return this;
117   }
118
119   /**
120    * Bean property getter:  <property>description</property>.
121    *
122    * <p>
123    * The URL pointing to the contact information.
124    *
125    * @return The property value, or <jk>null</jk> if it is not set.
126    */
127   public String getTokenUrl() {
128      return tokenUrl;
129   }
130
131   /**
132    * Bean property setter:  <property>description</property>.
133    * @param value
134    *    The new value for this property.
135    *    <br>Can be <jk>null</jk> to unset the property.
136    * @return This object
137    */
138   public OAuthFlow setTokenUrl(String value) {
139      tokenUrl = value;
140      return this;
141   }
142
143   /**
144    * Bean property getter:  <property>externalValue</property>.
145    *
146    * <p>
147    * The email address of the contact person/organization.
148    *
149    * @return The property value, or <jk>null</jk> if it is not set.
150    */
151   public String getRefreshUrl() {
152      return refreshUrl;
153   }
154
155   /**
156    * Bean property setter:  <property>externalValue</property>.
157    *
158    * <p>
159    * The email address of the contact person/organization.
160    *
161    * @param value
162    *    The new value for this property.
163    *    <br>MUST be in the format of an email address.
164    *    <br>Can be <jk>null</jk> to unset the property.
165    * @return This object
166    */
167   public OAuthFlow setRefreshUrl(String value) {
168      refreshUrl = value;
169      return this;
170   }
171
172   /**
173    * Bean property getter:  <property>examples</property>.
174    *
175    * <p>
176    * An example of the response message.
177    *
178    * @return The property value, or <jk>null</jk> if it is not set.
179    */
180   public Map<String,String> getScopes() {
181      return scopes;
182   }
183
184   /**
185    * Bean property setter:  <property>examples</property>.
186    *
187    * <p>
188    * An example of the response message.
189    *
190    * @param value
191    *    The new value for this property.
192    *    <br>Keys must be MIME-type strings.
193    *    <br>Can be <jk>null</jk> to unset the property.
194    * @return This object
195    */
196   public OAuthFlow setScopes(Map<String,String> value) {
197      scopes = copyOf(value);
198      return this;
199   }
200
201   /**
202    * Adds a single value to the <property>examples</property> property.
203    *
204    * @param name The mime-type string.
205    * @param description The example.
206    * @return This object
207    */
208   public OAuthFlow addScope(String name, String description) {
209      scopes = mapBuilder(scopes).sparse().add(name, description).build();
210      return this;
211   }
212
213   // <FluentSetters>
214
215   // </FluentSetters>
216
217   @Override /* OpenApiElement */
218   public <T> T get(String property, Class<T> type) {
219      if (property == null)
220         return null;
221      switch (property) {
222         case "refreshUrl": return toType(getRefreshUrl(), type);
223         case "tokenUrl": return toType(getTokenUrl(), type);
224         case "authorizationUrl": return toType(getAuthorizationUrl(), type);
225         case "scopes": return toType(getScopes(), type);
226         default: return super.get(property, type);
227      }
228   }
229
230   @Override /* OpenApiElement */
231   public OAuthFlow set(String property, Object value) {
232      if (property == null)
233         return this;
234      switch (property) {
235         case "authorizationUrl": return setAuthorizationUrl(stringify(value));
236         case "tokenUrl": return setTokenUrl(stringify(value));
237         case "refreshUrl": return setRefreshUrl(stringify(value));
238         case "scopes": return setScopes(mapBuilder(String.class,String.class).sparse().addAny(value).build());
239         default:
240            super.set(property, value);
241            return this;
242      }
243   }
244
245   @Override /* OpenApiElement */
246   public Set<String> keySet() {
247      Set<String> s = setBuilder(String.class)
248         .addIf(authorizationUrl != null, "authorizationUrl")
249         .addIf(tokenUrl != null, "tokenUrl")
250         .addIf(refreshUrl != null, "refreshUrl")
251         .addIf(scopes != null, "scopes")
252         .build();
253      return new MultiSet<>(s, super.keySet());
254   }
255}