KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > mail > servlet > MessageContent


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: MessageContent.java,v 1.4 2006/03/29 15:43:36 wfro Exp $
5  * Description: openCRX EMailImporter
6  * Revision: $Revision: 1.4 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/29 15:43:36 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004-2006, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.mail.servlet;
57
58 import java.io.ByteArrayInputStream JavaDoc;
59 import java.io.InputStream JavaDoc;
60
61 import javax.mail.internet.ContentType JavaDoc;
62
63 /**
64  * Utility class to be able to handle non text content of email messages.
65  */

66 public class MessageContent {
67     
68   //-------------------------------------------------------------------------
69
/**
70    * Constructor.
71    * MessageContent instances can only be created by providing values for each
72    * of it's members, no setter methods are provided.
73    *
74    * @param id The id, e.g. the filename of the message content
75    * @param mimeType The mime type of the content
76    * @param binaryContent The content
77    */

78   public MessageContent (
79       String JavaDoc id,
80       String JavaDoc mimeType,
81       Object JavaDoc binaryContent
82   ) {
83     this.id = id;
84     this.contentType = mimeType;
85     this.content = binaryContent;
86   }
87
88   //-------------------------------------------------------------------------
89
/**
90    * Returns an InputStream to be able to read the real content of the
91    * MessageContent object from.
92    *
93    * @return The InputStream to read the message content from
94    */

95   public InputStream JavaDoc getInputStream() {
96     byte buffer[];
97     InputStream JavaDoc is = null;
98     if(this.content instanceof byte[]) {
99       buffer = (byte[]) this.content;
100       is = new ByteArrayInputStream JavaDoc(buffer);
101     }
102     return is;
103   }
104   
105   //-------------------------------------------------------------------------
106
/**
107    * Returns the message content as Object.
108    *
109    * @return The message content in object form
110    */

111   public Object JavaDoc getContent(
112   ) {
113       return this.content;
114   }
115   
116   //-------------------------------------------------------------------------
117
/**
118    * Returns the type of this instance of MessageContent.
119    * @return The message content's type
120    */

121   public String JavaDoc getContentType() {
122     return contentType;
123   }
124
125   //-------------------------------------------------------------------------
126
/**
127    * Returns the id of this instance of MessageContent.
128    * @return The message content's id
129    */

130   public String JavaDoc getId() {
131     return id;
132   }
133   
134   //-------------------------------------------------------------------------
135
/* (non-Javadoc)
136    * @see java.lang.Object#toString()
137    */

138   public String JavaDoc toString() {
139     ContentType JavaDoc mimeType = null;
140     try {
141       mimeType = new ContentType JavaDoc(this.contentType);
142     }
143     catch (Exception JavaDoc e) {
144     }
145     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
146     buffer.append("[content id '" + this.id + "', ");
147     buffer.append("content type '" + this.contentType + "'");
148     if(mimeType != null && (mimeType.match("text/plain") || mimeType.match("text/html"))) {
149       buffer.append(", content '" + this.content + "'");
150     }
151     if(this.content instanceof byte[]) {
152       buffer.append(", contentLength:" + ((byte[]) this.content).length);
153     }
154     buffer.append("]");
155     return buffer.toString();
156   }
157
158   //-------------------------------------------------------------------------
159
// Members
160
//-------------------------------------------------------------------------
161
private final String JavaDoc id;
162   private final String JavaDoc contentType;
163   private final Object JavaDoc content;
164 }
165
Popular Tags