66import java .io .InputStream ;
77import java .math .BigInteger ;
88import java .security .SecureRandom ;
9- import java .util .Arrays ;
10- import java .util .List ;
119import java .util .Map ;
1210
1311import com .microsoft .graph .options .HeaderOption ;
@@ -81,68 +79,73 @@ private void writePartData(String partContent, byte[] byteArray) throws IOExcept
8179 out .write (returnContent .getBytes (MULTIPART_ENCODING ));
8280 }
8381
84- private String createPartHeader (Map <String , String > headers , String name , String contentType , String filename ) {
85- String partContent = addBoundary ();
86- if (headers != null ) {
87- List <String > listContentDisposition = Arrays .asList ("filename" ,"creation-date" ,"modification-date" ,"read-date" ,
88- "size" ,"name" ,"voice" ,"handling" ,"preview-type" );
89- List <String > mainHeaders = Arrays .asList ("Content-Disposition" ,"Content-Type" ,"charset" );
90-
91- if (headers .containsKey ("Content-Disposition" )) {
92- partContent += "Content-Disposition:" +headers .get ("Content-Disposition" )+";" ;
93- for (Map .Entry <String ,String > entry : headers .entrySet ()) {
94- if (listContentDisposition .contains (entry .getKey ())) {
95- partContent += " " + entry .getKey () + "=\" " + entry .getValue () + "\" ;" ;
96- }
97- }
98- partContent = partContent .substring (0 , partContent .length ()-1 );
99- partContent += RETURN ;
100-
101- }
102-
103- if (headers .containsKey ("Content-Type" )) {
104- partContent += "Content-Type:" +headers .get ("Content-Type" )+";" ;
105- if (headers .containsKey ("charset" )) {
106- partContent += "charset=\" " + headers .get ("charset" ) + "\" ;" ;
107- }
108- partContent = partContent .substring (0 , partContent .length ()-1 );
109- partContent += RETURN ;
110- }
111-
112- for (Map .Entry <String ,String > entry : headers .entrySet ()) {
113- if (mainHeaders .contains (entry .getKey ())==false && listContentDisposition .contains (entry .getKey ())==false ) {
114- partContent += entry .getKey () +":" +entry .getValue () + RETURN ;
115- }
116- }
117- partContent += RETURN ;
118- }
119- else if (filename != null && name != null ) {
120- partContent +=
121- "Content-Disposition:form-data; name=\" " + name + "\" " + "; filename=\" " + filename + "\" " + RETURN +
122- "Content-Type:" + contentType + RETURN +
123- RETURN ;
124- }
125- else if (filename != null ) {
126- partContent +=
127- "Content-Disposition:form-data; filename=\" " + filename + "\" " + RETURN +
128- "Content-Type:" + contentType + RETURN +
129- RETURN ;
130- }
131- else if (name != null ){
132- partContent +=
133- "Content-Disposition:form-data; name=\" " + name + "\" " + RETURN +
134- "Content-Type:" + contentType + RETURN +
135- RETURN ;
82+ private String createPartHeader (String name , String contentType , String filename ) {
83+ String partContent = addBoundary ();
84+ String partContentWithNameAndFilename = "Content-Disposition: form-data; name=\" %s\" ; filename=\" %s\" " + RETURN + "Content-Type:%s" + RETURN + RETURN ;
85+ String partContentWithFilename = "Content-Disposition: form-data; filename=\" %s\" " + RETURN + "Content-Type:%s" + RETURN + RETURN ;
86+ String partContentWithNameAndContentType = "Content-Disposition: form-data; name=\" %s\" " + RETURN + "Content-Type:%s" + RETURN + RETURN ;
87+ String partContentWithContentType = "Content-Disposition: form-data" + RETURN + "Content-Type:%s" + RETURN + RETURN ;
88+
89+ if (filename != null && name != null )
90+ partContent += String .format (partContentWithNameAndFilename , name , filename , contentType );
91+ else if (filename != null )
92+ partContent += String .format (partContentWithFilename , filename , contentType );
93+ else if (name != null )
94+ partContent += String .format (partContentWithNameAndContentType , name , contentType );
95+ else
96+ partContent += String .format (partContentWithContentType , contentType );
97+
98+ return partContent ;
99+ }
100+
101+ /**
102+ * Create content headers value and parameter
103+ * @param contentValue The content header value
104+ * @param contentDispParameter Map containing content paramter's key and value pair
105+ * @return content header value and parameter string
106+ */
107+ public static String createContentHeaderValue (String contentValue , Map <String , String > contentDispParameter ) {
108+ String contentHeaderValue = contentValue ;
109+
110+ if (contentDispParameter != null ) {
111+ for (Map .Entry <String ,String > entry : contentDispParameter .entrySet ())
112+ contentHeaderValue += ";" + entry .getKey () + "=\" " + entry .getValue () + "\" " ;
136113 }
137- else {
138- partContent +=
139- "Content-Disposition:form-data;" + RETURN +
140- "Content-Type:" + contentType + RETURN +
141- RETURN ;
142- }
143- return partContent ;
114+ return contentHeaderValue ;
144115 }
145116
117+ /**
118+ * Create content headers header-name, value and parameter string
119+ * @param headers Map containing Header-name and header-value pair
120+ */
121+ private String createPartHeader (Map <String , String > headers ) {
122+ String partContent = addBoundary ();
123+ String defaultPartContent = "Content-Disposition: form-data;" + RETURN + "Content-Type:" + contentType + RETURN + RETURN ;
124+
125+ if (headers != null ) {
126+ for (Map .Entry <String ,String > entry : headers .entrySet ())
127+ partContent += entry .getKey () +": " +entry .getValue () + RETURN ;
128+ partContent += RETURN ;
129+ }
130+ else
131+ partContent += defaultPartContent ;
132+ return partContent ;
133+ }
134+
135+ /**
136+ * Add multipart content headers and byte content
137+ * @param name The multipart content name
138+ * @param contentType The multipart Content-Type
139+ * @param filename The multipart content file name
140+ * @param byteArray The multipart byte content
141+ * @throws IOException
142+ */
143+ private void addData (String name , String contentType , String filename , byte [] byteArray ) throws IOException {
144+ String partContent = createPartHeader (name , contentType , filename );
145+ writePartData (partContent , byteArray );
146+ }
147+
148+
146149 /**
147150 * Add a part to the multipart body
148151 * @param name The name of the part
@@ -151,8 +154,7 @@ else if(name != null){
151154 * @throws IOException Throws an exception if the output stream cannot be written to
152155 */
153156 public void addFormData (String name , String contentType , byte [] byteArray ) throws IOException {
154- String partContent = createPartHeader (null , name , contentType , null );
155- writePartData (partContent , byteArray );
157+ addData (name , contentType , null , byteArray );
156158 }
157159
158160 /**
@@ -162,18 +164,28 @@ public void addFormData(String name, String contentType, byte[] byteArray) throw
162164 * @throws IOException Throws an exception if the output stream cannot be written to
163165 */
164166 public void addPart (String contentType , byte [] byteArray ) throws IOException {
165- String partContent = createPartHeader (null , null , contentType , null );
166- writePartData (partContent , byteArray );
167+ addData (null , contentType , null , byteArray );
167168 }
168169
170+ /**
171+ * Add a part to the multipart body
172+ * @param headers Map containing Header's header-name(eg: Content-Disposition, Content-Type, etc..) and header's value-parameter string
173+ * @param content The byte[] contents of the resource
174+ * @throws IOException Throws an exception if the output stream cannot be written to
175+ */
176+ public void addPart (Map <String , String > headers , byte [] content ) throws IOException {
177+ String partContent = createPartHeader (headers );
178+ writePartData (partContent , content );
179+ }
180+
169181 /**
170182 * Add an HTML part to the multipart body
171183 * @param name The name of the part
172184 * @param content The HTML body for the part
173185 * @throws IOException Throws an exception if the output stream cannot be written to
174186 */
175- public void addHtmlPart (String name , String content ) throws IOException {
176- addFormData (name , "text/html" , content . getBytes ( MULTIPART_ENCODING ) );
187+ public void addHtmlPart (String name , byte [] content ) throws IOException {
188+ addFormData (name , "text/html" , content );
177189 }
178190
179191 /**
@@ -186,19 +198,7 @@ public void addHtmlPart(String name, String content) throws IOException {
186198 public void addFilePart (String name , String contentType , java .io .File file ) throws IOException {
187199 InputStream fileStream = new FileInputStream (file );
188200 byte [] fileBytes = getByteArray (fileStream );
189- String partContent = createPartHeader (null , name , contentType , file .getName ());
190- writePartData (partContent , fileBytes );
191- }
192-
193- /**
194- * Add a part to the multipart body
195- * @param headers Map containing Header's key and value pair
196- * @param content The byte[] contents of the resource
197- * @throws IOException Throws an exception if the output stream cannot be written to
198- */
199- public void addPart (Map <String , String > headers , byte [] content ) throws IOException {
200- String partContent = createPartHeader (headers , null , null , null );
201- writePartData (partContent , content );
201+ addData (name , contentType , file .getName (), fileBytes );
202202 }
203203
204204 /**
0 commit comments