1 | /* |
2 | * Copyright 2006-2010 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.item.mail; |
17 | |
18 | import org.springframework.mail.MailException; |
19 | import org.springframework.mail.MailMessage; |
20 | import org.springframework.mail.MailSendException; |
21 | |
22 | /** |
23 | * This {@link MailErrorHandler} implementation simply rethrows the exception it |
24 | * receives. |
25 | * |
26 | * @author Dan Garrette |
27 | * @author Dave Syer |
28 | * |
29 | * @since 2.1 |
30 | */ |
31 | public class DefaultMailErrorHandler implements MailErrorHandler { |
32 | |
33 | private static final int DEFAULT_MAX_MESSAGE_LENGTH = 1024; |
34 | |
35 | private int maxMessageLength = DEFAULT_MAX_MESSAGE_LENGTH; |
36 | |
37 | /** |
38 | * The limit for the size of message that will be copied to the exception |
39 | * message. Output will be truncated beyond that. Default value is 1024. |
40 | * |
41 | * @param maxMessageLength the maximum message length |
42 | */ |
43 | public void setMaxMessageLength(int maxMessageLength) { |
44 | this.maxMessageLength = maxMessageLength; |
45 | } |
46 | |
47 | /** |
48 | * Wraps the input exception with a runtime {@link MailException}. The |
49 | * exception message will contain the failed message (using toString). |
50 | * |
51 | * @param message a failed message |
52 | * @param exception a MessagingException |
53 | * @throws MailException a translation of the Exception |
54 | * @see MailErrorHandler#handle(MailMessage, Exception) |
55 | */ |
56 | @Override |
57 | public void handle(MailMessage message, Exception exception) throws MailException { |
58 | String msg = message.toString(); |
59 | throw new MailSendException("Mail server send failed: " |
60 | + msg.substring(0, Math.min(maxMessageLength, msg.length())), exception); |
61 | } |
62 | } |