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