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
21 package org.apache.mailet.base.mail;
22
23 import java.awt.datatransfer.DataFlavor;
24 import java.awt.datatransfer.UnsupportedFlavorException;
25 import java.io.IOException;
26
27 import javax.activation.ActivationDataFlavor;
28 import javax.activation.DataContentHandler;
29 import javax.activation.DataSource;
30 import javax.mail.MessagingException;
31
32
33 /**
34 * Abstract class providing common Data Handler behavior.
35 */
36 public abstract class AbstractDataContentHandler implements DataContentHandler
37 {
38
39 private ActivationDataFlavor fieldDataFlavor;
40
41 /**
42 * Default Constructor
43 */
44 public AbstractDataContentHandler()
45 {
46 super();
47 }
48
49 /**
50 * Update the current DataFlavor.
51 *
52 */
53 protected void updateDataFlavor()
54 {
55 setDataFlavor(computeDataFlavor());
56 }
57
58 /**
59 * Compute an ActivationDataFlavor.
60 *
61 * @return A new ActivationDataFlavor
62 */
63 abstract protected ActivationDataFlavor computeDataFlavor();
64
65 protected void setDataFlavor(ActivationDataFlavor aDataFlavor)
66 {
67 fieldDataFlavor = aDataFlavor;
68 }
69
70 /**
71 * @see javax.activation.DataContentHandler#getContent(javax.activation.DataSource)
72 */
73 public Object getContent(DataSource aDataSource) throws IOException
74 {
75 Object content = null;
76 try
77 {
78 content = computeContent(aDataSource);
79 }
80 catch (MessagingException e)
81 {
82 // No-op
83 }
84 return content;
85 }
86
87 /**
88 * Compute the content from aDataSource.
89 *
90 * @param aDataSource
91 * @return new Content built from the DataSource
92 * @throws MessagingException
93 */
94 abstract protected Object computeContent(DataSource aDataSource)
95 throws MessagingException;
96
97 /**
98 * @see javax.activation.DataContentHandler#getTransferData(java.awt.datatransfer.DataFlavor,
99 * javax.activation.DataSource)
100 */
101 public Object getTransferData(DataFlavor aDataFlavor, DataSource aDataSource)
102 throws UnsupportedFlavorException, IOException
103 {
104 Object content = null;
105 if (getDataFlavor().equals(aDataFlavor))
106 content = getContent(aDataSource);
107 return content;
108 }
109
110 /**
111 * @see javax.activation.DataContentHandler#getTransferDataFlavors()
112 */
113 public DataFlavor[] getTransferDataFlavors()
114 {
115 return new DataFlavor[]{getDataFlavor()};
116 }
117
118 /**
119 * Get the DataFlavor, lazily initialised if required.
120 *
121 * @return Returns the dataFlavor, lazily initialised.
122 */
123 protected ActivationDataFlavor getDataFlavor()
124 {
125 ActivationDataFlavor dataFlavor = null;
126 if (null == (dataFlavor = getDataFlavorBasic()))
127 {
128 updateDataFlavor();
129 return getDataFlavor();
130 }
131 return dataFlavor;
132 }
133
134 /**
135 * Get the DataFlavor.
136 *
137 * @return Returns the dataFlavor.
138 */
139 private ActivationDataFlavor getDataFlavorBasic()
140 {
141 return fieldDataFlavor;
142 }
143
144 }