Search in sources :

Example 1 with HeaderFactoryExt

use of gov.nist.javax.sip.header.HeaderFactoryExt in project XobotOS by xamarin.

the class MultipartMimeContentImpl method createContentList.

/**
     * unpack a multipart mime packet and return a list of content packets.
     * 
     * @return -- an iterator of Content blocks.
     * 
     */
public void createContentList(String body) throws ParseException {
    try {
        HeaderFactoryExt headerFactory = new HeaderFactoryImpl();
        String delimiter = this.getContentTypeHeader().getParameter(BOUNDARY);
        if (delimiter == null) {
            this.contentList = new LinkedList<Content>();
            ContentImpl content = new ContentImpl(body, delimiter);
            content.setContentTypeHeader(this.getContentTypeHeader());
            this.contentList.add(content);
            return;
        }
        String[] fragments = body.split("--" + delimiter + "\r\n");
        for (String nextPart : fragments) {
            if (nextPart == null) {
                return;
            }
            StringBuffer strbuf = new StringBuffer(nextPart);
            while (strbuf.length() > 0 && (strbuf.charAt(0) == '\r' || strbuf.charAt(0) == '\n')) strbuf.deleteCharAt(0);
            if (strbuf.length() == 0)
                continue;
            nextPart = strbuf.toString();
            int position = nextPart.indexOf("\r\n\r\n");
            int off = 4;
            if (position == -1) {
                position = nextPart.indexOf("\n");
                off = 2;
            }
            if (position == -1)
                throw new ParseException("no content type header found in " + nextPart, 0);
            String rest = nextPart.substring(position + off);
            if (rest == null)
                throw new ParseException("No content [" + nextPart + "]", 0);
            // logger.debug("rest = [[" + rest + "]]");
            String headers = nextPart.substring(0, position);
            ContentImpl content = new ContentImpl(rest, boundary);
            String[] headerArray = headers.split("\r\n");
            for (String hdr : headerArray) {
                Header header = headerFactory.createHeader(hdr);
                if (header instanceof ContentTypeHeader) {
                    content.setContentTypeHeader((ContentTypeHeader) header);
                } else if (header instanceof ContentDispositionHeader) {
                    content.setContentDispositionHeader((ContentDispositionHeader) header);
                } else {
                    throw new ParseException("Unexpected header type " + header.getName(), 0);
                }
                contentList.add(content);
            }
        }
    } catch (StringIndexOutOfBoundsException ex) {
        throw new ParseException("Invalid Multipart mime format", 0);
    }
}
Also used : ContentTypeHeader(javax.sip.header.ContentTypeHeader) HeaderFactoryExt(gov.nist.javax.sip.header.HeaderFactoryExt) ContentDispositionHeader(javax.sip.header.ContentDispositionHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) Header(javax.sip.header.Header) HeaderFactoryImpl(gov.nist.javax.sip.header.HeaderFactoryImpl) ContentDispositionHeader(javax.sip.header.ContentDispositionHeader) ParseException(java.text.ParseException)

Aggregations

HeaderFactoryExt (gov.nist.javax.sip.header.HeaderFactoryExt)1 HeaderFactoryImpl (gov.nist.javax.sip.header.HeaderFactoryImpl)1 ParseException (java.text.ParseException)1 ContentDispositionHeader (javax.sip.header.ContentDispositionHeader)1 ContentTypeHeader (javax.sip.header.ContentTypeHeader)1 Header (javax.sip.header.Header)1