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