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