Skip to content

Commit 23abe70

Browse files
committed
- adds missing manually created types
1 parent 60d5e8a commit 23abe70

File tree

6 files changed

+726
-0
lines changed

6 files changed

+726
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.microsoft.graph.models.extensions;
2+
3+
4+
import java.text.ParseException;
5+
import java.util.Locale;
6+
7+
/**
8+
* A timezone-nonspecific date
9+
*/
10+
public class DateOnly {
11+
12+
/**
13+
* The year
14+
*/
15+
private final int mYear;
16+
17+
/**
18+
* The month
19+
*/
20+
private final int mMonth;
21+
22+
/**
23+
* The day
24+
*/
25+
private final int mDay;
26+
27+
/**
28+
* Constructs a timezone-nonspecific DateOnly
29+
*
30+
* @param dateStr date string of the form <code>yyyy-mm-dd</code>
31+
* @return the parsed DateOnly instance
32+
* @exception ParseException If there was a failure parsing the dateStr
33+
*/
34+
public static DateOnly parse(final String dateStr) throws ParseException {
35+
// break the date up into its constituent parts
36+
String[] dateInfo = dateStr.split("-");
37+
38+
// validate the split date string
39+
final int expectedLength = 3;
40+
if (dateInfo.length != expectedLength) {
41+
throw new ParseException(
42+
"Expected datestring format 'yyyy-mm-dd' but found: " + dateStr, 0
43+
);
44+
}
45+
46+
// array indices for date parsing
47+
final int indYear = 0;
48+
final int indMonth = 1;
49+
final int indDay = 2;
50+
51+
// unpack this array
52+
int year = Integer.parseInt(dateInfo[indYear]);
53+
int month = Integer.parseInt(dateInfo[indMonth]);
54+
int day = Integer.parseInt(dateInfo[indDay]);
55+
56+
return new DateOnly(year, month, day);
57+
}
58+
59+
/**
60+
* Constructs a timezone-nonspecific DateOnly
61+
*
62+
* @param year the year
63+
* @param month 1-indexed month value (Jan == 1)
64+
* @param day day of the month
65+
*/
66+
public DateOnly(final int year, final int month, final int day) {
67+
mYear = year;
68+
mMonth = month;
69+
mDay = day;
70+
}
71+
72+
/**
73+
* Gets the year
74+
*
75+
* @return the year
76+
*/
77+
public int getYear() {
78+
return mYear;
79+
}
80+
81+
/**
82+
* Gets the month
83+
*
84+
* @return the month
85+
*/
86+
public int getMonth() {
87+
return mMonth;
88+
}
89+
90+
/**
91+
* Gets the day
92+
*
93+
* @return the day
94+
*/
95+
public int getDay() {
96+
return mDay;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return String.format(
102+
Locale.ROOT,
103+
"%04d-%02d-%02d", mYear, mMonth, mDay
104+
);
105+
}
106+
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package com.microsoft.graph.models.extensions;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.math.BigInteger;
8+
import java.security.SecureRandom;
9+
import java.util.Map;
10+
11+
import com.google.common.annotations.VisibleForTesting;
12+
import com.microsoft.graph.options.HeaderOption;
13+
14+
/**
15+
* Helper for submitting multipart data
16+
*
17+
* This follows the Network Working Group's RFC
18+
* on multipart/form-data posting format:
19+
* https://www.ietf.org/rfc/rfc2388.txt
20+
*/
21+
public class Multipart {
22+
private String boundary;
23+
private static final String RETURN = "\r\n";
24+
private ByteArrayOutputStream out;
25+
public static final String MULTIPART_ENCODING = "US-ASCII";
26+
private String contentType = "multipart/form-data";
27+
28+
/**
29+
* Create a new multipart object
30+
*/
31+
public Multipart() {
32+
out = new ByteArrayOutputStream();
33+
boundary = "part_" + new BigInteger(130, new SecureRandom()).toString();
34+
}
35+
36+
/**
37+
* Get the multipart boundary for use in the request header
38+
* @return the multipart boundary
39+
*/
40+
public String getBoundary() {
41+
return boundary;
42+
}
43+
44+
/**
45+
* Set the multipart boundary for use in the request header
46+
* @param boundary The multipart boundary
47+
*/
48+
public void setBoundary(String boundary) {
49+
this.boundary = boundary;
50+
}
51+
52+
/**
53+
* Get the contentType for use in the request header
54+
* @return the multipart Content-Type
55+
*/
56+
public String getContentType() {
57+
return contentType;
58+
}
59+
60+
/**
61+
* Set the contentType for use in the request header
62+
* @param contentType The multipart Content-Type
63+
*/
64+
public void setContentType(String contentType) {
65+
this.contentType = contentType;
66+
}
67+
68+
/**
69+
* Get the Content-Type header to send the multipart request
70+
* @return the multipart header option
71+
*/
72+
public HeaderOption header() {
73+
return new HeaderOption("Content-Type", contentType + "; boundary=\"" + boundary + "\"");
74+
}
75+
76+
private void writePartData(String partContent, byte[] byteArray) throws IOException{
77+
out.write(partContent.getBytes(MULTIPART_ENCODING));
78+
out.write(byteArray);
79+
String returnContent = RETURN + RETURN;
80+
out.write(returnContent.getBytes(MULTIPART_ENCODING));
81+
}
82+
83+
/**
84+
* Create content headers value and parameter
85+
* @param name The content header name
86+
* @param contentType The content header Content-Type
87+
* @param filename The content header filename
88+
* @return content header value and parameter string
89+
*/
90+
@VisibleForTesting String createPartHeader(String name, String contentType, String filename) {
91+
StringBuilder partContent = new StringBuilder(addBoundary());
92+
partContent.append("Content-Disposition: form-data");
93+
if(filename != null) {
94+
if(name != null)
95+
partContent.append("; name=\"").append(name).append("\"; filename=\"").append(filename).append("\"");
96+
else
97+
partContent.append("; filename=\"").append(filename).append("\"");
98+
}
99+
else if(name != null)
100+
partContent.append("; name=\"").append(name).append("\"");
101+
if(contentType != null)
102+
partContent.append(RETURN).append("Content-Type: ").append(contentType);
103+
partContent.append(RETURN).append(RETURN);
104+
return partContent.toString();
105+
}
106+
107+
/**
108+
* Create content headers value and parameter
109+
* @param contentValue The content header value
110+
* @param contentDispParameter Map containing content paramter's key and value pair
111+
* @return content header value and parameter string
112+
*/
113+
public static String createContentHeaderValue(String contentValue, Map<String, String> contentDispParameter) {
114+
String contentHeaderValue = contentValue;
115+
116+
if(contentDispParameter != null) {
117+
for(Map.Entry<String,String> entry : contentDispParameter.entrySet())
118+
contentHeaderValue += ";" + entry.getKey() + "=\"" + entry.getValue() + "\"";
119+
}
120+
return contentHeaderValue;
121+
}
122+
123+
/**
124+
* Create content headers header-name, value and parameter string
125+
* @param headers Map containing Header-name and header-value pair
126+
*/
127+
private String createPartHeader(Map<String, String> headers) {
128+
String partContent = addBoundary();
129+
String defaultPartContent = "Content-Disposition: form-data;" + RETURN + "Content-Type: " + contentType + RETURN + RETURN;
130+
131+
if(headers != null) {
132+
for(Map.Entry<String,String> entry : headers.entrySet())
133+
partContent += entry.getKey() +": "+entry.getValue() + RETURN;
134+
partContent += RETURN;
135+
}
136+
else
137+
partContent += defaultPartContent;
138+
return partContent;
139+
}
140+
141+
/**
142+
* Add multipart content headers and byte content
143+
* @param name The multipart content name
144+
* @param contentType The multipart Content-Type
145+
* @param filename The multipart content file name
146+
* @param byteArray The multipart byte content
147+
* @throws IOException
148+
*/
149+
private void addData(String name, String contentType, String filename, byte[] byteArray) throws IOException {
150+
String partContent = createPartHeader(name, contentType, filename);
151+
writePartData(partContent, byteArray);
152+
}
153+
154+
/**
155+
* Add a part to the multipart body
156+
* @param name The name of the part
157+
* @param contentType The MIME type (text/html, video/mp4, etc.)
158+
* @param byteArray The byte[] contents of the resource
159+
* @throws IOException Throws an exception if the output stream cannot be written to
160+
*/
161+
public void addFormData(String name, String contentType, byte[] byteArray) throws IOException {
162+
addData(name, contentType, null, byteArray);
163+
}
164+
165+
/**
166+
* Add a part to the multipart body
167+
* @param contentType The MIME type (text/html, video/mp4, etc.)
168+
* @param byteArray The byte[] contents of the resource
169+
* @throws IOException Throws an exception if the output stream cannot be written to
170+
*/
171+
public void addPart(String contentType, byte[] byteArray) throws IOException {
172+
addData(null, contentType, null, byteArray);
173+
}
174+
175+
/**
176+
* Add a part to the multipart body
177+
* @param headers Map containing Header's header-name(eg: Content-Disposition, Content-Type, etc..) and header's value-parameter string
178+
* @param content The byte[] contents of the resource
179+
* @throws IOException Throws an exception if the output stream cannot be written to
180+
*/
181+
public void addPart(Map<String, String> headers, byte[] content) throws IOException{
182+
String partContent = createPartHeader(headers);
183+
writePartData(partContent, content);
184+
}
185+
186+
/**
187+
* Add an HTML part to the multipart body
188+
* @param name The name of the part
189+
* @param content The HTML body for the part
190+
* @throws IOException Throws an exception if the output stream cannot be written to
191+
*/
192+
public void addHtmlPart(String name, byte[] content) throws IOException {
193+
addFormData(name, "text/html", content);
194+
}
195+
196+
/**
197+
* Add a file part to the multipart body
198+
* @param name The name of the part
199+
* @param contentType The MIME type of the file (application/pdf, video/mp4, etc.)
200+
* @param file The file
201+
* @throws IOException Throws an exception if the output stream cannot be written to
202+
*/
203+
public void addFilePart(String name, String contentType, java.io.File file) throws IOException {
204+
InputStream fileStream = new FileInputStream(file);
205+
byte[] fileBytes = getByteArray(fileStream);
206+
addData(name, contentType, file.getName(), fileBytes);
207+
}
208+
209+
/**
210+
* Adds a boundary at the beginning of a new part
211+
* @return The boundary
212+
*/
213+
private String addBoundary() {
214+
return "--" + boundary + RETURN;
215+
}
216+
217+
/**
218+
* Adds a boundary at the end of the multipart body
219+
* @return The boundary
220+
*/
221+
private String addEnding() {
222+
return "--" + boundary + "--";
223+
}
224+
225+
/**
226+
* Returns a full multipart body byte array
227+
* @return The byte[] representation of the multipart object
228+
* @throws IOException Throws an exception if the output stream cannot be written to
229+
*/
230+
public byte[] content() throws IOException {
231+
ByteArrayOutputStream finalStream = out;
232+
finalStream.write(addEnding().getBytes(MULTIPART_ENCODING));
233+
return finalStream.toByteArray();
234+
}
235+
236+
/**
237+
* Helper method to convert an InputStream to a byte[]
238+
* @param in The input stream to convert
239+
* @return The byte[]
240+
* @throws IOException Throws an exception if the output stream cannot be written to
241+
*/
242+
private byte[] getByteArray(InputStream in) throws IOException {
243+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
244+
int nRead;
245+
byte[] data = new byte[16384];
246+
try {
247+
while ((nRead = in.read(data, 0, data.length)) != -1) {
248+
buffer.write(data, 0, nRead);
249+
}
250+
} finally {
251+
in.close();
252+
}
253+
buffer.flush();
254+
return buffer.toByteArray();
255+
}
256+
}

0 commit comments

Comments
 (0)