Example usage for javax.mail.internet MimeBodyPart removeHeader

List of usage examples for javax.mail.internet MimeBodyPart removeHeader

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart removeHeader.

Prototype

@Override
public void removeHeader(String name) throws MessagingException 

Source Link

Document

Remove all headers with this name.

Usage

From source file:dtw.webmail.util.FormdataMultipart.java

/**
 * Processes a body part of the form-data.
 * Extracts parameters and set values, and
 * leaves over the attachments.//from w w w  .j  a  v a  2  s  .c  o  m
 *
 * @param mbp the <tt>MimeBodyPart</tt> to be processed.
 *
 * @throws IOException if i/o operations fail.
 * @throws MessagingException if parsing or part handling with
 *         Mail API classes fails.
 */
private void processBodyPart(MimeBodyPart mbp) throws MessagingException, IOException {

    //String contenttype=new String(mbp.getContentType());
    //JwmaKernel.getReference().debugLog().write("Processing "+contenttype);

    //check if a content-type is given
    String[] cts = mbp.getHeader("Content-Type");
    if (cts == null || cts.length == 0) {
        //this is a parameter, get it out and
        //remove the part.
        String controlname = extractName((mbp.getHeader("Content-Disposition"))[0]);

        //JwmaKernel.getReference().debugLog().write("Processing control:"+controlname);
        //retrieve value observing encoding
        InputStream in = mbp.getInputStream();
        String[] encoding = mbp.getHeader("Content-Transfer-Encoding");
        if (encoding != null && encoding.length > 0) {
            in = MimeUtility.decode(in, encoding[0]);
        }

        String value = extractValue(in);
        if (value != null || !value.trim().equals("")) {
            addParameter(controlname, value);
        }
        //flag removal
        m_Removed = true;
        removeBodyPart(mbp);
    } else {
        String filename = extractFileName((mbp.getHeader("Content-Disposition"))[0]);

        //normally without file the control should be not successful.
        //but neither netscape nor mircosoft iexploder care much.
        //the only feature is an empty filename.
        if (filename.equals("")) {
            //kick it out too
            m_Removed = true;
            removeBodyPart(mbp);
        } else {

            //JwmaKernel.getReference().debugLog().write("Incoming filename="+filename);

            //IExploder sends files with complete path.
            //jwma doesnt want this.
            int lastindex = filename.lastIndexOf("\\");
            if (lastindex != -1) {
                filename = filename.substring(lastindex + 1, filename.length());
            }

            //JwmaKernel.getReference().debugLog().write("Outgoing filename="+filename);

            //Observe a possible encoding
            InputStream in = mbp.getInputStream();
            String[] encoding = mbp.getHeader("Content-Transfer-Encoding");
            if (encoding != null && encoding.length > 0) {
                in = MimeUtility.decode(in, encoding[0]);
            }
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            OutputStream out = (OutputStream) bout;

            int i = 0;
            while ((i = in.read()) != -1) {
                //maybe more efficient in buffers, but well
                out.write(i);
            }
            out.flush();
            out.close();

            //create the datasource
            MimeBodyPartDataSource mbpds = new MimeBodyPartDataSource(
                    //    contenttype,filename,bout.toByteArray()
                    cts[0], filename, bout.toByteArray());

            //Re-set the Content-Disposition header, in case
            //the file name was changed
            mbp.removeHeader("Content-Disposition");
            mbp.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

            //set a base64 transferencoding und the data handler
            mbp.addHeader("Content-Transfer-Encoding", "base64");
            mbp.setDataHandler(new DataHandler(mbpds));
        }
    }
}