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
20 package org.apache.james.transport.mailets;
21
22 import org.apache.mailet.base.FlowedMessageUtils;
23 import org.apache.mailet.base.GenericMailet;
24 import org.apache.mailet.Mail;
25 import org.apache.mailet.MailetException;
26
27 import javax.mail.MessagingException;
28
29 import java.io.IOException;
30
31 /**
32 * Convert a message to format=flowed
33 */
34 public class WrapText extends GenericMailet {
35 private static final String PARAMETER_NAME_FLOWED_DELSP = "delsp";
36 private static final String PARAMETER_NAME_WIDTH = "width";
37
38 private boolean optionFlowedDelsp = false;
39 private int optionWidth = FlowedMessageUtils.RFC2646_WIDTH;
40
41 /**
42 * returns a String describing this mailet.
43 *
44 * @return A desciption of this mailet
45 */
46 public String getMailetInfo() {
47 return "WrapText";
48 }
49
50 private static boolean getBooleanParameter(String v, boolean def) {
51 return def ?
52 !(v != null && (v.equalsIgnoreCase("false") || v.equalsIgnoreCase("no"))) :
53 v != null && (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes")) ;
54 }
55
56 public void init() throws MailetException {
57 optionFlowedDelsp = getBooleanParameter(getInitParameter(PARAMETER_NAME_FLOWED_DELSP), optionFlowedDelsp);
58 optionWidth = Integer.parseInt(getInitParameter(PARAMETER_NAME_WIDTH, "" + optionWidth));
59 }
60
61 public void service(Mail mail) throws MailetException {
62 // TODO We could even manage the flow when the message is quoted-printable
63
64 try {
65 FlowedMessageUtils.flowMessage(mail.getMessage(), optionFlowedDelsp, optionWidth);
66
67 } catch (MessagingException e) {
68 throw new MailetException("Could not wrap message", e);
69
70 } catch (IOException e) {
71 throw new MailetException("Could not wrap message", e);
72 }
73
74 }
75 }