%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
com.ozacc.mail.fetch.impl.sk_jp.AttachmentsExtractor |
|
|
1 | /* |
|
2 | * @(#) $Id: AttachmentsExtractor.java,v 1.1.2.1 2005/01/18 07:20:59 otsuka Exp $ |
|
3 | * Copyright (c) 2000 Shin Kinoshita All Rights Reserved. |
|
4 | */ |
|
5 | package com.ozacc.mail.fetch.impl.sk_jp; |
|
6 | ||
7 | import java.io.IOException; |
|
8 | import java.io.InputStream; |
|
9 | import java.io.OutputStream; |
|
10 | import java.util.ArrayList; |
|
11 | import java.util.List; |
|
12 | import javax.mail.MessagingException; |
|
13 | import javax.mail.Part; |
|
14 | import javax.mail.internet.ContentType; |
|
15 | ||
16 | /** |
|
17 | * źÉÕ¥Õ¥¡¥¤¥?¤òÃ?½Ð¤¹¤?PartHandler¤Ç¤¹¡£ |
|
18 | * <p> |
|
19 | * MultipartUtility#process()¸Æ¤Ó½Ð¤·¸å¤ËgetFileNames()¤Ë¤è¤Ã¤Æ¡¢ źÉÕ¥Õ¥¡¥¤¥?̾¤ÎÇÛÎó¤òÆÀ¤?¤³¤È¤¬¤Ç¤¤Þ¤¹¡£ |
|
20 | * </p> |
|
21 | * <p> |
|
22 | * ¥Õ¥¡¥¤¥?̾ÇÛÎó¤Îindex¤ò»ØÄꤷ¤Æ¤½¤ÎźÉÕ¥Õ¥¡¥¤¥?¤ËÂФ¹¤? |
|
23 | * InputStream¤òÆÀ¤¿¤ê¡¢ÅϤµ¤?¤¿OutputStream¤ËÂФ·¤Æ½ñ¤½Ð¤¹¤³¤È¤¬¤Ç¤¤Þ¤¹¡£ |
|
24 | * </p> |
|
25 | * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:59 $ |
|
26 | * @author Shin |
|
27 | */ |
|
28 | public class AttachmentsExtractor implements PartHandler { |
|
29 | ||
30 | /** message/*¤Î¥Ñ¡¼¥È¤ò̵»?¤·¤Þ¤¹¡£ */ |
|
31 | public static final int MODE_IGNORE_MESSAGE = 1; |
|
32 | ||
33 | /** Content-Disposition: inline; ¥Ñ¡¼¥È¤Ïfilename¤¬¤¢¤Ã¤Æ¤â̵»?¤·¤Þ¤¹¡£ */ |
|
34 | public static final int MODE_IGNORE_INLINE = 2; |
|
35 | ||
36 | private final int mode; |
|
37 | ||
38 | 0 | private final List attachmentParts = new ArrayList(); |
39 | ||
40 | /** |
|
41 | * źÉÕ¥Õ¥¡¥¤¥?°?Í÷¤òÆÀ¤?¤¿¤á¤ÎPartHandler¤òºûÜ®¤·¤Þ¤¹¡£ message/*¤Î¥Ñ¡¼¥È¤äinline³ûÀÄ¥Õ¥¡¥¤¥?̾»ØÄꤢ¤ê¤Î¥Ñ¡¼¥È¤? |
|
42 | * źÉÕ¥Õ¥¡¥¤¥?¤È¤·¤Æ°·¤¤¤Þ¤¹¡£ |
|
43 | */ |
|
44 | public AttachmentsExtractor() { |
|
45 | 0 | this(0); |
46 | 0 | } |
47 | ||
48 | /** |
|
49 | * źÉÕ¥Õ¥¡¥¤¥?°?Í÷¤òÆÀ¤?¤¿¤á¤ÎPartHandler¤òºûÜ®¤·¤Þ¤¹¡£ |
|
50 | * @param mode ưºûÁ⡼¥É¡£MODE_¤Ç»Ï¤Þ¤?¼±Ê̻Ҥòor»ØÄꤷ¤Þ¤¹¡£ |
|
51 | */ |
|
52 | 0 | public AttachmentsExtractor(int mode) { |
53 | 0 | this.mode = mode; |
54 | 0 | } |
55 | ||
56 | /** MultipartUtility#process()¤«¤é¸Æ¤Ó¤À¤µ¤?¤?¥á¥½¥Ã¥É¤Ç¤¹¡£ */ |
|
57 | public boolean processPart(Part part, ContentType context) throws MessagingException, |
|
58 | IOException { |
|
59 | 0 | if (part.isMimeType("message/*")) { |
60 | 0 | if ((mode & MODE_IGNORE_MESSAGE) != 0) { |
61 | 0 | return true; |
62 | } |
|
63 | 0 | attachmentParts.add(part); |
64 | 0 | return true; |
65 | } |
|
66 | 0 | if (MailUtility.getFileName(part) == null) { |
67 | 0 | return true; |
68 | } |
|
69 | 0 | if ((mode & MODE_IGNORE_INLINE) != 0 && Part.INLINE.equalsIgnoreCase(part.getDisposition())) { |
70 | 0 | return true; |
71 | } |
|
72 | 0 | attachmentParts.add(part); |
73 | 0 | return true; |
74 | } |
|
75 | ||
76 | /** |
|
77 | * źÉÕ¥Õ¥¡¥¤¥?¸Ä¿ô¤òÊÖ¤·¤Þ¤¹¡£ |
|
78 | */ |
|
79 | public int getCount() { |
|
80 | 0 | return attachmentParts.size(); |
81 | } |
|
82 | ||
83 | /** |
|
84 | * źÉÕ¥Õ¥¡¥¤¥?̾¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£ |
|
85 | * <P> |
|
86 | * źÉÕ¥Õ¥¡¥¤¥?¤¬Â¸ºß¤·¤Ê¤¤¾?¹ç¤Ï¶õ¤ÎÇÛÎó¤òÊÖ¤·¤Þ¤¹¡£ <BR> |
|
87 | * ¥Õ¥¡¥¤¥?̾¤ÏƱ°?¤Î¤â¤Î¤¬Ê£¿ô¸ºß¤¹¤?»ö¤â¤¢¤ê¤¨¤Þ¤¹¡£ |
|
88 | * </P> |
|
89 | */ |
|
90 | public String[] getFileNames() throws MessagingException { |
|
91 | 0 | String[] names = new String[getCount()]; |
92 | 0 | for (int i = 0; i < names.length; i++) { |
93 | 0 | names[i] = getFileName(i); |
94 | } |
|
95 | 0 | return names; |
96 | } |
|
97 | ||
98 | /** |
|
99 | * »ØÄ?źÉÕ¥Õ¥¡¥¤¥?¤Î¥Õ¥¡¥¤¥?̾¤òÊÖ¤·¤Þ¤¹¡£ |
|
100 | */ |
|
101 | public String getFileName(int index) throws MessagingException { |
|
102 | 0 | Part part = (Part)attachmentParts.get(index); |
103 | 0 | String name = MailUtility.getFileName(part); |
104 | 0 | if (name == null) { |
105 | // źÉÕ¥Õ¥¡¥¤¥?̾¤¬¼èÆÀ¤Ç¤¤Ê¤¤¾?¹ç¤Ï¡¢»ØÄꤵ¤?¤Æ¤¤¤Ê¤«¤Ã¤¿¾?¹ç¤«¡¢ |
|
106 | // ¤¢¤?¤¤¤Ïmessage/*¤Î¥Ñ¡¼¥È¤Î¾?¹ç¤Ç¤¹¡£ |
|
107 | // ¤³¤Î¾?¹ç¤Ï²¾¤Î¥Õ¥¡¥¤¥?̾¤òÉÕ¤±¤?¤³¤È¤È¤·¤Þ¤¹¡£ |
|
108 | 0 | if (part.isMimeType("message/*")) { |
109 | // If part is Message, create temporary filename. |
|
110 | 0 | name = "message" + index + ".eml"; |
111 | } else { |
|
112 | 0 | name = "file" + index + ".tmp"; |
113 | } |
|
114 | } |
|
115 | 0 | return name; |
116 | } |
|
117 | ||
118 | /** |
|
119 | * »ØÄ?źÉÕ¥Õ¥¡¥¤¥?¤ÎContent-Type¤òÊÖ¤·¤Þ¤¹¡£ |
|
120 | */ |
|
121 | public String getContentType(int index) throws MessagingException { |
|
122 | 0 | return MailUtility.unfold(((Part)attachmentParts.get(index)).getContentType()); |
123 | } |
|
124 | ||
125 | /** |
|
126 | * »ØÄ?źÉÕ¥Õ¥¡¥¤¥?¤Î¥µ¥¤¥º¤òÊÖ¤·¤Þ¤¹¡£ |
|
127 | */ |
|
128 | public int getSize(class="keyword">int index) throws MessagingException { |
|
129 | 0 | return ((Part)attachmentParts.get(index)).getSize(); |
130 | } |
|
131 | ||
132 | /** |
|
133 | * »ØÄ?źÉÕ¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤à¥¹¥È¥ê¡¼¥à¤òÊÖ¤·¤Þ¤¹¡£ |
|
134 | */ |
|
135 | public InputStream getInputStream(int index) throws MessagingException, IOException { |
|
136 | 0 | return ((Part)attachmentParts.get(index)).getInputStream(); |
137 | } |
|
138 | ||
139 | /** |
|
140 | * »ØÄ?źÉÕ¥Õ¥¡¥¤¥?¤ò»ØÄ?¥¹¥È¥ê¡¼¥à¤Ë½ñ¤½Ð¤·¤Þ¤¹¡£ |
|
141 | */ |
|
142 | public void writeTo(int index, OutputStream out) throws MessagingException, IOException { |
|
143 | 0 | InputStream in = getInputStream(index); |
144 | 0 | byte[] buf = new byte[1024]; |
145 | int len; |
|
146 | 0 | while ((len = in.read(buf)) != -1) { |
147 | 0 | out.write(buf, 0, len); |
148 | } |
|
149 | 0 | } |
150 | ||
151 | public static void main(String[] args) throws Exception { |
|
152 | 0 | javax.mail.internet.MimeMessage msg = new javax.mail.internet.MimeMessage( |
153 | 0 | javax.mail.Session.getDefaultInstance(System.getProperties(), null), System.in); |
154 | 0 | AttachmentsExtractor h = new AttachmentsExtractor(); |
155 | 0 | MultipartUtility.process(msg, h); |
156 | 0 | for (int i = 0; i < h.getCount(); i++) { |
157 | 0 | System.out.println("Attachment no : " + i); |
158 | 0 | System.out.println("Filename = " + h.getFileName(i)); |
159 | 0 | System.out.println("******************"); |
160 | 0 | h.writeTo(i, System.out); |
161 | } |
|
162 | 0 | } |
163 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |