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: ****************************************************************/package org.apache.james.util.mail.handlers;
019:
020: import java.awt.datatransfer.DataFlavor;
021: import java.awt.datatransfer.UnsupportedFlavorException;
022: import java.io.IOException;
023:
024: import javax.activation.ActivationDataFlavor;
025: import javax.activation.DataContentHandler;
026: import javax.activation.DataSource;
027: import javax.mail.MessagingException;
028:
029: /**
030: * Abstract class providing common Data Handler behavior.
031: */
032: public abstract class AbstractDataContentHandler implements
033: DataContentHandler {
034:
035: private ActivationDataFlavor fieldDataFlavor;
036:
037: /**
038: * Default Constructor
039: */
040: public AbstractDataContentHandler() {
041: super ();
042: }
043:
044: /**
045: * Update the current DataFlavor.
046: *
047: */
048: protected void updateDataFlavor() {
049: setDataFlavor(computeDataFlavor());
050: }
051:
052: /**
053: * Compute an ActivationDataFlavor.
054: *
055: * @return A new ActivationDataFlavor
056: */
057: abstract protected ActivationDataFlavor computeDataFlavor();
058:
059: protected void setDataFlavor(ActivationDataFlavor aDataFlavor) {
060: fieldDataFlavor = aDataFlavor;
061: }
062:
063: /**
064: * @see javax.activation.DataContentHandler#getContent(javax.activation.DataSource)
065: */
066: public Object getContent(DataSource aDataSource) throws IOException {
067: Object content = null;
068: try {
069: content = computeContent(aDataSource);
070: } catch (MessagingException e) {
071: // No-op
072: }
073: return content;
074: }
075:
076: /**
077: * Compute the content from aDataSource.
078: *
079: * @param aDataSource
080: * @return new Content built from the DataSource
081: * @throws MessagingException
082: */
083: abstract protected Object computeContent(DataSource aDataSource)
084: throws MessagingException;
085:
086: /**
087: * @see javax.activation.DataContentHandler#getTransferData(java.awt.datatransfer.DataFlavor,
088: * javax.activation.DataSource)
089: */
090: public Object getTransferData(DataFlavor aDataFlavor,
091: DataSource aDataSource) throws UnsupportedFlavorException,
092: IOException {
093: Object content = null;
094: if (getDataFlavor().equals(aDataFlavor))
095: content = getContent(aDataSource);
096: return content;
097: }
098:
099: /**
100: * @see javax.activation.DataContentHandler#getTransferDataFlavors()
101: */
102: public DataFlavor[] getTransferDataFlavors() {
103: return new DataFlavor[] { getDataFlavor() };
104: }
105:
106: /**
107: * Get the DataFlavor, lazily initialised if required.
108: *
109: * @return Returns the dataFlavor, lazily initialised.
110: */
111: protected ActivationDataFlavor getDataFlavor() {
112: ActivationDataFlavor dataFlavor = null;
113: if (null == (dataFlavor = getDataFlavorBasic())) {
114: updateDataFlavor();
115: return getDataFlavor();
116: }
117: return dataFlavor;
118: }
119:
120: /**
121: * Get the DataFlavor.
122: *
123: * @return Returns the dataFlavor.
124: */
125: private ActivationDataFlavor getDataFlavorBasic() {
126: return fieldDataFlavor;
127: }
128:
129: }
|