196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project/*
296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * you may not use this file except in compliance with the License.
696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * You may obtain a copy of the License at
796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project *
1096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * See the License for the specific language governing permissions and
1496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project * limitations under the License.
1596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project */
1696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
1796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectpackage com.android.email.mail.internet;
1896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
1996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.BufferedWriter;
2096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.IOException;
2196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.InputStream;
2296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.OutputStream;
2396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport java.io.OutputStreamWriter;
2496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
2596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport com.android.email.mail.BodyPart;
2696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport com.android.email.mail.MessagingException;
2796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectimport com.android.email.mail.Multipart;
2896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
2996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Projectpublic class MimeMultipart extends Multipart {
3096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    protected String mPreamble;
3196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
3296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    protected String mContentType;
3396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
3496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    protected String mBoundary;
3596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
3696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    protected String mSubType;
3796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
3896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public MimeMultipart() throws MessagingException {
3996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        mBoundary = generateBoundary();
4096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        setSubType("mixed");
4196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
4296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
4396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public MimeMultipart(String contentType) throws MessagingException {
4496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        this.mContentType = contentType;
4596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        try {
4696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            mSubType = MimeUtility.getHeaderParameter(contentType, null).split("/")[1];
4796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            mBoundary = MimeUtility.getHeaderParameter(contentType, "boundary");
4896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            if (mBoundary == null) {
4996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                throw new MessagingException("MultiPart does not contain boundary: " + contentType);
5096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            }
5196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        } catch (Exception e) {
5296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            throw new MessagingException(
5396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                    "Invalid MultiPart Content-Type; must contain subtype and boundary. ("
5496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project                            + contentType + ")", e);
5596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
5696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
5796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
5896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public String generateBoundary() {
5996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        StringBuffer sb = new StringBuffer();
6096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        sb.append("----");
6196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        for (int i = 0; i < 30; i++) {
6296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            sb.append(Integer.toString((int)(Math.random() * 35), 36));
6396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
6496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return sb.toString().toUpperCase();
6596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
6696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
6796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public String getPreamble() throws MessagingException {
6896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return mPreamble;
6996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
7096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
7196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public void setPreamble(String preamble) throws MessagingException {
7296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        this.mPreamble = preamble;
7396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
7496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
7596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public String getContentType() throws MessagingException {
7696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return mContentType;
7796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
7896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
7996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public void setSubType(String subType) throws MessagingException {
8096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        this.mSubType = subType;
8196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        mContentType = String.format("multipart/%s; boundary=\"%s\"", subType, mBoundary);
8296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
8396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
8496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public void writeTo(OutputStream out) throws IOException, MessagingException {
8596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
8696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
8796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        if (mPreamble != null) {
8896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            writer.write(mPreamble + "\r\n");
8996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
9096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
9196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        for (int i = 0, count = mParts.size(); i < count; i++) {
9296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            BodyPart bodyPart = (BodyPart)mParts.get(i);
9396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            writer.write("--" + mBoundary + "\r\n");
9496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            writer.flush();
9596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            bodyPart.writeTo(out);
9696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project            writer.write("\r\n");
9796c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        }
9896c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
9996c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        writer.write("--" + mBoundary + "--\r\n");
10096c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        writer.flush();
10196c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
10296c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project
10396c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    public InputStream getInputStream() throws MessagingException {
10496c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project        return null;
10596c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project    }
10696c5af40d639d629267794f4f0338a267ff94ce5The Android Open Source Project}
107