1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.phone.common.mail.internet;
17 
18 import com.android.phone.common.mail.BodyPart;
19 import com.android.phone.common.mail.MessagingException;
20 import com.android.phone.common.mail.Multipart;
21 
22 import java.io.BufferedWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 
28 public class MimeMultipart extends Multipart {
29     protected String mPreamble;
30 
31     protected String mContentType;
32 
33     protected String mBoundary;
34 
35     protected String mSubType;
36 
MimeMultipart()37     public MimeMultipart() throws MessagingException {
38         mBoundary = generateBoundary();
39         setSubType("mixed");
40     }
41 
MimeMultipart(String contentType)42     public MimeMultipart(String contentType) throws MessagingException {
43         this.mContentType = contentType;
44         try {
45             mSubType = MimeUtility.getHeaderParameter(contentType, null).split("/")[1];
46             mBoundary = MimeUtility.getHeaderParameter(contentType, "boundary");
47             if (mBoundary == null) {
48                 throw new MessagingException("MultiPart does not contain boundary: " + contentType);
49             }
50         } catch (Exception e) {
51             throw new MessagingException(
52                     "Invalid MultiPart Content-Type; must contain subtype and boundary. ("
53                             + contentType + ")", e);
54         }
55     }
56 
generateBoundary()57     public String generateBoundary() {
58         StringBuffer sb = new StringBuffer();
59         sb.append("----");
60         for (int i = 0; i < 30; i++) {
61             sb.append(Integer.toString((int)(Math.random() * 35), 36));
62         }
63         return sb.toString().toUpperCase();
64     }
65 
getPreamble()66     public String getPreamble() throws MessagingException {
67         return mPreamble;
68     }
69 
setPreamble(String preamble)70     public void setPreamble(String preamble) throws MessagingException {
71         this.mPreamble = preamble;
72     }
73 
74     @Override
getContentType()75     public String getContentType() throws MessagingException {
76         return mContentType;
77     }
78 
setSubType(String subType)79     public void setSubType(String subType) throws MessagingException {
80         this.mSubType = subType;
81         mContentType = String.format("multipart/%s; boundary=\"%s\"", subType, mBoundary);
82     }
83 
84     @Override
writeTo(OutputStream out)85     public void writeTo(OutputStream out) throws IOException, MessagingException {
86         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
87 
88         if (mPreamble != null) {
89             writer.write(mPreamble + "\r\n");
90         }
91 
92         for (int i = 0, count = mParts.size(); i < count; i++) {
93             BodyPart bodyPart = mParts.get(i);
94             writer.write("--" + mBoundary + "\r\n");
95             writer.flush();
96             bodyPart.writeTo(out);
97             writer.write("\r\n");
98         }
99 
100         writer.write("--" + mBoundary + "--\r\n");
101         writer.flush();
102     }
103 
104     @Override
getInputStream()105     public InputStream getInputStream() throws MessagingException {
106         return null;
107     }
108 
getSubTypeForTest()109     public String getSubTypeForTest() {
110         return mSubType;
111     }
112 }
113