22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .io .InputStream ;
56import java .net .Socket ;
67import java .net .URL ;
78import java .security .GeneralSecurityException ;
1314import java .util .function .Supplier ;
1415import java .util .logging .Level ;
1516import java .util .logging .Logger ;
16-
1717import javax .net .ssl .HostnameVerifier ;
1818import javax .net .ssl .SSLContext ;
1919import javax .net .ssl .SSLEngine ;
3030import javax .ws .rs .core .MultivaluedMap ;
3131import javax .ws .rs .core .Response ;
3232import javax .ws .rs .core .StreamingOutput ;
33-
3433import org .gitlab4j .api .Constants .TokenType ;
3534import org .gitlab4j .api .GitLabApi .ApiVersion ;
3635import org .gitlab4j .api .utils .JacksonJson ;
3938import org .glassfish .jersey .client .ClientConfig ;
4039import org .glassfish .jersey .client .ClientProperties ;
4140import org .glassfish .jersey .client .JerseyClientBuilder ;
41+ import org .glassfish .jersey .media .multipart .BodyPart ;
4242import org .glassfish .jersey .media .multipart .Boundary ;
4343import org .glassfish .jersey .media .multipart .FormDataMultiPart ;
4444import org .glassfish .jersey .media .multipart .MultiPart ;
4545import org .glassfish .jersey .media .multipart .MultiPartFeature ;
4646import org .glassfish .jersey .media .multipart .file .FileDataBodyPart ;
47+ import org .glassfish .jersey .media .multipart .file .StreamDataBodyPart ;
4748
4849
4950/**
@@ -561,14 +562,13 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
561562 *
562563 * @param name the name for the form field that contains the file name
563564 * @param fileToUpload a File instance pointing to the file to upload
564- * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
565+ * @param mediaTypeString unused; will be removed in the next major version
565566 * @param pathArgs variable list of arguments used to build the URI
566567 * @return a ClientResponse instance with the data returned from the endpoint
567568 * @throws IOException if an error occurs while constructing the URL
568569 */
569570 protected Response upload (String name , File fileToUpload , String mediaTypeString , Object ... pathArgs ) throws IOException {
570- URL url = getApiUrl (pathArgs );
571- return (upload (name , fileToUpload , mediaTypeString , null , url ));
571+ return upload (name , fileToUpload , mediaTypeString , null , pathArgs );
572572 }
573573
574574 /**
@@ -577,7 +577,7 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
577577 *
578578 * @param name the name for the form field that contains the file name
579579 * @param fileToUpload a File instance pointing to the file to upload
580- * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
580+ * @param mediaTypeString unused; will be removed in the next major version
581581 * @param formData the Form containing the name/value pairs
582582 * @param pathArgs variable list of arguments used to build the URI
583583 * @return a ClientResponse instance with the data returned from the endpoint
@@ -594,30 +594,38 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
594594 *
595595 * @param name the name for the form field that contains the file name
596596 * @param fileToUpload a File instance pointing to the file to upload
597- * @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
597+ * @param mediaTypeString unused; will be removed in the next major version
598598 * @param formData the Form containing the name/value pairs
599599 * @param url the fully formed path to the GitLab API endpoint
600600 * @return a ClientResponse instance with the data returned from the endpoint
601601 * @throws IOException if an error occurs while constructing the URL
602602 */
603603 protected Response upload (String name , File fileToUpload , String mediaTypeString , Form formData , URL url ) throws IOException {
604+ FileDataBodyPart filePart = new FileDataBodyPart (name , fileToUpload );
605+ return upload (filePart , formData , url );
606+ }
604607
605- MediaType mediaType = (mediaTypeString != null ? MediaType .valueOf (mediaTypeString ) : null );
606- try (FormDataMultiPart multiPart = new FormDataMultiPart ()) {
608+ protected Response upload (String name , InputStream inputStream , String filename , String mediaTypeString , Object ... pathArgs ) throws IOException {
609+ URL url = getApiUrl (pathArgs );
610+ return (upload (name , inputStream , filename , mediaTypeString , null , url ));
611+ }
607612
613+ protected Response upload (String name , InputStream inputStream , String filename , String mediaTypeString , Form formData , URL url ) throws IOException {
614+ StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart (name , inputStream , filename );
615+ return upload (streamDataBodyPart , formData , url );
616+ }
617+
618+ protected Response upload (BodyPart bodyPart , Form formData , URL url ) throws IOException {
619+ try (FormDataMultiPart multiPart = new FormDataMultiPart ()) {
608620 if (formData != null ) {
609- MultivaluedMap <String , String > formParams = formData .asMap ();
610- formParams .forEach ((key , values ) -> {
621+ formData .asMap ().forEach ((key , values ) -> {
611622 if (values != null ) {
612623 values .forEach (value -> multiPart .field (key , value ));
613624 }
614625 });
615626 }
616627
617- FileDataBodyPart filePart = mediaType != null ?
618- new FileDataBodyPart (name , fileToUpload , mediaType ) :
619- new FileDataBodyPart (name , fileToUpload );
620- multiPart .bodyPart (filePart );
628+ multiPart .bodyPart (bodyPart );
621629 final Entity <?> entity = Entity .entity (multiPart , Boundary .addBoundary (multiPart .getMediaType ()));
622630 return (invocation (url , null ).post (entity ));
623631 }
0 commit comments