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.httppart;
014
015import org.apache.juneau.*;
016import org.apache.juneau.http.annotation.*;
017import org.apache.juneau.serializer.*;
018
019/**
020 * Interface used to convert POJOs to simple strings in HTTP headers, query parameters, form-data parameters, and URI
021 * path variables.
022 *
023 * <p>
024 * The following default implementations are provided:
025 * <ul class='doctree'>
026 *    <li class='jc'>{@link org.apache.juneau.oapi.OpenApiSerializer} - Parts encoded based on OpenAPI schema.
027 *    <li class='jc'>{@link org.apache.juneau.uon.UonSerializer} - Parts encoded in UON notation.
028 *    <li class='jc'>{@link org.apache.juneau.httppart.SimplePartSerializer} - Parts encoded in plain text.
029 * </ul>
030 *
031 * <p>
032 * This class is used in the following locations:
033 * <ul>
034 *    <li class='ja'>{@link FormData#serializer()}
035 *    <li class='ja'>{@link Query#serializer()}
036 *    <li class='ja'>{@link Header#serializer()}
037 *    <li class='ja'>{@link Path#serializer()}
038 *    <li class='ja'>{@link Request#partSerializer()}
039 *    <li class='ja'>{@link Response#partSerializer()}
040 *    <li class='jc'><code>RestClientBuilder.partSerializer(Class)</code>
041 * </ul>
042 *
043 * <p>
044 * Implementations must include either a public no-args constructor or a public constructor that takes in a single
045 * {@link PropertyStore} object.
046 */
047public interface HttpPartSerializer {
048
049   /**
050    * Represent "no" part part serializer.
051    *
052    * <p>
053    * Used to represent the absence of a part serializer in annotations.
054    */
055   public static interface Null extends HttpPartSerializer {}
056
057   /**
058    * Creates a new serializer session.
059    *
060    * @param args The runtime arguments for the session.
061    * @return A new serializer session.
062    */
063   public HttpPartSerializerSession createPartSession(SerializerSessionArgs args);
064
065   /**
066    * Creates a new no-argument serializer session.
067    *
068    * @return A new serializer session.
069    */
070   public HttpPartSerializerSession createPartSession();
071
072   /**
073    * Convenience method for creating a no-arg session and serializing a part.
074    *
075    * @param partType The category of value being serialized.
076    * @param schema
077    *    Schema information about the part.
078    *    <br>May be <jk>null</jk>.
079    *    <br>Not all part serializer use the schema information.
080    * @param value The value being serialized.
081    * @return The serialized value.
082    * @throws SerializeException If a problem occurred while trying to parse the input.
083    * @throws SchemaValidationException If the output fails schema validation.
084    */
085   public String serialize(HttpPartType partType, HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException ;
086
087   /**
088    * Convenience method for creating a no-arg session and serializing a part with no specified part type.
089    *
090    * @param schema
091    *    Schema information about the part.
092    *    <br>May be <jk>null</jk>.
093    *    <br>Not all part serializer use the schema information.
094    * @param value The value being serialized.
095    * @return The serialized value.
096    * @throws SerializeException If a problem occurred while trying to parse the input.
097    * @throws SchemaValidationException If the output fails schema validation.
098    */
099   public String serialize(HttpPartSchema schema, Object value) throws SchemaValidationException, SerializeException ;
100
101   /**
102    * @deprecated Use {@link #serialize(HttpPartType, HttpPartSchema, Object)}
103    */
104   @SuppressWarnings("javadoc")
105   @Deprecated
106   public String serialize(HttpPartType type, Object value);
107}