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.rest.httppart; 018 019import static org.apache.juneau.common.utils.ThrowableUtils.*; 020import static org.apache.juneau.httppart.HttpPartType.*; 021 022import java.io.*; 023import java.lang.reflect.*; 024import java.util.*; 025import java.util.regex.*; 026 027import org.apache.http.*; 028import org.apache.juneau.*; 029import org.apache.juneau.common.utils.*; 030import org.apache.juneau.httppart.*; 031import org.apache.juneau.rest.*; 032 033/** 034 * Represents a single form-data parameter on an HTTP request. 035 * 036 * <p> 037 * Typically accessed through the {@link RequestFormParams} class. 038 * 039 * <p> 040 * Some important methods on this class are: 041 * </p> 042 * <ul class='javatree'> 043 * <li class='jc'>{@link RequestFormParam} 044 * <ul class='spaced-list'> 045 * <li>Methods for retrieving simple string values: 046 * <ul class='javatreec'> 047 * <li class='jm'>{@link RequestFormParam#asString() asString()} 048 * <li class='jm'>{@link RequestFormParam#get() get()} 049 * <li class='jm'>{@link RequestFormParam#isPresent() isPresent()} 050 * <li class='jm'>{@link RequestFormParam#orElse(String) orElse(String)} 051 * </ul> 052 * <li>Methods for retrieving as other common types: 053 * <ul class='javatreec'> 054 * <li class='jm'>{@link RequestFormParam#asBoolean() asBoolean()} 055 * <li class='jm'>{@link RequestFormParam#asBooleanPart() asBooleanPart()} 056 * <li class='jm'>{@link RequestFormParam#asCsvArray() asCsvArray()} 057 * <li class='jm'>{@link RequestFormParam#asCsvArrayPart() asCsvArrayPart()} 058 * <li class='jm'>{@link RequestFormParam#asDate() asDate()} 059 * <li class='jm'>{@link RequestFormParam#asDatePart() asDatePart()} 060 * <li class='jm'>{@link RequestFormParam#asInteger() asInteger()} 061 * <li class='jm'>{@link RequestFormParam#asIntegerPart() asIntegerPart()} 062 * <li class='jm'>{@link RequestFormParam#asLong() asLong()} 063 * <li class='jm'>{@link RequestFormParam#asLongPart() asLongPart()} 064 * <li class='jm'>{@link RequestFormParam#asMatcher(Pattern) asMatcher(Pattern)} 065 * <li class='jm'>{@link RequestFormParam#asMatcher(String) asMatcher(String)} 066 * <li class='jm'>{@link RequestFormParam#asMatcher(String,int) asMatcher(String,int)} 067 * <li class='jm'>{@link RequestFormParam#asStringPart() asStringPart()} 068 * <li class='jm'>{@link RequestFormParam#asUriPart() asUriPart()} 069 * </ul> 070 * <li>Methods for retrieving as custom types: 071 * <ul class='javatreec'> 072 * <li class='jm'>{@link RequestFormParam#as(Class) as(Class)} 073 * <li class='jm'>{@link RequestFormParam#as(ClassMeta) as(ClassMeta)} 074 * <li class='jm'>{@link RequestFormParam#as(Type,Type...) as(Type,Type...)} 075 * <li class='jm'>{@link RequestFormParam#parser(HttpPartParserSession) parser(HttpPartParserSession)} 076 * <li class='jm'>{@link RequestFormParam#schema(HttpPartSchema) schema(HttpPartSchema)} 077 * </ul> 078 * <li>Methods for performing assertion checks: 079 * <ul class='javatreec'> 080 * <li class='jm'>{@link RequestFormParam#assertCsvArray() assertCsvArray()} 081 * <li class='jm'>{@link RequestFormParam#assertDate() assertDate()} 082 * <li class='jm'>{@link RequestFormParam#assertInteger() assertInteger()} 083 * <li class='jm'>{@link RequestFormParam#assertLong() assertLong()} 084 * <li class='jm'>{@link RequestFormParam#assertString() assertString()} 085 * </ul> 086 * <li>Other methods: 087 * <ul class='javatreec'> 088 * <li class='jm'>{@link RequestFormParam#getName() getName()} 089 * <li class='jm'>{@link RequestFormParam#getValue() getValue()} 090* </ul> 091 * </ul> 092 * 093 * <h5 class='section'>See Also:</h5><ul> 094 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpParts">HTTP Parts</a> 095 * </ul> 096 */ 097public class RequestFormParam extends RequestHttpPart implements NameValuePair { 098 099 private final jakarta.servlet.http.Part part; 100 101 /** 102 * Constructor. 103 * 104 * @param request The request object. 105 * @param part The HTTP part. 106 */ 107 public RequestFormParam(RestRequest request, jakarta.servlet.http.Part part) { 108 super(FORMDATA, request, part.getName(), null); 109 this.part = part; 110 } 111 112 /** 113 * Constructor. 114 * 115 * @param request The request object. 116 * @param name The parameter name. 117 * @param value The parameter value. 118 */ 119 public RequestFormParam(RestRequest request, String name, String value) { 120 super(FORMDATA, request, name, value); 121 this.part = null; 122 } 123 124 //------------------------------------------------------------------------------------------------------------------ 125 // Retrievers 126 //------------------------------------------------------------------------------------------------------------------ 127 128 @Override /* RequestHttpPart */ 129 public String getValue() { 130 if (value == null && part != null) 131 try { 132 value = IOUtils.read(part.getInputStream()); 133 } catch (IOException e) { 134 throw asRuntimeException(e); 135 } 136 return value; 137 } 138 139 /** 140 * Returns this part value as an input stream. 141 * 142 * @return This part value as an input stream. 143 * @throws IOException If an error occurs in retrieving the content. 144 */ 145 public InputStream getStream() throws IOException { 146 if (value != null) 147 return new ByteArrayInputStream(value.getBytes(IOUtils.UTF8)); 148 return part.getInputStream(); 149 } 150 151 /** 152 * Returns the content type of this part. 153 * 154 * @return The content type of this part, or <jk>null</jk> if not known. 155 */ 156 public String getContentType() { 157 return (part == null ? null : part.getContentType()); 158 } 159 160 /** 161 * Returns the value of the specified mime header as a String. 162 * 163 * <p> 164 * If the Part did not include a header of the specified name, this method returns null. 165 * If there are multiple headers with the same name, this method returns the first header in the part. 166 * The header name is case insensitive. 167 * You can use this method with any request header. 168 * 169 * @param name The header name. 170 * @return The value of the specified mime header as a String. 171 */ 172 public String getHeader(String name) { 173 return part.getHeader(name); 174 } 175 176 /** 177 * Returns the header names of this param. 178 * 179 * @return The header names of this param. 180 */ 181 public Collection<String> getHeaderNames() { 182 return part.getHeaderNames(); 183 } 184 185 /** 186 * Returns the values of the param header with the given name. 187 * 188 * @param name The param name. 189 * @return The values of the param header with the given name. 190 */ 191 public Collection<String> getHeaders(String name) { 192 return part.getHeaders(name); 193 } 194 195 /** 196 * Returns the size of this file. 197 * 198 * @return A long specifying the size of this part, in bytes. 199 */ 200 public long getSize() { 201 return part.getSize(); 202 } 203 204 /** 205 * Returns the file name specified by the client. 206 * 207 * @return The file name specified by the client. 208 */ 209 public String getSubmittedFileName() { 210 return part.getSubmittedFileName(); 211 } 212 213 @Override /* GENERATED */ 214 public RequestFormParam schema(HttpPartSchema value) { 215 super.schema(value); 216 return this; 217 } 218 @Override /* GENERATED */ 219 public RequestFormParam parser(HttpPartParserSession value) { 220 super.parser(value); 221 return this; 222 } 223}