1 /***
2 * Title: S/MIME Project
3 * Description: Creating S/MIME email transport capabilities.
4 * Copyright: Copyright (c) 2001
5 * @Author Vladimir Radisic
6 * @Version 2.0.1
7 */
8
9 package org.webdocwf.util.smime.activation;
10
11
12 import org.webdocwf.util.smime.exception.SMIMEException;
13 import org.webdocwf.util.smime.util.ConvertAssist;
14 import javax.activation.DataSource;
15 import javax.activation.MimetypesFileTypeMap;
16 import java.io.ByteArrayInputStream;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.io.IOException;
20
21
22 /***
23 * StreamDataSource represents implementation of DataSource interfaces, and as
24 * result of that, objects of this class could be used within MimeBodyPart
25 * objects to help them to read data from objects which they conveys. This class
26 * is designed to help in getting data from objects of type InputStream and all
27 * its subclasses.
28 */
29 public class StreamDataSource implements DataSource {
30
31 /***
32 * Storage for obtained content-type.
33 */
34 private String contentType = null;
35
36 /***
37 * Storage for obtained file name.
38 */
39 private String fileName = "";
40
41 /***
42 * Storage for data derived from InputStream.
43 */
44 private byte[] att = null;
45
46 /***
47 * Constructs StreamDataSource with given input stream and coresponding string
48 * which contains virtual or real file name. Extension of file name is used to
49 * get appropriate mime-type. All mime-types which are predefined according to
50 * particular file extensions can be found in mime.types file in META-INF directory
51 * from smime.jar file. This file can be changed or extended so that it could adjust
52 * mime-types to aditional requests. For more information see Java documentation
53 * related to class MimetypesFileTypeMap.
54 */
55 public StreamDataSource(InputStream in0, String fileName0) throws SMIMEException {
56
57 att = ConvertAssist.inStreamToByteArray(in0);
58
59 if (fileName0 != null) {
60 contentType = new MimetypesFileTypeMap().getContentType(fileName0);
61 fileName = new String(fileName0);
62 }
63 if (contentType == null || contentType.equalsIgnoreCase(""))
64 contentType = "application/octet-stream";
65
66 contentType = contentType + "; name=\"" + fileName0 + "\"";
67 }
68
69 /***
70 * Sets content type. Using of this metod should be avoided because Content-Type
71 * is set in the process of construction of StreamDataSource object, by using
72 * information got from "fileName0" parameter. This method will override
73 * value set after construction. This method will be used when construction is
74 * performed with null value of "fileName0" parameter, or in case when automatic
75 * obtaining of value for content-type did not satisfy what is expected.
76 * @param contType0 Content-Type
77 */
78 public void setContentType(String contType0) {
79 contentType = contType0;
80 }
81
82 /***
83 * Implements getContentType method from DataSource interface
84 * @return Content-Type for MIME message header field
85 */
86 public String getContentType() {
87 return contentType;
88 }
89
90 /***
91 * Implements getInputStream method from DataSource interface
92 * @return CMS enveloped object
93 * @exception IOException
94 */
95 public InputStream getInputStream() throws IOException {
96 return new ByteArrayInputStream(att);
97 }
98
99 /***
100 * Implements getName method from DataSource interface
101 * @return Name: EnvelopedDataContentInfo
102 */
103 public String getName() {
104 return fileName;
105 }
106
107 /***
108 * Implements getOutputStream method from DataSource interface. This method is
109 * not in use.
110 * @return nothing
111 * @exception IOException is always thrown when this method is used.
112 */
113 public OutputStream getOutputStream() throws IOException {
114 throw new IOException("StreamDataSource does not support getOutputStream()");
115 }
116
117 }
This page was automatically generated by Maven