Source Code Cross Referenced for SendFile.java in  » Portal » Open-Portal » com » sun » portal » netfile » servlet » java1 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.netfile.servlet.java1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /** 
002:         * $Id: SendFile.java,v 1.18 2005/11/30 11:26:35 ss150821 Exp $ 
003:         * Copyright 2002 Sun Microsystems, Inc. All 
004:         * rights reserved. Use of this product is subject 
005:         * to license terms. Federal Acquisitions: 
006:         * Commercial Software -- Government Users 
007:         * Subject to Standard License Terms and 
008:         * Conditions. 
009:         * 
010:         * Sun, Sun Microsystems, the Sun logo, and Sun ONE 
011:         * are trademarks or registered trademarks of Sun Microsystems, 
012:         * Inc. in the United States and other countries. 
013:         */package com.sun.portal.netfile.servlet.java1;
014:
015:        import java.util.*;
016:        import com.sun.portal.log.common.PortalLogger;
017:        import java.util.logging.*;
018:        import java.io.*;
019:        import javax.mail.*;
020:        import javax.mail.Session;
021:        import javax.mail.internet.*;
022:        import javax.activation.*;
023:        import java.net.UnknownHostException;
024:
025:        /**
026:         * SendFile will create a multipart message with the second
027:         * block of the message being the given file.<p>
028:         *
029:         * this demonstrates how to use the FileDataSource to send
030:         * a file via mail.<p>
031:         *
032:         * usage: <code>java SendFile <i>to from smtp file true|false</i></code>
033:         * where <i>to</i> and <i>from</i> are the destination and
034:         * origin email addresses, respectively, and <i>smtp</i>
035:         * is the hostname of the machine that has smtp server
036:         * running. The file is the file to send. the message next parameter either turns on or turns off
037:         * debugging during sending.
038:         *
039:         * @version	1.4, 98/06/16
040:         * @author	Christopher Cotton
041:         */
042:
043:        class SendFile {
044:
045:            private static Logger logger = PortalLogger
046:                    .getLogger(SendFile.class);
047:            private String s_empty_string = "";
048:
049:            SendFile() {
050:            }
051:
052:            String encode(String s_to_be_encoded, String character_set)
053:                    throws Exception {
054:                if (character_set == null) {
055:                } else if (character_set.equals(s_empty_string)) {
056:                } else {
057:                    s_to_be_encoded = MimeUtility.encodeText(s_to_be_encoded,
058:                            character_set, "B");
059:                }
060:                return s_to_be_encoded;
061:            }
062:
063:            String sendAttachedFile(String to, String from, String replyto,
064:                    String cc, String bcc, String host, String subject,
065:                    String message, Map filetosendList, String dbgval,
066:                    String charSet,
067:                    NetFileResource nfr_user_locale_i18n_bucket,
068:                    String displayFileName) throws NetFileException {
069:                try {
070:                    if (host.trim().equals(s_empty_string)) {
071:                        throw new NetFileException(
072:                                NetFileException.KEY_IDENTIFIER_PREFIX
073:                                        + "mail_server_not_configured");
074:                    }
075:                    //            logger.info("Charset="+charSet);
076:                    Object[] params0 = { charSet };
077:                    logger.log(Level.INFO, "PSSRNF_CSPNSJ1201", params0);
078:                    if (charSet.equals(s_empty_string)) {
079:                        charSet = "UTF8";
080:                    }
081:
082:                    // convert java charset into its MIME charset name
083:                    charSet = MimeUtility.mimeCharset(charSet);
084:
085:                    boolean debug = Boolean.valueOf(dbgval).booleanValue();
086:                    InternetAddress[] toaddress = null, ccaddress = null, bccaddress = null;
087:                    // create some properties and get the default Session
088:                    Properties props = System.getProperties();
089:                    props.put("mail.smtp.host", host);
090:
091:                    Session session = Session.getDefaultInstance(props, null);
092:                    session.setDebug(debug);
093:                    if (!charSet.equals(s_empty_string)) {
094:                        String firstFrom = s_empty_string;
095:                        String restFrom = s_empty_string;
096:                        int leftAnglendx;
097:                        leftAnglendx = from.indexOf("<");
098:                        if (leftAnglendx >= 0) {
099:                            firstFrom = from.substring(0, leftAnglendx);
100:                            restFrom = from.substring(leftAnglendx);
101:                            firstFrom = encode(firstFrom, charSet);
102:                            from = firstFrom + restFrom;
103:                        }
104:
105:                        String firstTo = s_empty_string;
106:                        String restTo = s_empty_string;
107:                        leftAnglendx = to.indexOf("<");
108:                        if (leftAnglendx >= 0) {
109:                            firstTo = to.substring(0, leftAnglendx);
110:                            restTo = to.substring(leftAnglendx);
111:                            firstTo = encode(firstTo, charSet);
112:                            to = firstTo + restTo;
113:                        }
114:
115:                        String firstReply = s_empty_string;
116:                        String restReply = s_empty_string;
117:                        leftAnglendx = replyto.indexOf("<");
118:                        if (leftAnglendx >= 0) {
119:                            firstReply = replyto.substring(0, leftAnglendx);
120:                            restReply = replyto.substring(leftAnglendx);
121:                            firstReply = encode(firstReply, charSet);
122:                            replyto = firstReply + restReply;
123:                        }
124:
125:                        String firstCC = s_empty_string;
126:                        String restCC = s_empty_string;
127:                        leftAnglendx = cc.indexOf("<");
128:                        if (leftAnglendx >= 0) {
129:                            firstCC = cc.substring(0, leftAnglendx);
130:                            restCC = cc.substring(leftAnglendx);
131:                            firstCC = encode(firstCC, charSet);
132:                            cc = firstCC + restCC;
133:                        }
134:
135:                        String firstBCC = s_empty_string;
136:                        String restBCC = s_empty_string;
137:                        leftAnglendx = bcc.indexOf("<");
138:                        if (leftAnglendx >= 0) {
139:                            firstBCC = bcc.substring(0, leftAnglendx);
140:                            restBCC = bcc.substring(leftAnglendx);
141:                            firstBCC = encode(firstBCC, charSet);
142:                            bcc = firstBCC + restBCC;
143:                        }
144:                    }
145:                    // create a message
146:                    MimeMessage msg = new MimeMessage(session);
147:                    msg.setFrom(new InternetAddress(from));
148:
149:                    try {
150:                        toaddress = this .parseAddress(to, charSet);
151:                    } catch (AddressException ae) {
152:                        throw new NetFileException(
153:                                NetFileException.KEY_IDENTIFIER_PREFIX
154:                                        + "unable_to_parse_address");
155:                    }
156:
157:                    msg.setRecipients(Message.RecipientType.TO, toaddress);
158:                    if ((replyto == null) || (replyto.equals(s_empty_string))) {
159:                        replyto = from;
160:                    }
161:                    InternetAddress[] replytoaddress = { new InternetAddress(
162:                            replyto) };
163:                    msg.setReplyTo(replytoaddress);
164:
165:                    if (cc.equals(s_empty_string) || (cc == null)) {
166:                    } else {
167:                        try {
168:                            ccaddress = this .parseAddress(cc, charSet);
169:                        } catch (AddressException ae) {
170:                            throw new NetFileException(
171:                                    NetFileException.KEY_IDENTIFIER_PREFIX
172:                                            + "unable_to_parse_address");
173:                        }
174:                        msg.setRecipients(Message.RecipientType.CC, ccaddress);
175:                    }
176:
177:                    if (bcc.equals(s_empty_string) || (bcc == null)) {
178:                    } else {
179:                        try {
180:                            bccaddress = this .parseAddress(bcc, charSet);
181:                        } catch (AddressException ae) {
182:                            throw new NetFileException(
183:                                    NetFileException.KEY_IDENTIFIER_PREFIX
184:                                            + "unable_to_parse_address");
185:                        }
186:                        msg
187:                                .setRecipients(Message.RecipientType.BCC,
188:                                        bccaddress);
189:                    }
190:                    msg.setSubject(encode(subject, charSet));
191:                    // create and fill the first message part
192:                    MimeBodyPart mbp1 = new MimeBodyPart();
193:                    mbp1.setText(message, charSet);
194:                    Multipart mp = new MimeMultipart();
195:                    mp.addBodyPart(mbp1);
196:
197:                    Iterator it = filetosendList.keySet().iterator();
198:                    while (it.hasNext()) {
199:                        Object theFileName = it.next();
200:                        String thefile = (filetosendList.get(theFileName))
201:                                .toString();
202:
203:                        // create the second message part
204:                        MimeBodyPart mbp2 = new MimeBodyPart();
205:
206:                        // attach the file to the message
207:                        FileDataSource fds = new FileDataSource(theFileName
208:                                .toString());
209:                        mbp2.setDataHandler(new DataHandler(fds));
210:
211:                        mbp2.setFileName(encode(thefile, charSet));
212:                        mbp2.setHeader("Content-Type",
213:                                "application/octet-stream; charset=" + charSet);
214:                        mbp2.setHeader("Content-Transfer-Encoding", "base64");
215:
216:                        mp.addBodyPart(mbp2);
217:                    }
218:
219:                    // add the Multipart to the message
220:                    msg.setContent(mp);
221:
222:                    // set the Date: header
223:                    msg.setSentDate(new Date());
224:
225:                    // send the message
226:                    Transport.send(msg);
227:                } catch (Exception e) {
228:                    processException(e, host);
229:                }
230:                //        logger.info("Did not throw exception properly");
231:                logger.info("PSSRNF_CSPNSJ1202");
232:                return nfr_user_locale_i18n_bucket.getString("info8");
233:            }
234:
235:            private void processException(Exception e, String host)
236:                    throws NetFileException {
237:                //        logger.log(Level.SEVERE, "Exception is sending the message",e);
238:                logger.log(Level.SEVERE, "PSSRNF_CSPNSJ1203");
239:                if (e instanceof  MessagingException) {
240:                    MessagingException me = (MessagingException) e;
241:                    while (true) {
242:                        Exception mex = me.getNextException();
243:                        if (mex != null) {
244:                            if (mex instanceof  UnknownHostException) {
245:                                throw new NetFileException(new String[] {
246:                                        host,
247:                                        NetFileException.KEY_IDENTIFIER_PREFIX
248:                                                + "textseperator",
249:                                        NetFileException.KEY_IDENTIFIER_PREFIX
250:                                                + "mail_server_unknown" });
251:                            } else if (mex instanceof  AuthenticationFailedException) {
252:                                throw new NetFileException(new String[] {
253:                                        host,
254:                                        NetFileException.KEY_IDENTIFIER_PREFIX
255:                                                + "textseperator",
256:                                        NetFileException.KEY_IDENTIFIER_PREFIX
257:                                                + "mail_server_auth_failed" });
258:                            } else if (mex instanceof  SendFailedException) {
259:                                throw new NetFileException(new String[] {
260:                                        host,
261:                                        NetFileException.KEY_IDENTIFIER_PREFIX
262:                                                + "textseperator",
263:                                        NetFileException.KEY_IDENTIFIER_PREFIX
264:                                                + "invalid_address" });
265:                            } else if (mex instanceof  MessagingException) {
266:                                me = (MessagingException) mex;
267:                            }
268:                        } else {
269:                            break;
270:                        }
271:                    }
272:                    throw new NetFileException(new String[] {
273:                            host,
274:                            NetFileException.KEY_IDENTIFIER_PREFIX
275:                                    + "textseperator",
276:                            NetFileException.KEY_IDENTIFIER_PREFIX
277:                                    + "mail_not_sent" });
278:                } else if (e instanceof  UnsupportedEncodingException) {
279:                    throw new NetFileException(
280:                            NetFileException.KEY_IDENTIFIER_PREFIX
281:                                    + "unsupported_encoding");
282:                } else {
283:                    //            logger.info("Throwing generic exception");
284:                    logger.info("PSSRNF_CSPNSJ1204");
285:                    throw new NetFileException(
286:                            NetFileException.KEY_IDENTIFIER_PREFIX + "error26");
287:                }
288:            }
289:
290:            /*
291:             * Parse s into an array of InternetAddresses that are properly encoded
292:             * based on the charset encoding.
293:             */
294:
295:            InternetAddress[] parseAddress(String s, String charset)
296:                    throws AddressException {
297:                InternetAddress[] addrs = InternetAddress.parse(s);
298:
299:                if (charset != null) {
300:                    for (int i = 0; i < addrs.length; i++) {
301:                        String personal = addrs[i].getPersonal();
302:                        if (personal != null) {
303:                            try {
304:                                personal = encode(personal, charset);
305:                                addrs[i].setPersonal(personal);
306:                            } catch (Exception e) {
307:                            }
308:                        }
309:                    }
310:                }
311:                return addrs;
312:            }
313:
314:            /**
315:             * Bug 4494943
316:             * Created to parse the string for comma delimited internet mail addresses
317:             * The use of personal names is not allowed and no effort is being made
318:             * as part of this function to parse for personal names and seperate them from
319:             * the internet email addresses.
320:             *
321:             * Thus email are expected to be entered by the user of two forms
322:             * 1. internetEmailAddress@domain
323:             * 2. emailAddress
324:             * Both forms should have commas seperators.
325:             *
326:             * This function has been created as the InternetAddress.parse(String) function
327:             * is throwing AddressException for valid email addresses.
328:             *
329:             */
330:            InternetAddress[] parseAddress(String s) throws AddressException {
331:                javax.mail.internet.InternetAddress[] addrs = null;
332:                javax.mail.internet.InternetAddress addr = null;
333:                ArrayList addrsList = new ArrayList();
334:
335:                java.util.StringTokenizer parsedAddrs = new java.util.StringTokenizer(
336:                        s, ",");
337:
338:                while (parsedAddrs.hasMoreTokens()) {
339:                    addr = new javax.mail.internet.InternetAddress(parsedAddrs
340:                            .nextToken());
341:                    addrsList.add(addr);
342:                }
343:
344:                addrs = new InternetAddress[addrsList.size()];
345:                addrsList.toArray(addrs);
346:                return addrs;
347:            }
348:
349:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.