001: /*
002: * DownloadAttachmentTag
003: * @(#)DoanloadAttachmentMessageTag.java 1.1 05/03/03 Sun Microsystems
004: *
005: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
006: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
007: */
008:
009: package com.sun.portal.wireless.taglibs.mail;
010:
011: import javax.mail.Part;
012: import javax.mail.MessagingException;
013: import java.util.StringTokenizer;
014: import javax.servlet.jsp.JspException;
015: import javax.servlet.http.HttpServletResponse;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.ServletOutputStream;
018: import java.io.InputStream;
019: import java.io.BufferedInputStream;
020: import java.io.PrintWriter;
021:
022: import com.sun.portal.wireless.taglibs.base.*;
023:
024: import com.sun.portal.wireless.transcode.*;
025: import com.iplanet.am.util.AMClientDetector;
026: import com.iplanet.services.cdm.Client;
027: import com.iplanet.services.cdm.ClientsManager;
028: import com.iplanet.services.cdm.ClientException;
029: import java.util.Set;
030: import java.io.ByteArrayOutputStream;
031: import java.io.ByteArrayInputStream;
032:
033: /**
034: *
035: * @version 1.0
036: * @see com.sun.portal.wireless.taglibs.base.BeanTag
037: *
038: * @author Bhavanishankar
039: */
040:
041: public class DownloadAttachmentTag extends BeanSupport {
042:
043: /**
044: * Read the attachment.
045: *
046: * @return SKIP_INCLUDE
047: * @exception JspException
048: */
049: public int doStartTag() throws JspException {
050:
051: try {
052:
053: bean = findBean();
054:
055: if (getProperty() == null
056: || getProperty().trim().length() == 0) {
057: setProperty("part");
058: }
059:
060: Part part = (Part) readProperty(bean);
061:
062: InputStream input = part.getInputStream();
063: BufferedInputStream bis = null;
064:
065: HttpServletResponse res = (HttpServletResponse) pageContext
066: .getResponse();
067:
068: // Transcode the attachment if it is an image
069: String format = getContentType(part);
070: if (format.toLowerCase().startsWith("image/")) {
071: try {
072: // We need to transcode
073: Transcoder trans = new Transcoder();
074: ByteArrayOutputStream os = new ByteArrayOutputStream();
075: byte[] arr = new byte[4096];
076: int num;
077: while ((num = input.read(arr, 0, 4096)) > 0) {
078: os.write(arr, 0, num);
079: }
080: arr = os.toByteArray();
081: input = new ByteArrayInputStream(arr);
082:
083: trans.setImage(input, format);
084: HttpServletRequest req = (HttpServletRequest) pageContext
085: .getRequest();
086: AMClientDetector cd = new AMClientDetector();
087: String clientType = cd.getClientType(req);
088: Client client = ClientsManager
089: .getInstance(clientType);
090: Set accept = client
091: .getProperties(TranscodeConstants.ACCEPT_PROPERTY);
092: trans.setTargetType(null, accept);
093:
094: ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
095: String ctype = trans.transcode(byteArray);
096: res.setContentType(ctype);
097: bis = new BufferedInputStream(
098: new ByteArrayInputStream(byteArray
099: .toByteArray()));
100: } catch (Exception e) {
101: res.setContentType(getContentType(part));
102: input.reset();
103: bis = new BufferedInputStream(input);
104: }
105: } else {
106: res.setContentType(format);
107: bis = new BufferedInputStream(input);
108: }
109: res.setHeader("Content-Disposition", "attachment;filename="
110: + part.getFileName());
111:
112: if (format != null
113: && format.toLowerCase().startsWith("text")) {
114: PrintWriter out = res.getWriter();
115: byte[] buf = new byte[4 * 1024]; // 4K buffer
116: StringBuffer content = new StringBuffer();
117: int bytesRead = -1;
118: while ((bytesRead = bis.read(buf, 0, 4 * 1024)) != -1) {
119: content.append(new String(buf));
120: }
121: out.write(content.toString());
122: } else {
123: ServletOutputStream out = res.getOutputStream();
124: byte[] buf = new byte[4 * 1024]; // 4K buffer
125: int bytesRead = -1;
126: while ((bytesRead = bis.read(buf, 0, 4 * 1024)) != -1) {
127: out.write(buf, 0, bytesRead);
128: }
129: }
130: res.flushBuffer();
131: bis.close();
132: } catch (Exception e) {
133: throw new JspException(getClass().getName()
134: + " : Failed to read email attachment", e);
135: }
136:
137: return SKIP_BODY;
138:
139: }
140:
141: /**
142: *
143: * Skip evaluating the page.
144: *
145: * @return SKIP_PAGE
146: * @exception JspException
147: */
148: public int doEndTag() throws JspException {
149:
150: return SKIP_PAGE;
151: }
152:
153: /**
154: * Parse the contenttype string.
155: *
156: * @return the contenttype.
157: */
158: private String getContentType(Part part) throws MessagingException {
159: String ct = part.getContentType();
160: if (ct == null)
161: return "application/octet-stream";
162: StringTokenizer st = new StringTokenizer(ct, ";");
163: return st.nextToken().toLowerCase();
164: }
165:
166: }
|