1 36 package org.columba.ristretto.message; 37 38 import java.io.InputStream ; 39 import java.nio.charset.Charset ; 40 import java.nio.charset.IllegalCharsetNameException ; 41 import java.nio.charset.UnsupportedCharsetException ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 import org.columba.ristretto.coder.EncodedWord; 46 import org.columba.ristretto.io.Streamable; 47 import org.columba.ristretto.parser.MimeTypeParser; 48 import org.columba.ristretto.parser.ParserException; 49 50 55 public class MimeHeader implements Streamable { 56 57 60 public final static int PLAIN = 0; 64 public final static int QUOTED_PRINTABLE = 1; 65 68 public final static int BASE64 = 2; 69 70 private Header header; 71 72 78 public MimeHeader() { 79 this(new Header()); 80 setMimeType(new MimeType("text","plain")); 81 } 82 83 88 public MimeHeader( Header header ) { 89 this.header = header; 90 } 91 92 99 public MimeHeader( String type, String subtype ) { 100 this.header = new Header(); 101 setMimeType( new MimeType(type, subtype)); 102 } 103 104 112 public MimeType getMimeType() { 113 try { 114 return MimeTypeParser.parse( header.get("Content-Type") ); 115 } catch (ParserException e) { 116 return new MimeType(); 117 } 118 } 119 120 127 public String getContentType() { 128 return getMimeType().getType(); 129 } 130 131 138 public String getContentSubtype() { 139 return getMimeType().getSubtype(); 140 } 141 142 152 public String getContentDisposition() { 153 return this.header.get("Content-Disposition"); 154 } 155 156 166 public String getContentParameter(String key) { 167 return getParameter(header.get("Content-Type"), key); 168 } 169 170 178 public int getContentTransferEncoding() { 179 String value = header.get("Content-Transfer-Encoding"); 180 if( value != null ) { 181 if( value.equalsIgnoreCase("quoted-printable") ) return QUOTED_PRINTABLE; 182 if( value.equalsIgnoreCase("base64") ) return BASE64; 183 } 184 185 return PLAIN; 186 } 187 188 197 public String getContentID() { 198 String result = header.get("Content-ID"); 199 if( result == null ) { 200 return header.get("Content-Id"); 201 } 202 return result; 203 } 204 205 212 public String getContentDescription() { 213 return header.get("Content-Description"); 214 } 215 216 225 public String getDispositionParameter(String key) { 226 return getParameter(header.get("Content-Disposition"), key); 227 } 228 229 protected String getParameter(String headerLine, String key) { 230 if( headerLine == null) return null; 231 Pattern parameterPattern = Pattern.compile(key + "\\s*=\\s*((\"([^\"]+)\")|([^\r\n\\s;]+))", Pattern.CASE_INSENSITIVE); 232 Matcher matcher = parameterPattern.matcher( headerLine ); 233 if( matcher.find() ) { 234 if( matcher.group(3) != null ) { 235 return matcher.group(3); 236 } else { 237 return matcher.group(4); 238 } 239 } 240 return null; 241 } 242 243 protected String appendParameter(String headerLine, String key, String value) { 244 StringBuffer result = new StringBuffer (headerLine); 245 result.ensureCapacity(headerLine.length() + 2 + key.length() + 1 + value.length()); 246 result.append("; "); 247 result.append(key); 248 result.append('='); 249 result.append(value); 250 return result.toString(); 251 } 252 253 265 public String getFileName() { 266 String result = null; 267 268 result = getContentParameter("name"); 269 if (result != null) 270 return EncodedWord.decode(result).toString(); 271 272 result = getDispositionParameter("filename"); 273 if (result != null) 274 return EncodedWord.decode(result).toString(); 275 276 return result; 277 } 278 279 282 public String toString() { 283 return header.toString(); 284 } 285 286 299 public void putContentParameter(String key, String value) { 300 header.set("Content-Type", appendParameter(header.get("Content-Type") , key, "\"" + value + "\"")); 301 } 302 303 316 public void putDispositionParameter(String key, String value) { 317 header.set("Content-Disposition", appendParameter(header.get("Content-Disposition") , key, "\"" + value + "\"" )); 318 } 319 320 329 public void setMimeType( MimeType type ) { 330 header.set("Content-Type", type.toString()); 331 } 332 333 343 public void setContentDescription( String description ) { 344 header.set("Content-Description", description ); 345 } 346 347 360 public void setContentDisposition( String disposition ) { 361 header.set("Content-Disposition", disposition ); 362 } 363 364 373 public void setContentID( String id) { 374 header.set("Content-ID", id); 375 } 376 377 385 public void setContentTransferEncoding(String encoding) { 386 header.set("Content-Transfer-Encoding", encoding); 387 } 388 389 392 public int count() { 393 return header.count(); 394 } 395 396 402 public String get(String key) { 403 return header.get(key); 404 } 405 406 412 public void set(String key, Object value) { 413 header.set(key, value); 414 } 415 416 424 public Charset getCharset() { 425 String charsetField = getContentParameter("charset"); 426 427 if( charsetField != null ) { 428 try { 429 return Charset.forName( charsetField ); 430 } catch ( IllegalCharsetNameException e ) { 431 return Charset.forName( System.getProperty("file.encoding")); 432 } catch ( UnsupportedCharsetException e ) { 433 return Charset.forName( System.getProperty("file.encoding")); 434 } 435 } else { 436 return Charset.forName( System.getProperty("file.encoding")); 437 } 438 } 439 440 443 public Header getHeader() { 444 return header; 445 } 446 447 452 public void setHeader(Header header) { 453 this.header = header; 454 } 455 456 459 public InputStream getInputStream() { 460 return header.getInputStream(); 461 } 462 463 } 464 | Popular Tags |