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.server.httppart; 018 019import static org.apache.juneau.commons.httppart.HttpPartType.*; 020import static org.apache.juneau.commons.utils.IoUtils.*; 021import static org.apache.juneau.commons.utils.Shorts.*; 022import static org.apache.juneau.commons.utils.ThrowableUtils.*; 023 024import java.io.*; 025import java.lang.reflect.*; 026import java.util.*; 027import java.util.regex.*; 028 029import org.apache.juneau.commons.utils.*; 030import org.apache.juneau.http.HttpPart; 031import org.apache.juneau.marshall.*; 032import org.apache.juneau.marshall.httppart.*; 033import org.apache.juneau.rest.server.*; 034 035/** 036 * Represents a single form-data parameter on an HTTP request. 037 * 038 * <p> 039 * Typically accessed through the {@link RequestFormParamList} 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/RestServerHttpParts">HTTP parts in REST servers</a> 097 * </ul> 098 */ 099@SuppressWarnings({ 100 "java:S2160", // equals() inherited from parent context; form param identity based on name+value from parent 101 "resource" // RequestFormParam manages Closeable resources 102}) 103public class RequestFormParam extends RequestHttpPart implements HttpPart { 104 105 private final jakarta.servlet.http.Part part; 106 107 /** 108 * Constructor. 109 * 110 * @param request The request object. 111 * @param part The HTTP part. 112 */ 113 public RequestFormParam(RestRequest request, jakarta.servlet.http.Part part) { 114 super(FORMDATA, request, part.getName(), null); 115 this.part = part; 116 } 117 118 /** 119 * Constructor. 120 * 121 * @param request The request object. 122 * @param name The parameter name. 123 * @param value The parameter value. 124 */ 125 public RequestFormParam(RestRequest request, String name, String value) { 126 super(FORMDATA, request, name, value); 127 this.part = null; 128 } 129 130 @Override /* Overridden from RequestHttpPart */ 131 public RequestFormParam def(String def) { 132 super.def(def); 133 return this; 134 } 135 136 /** 137 * Returns the content type of this part. 138 * 139 * @return The content type of this part, or <jk>null</jk> if not known. 140 */ 141 public String getContentType() { return (part == null ? null : part.getContentType()); } 142 143 /** 144 * Returns the value of the specified mime header as a String. 145 * 146 * <p> 147 * If the Part did not include a header of the specified name, this method returns null. 148 * If there are multiple headers with the same name, this method returns the first header in the part. 149 * The header name is case insensitive. 150 * You can use this method with any request header. 151 * 152 * @param name The header name. 153 * @return The value of the specified mime header as a String. 154 */ 155 public String getHeader(String name) { 156 return part.getHeader(name); 157 } 158 159 /** 160 * Returns the header names of this param. 161 * 162 * @return The header names of this param. 163 */ 164 public Collection<String> getHeaderNames() { return part.getHeaderNames(); } 165 166 /** 167 * Returns the values of the param header with the given name. 168 * 169 * @param name The param name. 170 * @return The values of the param header with the given name. 171 */ 172 public Collection<String> getHeaders(String name) { 173 return part.getHeaders(name); 174 } 175 176 /** 177 * Returns the size of this file. 178 * 179 * @return A long specifying the size of this part, in bytes. 180 */ 181 public long getSize() { return part.getSize(); } 182 183 /** 184 * Returns this part value as an input stream. 185 * 186 * @return This part value as an input stream. 187 * @throws IOException If an error occurs in retrieving the content. 188 */ 189 public InputStream getStream() throws IOException { 190 if (nn(value)) 191 return new ByteArrayInputStream(value.getBytes(UTF8)); 192 return part.getInputStream(); 193 } 194 195 /** 196 * Returns the file name specified by the client. 197 * 198 * @return The file name specified by the client. 199 */ 200 public String getSubmittedFileName() { return part.getSubmittedFileName(); } 201 202 @Override /* Overridden from RequestHttpPart */ 203 public String getValue() { 204 if (value == null && nn(part)) 205 try { 206 value = IoUtils.read(part.getInputStream()); 207 } catch (IOException e) { 208 throw toRex(e); 209 } 210 return value; 211 } 212 213 @Override /* Overridden from RequestHttpPart */ 214 public RequestFormParam parser(HttpPartParserSession value) { 215 super.parser(value); 216 return this; 217 } 218 219 @Override /* Overridden from RequestHttpPart */ 220 public RequestFormParam schema(HttpPartSchema value) { 221 super.schema(value); 222 return this; 223 } 224}