001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.model;
009:
010: import java.util.*;
011: import java.io.InputStream;
012: import java.io.ByteArrayOutputStream;
013: import javax.mail.*;
014:
015: import org.apache.log4j.Logger;
016:
017: //import dtw.webmail.JwmaKernel;
018:
019: /**
020: * Class implementing the JwmaMessagePart model.
021: *
022: * @author Dieter Wimberger
023: * @version 0.9.7 07/02/2003
024: */
025: public class JwmaMessagePartImpl implements JwmaMessagePart {
026:
027: //logging
028: private static Logger log = Logger
029: .getLogger(JwmaMessagePartImpl.class);
030:
031: //instance attributes
032: private String m_ContentType;
033: private String m_Description;
034: private String m_Name;
035: private String m_TextContent = "";
036: private int m_Number;
037: private int m_Size;
038: private Part m_Part;
039:
040: /**
041: * Private empty constructor, to prevent construction.
042: */
043: private JwmaMessagePartImpl() {
044: }//constructor
045:
046: /**
047: * Constructs a <tt>JwmaMessagePartImpl</tt> with a
048: * given part and number.
049: *
050: * @param part the part that is wrapped.
051: * @param number an <tt>int</tt> that represents the part number.
052: */
053: private JwmaMessagePartImpl(Part p, int number) {
054: m_Part = p;
055: m_Number = number;
056: }//constructor
057:
058: /**
059: * Constructs a <tt>JwmaMessagePartImpl</tt> with a
060: * given part number.
061: *
062: * @param number an <tt>int</tt> that represents the part number.
063: */
064: private JwmaMessagePartImpl(int number) {
065: m_Number = number;
066: }//constructor
067:
068: public int getPartNumber() {
069: return m_Number;
070: }//getNumber
071:
072: public boolean isMimeType(String type) {
073: if (m_Part != null) {
074: try {
075: return m_Part.isMimeType(type);
076: } catch (MessagingException mex) {
077: log.error("isMimeType()", mex);
078: }
079: }
080: //FIX: probably evaluate based on m_ContentType otherwise
081: return false;
082:
083: }//isMimeType
084:
085: public String getContentType() {
086: return m_ContentType;
087: }//getContentType
088:
089: /**
090: * Gets the <tt>javax.mail.Part</tt> wrapped by this instance.
091: *
092: * @return the wrapped part instance.
093: */
094: public Part getPart() {
095: return m_Part;
096: }//getPart
097:
098: public String getTextContent() {
099: return m_TextContent;
100: }//getTextContent
101:
102: private void setTextContent(String text) {
103: m_TextContent = text;
104: }//setTextContent
105:
106: /**
107: * Sets the content-type (mime) of this <tt>JwmaMessagePart</tt>.
108: *
109: * @param type the content type of this <tt>JwmaMessagePart</tt>
110: * as <tt>String</tt>.
111: */
112: public void setContentType(String type) {
113: m_ContentType = type;
114: }//setContentType
115:
116: public int getSize() {
117: return m_Size;
118: }//getSize
119:
120: /**
121: * Sets the size of this <tt>JwmaMessagePart</tt>.
122: *
123: * @param size of this <tt>JwmaMessagePart</tt> in bytes.
124: */
125: private void setSize(int size) {
126: m_Size = size;
127: }//setSize
128:
129: public String getName() {
130: return m_Name;
131: }//getName
132:
133: /**
134: * Sets the name of this <tt>JwmaMessagePart</tt>.
135: *
136: * @param name the name of this <tt>JwmaMessagePart</tt>
137: * as <tt>String</tt>.
138: */
139: public void setName(String name) {
140: m_Name = name;
141: }//setName
142:
143: public String getDescription() {
144: return m_Description;
145: }//getDescription
146:
147: /**
148: * Sets the description of this <tt>JwmaMessagePart</tt>.
149: *
150: * @param description of this <tt>JwmaMessagePart</tt>
151: * as <tt>String</tt>.
152: */
153: public void setDescription(String description) {
154: m_Description = description;
155: }//setDescription
156:
157: /**
158: * Creates a <tt>JwmaMessagePartImpl</tt> instance from a given
159: * <tt>javax.mail.Part</tt> instance.
160: *
161: * @param part a <tt>javax.mail.Part</tt> instance.
162: * @param number the number of the part as <tt>int</tt>.
163: *
164: * @return the newly created instance.
165: * @throws JwmaException if it fails to create the new instance.
166: */
167: public static JwmaMessagePartImpl createJwmaMessagePartImpl(
168: Part part, int number) throws JwmaException {
169: JwmaMessagePartImpl partinfo = new JwmaMessagePartImpl(part,
170: number);
171:
172: //content type
173: try {
174: partinfo.setContentType(part.getContentType());
175:
176: //size
177: int size = part.getSize();
178: //JwmaKernel.getReference().debugLog().write("Part size="+size);
179:
180: //correct size of encoded parts
181: String[] encoding = part
182: .getHeader("Content-Transfer-Encoding");
183: if (encoding != null
184: && encoding.length > 0
185: && (encoding[0].equalsIgnoreCase("base64") || encoding[0]
186: .equalsIgnoreCase("uuencode"))) {
187:
188: //an encoded file is about 35% smaller in reality,
189: //so correct the size
190: size = (int) (size * 0.65);
191: }
192:
193: partinfo.setSize(size);
194: //description
195: partinfo.setDescription(part.getDescription());
196:
197: //filename
198: partinfo.setName(part.getFileName());
199:
200: //textcontent
201: if (partinfo.isMimeType("text/*")) {
202: Object content = part.getContent();
203: if (content instanceof String) {
204: partinfo.setTextContent((String) content);
205: } else if (content instanceof InputStream) {
206: InputStream in = (InputStream) content;
207: ByteArrayOutputStream bout = new ByteArrayOutputStream();
208: byte[] buffer = new byte[8192];
209: int amount = 0;
210: while ((amount = in.read(buffer)) >= 0) {
211: bout.write(buffer, 0, amount);
212: }
213: partinfo
214: .setTextContent(new String(bout.toString()));
215: }
216: }
217: } catch (Exception mex) {
218: throw new JwmaException("jwma.messagepart.failedcreation",
219: true).setException(mex);
220: }
221: return partinfo;
222: }//createJwmaMessagePartImpl
223:
224: /**
225: * Creates a <tt>JwmaMessagePartImpl</tt> instance with a given
226: * part number.
227: *
228: * @param number the number of the part as <tt>int</tt>.
229: *
230: * @return the newly created instance.
231: */
232: public static JwmaMessagePartImpl createJwmaMessagePartImpl(
233: int number) {
234: return new JwmaMessagePartImpl(number);
235: }//createMessagePartImpl
236:
237: }//class JwmaMessagePartImpl
|