Created
March 17, 2023 08:02
-
-
Save ochinchina/6da715bf3a331d0ecb7213284be251ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.io.CharStreams; | |
import javax.activation.DataSource; | |
import javax.mail.BodyPart; | |
import javax.mail.Header; | |
import javax.mail.MessagingException; | |
import javax.mail.internet.MimeMultipart; | |
import java.io.*; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
public class MultipartParseTest { | |
public static void main(String...args) throws MessagingException, IOException { | |
MemDataSource memDataSource = new MemDataSource(); | |
MimeMultipart multipart = new MimeMultipart(memDataSource); | |
for( int i = 0; i < multipart.getCount(); i++ ) { | |
BodyPart bodyPart = multipart.getBodyPart(i); | |
System.out.println("=========================="); | |
System.out.println(bodyPart.getContentType()); | |
System.out.println(getHeaders(bodyPart)); | |
System.out.println(bodyPart.getContent()); | |
System.out.println(CharStreams.toString(new InputStreamReader(bodyPart.getInputStream()))); | |
} | |
} | |
private static HashMap<String, String> getHeaders(BodyPart bodyPart) throws MessagingException { | |
Enumeration allHeaders = bodyPart.getAllHeaders(); | |
HashMap<String, String> r = new HashMap<>(); | |
while(allHeaders.hasMoreElements()) { | |
Header header = (Header)allHeaders.nextElement(); | |
r.put(header.getName(), header.getValue()); | |
} | |
return r; | |
} | |
private static class MemDataSource implements DataSource { | |
private String content = """ | |
------=_Part_0_1119962679.1679025450048 | |
Content-Type: application/json | |
Content-ID: jsonData | |
{"n1MessageContainer":{"n1MessageClass":"LPP","n1MessageContent":{"contentId":"binaryDataN1Message"},"nfId":"250787ff-edac-4779-a5a2-542ea214b250"},"lcsCorrelationId":"be6e5ae8-4d00-4ece-b412-b72f83aba8d2"} | |
------=_Part_0_1119962679.1679025450048 | |
Content-Type: application/vnd.3gpp.5gnas | |
Content-ID: binaryDataN1Message | |
this is the content | |
------=_Part_0_1119962679.1679025450048-- | |
"""; | |
private String content_1 = """ | |
GET /namf-comm/v1/ue-contexts/imsi-1480136000106/n1-n2-messages HTTP/1.1 | |
user-agent: LMF | |
accept: application/json | |
content-type: multipart/related; boundary="----=_Part_0_1119962679.1679025450048" | |
3gpp-sbi-discovery-requester-nf-type: LMF | |
3gpp-sbi-discovery-target-nf-type: AMF | |
3gpp-sbi-discovery-target-nf-instance-id: 250787ff-edac-4779-a5a2-542ea214b250 | |
3gpp-sbi-discovery-service-names: ["namf-comm"] | |
------=_Part_0_1119962679.1679025450048 | |
Content-Type: application/json | |
Content-ID: jsonData | |
{"n1MessageContainer":{"n1MessageClass":"LPP","n1MessageContent":{"contentId":"binaryDataN1Message"},"nfId":"250787ff-edac-4779-a5a2-542ea214b250"},"lcsCorrelationId":"be6e5ae8-4d00-4ece-b412-b72f83aba8d2"} | |
------=_Part_0_1119962679.1679025450048 | |
Content-Type: application/vnd.3gpp.5gnas | |
Content-ID: binaryDataN1Message | |
this is the content | |
------=_Part_0_1119962679.1679025450048-- | |
"""; | |
@Override | |
public InputStream getInputStream() throws IOException { | |
return new ByteArrayInputStream(content.getBytes()); | |
} | |
@Override | |
public OutputStream getOutputStream() throws IOException { | |
return new ByteArrayOutputStream(); | |
} | |
@Override | |
public String getContentType() { | |
return "multipart/related; boundary=\"----=_Part_0_1119962679.1679025450048\""; | |
} | |
@Override | |
public String getName() { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment