Skip to content

Commit 579995a

Browse files
author
Nakul Sabharwal
committed
Changed html multipart to take byte content, added unit test, modified addPart for user defined multipart headers, values and parameters.
1 parent e2a123b commit 579995a

File tree

2 files changed

+137
-81
lines changed

2 files changed

+137
-81
lines changed

src/main/java/com/microsoft/graph/models/extensions/Multipart.java

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.io.InputStream;
77
import java.math.BigInteger;
88
import java.security.SecureRandom;
9-
import java.util.Arrays;
10-
import java.util.List;
119
import java.util.Map;
1210

1311
import 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
/**

src/test/java/com/microsoft/graph/functional/OneNoteTests.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import java.io.BufferedReader;
99
import java.io.ByteArrayOutputStream;
1010
import java.io.File;
11+
import java.io.FileInputStream;
1112
import java.io.InputStream;
1213
import java.io.InputStreamReader;
1314
import java.io.UnsupportedEncodingException;
1415
import java.util.ArrayList;
16+
import java.util.HashMap;
1517
import java.util.List;
18+
import java.util.Map;
1619

1720
import org.junit.Before;
1821
import org.junit.Ignore;
@@ -412,7 +415,7 @@ public void testMultipartPost(){
412415
File imgFile = new File("src/test/resources/hamilton.jpg");
413416
File pdfFile = new File("src/test/resources/document.pdf");
414417

415-
multipart.addHtmlPart("Presentation", htmlContent);
418+
multipart.addHtmlPart("Presentation", htmlContent.getBytes(HTML_ENCODING));
416419
multipart.addFilePart("hamilton", "image/jpg", imgFile);
417420
multipart.addFilePart("metadata", "application/pdf", pdfFile);
418421

@@ -431,6 +434,59 @@ public void testMultipartPost(){
431434
fail("Unable to write to output stream");
432435
}
433436
}
437+
438+
/**
439+
* Test posting multipart content to a page
440+
*/
441+
@Test
442+
public void testMultipartHeadersMapPost(){
443+
try {
444+
Multipart multipart = new Multipart();
445+
446+
String htmlContent = "<!DOCTYPE html>\r\n" +
447+
"<html lang=\"en-US\">\r\n" +
448+
"<head>\r\n" +
449+
"<title>Test Multipart Page</title>\r\n" +
450+
"<meta name=\"created\" content=\"2001-01-01T01:01+0100\">\r\n" +
451+
"</head>\r\n" +
452+
"<body>\r\n" +
453+
"<p>\r\n" +
454+
"<img src=\"name:image\" />\r\n" +
455+
"</p>\r\n" +
456+
"<p>\r\n" +
457+
"<object data=\"name:attachment\" data-attachment=\"document.pdf\" /></p>\r\n" +
458+
"\r\n" +
459+
"</body>\r\n" +
460+
"</html>";
461+
File imgFile = new File("src/test/resources/hamilton.jpg");
462+
File pdfFile = new File("src/test/resources/document.pdf");
463+
464+
Map<String, String> htmlHeaderMap = new HashMap<>();
465+
Map<String, String> contentDispMap = new HashMap<>();
466+
contentDispMap.put("name","Presentation" );
467+
htmlHeaderMap.put("Content-Disposition", Multipart.createContentHeaderValue("form-data", contentDispMap));
468+
htmlHeaderMap.put("Content-Type", Multipart.createContentHeaderValue("text/html", null));
469+
multipart.addPart(htmlHeaderMap, htmlContent.getBytes(HTML_ENCODING));
470+
471+
InputStream fileStream = new FileInputStream(imgFile);
472+
multipart.addFormData("hamilton", "image/jpg", getByteArray(fileStream));
473+
multipart.addFilePart("metadata", "application/pdf", pdfFile);
474+
475+
// Add multipart request header
476+
List<Option> options = new ArrayList<Option>();
477+
options.add(multipart.header());
478+
479+
// Post the multipart content
480+
OnenotePage page = orb
481+
.sections(testSection.id)
482+
.pages()
483+
.buildRequest(options)
484+
.post(multipart.content());
485+
assertNotNull(page);
486+
} catch (Exception e) {
487+
fail("Unable to write to output stream");
488+
}
489+
}
434490

435491
/**
436492
* Test patching a page's content

0 commit comments

Comments
 (0)