1 /************************************************************************
2 * Copyright (c) 2003-2006 The Apache Software Foundation. *
3 * All rights reserved. *
4 * ------------------------------------------------------------------- *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you *
6 * may not use this file except in compliance with the License. You *
7 * may obtain a copy of the License at: *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14 * implied. See the License for the specific language governing *
15 * permissions and limitations under the License. *
16 ***********************************************************************/
17 package org.apache.james.util.mail.handlers;
18
19 import java.awt.datatransfer.DataFlavor;
20 import java.awt.datatransfer.UnsupportedFlavorException;
21 import java.io.IOException;
22
23 import javax.activation.ActivationDataFlavor;
24 import javax.activation.DataContentHandler;
25 import javax.activation.DataSource;
26 import javax.mail.MessagingException;
27
28
29 /***
30 * Abstract class providing common Data Handler behavior.
31 */
32 public abstract class AbstractDataContentHandler implements DataContentHandler
33 {
34
35 private ActivationDataFlavor fieldDataFlavor;
36
37 /***
38 * Default Constructor
39 */
40 public AbstractDataContentHandler()
41 {
42 super();
43 }
44
45 /***
46 * Update the current DataFlavor.
47 *
48 */
49 protected void updateDataFlavor()
50 {
51 setDataFlavor(computeDataFlavor());
52 }
53
54 /***
55 * Compute an ActivationDataFlavor.
56 *
57 * @return A new ActivationDataFlavor
58 */
59 abstract protected ActivationDataFlavor computeDataFlavor();
60
61 protected void setDataFlavor(ActivationDataFlavor aDataFlavor)
62 {
63 fieldDataFlavor = aDataFlavor;
64 }
65
66 /***
67 * @see javax.activation.DataContentHandler#getContent(javax.activation.DataSource)
68 */
69 public Object getContent(DataSource aDataSource) throws IOException
70 {
71 Object content = null;
72 try
73 {
74 content = computeContent(aDataSource);
75 }
76 catch (MessagingException e)
77 {
78
79 }
80 return content;
81 }
82
83 /***
84 * Compute the content from aDataSource.
85 *
86 * @param aDataSource
87 * @return new Content built from the DataSource
88 * @throws MessagingException
89 */
90 abstract protected Object computeContent(DataSource aDataSource)
91 throws MessagingException;
92
93 /***
94 * @see javax.activation.DataContentHandler#getTransferData(java.awt.datatransfer.DataFlavor,
95 * javax.activation.DataSource)
96 */
97 public Object getTransferData(DataFlavor aDataFlavor, DataSource aDataSource)
98 throws UnsupportedFlavorException, IOException
99 {
100 Object content = null;
101 if (getDataFlavor().equals(aDataFlavor))
102 content = getContent(aDataSource);
103 return content;
104 }
105
106 /***
107 * @see javax.activation.DataContentHandler#getTransferDataFlavors()
108 */
109 public DataFlavor[] getTransferDataFlavors()
110 {
111 return new DataFlavor[]{getDataFlavor()};
112 }
113
114 /***
115 * Get the DataFlavor, lazily initialised if required.
116 *
117 * @return Returns the dataFlavor, lazily initialised.
118 */
119 protected ActivationDataFlavor getDataFlavor()
120 {
121 ActivationDataFlavor dataFlavor = null;
122 if (null == (dataFlavor = getDataFlavorBasic()))
123 {
124 updateDataFlavor();
125 return getDataFlavor();
126 }
127 return dataFlavor;
128 }
129
130 /***
131 * Get the DataFlavor.
132 *
133 * @return Returns the dataFlavor.
134 */
135 private ActivationDataFlavor getDataFlavorBasic()
136 {
137 return fieldDataFlavor;
138 }
139
140 }