Skip to content

Commit cee18f5

Browse files
Mike Kistlerpadamstx
authored andcommitted
New FileWithMetadata class to support arrays of files in formdata
1 parent 01d2aac commit cee18f5

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2019.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.ibm.cloud.sdk.core.service.model;
15+
16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.FileNotFoundException;
19+
import java.io.InputStream;
20+
21+
/**
22+
* A file and its associated metadata
23+
*/
24+
public class FileWithMetadata {
25+
26+
private InputStream data;
27+
private String filename;
28+
private String contentType;
29+
30+
/**
31+
* Builder.
32+
*/
33+
public static class Builder {
34+
private InputStream data;
35+
private String filename;
36+
private String contentType;
37+
38+
private Builder(FileWithMetadata fileWithMetadata) {
39+
this.data = fileWithMetadata.data;
40+
this.filename = fileWithMetadata.filename;
41+
this.contentType = fileWithMetadata.contentType;
42+
}
43+
44+
/**
45+
* Instantiates a new builder.
46+
*/
47+
public Builder() {
48+
}
49+
50+
/**
51+
* Instantiates a new builder with required properties.
52+
*
53+
* @param data the data / contents of the file
54+
*/
55+
public Builder(InputStream data) {
56+
this.data = data;
57+
}
58+
59+
/**
60+
* Instantiates a new builder with required properties.
61+
*
62+
* @param file the file to use as the source of file contents and filename
63+
*
64+
* @throws FileNotFoundException if the file could not be found
65+
*/
66+
public Builder(File file) throws FileNotFoundException {
67+
this.data = new FileInputStream(file);
68+
this.filename = file.getName();
69+
}
70+
71+
/**
72+
* Builds a FileWithMetadata.
73+
*
74+
* @return the fileWithMetadata
75+
*/
76+
public FileWithMetadata build() {
77+
return new FileWithMetadata(this);
78+
}
79+
80+
/**
81+
* Set the filename.
82+
*
83+
* @param filename the filename
84+
* @return the FileWithMetadata builder
85+
*/
86+
public Builder filename(String filename) {
87+
this.filename = filename;
88+
return this;
89+
}
90+
91+
/**
92+
* Set the contentType.
93+
*
94+
* @param contentType the contentType
95+
* @return the FileWithMetadata builder
96+
*/
97+
public Builder contentType(String contentType) {
98+
this.contentType = contentType;
99+
return this;
100+
}
101+
}
102+
103+
private FileWithMetadata(Builder builder) {
104+
com.ibm.cloud.sdk.core.util.Validator.notNull(builder.data,
105+
"data cannot be null");
106+
data = builder.data;
107+
filename = builder.filename;
108+
contentType = builder.contentType;
109+
}
110+
111+
/**
112+
* New builder.
113+
*
114+
* @return a FileWithMetadata builder
115+
*/
116+
public Builder newBuilder() {
117+
return new Builder(this);
118+
}
119+
120+
/**
121+
* The data / contents of the file
122+
*/
123+
public InputStream data() {
124+
return this.data;
125+
}
126+
127+
/**
128+
* The filename for file.
129+
*/
130+
public String filename() {
131+
return this.filename;
132+
};
133+
134+
/**
135+
* The content type of file. Values for this parameter can be obtained from the HttpMediaType class.
136+
*/
137+
public String contentType() {
138+
return this.contentType;
139+
};
140+
141+
}

0 commit comments

Comments
 (0)