1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19 package org.apache.james.postage.client;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.Reader;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.MimeMessage;
28
29 import org.apache.james.fetchmail.ReaderInputStream;
30 import org.apache.james.postage.mail.MailAnalyzeStrategy;
31 import org.apache.james.postage.result.PostageRunnerResult;
32
33 public class POP3MailAnalyzeStrategy extends MailAnalyzeStrategy {
34
35 private int mailNumber;
36 private int mailIndex;
37 private org.apache.commons.net.pop3.POP3Client pop3Client;
38
39 public POP3MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results,
40 org.apache.commons.net.pop3.POP3Client pop3Client,
41 int mailNumber, int mailIndex) {
42 super(receivingQueueName, results);
43 this.pop3Client = pop3Client;
44 this.mailNumber = mailNumber;
45 this.mailIndex = mailIndex;
46 }
47
48 protected MimeMessage loadMessage() throws Exception {
49 Reader reader = pop3Client.retrieveMessage(mailNumber);
50 BufferedReader mailReader = new BufferedReader(reader);
51 InputStream in = new ReaderInputStream(mailReader);
52 MimeMessage message;
53 try {
54 message = new MimeMessage(null, in);
55 in.close();
56 } catch (IOException e) {
57 log.info("failed to close POP3 mail reader.");
58 throw e;
59 } catch (MessagingException e) {
60 log.info("failed to process POP3 mail. remains on server");
61 throw e;
62 } finally {
63 if (in != null) {
64 try {
65 in.close();
66 } catch (IOException e) {
67 log.warn("error closing mail input stream");
68 }
69 }
70 if (mailReader != null) {
71 try {
72 mailReader.close();
73 } catch (IOException e) {
74 log.warn("error closing mail reader");
75 }
76 }
77 if (reader != null) {
78 try {
79 reader.close();
80 } catch (IOException e) {
81 log.warn("error closing (mail) reader");
82 }
83 }
84 }
85
86 return message;
87 }
88
89 protected void dismissMessage() throws Exception {
90 try {
91 pop3Client.deleteMessage(mailIndex + 1);
92 } catch (Exception e) {
93 log.info("failed to delete POP3 mail.");
94 throw e;
95 }
96 }
97
98
99 }