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.UriResolver;
020import org.apache.juneau.annotation.Bean;
021import org.apache.juneau.internal.*;
022
023import java.net.URI;
024import java.net.URL;
025import java.util.*;
026
027/**
028 * TODO
029 */
030@Bean(properties="description,content,required,*")
031@FluentSetters
032public class RequestBodyInfo extends OpenApiElement{
033
034   private String description;
035   private Map<String,MediaType> content;
036   private Boolean required;
037
038   /**
039    * Default constructor.
040    */
041   public RequestBodyInfo() { }
042
043   /**
044    * Copy constructor.
045    *
046    * @param copyFrom The object to copy.
047    */
048   public RequestBodyInfo(RequestBodyInfo copyFrom) {
049      super(copyFrom);
050
051      this.description = copyFrom.description;
052      this.required = copyFrom.required;
053      if (copyFrom.content == null) {
054         this.content = null;
055      } else {
056         this.content = new LinkedHashMap<>();
057         for (Map.Entry<String,MediaType> e : copyFrom.content.entrySet())
058            this.content.put(e.getKey(),  e.getValue().copy());
059      }
060   }
061
062   /**
063    * Make a deep copy of this object.
064    *
065    * @return A deep copy of this object.
066    */
067   public RequestBodyInfo copy() {
068      return new RequestBodyInfo(this);
069   }
070
071   @Override /* OpenApiElement */
072   protected RequestBodyInfo strict() {
073      super.strict();
074      return this;
075   }
076
077   /**
078    * Bean property getter:  <property>contentType</property>.
079    *
080    * <p>
081    * The URL pointing to the contact information.
082    *
083    * @return The property value, or <jk>null</jk> if it is not set.
084    */
085   public String getDescription() {
086      return description;
087   }
088
089   /**
090    * Bean property setter:  <property>url</property>.
091    *
092    * <p>
093    * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
094    * <br>Strings must be valid URIs.
095    *
096    * <p>
097    * URIs defined by {@link UriResolver} can be used for values.
098    *
099    * @param value
100    *    The new value for this property.
101    *    <br>Can be <jk>null</jk> to unset the property.
102    * @return This object
103    */
104   public RequestBodyInfo setDescription(String value) {
105      description = value;
106      return this;
107   }
108
109   /**
110    * Bean property getter:  <property>content</property>.
111    *
112    * @return The property value, or <jk>null</jk> if it is not set.
113    */
114   public Map<String, MediaType> getContent() {
115      return content;
116   }
117
118   /**
119    * Bean property setter:  <property>content</property>.
120    *
121    * @param value
122    *    The new value for this property.
123    * @return This object
124    */
125   public RequestBodyInfo setContent(Map<String, MediaType> value) {
126      content = copyOf(value);
127      return this;
128   }
129
130   /**
131    * Adds one or more values to the <property>content</property> property.
132    *
133    * @param key The mapping key.
134    * @param value
135    *    The values to add to this property.
136    *    <br>Ignored if <jk>null</jk>.
137    * @return This object
138    */
139   public RequestBodyInfo addContent(String key, MediaType value) {
140      content = mapBuilder(content).sparse().add(key, value).build();
141      return this;
142   }
143
144   /**
145    * Bean property getter:  <property>required</property>.
146    *
147    * <p>
148    * The type of the object.
149    *
150    * @return The property value, or <jk>null</jk> if it is not set.
151    */
152   public Boolean getRequired() {
153      return required;
154   }
155
156   /**
157    * Bean property setter:  <property>explode</property>.
158    *
159    * <p>
160    * The type of the object.
161    *
162    * @param value
163    *    The new value for this property.
164    *    <br>Property value is required.
165    *    </ul>
166    * @return This object
167    */
168   public RequestBodyInfo setRequired(Boolean value) {
169      required = value;
170      return this;
171   }
172
173   // <FluentSetters>
174
175   // </FluentSetters>
176
177   @Override /* OpenApiElement */
178   public <T> T get(String property, Class<T> type) {
179      if (property == null)
180         return null;
181      switch (property) {
182         case "description": return toType(getDescription(), type);
183         case "content": return toType(getContent(), type);
184         case "required": return toType(getRequired(), type);
185         default: return super.get(property, type);
186      }
187   }
188
189   @Override /* OpenApiElement */
190   public RequestBodyInfo set(String property, Object value) {
191      if (property == null)
192         return this;
193      switch (property) {
194         case "description": return setDescription(stringify(value));
195         case "content": return setContent(mapBuilder(String.class,MediaType.class).sparse().addAny(value).build());
196         case "required": return setRequired(toBoolean(value));
197         default:
198            super.set(property, value);
199            return this;
200      }
201   }
202
203   @Override /* OpenApiElement */
204   public Set<String> keySet() {
205      Set<String> s = setBuilder(String.class)
206            .addIf(description != null, "description")
207            .addIf(content != null, "content")
208            .addIf(required != null, "required")
209            .build();
210      return new MultiSet<>(s, super.keySet());
211   }
212}