|
2 | 2 |
|
3 | 3 | import java.io.File; |
4 | 4 | import java.io.IOException; |
| 5 | +import java.io.InputStream; |
5 | 6 | import java.net.Socket; |
6 | 7 | import java.net.URL; |
7 | 8 | import java.security.GeneralSecurityException; |
|
12 | 13 | import java.util.Map; |
13 | 14 | import java.util.logging.Level; |
14 | 15 | import java.util.logging.Logger; |
15 | | - |
16 | 16 | import javax.net.ssl.HostnameVerifier; |
17 | 17 | import javax.net.ssl.SSLContext; |
18 | 18 | import javax.net.ssl.SSLEngine; |
|
29 | 29 | import javax.ws.rs.core.MultivaluedMap; |
30 | 30 | import javax.ws.rs.core.Response; |
31 | 31 | import javax.ws.rs.core.StreamingOutput; |
32 | | - |
33 | 32 | import org.gitlab4j.api.Constants.TokenType; |
34 | 33 | import org.gitlab4j.api.GitLabApi.ApiVersion; |
35 | 34 | import org.gitlab4j.api.utils.JacksonJson; |
|
38 | 37 | import org.glassfish.jersey.client.ClientConfig; |
39 | 38 | import org.glassfish.jersey.client.ClientProperties; |
40 | 39 | import org.glassfish.jersey.client.JerseyClientBuilder; |
| 40 | +import org.glassfish.jersey.media.multipart.BodyPart; |
41 | 41 | import org.glassfish.jersey.media.multipart.Boundary; |
42 | 42 | import org.glassfish.jersey.media.multipart.FormDataMultiPart; |
43 | 43 | import org.glassfish.jersey.media.multipart.MultiPart; |
44 | 44 | import org.glassfish.jersey.media.multipart.MultiPartFeature; |
45 | 45 | import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; |
| 46 | +import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart; |
46 | 47 |
|
47 | 48 |
|
48 | 49 | /** |
@@ -566,8 +567,7 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path |
566 | 567 | * @throws IOException if an error occurs while constructing the URL |
567 | 568 | */ |
568 | 569 | protected Response upload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException { |
569 | | - URL url = getApiUrl(pathArgs); |
570 | | - return (upload(name, fileToUpload, mediaTypeString, null, url)); |
| 570 | + return upload(name, fileToUpload, mediaTypeString, null, pathArgs); |
571 | 571 | } |
572 | 572 |
|
573 | 573 | /** |
@@ -600,23 +600,37 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString |
600 | 600 | * @throws IOException if an error occurs while constructing the URL |
601 | 601 | */ |
602 | 602 | protected Response upload(String name, File fileToUpload, String mediaTypeString, Form formData, URL url) throws IOException { |
| 603 | + MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null); |
| 604 | + FileDataBodyPart filePart = mediaType != null ? |
| 605 | + new FileDataBodyPart(name, fileToUpload, mediaType) : |
| 606 | + new FileDataBodyPart(name, fileToUpload); |
| 607 | + return upload(filePart, formData, url); |
| 608 | + } |
603 | 609 |
|
| 610 | + protected Response upload(String name, InputStream inputStream, String filename, String mediaTypeString, Object... pathArgs) throws IOException { |
| 611 | + URL url = getApiUrl(pathArgs); |
| 612 | + return (upload(name, inputStream, filename, mediaTypeString, null, url)); |
| 613 | + } |
| 614 | + |
| 615 | + protected Response upload(String name, InputStream inputStream, String filename, String mediaTypeString, Form formData, URL url) throws IOException { |
604 | 616 | MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null); |
605 | | - try (FormDataMultiPart multiPart = new FormDataMultiPart()) { |
| 617 | + StreamDataBodyPart streamDataBodyPart = mediaType != null ? |
| 618 | + new StreamDataBodyPart(name, inputStream, filename, mediaType) : |
| 619 | + new StreamDataBodyPart(name, inputStream, filename); |
| 620 | + return upload(streamDataBodyPart, formData, url); |
| 621 | + } |
606 | 622 |
|
| 623 | + protected Response upload(BodyPart bodyPart, Form formData, URL url) throws IOException { |
| 624 | + try (FormDataMultiPart multiPart = new FormDataMultiPart()) { |
607 | 625 | if (formData != null) { |
608 | | - MultivaluedMap<String, String> formParams = formData.asMap(); |
609 | | - formParams.forEach((key, values) -> { |
| 626 | + formData.asMap().forEach((key, values) -> { |
610 | 627 | if (values != null) { |
611 | 628 | values.forEach(value -> multiPart.field(key, value)); |
612 | 629 | } |
613 | 630 | }); |
614 | 631 | } |
615 | 632 |
|
616 | | - FileDataBodyPart filePart = mediaType != null ? |
617 | | - new FileDataBodyPart(name, fileToUpload, mediaType) : |
618 | | - new FileDataBodyPart(name, fileToUpload); |
619 | | - multiPart.bodyPart(filePart); |
| 633 | + multiPart.bodyPart(bodyPart); |
620 | 634 | final Entity<?> entity = Entity.entity(multiPart, Boundary.addBoundary(multiPart.getMediaType())); |
621 | 635 | return (invocation(url, null).post(entity)); |
622 | 636 | } |
|
0 commit comments