66import java .io .InputStream ;
77import java .math .BigInteger ;
88import java .security .SecureRandom ;
9+ import java .util .Arrays ;
10+ import java .util .List ;
11+ import java .util .Map ;
912
1013import com .microsoft .graph .options .HeaderOption ;
1114
@@ -20,6 +23,8 @@ public class Multipart {
2023 private String boundary ;
2124 private static final String RETURN = "\r \n " ;
2225 private ByteArrayOutputStream out ;
26+ public static final String MULTIPART_ENCODING = "US-ASCII" ;
27+ private String contentType = "multipart/form-data" ;
2328
2429 /**
2530 * Create a new multipart object
@@ -33,46 +38,78 @@ public Multipart() {
3338 * Get the multipart boundary for use in the request header
3439 * @return the multipart boundary
3540 */
36- public String boundary () {
41+ public String getBoundary () {
3742 return boundary ;
3843 }
3944
45+ /**
46+ * Set the multipart boundary for use in the request header
47+ * @param boundary The multipart boundary
48+ */
49+ public void setBoundary (String boundary ) {
50+ this .boundary = boundary ;
51+ }
52+
53+ /**
54+ * Get the contentType for use in the request header
55+ * @return the multipart Content-Type
56+ */
57+ public String getContentType () {
58+ return contentType ;
59+ }
60+
61+ /**
62+ * Set the contentType for use in the request header
63+ * @param contentType The multipart Content-Type
64+ */
65+ public void setContentType (String contentType ) {
66+ this .contentType = contentType ;
67+ }
68+
4069 /**
4170 * Get the Content-Type header to send the multipart request
4271 * @return the multipart header option
4372 */
4473 public HeaderOption header () {
45- return new HeaderOption ("Content-Type" , "multipart/form-data ; boundary=\" " + boundary + "\" " );
74+ return new HeaderOption ("Content-Type" , contentType + " ; boundary=\" " + boundary + "\" " );
4675 }
4776
4877 /**
49- * Add a string part to the multipart body
78+ * Add a part to the multipart body
5079 * @param name The name of the part
51- * @param contentType The MIME type (text/html, text/plain , etc.)
52- * @param content The string content to include
80+ * @param contentType The MIME type (text/html, video/mp4 , etc.)
81+ * @param byteArray The byte[] contents of the resource
5382 * @throws IOException Throws an exception if the output stream cannot be written to
5483 */
55- public void addPart (String name , String contentType , String content ) throws IOException {
56- addPart (name , contentType , content .getBytes ());
84+ public void addFormData (String name , String contentType , byte [] byteArray ) throws IOException {
85+ String partContent = addBoundary ();
86+ partContent +=
87+ "Content-Disposition:form-data; name=\" " + name + "\" " + RETURN +
88+ "Content-Type:" + contentType + RETURN +
89+ RETURN ;
90+ out .write (partContent .getBytes (MULTIPART_ENCODING ));
91+ out .write (byteArray );
92+ String returnContent = RETURN + RETURN ;
93+ out .write (returnContent .getBytes (MULTIPART_ENCODING ));
94+ System .out .println (partContent );
5795 }
5896
5997 /**
6098 * Add a part to the multipart body
61- * @param name The name of the part
6299 * @param contentType The MIME type (text/html, video/mp4, etc.)
63100 * @param byteArray The byte[] contents of the resource
64101 * @throws IOException Throws an exception if the output stream cannot be written to
65102 */
66- public void addPart (String name , String contentType , byte [] byteArray ) throws IOException {
103+ public void addPart (String contentType , byte [] byteArray ) throws IOException {
67104 String partContent = addBoundary ();
68105 partContent +=
69- "Content-Disposition:form-data; name= \" " + name + " \" " + RETURN +
106+ "Content-Disposition:form-data;" + RETURN +
70107 "Content-Type:" + contentType + RETURN +
71108 RETURN ;
72- out .write (partContent .getBytes ());
109+ out .write (partContent .getBytes (MULTIPART_ENCODING ));
73110 out .write (byteArray );
74111 String returnContent = RETURN + RETURN ;
75- out .write (returnContent .getBytes ());
112+ out .write (returnContent .getBytes (MULTIPART_ENCODING ));
76113 }
77114
78115 /**
@@ -82,7 +119,7 @@ public void addPart(String name, String contentType, byte[] byteArray) throws IO
82119 * @throws IOException Throws an exception if the output stream cannot be written to
83120 */
84121 public void addHtmlPart (String name , String content ) throws IOException {
85- addPart (name , "text/html" , content );
122+ addFormData (name , "text/html" , content . getBytes ( MULTIPART_ENCODING ) );
86123 }
87124
88125 /**
@@ -95,7 +132,63 @@ public void addHtmlPart(String name, String content) throws IOException {
95132 public void addFilePart (String name , String contentType , java .io .File file ) throws IOException {
96133 InputStream fileStream = new FileInputStream (file );
97134 byte [] fileBytes = getByteArray (fileStream );
98- addPart (name , contentType , fileBytes );
135+ String partContent = addBoundary ();
136+ partContent +=
137+ "Content-Disposition:form-data; name=\" " + name + "\" " + "; filename=\" " + file .getName () + "\" " + RETURN +
138+ "Content-Type:" + contentType + RETURN +
139+ RETURN ;
140+ System .out .println (partContent );
141+ out .write (partContent .getBytes (MULTIPART_ENCODING ));
142+ out .write (fileBytes );
143+ String returnContent = RETURN + RETURN ;
144+ out .write (returnContent .getBytes (MULTIPART_ENCODING ));
145+ }
146+
147+ /**
148+ * Add a part to the multipart body
149+ * @param headers Map containing Header's key and value pair
150+ * @param content The byte[] contents of the resource
151+ * @throws IOException Throws an exception if the output stream cannot be written to
152+ */
153+ public void addPart (Map <String , String > headers , byte [] content ) throws IOException {
154+ String partContent = addBoundary ();
155+ List <String > listContentDisposition = Arrays .asList ("filename" ,"creation-date" ,"modification-date" ,"read-date" ,
156+ "size" ,"name" ,"voice" ,"handling" ,"preview-type" );
157+ List <String > mainHeaders = Arrays .asList ("Content-Disposition" ,"Content-Type" ,"charset" );
158+
159+ if (headers .containsKey ("Content-Disposition" )) {
160+ partContent += "Content-Disposition:" +headers .get ("Content-Disposition" )+";" ;
161+ for (Map .Entry <String ,String > entry : headers .entrySet ()) {
162+ if (listContentDisposition .contains (entry .getKey ())) {
163+ partContent += " " + entry .getKey () + "=\" " + entry .getValue () + "\" ;" ;
164+ }
165+ }
166+ partContent = partContent .substring (0 , partContent .length ()-1 );
167+ partContent += RETURN ;
168+
169+ }
170+
171+ if (headers .containsKey ("Content-Type" )) {
172+ partContent += "Content-Type:" +headers .get ("Content-Type" )+";" ;
173+ if (headers .containsKey ("charset" )) {
174+ partContent += "charset=\" " + headers .get ("charset" ) + "\" ;" ;
175+ }
176+ partContent = partContent .substring (0 , partContent .length ()-1 );
177+ partContent += RETURN ;
178+ }
179+
180+ for (Map .Entry <String ,String > entry : headers .entrySet ()) {
181+ if (mainHeaders .contains (entry .getKey ())==false && listContentDisposition .contains (entry .getKey ())==false ) {
182+ partContent += entry .getKey () +":" +entry .getValue () + RETURN ;
183+ }
184+ }
185+
186+ System .out .println (partContent );
187+ partContent += RETURN ;
188+ out .write (partContent .getBytes (MULTIPART_ENCODING ));
189+ out .write (content );
190+ String returnContent = RETURN + RETURN ;
191+ out .write (returnContent .getBytes (MULTIPART_ENCODING ));
99192 }
100193
101194 /**
@@ -121,7 +214,7 @@ private String addEnding() {
121214 */
122215 public byte [] content () throws IOException {
123216 ByteArrayOutputStream finalStream = out ;
124- finalStream .write (addEnding ().getBytes ());
217+ finalStream .write (addEnding ().getBytes (MULTIPART_ENCODING ));
125218 return finalStream .toByteArray ();
126219 }
127220
0 commit comments