1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.impl;
20
21 import java.awt.datatransfer.DataFlavor;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.logging.Logger;
25
26 import javax.activation.DataHandler;
27 import javax.activation.DataSource;
28
29 import net.sourceforge.mflow.api.ContentHandler;
30 import net.sourceforge.mflow.util.IOUtil;
31
32 /***
33 * A JavaBean compliant wrapper for a DataHandler
34 *
35 * @author <a href="mailto:david@carr.name">David Carr</a>
36 * @version $Revision: 1.4 $
37 */
38 public class ContentHandlerImpl implements ContentHandler {
39 private static Logger log =
40 Logger.getLogger(ContentHandlerImpl.class.getName());
41 private DataHandler data;
42 private Object contentCache;
43
44 /***
45 * Constructor taking a content Object and a MIME type
46 *
47 * @param obj the content object to use to create a DataHandler
48 * @param mimeType the MIME type to use to create a DataHandler
49 */
50 ContentHandlerImpl(Object obj, String mimeType) {
51 this.data = new DataHandler(obj, mimeType);
52 }
53
54 /***
55 * Constructor taking a DataSource
56 *
57 * @param ds the DataSource to use to create a DataHandler
58 */
59 ContentHandlerImpl(DataSource ds) {
60 this.data = new DataHandler(ds);
61 }
62
63 /***
64 * Constructor taking a DataHandler
65 *
66 * @param dh the DataHandler to wrap
67 */
68 ContentHandlerImpl(DataHandler dh) {
69 this.data = dh;
70 }
71
72 public Object getContent() {
73 if(contentCache != null) {
74 return contentCache;
75 }
76 Object o = null;
77 try {
78 o = this.data.getContent();
79 } catch (IOException ioe) {
80 log.throwing("ContentHandler", "getContent()", ioe);
81 }
82 if (o == null) {
83 return null;
84 } else if (o instanceof InputStream) {
85 InputStream is = (InputStream) o;
86 try {
87 o = IOUtil.readStreamToByteArray(is);
88 } catch (IOException ioe) {
89 log.throwing("ContentHandler", "getContent()", ioe);
90 o = null;
91 }
92 }
93 contentCache = o;
94 return o;
95 }
96
97 public String getContentType() {
98 return this.data.getContentType();
99 }
100
101 public void setContentType(String mimeType) {
102 this.data = new DataHandler(getContent(), mimeType);
103 }
104
105 public void setContent(Object o) {
106 this.data = new DataHandler(o, this.data.getContentType());
107 contentCache = null;
108 }
109
110 public DataHandler getDataHandler() {
111 return this.data;
112 }
113
114 public DataFlavor[] getTransferDataFlavors() {
115 return this.data.getTransferDataFlavors();
116 }
117 }
This page was automatically generated by Maven