Skip to content

Commit 03d69b8

Browse files
committed
Mods to support file upload (#171).
1 parent f1a7383 commit 03d69b8

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@
227227
<artifactId>jersey-apache-connector</artifactId>
228228
<version>${jersey.version}</version>
229229
</dependency>
230+
<dependency>
231+
<groupId>org.glassfish.jersey.media</groupId>
232+
<artifactId>jersey-media-multipart</artifactId>
233+
<version>${jersey.version}</version>
234+
</dependency>
235+
230236
<dependency>
231237
<groupId>javax.servlet</groupId>
232238
<artifactId>javax.servlet-api</artifactId>

src/main/java/org/gitlab4j/api/AbstractApi.java

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.gitlab4j.api;
22

3+
import java.io.File;
34
import java.net.URL;
45
import java.net.URLEncoder;
56

@@ -147,7 +148,7 @@ protected Response post(Response.Status expectedStatus, Object payload, Object..
147148
* a ClientResponse instance with the data returned from the endpoint.
148149
*
149150
* @param expectedStatus the HTTP status that should be returned from the server
150-
* @param stream the StreamingOutput taht will be used for the POST data
151+
* @param stream the StreamingOutput that will be used for the POST data
151152
* @param mediaType the content-type for the streamed data
152153
* @param pathArgs variable list of arguments used to build the URI
153154
* @return a ClientResponse instance with the data returned from the endpoint
@@ -197,6 +198,46 @@ protected Response post(Response.Status expectedStatus, Form formData, URL url)
197198
}
198199
}
199200

201+
/**
202+
* Perform a file upload with the specified File instance and path objects, returning
203+
* a ClientResponse instance with the data returned from the endpoint.
204+
*
205+
* @param expectedStatus the HTTP status that should be returned from the server
206+
* @param name the name for the form field that contains the file name
207+
* @param fileToUpload a File instance pointing to the file to upload
208+
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
209+
* @param pathArgs variable list of arguments used to build the URI
210+
* @return a ClientResponse instance with the data returned from the endpoint
211+
* @throws GitLabApiException if any exception occurs during execution
212+
*/
213+
protected Response upload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Object... pathArgs) throws GitLabApiException {
214+
try {
215+
return validate(getApiClient().upload(name, fileToUpload, mediaType, pathArgs), expectedStatus);
216+
} catch (Exception e) {
217+
throw handle(e);
218+
}
219+
}
220+
221+
/**
222+
* Perform a file upload with the specified File instance and path objects, returning
223+
* a ClientResponse instance with the data returned from the endpoint.
224+
*
225+
* @param expectedStatus the HTTP status that should be returned from the server
226+
* @param name the name for the form field that contains the file name
227+
* @param fileToUpload a File instance pointing to the file to upload
228+
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
229+
* @param url the fully formed path to the GitLab API endpoint
230+
* @return a ClientResponse instance with the data returned from the endpoint
231+
* @throws GitLabApiException if any exception occurs during execution
232+
*/
233+
protected Response upload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, URL url) throws GitLabApiException {
234+
try {
235+
return validate(getApiClient().upload(name, fileToUpload, mediaType, url), expectedStatus);
236+
} catch (Exception e) {
237+
throw handle(e);
238+
}
239+
}
240+
200241
/**
201242
* Perform an HTTP PUT call with the specified form data and path objects, returning
202243
* a ClientResponse instance with the data returned from the endpoint.

src/main/java/org/gitlab4j/api/GitLabApiClient.java

+48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.gitlab4j.api;
22

3+
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE;
4+
5+
import java.io.File;
36
import java.io.IOException;
47
import java.net.Socket;
58
import java.net.URL;
@@ -33,6 +36,11 @@
3336
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
3437
import org.glassfish.jersey.client.ClientConfig;
3538
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+
3644

3745
/**
3846
* 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
224232
}
225233

226234
clientConfig.register(JacksonJson.class);
235+
clientConfig.register(MultiPartFeature.class);
227236
}
228237

229238
/**
@@ -450,6 +459,45 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
450459
return (invocation(url, null).post(Entity.entity(stream, mediaType)));
451460
}
452461

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+
453501
/**
454502
* Perform an HTTP PUT call with the specified form data and path objects, returning
455503
* a ClientResponse instance with the data returned from the endpoint.

0 commit comments

Comments
 (0)