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.httppart; 018 019import static org.apache.juneau.commons.utils.AssertionUtils.*; 020 021import java.lang.reflect.*; 022 023import org.apache.juneau.*; 024import org.apache.juneau.parser.*; 025 026/** 027 * Base class for implementations of {@link HttpPartParser} 028 * 029 * <h5 class='section'>Notes:</h5><ul> 030 * <li class='note'>This class is thread safe and reusable. 031 * </ul> 032 * 033 * <h5 class='section'>See Also:</h5><ul> 034 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a> 035 * </ul> 036 */ 037public abstract class BaseHttpPartParser extends BeanContextable implements HttpPartParser { 038 039 /** 040 * Builder class. 041 */ 042 public abstract static class Builder extends BeanContextable.Builder { 043 044 /** 045 * Constructor. 046 */ 047 protected Builder() {} 048 049 /** 050 * Copy constructor. 051 * 052 * @param builder The builder to copy. 053 * <br>Cannot be <jk>null</jk>. 054 */ 055 protected Builder(Builder builder) { 056 super(assertArgNotNull("builder", builder)); 057 } 058 } 059 060 /** 061 * Constructor. 062 * 063 * @param builder The builder for this object. 064 */ 065 protected BaseHttpPartParser(Builder builder) { 066 super(builder); 067 } 068 069 @Override /* Overridden from HttpPartParser */ 070 public <T> ClassMeta<T> getClassMeta(Class<T> c) { 071 return BeanContext.DEFAULT.getClassMeta(c); 072 } 073 074 @Override /* Overridden from HttpPartParser */ 075 public <T> ClassMeta<T> getClassMeta(Type t, Type...args) { 076 return BeanContext.DEFAULT.getClassMeta(t, args); 077 } 078 079 /** 080 * Converts the specified input to the specified class type. 081 * 082 * @param <T> The POJO type to transform the input into. 083 * @param partType The part type being parsed. 084 * <br>Can be <jk>null</jk> (will default to {@link HttpPartType#OTHER}). 085 * @param schema 086 * Schema information about the part. 087 * <br>Can be <jk>null</jk>. 088 * <br>Not all part parsers use the schema information. 089 * @param in The input being parsed. 090 * <br>Can be <jk>null</jk> (will return <jk>null</jk> or use schema default if available). 091 * @param toType The POJO type to transform the input into. 092 * <br>Cannot be <jk>null</jk>. 093 * @return The parsed value. 094 * @throws ParseException Malformed input encountered. 095 * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation. 096 */ 097 public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Class<T> toType) throws ParseException, SchemaValidationException { 098 return getPartSession().parse(partType, schema, in, getClassMeta(assertArgNotNull("toType", toType))); 099 } 100 101 /** 102 * Converts the specified input to the specified class type. 103 * 104 * @param <T> The POJO type to transform the input into. 105 * @param partType The part type being parsed. 106 * <br>Can be <jk>null</jk> (will default to {@link HttpPartType#OTHER}). 107 * @param schema 108 * Schema information about the part. 109 * <br>Can be <jk>null</jk>. 110 * <br>Not all part parsers use the schema information. 111 * @param in The input being parsed. 112 * <br>Can be <jk>null</jk> (will return <jk>null</jk> or use schema default if available). 113 * @param toType The POJO type to transform the input into. 114 * <br>Cannot be <jk>null</jk>. 115 * @return The parsed value. 116 * @throws ParseException Malformed input encountered. 117 * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation. 118 */ 119 public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, ClassMeta<T> toType) throws ParseException, SchemaValidationException { 120 return getPartSession().parse(partType, schema, in, assertArgNotNull("toType", toType)); 121 } 122 123 /** 124 * Converts the specified input to the specified class type. 125 * 126 * @param <T> The POJO type to transform the input into. 127 * @param partType The part type being parsed. 128 * <br>Can be <jk>null</jk> (will default to {@link HttpPartType#OTHER}). 129 * @param schema 130 * Schema information about the part. 131 * <br>Can be <jk>null</jk>. 132 * <br>Not all part parsers use the schema information. 133 * @param in The input being parsed. 134 * <br>Can be <jk>null</jk> (will return <jk>null</jk> or use schema default if available). 135 * @param toType The POJO type to transform the input into. 136 * <br>Cannot be <jk>null</jk>. 137 * @param toTypeArgs The generic type arguments of the POJO type to transform the input into. 138 * <br>Cannot contain <jk>null</jk> values. 139 * @return The parsed value. 140 * @throws ParseException Malformed input encountered. 141 * @throws SchemaValidationException If the input or resulting HTTP part object fails schema validation. 142 */ 143 public <T> T parse(HttpPartType partType, HttpPartSchema schema, String in, Type toType, Type...toTypeArgs) throws ParseException, SchemaValidationException { 144 assertArgNoNulls("toTypeArgs", toTypeArgs); 145 return getPartSession().parse(partType, schema, in, getClassMeta(assertArgNotNull("toType", toType), toTypeArgs)); 146 } 147}