Skip to content

Commit 41f0e99

Browse files
author
Nakul Sabharwal
committed
getbytes encoding fix in Multipart, OneNoteTests and MockConnection. Added function addPart in class Multipart for user provided headers and byte content.
1 parent 7839b0e commit 41f0e99

File tree

3 files changed

+115
-19
lines changed

3 files changed

+115
-19
lines changed

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

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
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;
11+
import java.util.Map;
912

1013
import 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

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.File;
1111
import java.io.InputStream;
1212
import java.io.InputStreamReader;
13+
import java.io.UnsupportedEncodingException;
1314
import java.util.ArrayList;
1415
import java.util.List;
1516

@@ -49,6 +50,7 @@ public class OneNoteTests {
4950
private OnenotePage testPage;
5051
private OnenoteSection testSection;
5152
private SectionGroup testSectionGroup2;
53+
private final String HTML_ENCODING= "US-ASCII";
5254

5355
@Before
5456
public void setUp() {
@@ -288,10 +290,10 @@ public void testGetPreview() {
288290

289291
/**
290292
* Test posting a page stream to a page
291-
* @throws InterruptedException
293+
* @throws InterruptedException, UnsupportedEncodingException
292294
*/
293295
@Test
294-
public void testPostToNotebook() throws InterruptedException {
296+
public void testPostToNotebook() throws InterruptedException, UnsupportedEncodingException {
295297
SectionGroup sectionGroupData = new SectionGroup();
296298

297299
// Currently, there is no way to delete sections or section groups, so let's create a random one
@@ -319,7 +321,7 @@ public void testPostToNotebook() throws InterruptedException {
319321
// Test HTML content
320322
String content = "<html><head><title>Test Title</title></head><body>Test body</body></html>";
321323

322-
byte[] pageStream = content.getBytes();
324+
byte[] pageStream = content.getBytes(HTML_ENCODING);
323325
List<Option> options = new ArrayList<Option>();
324326
options.add(new HeaderOption("Content-Type", "application/xhtml+xml"));
325327
OnenotePage page = orb

src/test/java/com/microsoft/graph/http/MockConnection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class MockConnection implements IConnection {
1818
private final ITestConnectionData mData;
1919
private HashMap<String, String> mHeaders = new HashMap<>();
2020
private Boolean mFollowRedirects;
21+
private final String JSON_ENCODING = "UTF-8";
2122

2223
public MockConnection(ITestConnectionData data) {
2324
mData = data;
@@ -40,7 +41,7 @@ public OutputStream getOutputStream() throws IOException {
4041

4142
@Override
4243
public InputStream getInputStream() throws IOException {
43-
return new ByteArrayInputStream(mData.getJsonResponse().getBytes());
44+
return new ByteArrayInputStream(mData.getJsonResponse().getBytes(JSON_ENCODING));
4445
}
4546

4647
@Override

0 commit comments

Comments
 (0)