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