|
1 | 1 | package org.gitlab4j.api;
|
2 | 2 |
|
| 3 | +import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE; |
| 4 | + |
| 5 | +import java.io.File; |
3 | 6 | import java.io.IOException;
|
4 | 7 | import java.net.Socket;
|
5 | 8 | import java.net.URL;
|
|
33 | 36 | import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
34 | 37 | import org.glassfish.jersey.client.ClientConfig;
|
35 | 38 | import org.glassfish.jersey.client.ClientProperties;
|
| 39 | +import org.glassfish.jersey.media.multipart.FormDataMultiPart; |
| 40 | +import org.glassfish.jersey.media.multipart.MultiPart; |
| 41 | +import org.glassfish.jersey.media.multipart.MultiPartFeature; |
| 42 | +import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; |
| 43 | + |
36 | 44 |
|
37 | 45 | /**
|
38 | 46 | * This class utilizes the Jersey client package to communicate with a GitLab API endpoint.
|
@@ -224,6 +232,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
|
224 | 232 | }
|
225 | 233 |
|
226 | 234 | clientConfig.register(JacksonJson.class);
|
| 235 | + clientConfig.register(MultiPartFeature.class); |
227 | 236 | }
|
228 | 237 |
|
229 | 238 | /**
|
@@ -450,6 +459,45 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
|
450 | 459 | return (invocation(url, null).post(Entity.entity(stream, mediaType)));
|
451 | 460 | }
|
452 | 461 |
|
| 462 | + /** |
| 463 | + * Perform a file upload as part of the , returning |
| 464 | + * a ClientResponse instance with the data returned from the endpoint. |
| 465 | + * |
| 466 | + * @param name the name for the form field that contains the file name |
| 467 | + * @param fileToUpload a File instance pointing to the file to upload |
| 468 | + * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload |
| 469 | + * @param pathArgs variable list of arguments used to build the URI |
| 470 | + * @return a ClientResponse instance with the data returned from the endpoint |
| 471 | + * @throws IOException if an error occurs while constructing the URL |
| 472 | + */ |
| 473 | + protected Response upload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException { |
| 474 | + URL url = getApiUrl(pathArgs); |
| 475 | + return (upload(name, fileToUpload, mediaTypeString, url)); |
| 476 | + } |
| 477 | + |
| 478 | + /** |
| 479 | + * Perform a file upload using multipart/form-data, returning |
| 480 | + * a ClientResponse instance with the data returned from the endpoint. |
| 481 | + * |
| 482 | + * @param name the name for the form field that contains the file name |
| 483 | + * @param fileToUpload a File instance pointing to the file to upload |
| 484 | + * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload |
| 485 | + * @param url the fully formed path to the GitLab API endpoint |
| 486 | + * @return a ClientResponse instance with the data returned from the endpoint |
| 487 | + * @throws IOException if an error occurs while constructing the URL |
| 488 | + */ |
| 489 | + protected Response upload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException { |
| 490 | + |
| 491 | + MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null); |
| 492 | + try (MultiPart multiPart = new FormDataMultiPart()) { |
| 493 | + FileDataBodyPart filePart = mediaType != null ? |
| 494 | + new FileDataBodyPart(name, fileToUpload, mediaType) : |
| 495 | + new FileDataBodyPart(name, fileToUpload); |
| 496 | + multiPart.bodyPart(filePart); |
| 497 | + return (invocation(url, null).post(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE))); |
| 498 | + } |
| 499 | + } |
| 500 | + |
453 | 501 | /**
|
454 | 502 | * Perform an HTTP PUT call with the specified form data and path objects, returning
|
455 | 503 | * a ClientResponse instance with the data returned from the endpoint.
|
|
0 commit comments