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 java.lang.reflect.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.parser.*;
019
020/**
021 * Interface used to convert HTTP headers, query parameters, form-data parameters, and URI path variables to POJOs
022 *
023 * <p>
024 * The following default implementations are provided:
025 * <ul class='doctree'>
026 *    <li class='jc'>{@link org.apache.juneau.oapi.OpenApiParser} - Parts encoded in based on OpenAPI schema.
027 *    <li class='jc'>{@link org.apache.juneau.uon.UonParser} - Parts encoded in UON notation.
028 *    <li class='jc'>{@link org.apache.juneau.httppart.SimplePartParser} - Parts encoded in plain text.
029 * </ul>
030 *
031 * <p>
032 * Implementations must include either a public no-args constructor or a public constructor that takes in a single
033 * {@link PropertyStore} object.
034 */
035public interface HttpPartParser {
036
037   /**
038    * Represent "no" part parser.
039    *
040    * <p>
041    * Used to represent the absence of a part parser in annotations.
042    */
043   public static interface Null extends HttpPartParser {}
044
045   /**
046    * Creates a new parser session.
047    *
048    * @param args The runtime arguments for the session.
049    * @return A new parser session.
050    */
051   public HttpPartParserSession createPartSession(ParserSessionArgs args);
052
053   /**
054    * Creates a new no-argument parser session.
055    *
056    * @return A new parser session.
057    */
058   public HttpPartParserSession createPartSession();
059
060   /**
061    * Convenience method for creating a no-arg session and parsing a part.
062    *
063    * @param partType The category of value being parsed.
064    * @param schema
065    *    Schema information about the part.
066    *    <br>May be <jk>null</jk>.
067    *    <br>Not all part parsers use the schema information.
068    * @param in The value being parsed.
069    * @param toType The POJO type to transform the input into.
070    * @return The parsed value.
071    * @throws ParseException Malformed input encountered.
072    * @throws SchemaValidationException If the input fails schema validation.
073    */
074   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException;
075
076   /**
077    * Convenience method for creating a no-arg session and parsing a part.
078    *
079    * @param partType The category of value being parsed.
080    * @param schema
081    *    Schema information about the part.
082    *    <br>May be <jk>null</jk>.
083    *    <br>Not all part parsers use the schema information.
084    * @param in The value being parsed.
085    * @param toType The POJO type to transform the input into.
086    * @param toTypeArgs The POJO type arguments for Collections and Maps.
087    * @return The parsed value.
088    * @throws ParseException Malformed input encountered.
089    * @throws SchemaValidationException If the input fails schema validation.
090    */
091   public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException;
092
093   /**
094    * Convenience method for creating a no-arg session and parsing a part of an unspecified part type.
095    *
096    * @param schema
097    *    Schema information about the part.
098    *    <br>May be <jk>null</jk>.
099    *    <br>Not all part parsers use the schema information.
100    * @param in The value being parsed.
101    * @param toType The POJO type to transform the input into.
102    * @return The parsed value.
103    * @throws ParseException Malformed input encountered.
104    * @throws SchemaValidationException If the input fails schema validation.
105    */
106   public <T> T parse(HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException;
107
108   /**
109    * Convenience method for creating a no-arg session and parsing a part of an unspecified part type.
110    *
111    * @param schema
112    *    Schema information about the part.
113    *    <br>May be <jk>null</jk>.
114    *    <br>Not all part parsers use the schema information.
115    * @param in The value being parsed.
116    * @param toType The POJO type to transform the input into.
117    * @param toTypeArgs The POJO type arguments for Collections and Maps.
118    * @return The parsed value.
119    * @throws ParseException Malformed input encountered.
120    * @throws SchemaValidationException If the input fails schema validation.
121    */
122   public <T> T parse(HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException;
123}