11package org .gitlab4j .api ;
22
3- import static javax .ws .rs .core .MediaType .MULTIPART_FORM_DATA_TYPE ;
4-
53import java .io .File ;
64import java .io .IOException ;
75import java .net .Socket ;
@@ -536,7 +534,24 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
536534 */
537535 protected Response upload (String name , File fileToUpload , String mediaTypeString , Object ... pathArgs ) throws IOException {
538536 URL url = getApiUrl (pathArgs );
539- return (upload (name , fileToUpload , mediaTypeString , url ));
537+ return (upload (name , fileToUpload , mediaTypeString , null , url ));
538+ }
539+
540+ /**
541+ * Perform a file upload using the specified media type, returning
542+ * a ClientResponse instance with the data returned from the endpoint.
543+ *
544+ * @param name the name for the form field that contains the file name
545+ * @param fileToUpload a File instance pointing to the file to upload
546+ * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
547+ * @param formData the Form containing the name/value pairs
548+ * @param pathArgs variable list of arguments used to build the URI
549+ * @return a ClientResponse instance with the data returned from the endpoint
550+ * @throws IOException if an error occurs while constructing the URL
551+ */
552+ protected Response upload (String name , File fileToUpload , String mediaTypeString , Form formData , Object ... pathArgs ) throws IOException {
553+ URL url = getApiUrl (pathArgs );
554+ return (upload (name , fileToUpload , mediaTypeString , formData , url ));
540555 }
541556
542557 /**
@@ -546,14 +561,25 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
546561 * @param name the name for the form field that contains the file name
547562 * @param fileToUpload a File instance pointing to the file to upload
548563 * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
564+ * @param formData the Form containing the name/value pairs
549565 * @param url the fully formed path to the GitLab API endpoint
550566 * @return a ClientResponse instance with the data returned from the endpoint
551567 * @throws IOException if an error occurs while constructing the URL
552568 */
553- protected Response upload (String name , File fileToUpload , String mediaTypeString , URL url ) throws IOException {
569+ protected Response upload (String name , File fileToUpload , String mediaTypeString , Form formData , URL url ) throws IOException {
554570
555571 MediaType mediaType = (mediaTypeString != null ? MediaType .valueOf (mediaTypeString ) : null );
556- try (MultiPart multiPart = new FormDataMultiPart ()) {
572+ try (FormDataMultiPart multiPart = new FormDataMultiPart ()) {
573+
574+ if (formData != null ) {
575+ MultivaluedMap <String , String > formParams = formData .asMap ();
576+ formParams .forEach ((key , values ) -> {
577+ if (values != null ) {
578+ values .forEach (value -> multiPart .field (key , value ));
579+ }
580+ });
581+ }
582+
557583 FileDataBodyPart filePart = mediaType != null ?
558584 new FileDataBodyPart (name , fileToUpload , mediaType ) :
559585 new FileDataBodyPart (name , fileToUpload );
0 commit comments