Skip to content

Instantly share code, notes, and snippets.

@musa-pro
Last active March 7, 2022 16:42
Show Gist options
  • Save musa-pro/dcef0bc23e48227e4b89f6e2095f7c1e to your computer and use it in GitHub Desktop.
Save musa-pro/dcef0bc23e48227e4b89f6e2095f7c1e to your computer and use it in GitHub Desktop.
Java Parser class to use with java azure function
package com.example.util;
import org.apache.tomcat.util.http.fileupload.MultipartStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
// Inspired by this post - https://stackoverflow.com/a/60882449/363867
public class FormDataParser {
private String contentType = "multipart/form-data";
private String body;
private Map<String, Object> formFields = new HashMap<String, Object>();
public FormDataParser(String body, String contentType){
this.body = body;
this.contentType = contentType;
if(!body.isEmpty()){
parseBody(body);
}
}
private void parseBody(String body){
try {
String boundary = contentType.split(";")[1].split("=")[1];
int bufSize = 1024;
InputStream in = new ByteArrayInputStream(body.getBytes()); // Convert body to an input stream
MultipartStream multipartStream = new MultipartStream(in, boundary.getBytes(), bufSize, null); // Using MultipartStream to parse body input stream
boolean nextPart = false;
nextPart = multipartStream.skipPreamble();
while (nextPart) {
String header = multipartStream.readHeaders();
String name = parseMimeHeader(header, "name=");
// multipartStream.readBodyData(System.out);
if(header.indexOf("filename=") != -1){
parseFileMime(name, header);
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
multipartStream.readBodyData(output);
String value=output.toString("UTF-8");
setValue(name, value);
nextPart = multipartStream.readBoundary();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String parseMimeHeader(String header, String name){
String fieldName = "";
try{
String[] headers = header.replace(";", "\r\n").split("\r\n");
for(String mime: headers){
System.out.println(mime);
mime = mime.trim();
int start =mime.indexOf(name);
if(start != -1){
start = mime.indexOf(name) + name.length()+1;
int end = mime.indexOf("\"", start);
fieldName = end != -1?mime.substring(start, end): mime.substring(start);
break;
}
}
}catch (Exception e) {
e.getStackTrace();
}
return fieldName;
}
//if there is file, parse extra information like filename and mimetype of the file
private void parseFileMime(String fileFieldName, String header) {
String filename = parseMimeHeader(header, "filename=");
String contentType = parseMimeHeader(header, "Content-Type:");
setValue(fileFieldName+"-filename", filename);
setValue(fileFieldName+"-Content-Type", contentType.trim());
}
private void setValue(String fieldName, String fieldValue){
formFields.put(fieldName, fieldValue);
}
public String get(String fieldName){
return (String) formFields.get(fieldName);
}
}
//to use this in azure function
/*
String contentType = request.getHeaders().get("content-type");
String body = request.getBody().get();
FormDataParser formDataParser = new FormDataParser(body, contentType);
String name = formDataParser.get("name");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment