001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020
021 package org.apache.mailet.base.mail;
022
023 import java.awt.datatransfer.DataFlavor;
024 import java.awt.datatransfer.UnsupportedFlavorException;
025 import java.io.IOException;
026
027 import javax.activation.ActivationDataFlavor;
028 import javax.activation.DataContentHandler;
029 import javax.activation.DataSource;
030 import javax.mail.MessagingException;
031
032
033 /**
034 * Abstract class providing common Data Handler behavior.
035 */
036 public abstract class AbstractDataContentHandler implements DataContentHandler
037 {
038
039 private ActivationDataFlavor fieldDataFlavor;
040
041 /**
042 * Default Constructor
043 */
044 public AbstractDataContentHandler()
045 {
046 super();
047 }
048
049 /**
050 * Update the current DataFlavor.
051 *
052 */
053 protected void updateDataFlavor()
054 {
055 setDataFlavor(computeDataFlavor());
056 }
057
058 /**
059 * Compute an ActivationDataFlavor.
060 *
061 * @return A new ActivationDataFlavor
062 */
063 abstract protected ActivationDataFlavor computeDataFlavor();
064
065 protected void setDataFlavor(ActivationDataFlavor aDataFlavor)
066 {
067 fieldDataFlavor = aDataFlavor;
068 }
069
070 /**
071 * @see javax.activation.DataContentHandler#getContent(javax.activation.DataSource)
072 */
073 public Object getContent(DataSource aDataSource) throws IOException
074 {
075 Object content = null;
076 try
077 {
078 content = computeContent(aDataSource);
079 }
080 catch (MessagingException e)
081 {
082 // No-op
083 }
084 return content;
085 }
086
087 /**
088 * Compute the content from aDataSource.
089 *
090 * @param aDataSource
091 * @return new Content built from the DataSource
092 * @throws MessagingException
093 */
094 abstract protected Object computeContent(DataSource aDataSource)
095 throws MessagingException;
096
097 /**
098 * @see javax.activation.DataContentHandler#getTransferData(java.awt.datatransfer.DataFlavor,
099 * 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;
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 }