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.voicemail.impl.mail.internet; 17 18 import com.android.voicemail.impl.mail.Body; 19 import com.android.voicemail.impl.mail.BodyPart; 20 import com.android.voicemail.impl.mail.MessagingException; 21 import com.android.voicemail.impl.mail.Multipart; 22 import java.io.BufferedWriter; 23 import java.io.IOException; 24 import java.io.OutputStream; 25 import java.io.OutputStreamWriter; 26 import java.util.regex.Pattern; 27 28 /** TODO this is a close approximation of Message, need to update along with Message. */ 29 public class MimeBodyPart extends BodyPart { 30 protected MimeHeader header = new MimeHeader(); 31 protected MimeHeader extendedHeader; 32 protected Body body; 33 protected int size; 34 35 // regex that matches content id surrounded by "<>" optionally. 36 private static final Pattern REMOVE_OPTIONAL_BRACKETS = Pattern.compile("^<?([^>]+)>?$"); 37 // regex that matches end of line. 38 private static final Pattern END_OF_LINE = Pattern.compile("\r?\n"); 39 MimeBodyPart()40 public MimeBodyPart() throws MessagingException { 41 this(null); 42 } 43 MimeBodyPart(Body body)44 public MimeBodyPart(Body body) throws MessagingException { 45 this(body, null); 46 } 47 MimeBodyPart(Body body, String mimeType)48 public MimeBodyPart(Body body, String mimeType) throws MessagingException { 49 if (mimeType != null) { 50 setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType); 51 } 52 setBody(body); 53 } 54 getFirstHeader(String name)55 protected String getFirstHeader(String name) throws MessagingException { 56 return header.getFirstHeader(name); 57 } 58 59 @Override addHeader(String name, String value)60 public void addHeader(String name, String value) throws MessagingException { 61 header.addHeader(name, value); 62 } 63 64 @Override setHeader(String name, String value)65 public void setHeader(String name, String value) throws MessagingException { 66 header.setHeader(name, value); 67 } 68 69 @Override getHeader(String name)70 public String[] getHeader(String name) throws MessagingException { 71 return header.getHeader(name); 72 } 73 74 @Override removeHeader(String name)75 public void removeHeader(String name) throws MessagingException { 76 header.removeHeader(name); 77 } 78 79 @Override getBody()80 public Body getBody() throws MessagingException { 81 return body; 82 } 83 84 @Override setBody(Body body)85 public void setBody(Body body) throws MessagingException { 86 this.body = body; 87 if (body instanceof Multipart) { 88 Multipart multipart = 89 ((Multipart) body); 90 multipart.setParent(this); 91 setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType()); 92 } else if (body instanceof TextBody) { 93 String contentType = String.format("%s;\n charset=utf-8", getMimeType()); 94 String name = MimeUtility.getHeaderParameter(getContentType(), "name"); 95 if (name != null) { 96 contentType += String.format(";\n name=\"%s\"", name); 97 } 98 setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType); 99 setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); 100 } 101 } 102 103 @Override getContentType()104 public String getContentType() throws MessagingException { 105 String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE); 106 if (contentType == null) { 107 return "text/plain"; 108 } else { 109 return contentType; 110 } 111 } 112 113 @Override getDisposition()114 public String getDisposition() throws MessagingException { 115 String contentDisposition = getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION); 116 if (contentDisposition == null) { 117 return null; 118 } else { 119 return contentDisposition; 120 } 121 } 122 123 @Override getContentId()124 public String getContentId() throws MessagingException { 125 String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID); 126 if (contentId == null) { 127 return null; 128 } else { 129 // remove optionally surrounding brackets. 130 return REMOVE_OPTIONAL_BRACKETS.matcher(contentId).replaceAll("$1"); 131 } 132 } 133 134 @Override getMimeType()135 public String getMimeType() throws MessagingException { 136 return MimeUtility.getHeaderParameter(getContentType(), null); 137 } 138 139 @Override isMimeType(String mimeType)140 public boolean isMimeType(String mimeType) throws MessagingException { 141 return getMimeType().equals(mimeType); 142 } 143 setSize(int size)144 public void setSize(int size) { 145 this.size = size; 146 } 147 148 @Override getSize()149 public int getSize() throws MessagingException { 150 return size; 151 } 152 153 /** 154 * Set extended header 155 * 156 * @param name Extended header name 157 * @param value header value - flattened by removing CR-NL if any remove header if value is null 158 * @throws MessagingException 159 */ 160 @Override setExtendedHeader(String name, String value)161 public void setExtendedHeader(String name, String value) throws MessagingException { 162 if (value == null) { 163 if (extendedHeader != null) { 164 extendedHeader.removeHeader(name); 165 } 166 return; 167 } 168 if (extendedHeader == null) { 169 extendedHeader = new MimeHeader(); 170 } 171 extendedHeader.setHeader(name, END_OF_LINE.matcher(value).replaceAll("")); 172 } 173 174 /** 175 * Get extended header 176 * 177 * @param name Extended header name 178 * @return header value - null if header does not exist 179 * @throws MessagingException 180 */ 181 @Override getExtendedHeader(String name)182 public String getExtendedHeader(String name) throws MessagingException { 183 if (extendedHeader == null) { 184 return null; 185 } 186 return extendedHeader.getFirstHeader(name); 187 } 188 189 /** Write the MimeMessage out in MIME format. */ 190 @Override writeTo(OutputStream out)191 public void writeTo(OutputStream out) throws IOException, MessagingException { 192 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); 193 header.writeTo(out); 194 writer.write("\r\n"); 195 writer.flush(); 196 if (body != null) { 197 body.writeTo(out); 198 } 199 } 200 } 201