![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.rest.httppart.RequestFormParam Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.rest.httppart;
import static org.apache.juneau.common.internal.ThrowableUtils.*;
import static org.apache.juneau.httppart.HttpPartType.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.regex.*;
import org.apache.http.*;
import org.apache.juneau.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.httppart.*;
import org.apache.juneau.rest.*;
/**
* Represents a single form-data parameter on an HTTP request.
*
*
* Typically accessed through the {@link RequestFormParams} class.
*
*
* Some important methods on this class are:
*
*
* - {@link RequestFormParam}
*
* - Methods for retrieving simple string values:
*
* - {@link RequestFormParam#asString() asString()}
*
- {@link RequestFormParam#get() get()}
*
- {@link RequestFormParam#isPresent() isPresent()}
*
- {@link RequestFormParam#orElse(String) orElse(String)}
*
* - Methods for retrieving as other common types:
*
* - {@link RequestFormParam#asBoolean() asBoolean()}
*
- {@link RequestFormParam#asBooleanPart() asBooleanPart()}
*
- {@link RequestFormParam#asCsvArray() asCsvArray()}
*
- {@link RequestFormParam#asCsvArrayPart() asCsvArrayPart()}
*
- {@link RequestFormParam#asDate() asDate()}
*
- {@link RequestFormParam#asDatePart() asDatePart()}
*
- {@link RequestFormParam#asInteger() asInteger()}
*
- {@link RequestFormParam#asIntegerPart() asIntegerPart()}
*
- {@link RequestFormParam#asLong() asLong()}
*
- {@link RequestFormParam#asLongPart() asLongPart()}
*
- {@link RequestFormParam#asMatcher(Pattern) asMatcher(Pattern)}
*
- {@link RequestFormParam#asMatcher(String) asMatcher(String)}
*
- {@link RequestFormParam#asMatcher(String,int) asMatcher(String,int)}
*
- {@link RequestFormParam#asStringPart() asStringPart()}
*
- {@link RequestFormParam#asUriPart() asUriPart()}
*
* - Methods for retrieving as custom types:
*
* - {@link RequestFormParam#as(Class) as(Class)}
*
- {@link RequestFormParam#as(ClassMeta) as(ClassMeta)}
*
- {@link RequestFormParam#as(Type,Type...) as(Type,Type...)}
*
- {@link RequestFormParam#parser(HttpPartParserSession) parser(HttpPartParserSession)}
*
- {@link RequestFormParam#schema(HttpPartSchema) schema(HttpPartSchema)}
*
* - Methods for performing assertion checks:
*
* - {@link RequestFormParam#assertCsvArray() assertCsvArray()}
*
- {@link RequestFormParam#assertDate() assertDate()}
*
- {@link RequestFormParam#assertInteger() assertInteger()}
*
- {@link RequestFormParam#assertLong() assertLong()}
*
- {@link RequestFormParam#assertString() assertString()}
*
* - Other methods:
*
* - {@link RequestFormParam#getName() getName()}
*
- {@link RequestFormParam#getValue() getValue()}
*
*
*
* See Also:
* - HTTP Parts
*
*/
public class RequestFormParam extends RequestHttpPart implements NameValuePair {
private final jakarta.servlet.http.Part part;
/**
* Constructor.
*
* @param request The request object.
* @param part The HTTP part.
*/
public RequestFormParam(RestRequest request, jakarta.servlet.http.Part part) {
super(FORMDATA, request, part.getName(), null);
this.part = part;
}
/**
* Constructor.
*
* @param request The request object.
* @param name The parameter name.
* @param value The parameter value.
*/
public RequestFormParam(RestRequest request, String name, String value) {
super(FORMDATA, request, name, value);
this.part = null;
}
//------------------------------------------------------------------------------------------------------------------
// Retrievers
//------------------------------------------------------------------------------------------------------------------
@Override /* RequestHttpPart */
public String getValue() {
if (value == null && part != null)
try {
value = IOUtils.read(part.getInputStream());
} catch (IOException e) {
throw asRuntimeException(e);
}
return value;
}
/**
* Returns this part value as an input stream.
*
* @return This part value as an input stream.
* @throws IOException If an error occurs in retrieving the content.
*/
public InputStream getStream() throws IOException {
if (value != null)
return new ByteArrayInputStream(value.getBytes(IOUtils.UTF8));
return part.getInputStream();
}
/**
* Returns the content type of this part.
*
* @return The content type of this part, or null if not known.
*/
public String getContentType() {
return (part == null ? null : part.getContentType());
}
/**
* Returns the value of the specified mime header as a String.
*
*
* If the Part did not include a header of the specified name, this method returns null.
* If there are multiple headers with the same name, this method returns the first header in the part.
* The header name is case insensitive.
* You can use this method with any request header.
*
* @param name The header name.
* @return The value of the specified mime header as a String.
*/
public String getHeader(String name) {
return part.getHeader(name);
}
/**
* Returns the header names of this param.
*
* @return The header names of this param.
*/
public Collection getHeaderNames() {
return part.getHeaderNames();
}
/**
* Returns the values of the param header with the given name.
*
* @param name The param name.
* @return The values of the param header with the given name.
*/
public Collection getHeaders(String name) {
return part.getHeaders(name);
}
/**
* Returns the size of this file.
*
* @return A long specifying the size of this part, in bytes.
*/
public long getSize() {
return part.getSize();
}
/**
* Returns the file name specified by the client.
*
* @return The file name specified by the client.
*/
public String getSubmittedFileName() {
return part.getSubmittedFileName();
}
//
@Override /* GENERATED */
public RequestFormParam schema(HttpPartSchema value) {
super.schema(value);
return this;
}
@Override /* GENERATED */
public RequestFormParam parser(HttpPartParserSession value) {
super.parser(value);
return this;
}
//
}