|
29 | 29 | import org.apache.http.HttpEntity; |
30 | 30 | import org.apache.http.entity.mime.MultipartEntityBuilder; |
31 | 31 | import org.apache.http.entity.mime.content.ByteArrayBody; |
| 32 | +import org.apache.http.entity.mime.content.StringBody; |
32 | 33 |
|
33 | 34 | import javax.ws.rs.core.HttpHeaders; |
34 | 35 | import javax.ws.rs.core.MediaType; |
|
38 | 39 | import java.io.IOException; |
39 | 40 | import java.io.InputStream; |
40 | 41 | import java.io.UnsupportedEncodingException; |
41 | | -import java.net.URLDecoder; |
42 | 42 | import java.net.URLEncoder; |
43 | 43 | import java.nio.charset.Charset; |
44 | 44 | import java.nio.charset.StandardCharsets; |
@@ -144,34 +144,40 @@ public AwsProxyRequestBuilder formFilePart(String fieldName, String fileName, by |
144 | 144 | multipartBuilder = MultipartEntityBuilder.create(); |
145 | 145 | } |
146 | 146 | multipartBuilder.addPart(fieldName, new ByteArrayBody(content, fileName)); |
147 | | - HttpEntity bodyEntity = multipartBuilder.build(); |
148 | | - InputStream bodyStream = bodyEntity.getContent(); |
149 | | - byte[] buffer = new byte[bodyStream.available()]; |
150 | | - IOUtils.readFully(bodyStream, buffer); |
151 | | - request.setBody("\n\n" + new String(buffer, Charset.defaultCharset())); |
152 | | - if (request.getMultiValueHeaders() == null) { |
153 | | - request.setMultiValueHeaders(new Headers()); |
154 | | - } |
155 | | - request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, bodyEntity.getContentType().getValue()); |
156 | | - if (bodyEntity.getContentEncoding() != null) { |
157 | | - request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, bodyEntity.getContentEncoding().getValue()); |
158 | | - } |
159 | | - request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, bodyEntity.getContentLength() + ""); |
| 147 | + buildMultipartBody(); |
160 | 148 | return this; |
161 | 149 | } |
162 | 150 |
|
163 | | - public AwsProxyRequestBuilder formFieldPart(String fieldName, String fieldValue) { |
| 151 | + public AwsProxyRequestBuilder formFieldPart(String fieldName, String fieldValue) |
| 152 | + throws IOException { |
164 | 153 | if (request.getMultiValueHeaders() == null) { |
165 | 154 | request.setMultiValueHeaders(new Headers()); |
166 | 155 | } |
167 | | - request.getMultiValueHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA); |
168 | 156 | if (multipartBuilder == null) { |
169 | 157 | multipartBuilder = MultipartEntityBuilder.create(); |
170 | 158 | } |
171 | | - // TODO: implement |
| 159 | + multipartBuilder.addPart(fieldName, new StringBody(fieldValue)); |
| 160 | + buildMultipartBody(); |
172 | 161 | return this; |
173 | 162 | } |
174 | 163 |
|
| 164 | + private void buildMultipartBody() |
| 165 | + throws IOException { |
| 166 | + HttpEntity bodyEntity = multipartBuilder.build(); |
| 167 | + InputStream bodyStream = bodyEntity.getContent(); |
| 168 | + byte[] buffer = new byte[bodyStream.available()]; |
| 169 | + IOUtils.readFully(bodyStream, buffer); |
| 170 | + byte[] finalBuffer = new byte[buffer.length + 1]; |
| 171 | + byte[] newLineBytes = "\n\n".getBytes(LambdaContainerHandler.getContainerConfig().getDefaultContentCharset()); |
| 172 | + System.arraycopy(newLineBytes, 0, finalBuffer, 0, newLineBytes.length); |
| 173 | + System.arraycopy(buffer, 0, finalBuffer, newLineBytes.length - 1, buffer.length); |
| 174 | + request.setBody(Base64.getMimeEncoder().encodeToString(finalBuffer)); |
| 175 | + request.setIsBase64Encoded(true); |
| 176 | + this.request.setMultiValueHeaders(new Headers()); |
| 177 | + header(HttpHeaders.CONTENT_TYPE, bodyEntity.getContentType().getValue()); |
| 178 | + header(HttpHeaders.CONTENT_LENGTH, bodyEntity.getContentLength() + ""); |
| 179 | + } |
| 180 | + |
175 | 181 |
|
176 | 182 | public AwsProxyRequestBuilder header(String key, String value) { |
177 | 183 | if (this.request.getMultiValueHeaders() == null) { |
|
0 commit comments