KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > URLDataSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)URLDataSource.java 1.10 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * The URLDataSource class provides an object that wraps a <code>URL</code>
38  * object in a DataSource interface. URLDataSource simplifies the handling
39  * of data described by URLs within the JavaBeans Activation Framework
40  * because this class can be used to create new DataHandlers. <i>NOTE: The
41  * DataHandler object creates a URLDataSource internally,
42  * when it is constructed with a URL.</i>
43  *
44  * @see javax.activation.DataSource
45  * @see javax.activation.DataHandler
46  */

47 public class URLDataSource implements DataSource JavaDoc {
48     private URL JavaDoc url = null;
49     private URLConnection JavaDoc url_conn = null;
50
51     /**
52      * URLDataSource constructor. The URLDataSource class will
53      * not open a connection to the URL until a method requiring it
54      * to do so is called.
55      *
56      * @param url The URL to be encapsulated in this object.
57      */

58     public URLDataSource(URL JavaDoc url) {
59     this.url = url;
60     }
61
62     /**
63      * Returns the value of the URL content-type header field.
64      * It calls the URL's <code>URLConnection.getContentType</code> method
65      * after retrieving a URLConnection object.
66      * <i>Note: this method attempts to call the <code>openConnection</code>
67      * method on the URL. If this method fails, or if a content type is not
68      * returned from the URLConnection, getContentType returns
69      * "application/octet-stream" as the content type.</i>
70      *
71      * @return the content type.
72      */

73     public String JavaDoc getContentType() {
74     String JavaDoc type = null;
75
76     try {
77         if (url_conn == null)
78         url_conn = url.openConnection();
79     } catch (IOException JavaDoc e) { }
80     
81     if (url_conn != null)
82         type = url_conn.getContentType();
83
84     if (type == null)
85         type = "application/octet-stream";
86     
87     return type;
88     }
89
90     /**
91      * Calls the <code>getFile</code> method on the URL used to
92      * instantiate the object.
93      *
94      * @return the result of calling the URL's getFile method.
95      */

96     public String JavaDoc getName() {
97     return url.getFile();
98     }
99
100     /**
101      * The getInputStream method from the URL. Calls the
102      * <code>openStream</code> method on the URL.
103      *
104      * @return the InputStream.
105      */

106     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
107     return url.openStream();
108     }
109
110     /**
111      * The getOutputStream method from the URL. First an attempt is
112      * made to get the URLConnection object for the URL. If that
113      * succeeds, the getOutputStream method on the URLConnection
114      * is returned.
115      *
116      * @return the OutputStream.
117      */

118     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
119     // get the url connection if it is available
120
url_conn = url.openConnection();
121     
122     if (url_conn != null) {
123         url_conn.setDoOutput(true);
124         return url_conn.getOutputStream();
125     } else
126         return null;
127     }
128
129     /**
130      * Return the URL used to create this DataSource.
131      *
132      * @return The URL.
133      */

134     public URL JavaDoc getURL() {
135     return url;
136     }
137 }
138
Popular Tags