/*
* Note this API has been marked as deprecated though
*/
logger.debug("Parsing...");
MultipartStream multipartStream = new MultipartStream(input, boundary);
ArrayList<RetrieveMMSMessageResponse.Attachment> attachments=new ArrayList<RetrieveMMSMessageResponse.Attachment>();
boolean nextPart = multipartStream.skipPreamble();
while(nextPart) {
String headerSection = multipartStream.readHeaders();
String partContentType=null;
String partContentName=null;
if (headerSection!=null) {
String[] headers=headerSection.split("\n");
for (String header:headers) {
header=header.trim();
if (header.startsWith("Content-Type:")) {
String value=header.substring(header.indexOf(":")+1).trim();
if (value.indexOf(";")!= -1) {
value=value.substring(0, value.indexOf(";"));
}
partContentType=value;
}
if (header.startsWith("Content-Disposition:")) {
String value=header.substring(header.indexOf(":")+1).trim();
if (value.indexOf(";")!= -1) {
value=value.substring(0, value.indexOf(";"));
}
logger.debug("got content disposition="+value);
if (header.indexOf("name=")!=-1) {
String name=header.substring(header.indexOf("name=")+5).replaceAll("\\\"", "");
logger.debug("Parsed name="+name);
partContentName=name;
}
}
}
}
// System.out.println("Headers: [" + headers+"]");
ByteArrayOutputStream data = new ByteArrayOutputStream();
multipartStream.readBodyData(data);
if (partContentType!=null && partContentType.equals("application/json") && partContentName!=null && partContentName.equals("root-fields")) {
logger.debug("Have the JSON response part:"+(new String(data.toByteArray())));
ObjectMapper mapper=new ObjectMapper();
ByteArrayInputStream bais = new ByteArrayInputStream(data.toByteArray());
InboundMessageWrapper inboundMessageWrapper=mapper.readValue(bais, InboundMessageWrapper.class);
if (inboundMessageWrapper!=null && inboundMessageWrapper.getInboundMessage()!=null) response.setInboundMessage(inboundMessageWrapper.getInboundMessage());
} else {
RetrieveMMSMessageResponse.Attachment attachment=new RetrieveMMSMessageResponse.Attachment();
attachment.setAttachmentContentType(partContentType);
attachment.setAttachmentName(partContentName);
attachment.setAttachmentData(data.toByteArray());
attachments.add(attachment);
}
nextPart = multipartStream.readBoundary();
}
response.setAttachments(attachments);
// logger.debug("starting conversion to mime message");