Files
2026-11-21 17:07:18 -05:00

212 lines
6.7 KiB
Java

/* */ package kong.unirest.cosmetics;
/* */
/* */ import java.io.File;
/* */ import java.io.InputStream;
/* */ import java.nio.charset.Charset;
/* */ import java.util.ArrayList;
/* */ import java.util.Collection;
/* */ import java.util.Collections;
/* */ import java.util.List;
/* */ import java.util.Map;
/* */ import java.util.Optional;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ class HttpRequestMultiPart
/* */ extends BaseRequest<MultipartBody>
/* */ implements MultipartBody
/* */ {
/* 34 */ private List<BodyPart> parameters = new ArrayList<>();
/* */
/* 36 */ private MultipartMode mode = MultipartMode.BROWSER_COMPATIBLE;
/* */ private Charset charSet;
/* */ private boolean forceMulti = false;
/* */ private ProgressMonitor monitor;
/* */
/* */ HttpRequestMultiPart(HttpRequestBody httpRequest) {
/* 42 */ super(httpRequest);
/* 43 */ this.charSet = httpRequest.getCharset();
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, String value) {
/* 48 */ addPart(new ParamPart(name, value));
/* 49 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, String value, String contentType) {
/* 54 */ addPart(name, value, contentType);
/* 55 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, Collection<?> collection) {
/* 60 */ for (Object current : collection) {
/* 61 */ addPart(name, current, null);
/* */ }
/* 63 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, InputStream value, ContentType contentType) {
/* 68 */ addPart(new InputStreamPart(name, value, contentType.toString()));
/* 69 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, File file) {
/* 74 */ addPart(new FilePart(file, name));
/* 75 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, File file, String contentType) {
/* 80 */ addPart(new FilePart(file, name, contentType));
/* 81 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName) {
/* 86 */ addPart(new InputStreamPart(name, stream, contentType.toString(), fileName));
/* */
/* 88 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, InputStream stream, String fileName) {
/* 93 */ addPart(new InputStreamPart(name, stream, ContentType.APPLICATION_OCTET_STREAM.toString(), fileName));
/* 94 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, byte[] bytes, ContentType contentType, String fileName) {
/* 99 */ addPart(new ByteArrayPart(name, bytes, contentType, fileName));
/* 100 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody field(String name, byte[] bytes, String fileName) {
/* 105 */ addPart(new ByteArrayPart(name, bytes, ContentType.APPLICATION_OCTET_STREAM, fileName));
/* 106 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody charset(Charset charset) {
/* 111 */ this.charSet = charset;
/* 112 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody contentType(String mimeType) {
/* 117 */ header("Content-Type", mimeType);
/* 118 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody mode(MultipartMode value) {
/* 123 */ this.mode = value;
/* 124 */ return this;
/* */ }
/* */
/* */
/* */ public MultipartBody uploadMonitor(ProgressMonitor uploadMonitor) {
/* 129 */ this.monitor = uploadMonitor;
/* 130 */ return this;
/* */ }
/* */
/* */
/* */ public Charset getCharset() {
/* 135 */ return this.charSet;
/* */ }
/* */
/* */ public MultipartBody fields(Map<String, Object> fields) {
/* 139 */ if (fields != null) {
/* 140 */ for (Map.Entry<String, Object> param : fields.entrySet()) {
/* 141 */ if (param.getValue() instanceof File) {
/* 142 */ field(param.getKey(), (File)param.getValue()); continue;
/* */ }
/* 144 */ field(param.getKey(), Util.nullToEmpty(param.getValue()));
/* */ }
/* */ }
/* */
/* 148 */ return this;
/* */ }
/* */
/* */ public MultipartBody field(String name, Object value, String contentType) {
/* 152 */ addPart(name, value, contentType);
/* 153 */ return this;
/* */ }
/* */
/* */ private void addPart(String name, Object value, String contentType) {
/* 157 */ if (value instanceof InputStream) {
/* 158 */ addPart(new InputStreamPart(name, (InputStream)value, contentType));
/* 159 */ } else if (value instanceof File) {
/* 160 */ addPart(new FilePart((File)value, name, contentType));
/* */ } else {
/* 162 */ addPart(new ParamPart(name, Util.nullToEmpty(value), contentType));
/* */ }
/* */ }
/* */
/* */ private void addPart(BodyPart value) {
/* 167 */ this.parameters.add(value);
/* 168 */ Collections.sort(this.parameters);
/* */ }
/* */
/* */
/* */ public Optional<Body> getBody() {
/* 173 */ return Optional.of(this);
/* */ }
/* */
/* */
/* */ public boolean isMultiPart() {
/* 178 */ return (this.forceMulti || multiParts().stream().anyMatch(BodyPart::isFile));
/* */ }
/* */
/* */
/* */ public boolean isEntityBody() {
/* 183 */ return false;
/* */ }
/* */
/* */
/* */ public Collection<BodyPart> multiParts() {
/* 188 */ return new ArrayList<>(this.parameters);
/* */ }
/* */
/* */
/* */ public MultipartMode getMode() {
/* 193 */ return this.mode;
/* */ }
/* */
/* */
/* */ public ProgressMonitor getMonitor() {
/* 198 */ return this.monitor;
/* */ }
/* */
/* */
/* */ MultipartBody forceMultiPart(boolean value) {
/* 203 */ this.forceMulti = value;
/* 204 */ return this;
/* */ }
/* */ }
/* Location: C:\Users\Xiao\Downloads\MinehutCosmetics.jar!\kon\\unirest\cosmetics\HttpRequestMultiPart.class
* Java compiler version: 11 (55.0)
* JD-Core Version: 1.1.3
*/