author | twisti |
Wed, 02 Dec 2015 11:40:18 -1000 | |
changeset 34510 | f3f38d88223e |
parent 25871 | b80b84e87032 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.activation; |
|
27 |
||
28 |
import java.net.URL; |
|
29 |
import java.net.URLConnection; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.OutputStream; |
|
32 |
import java.io.IOException; |
|
33 |
||
34 |
/** |
|
35 |
* The URLDataSource class provides an object that wraps a <code>URL</code> |
|
36 |
* object in a DataSource interface. URLDataSource simplifies the handling |
|
37 |
* of data described by URLs within the JavaBeans Activation Framework |
|
38 |
* because this class can be used to create new DataHandlers. <i>NOTE: The |
|
39 |
* DataHandler object creates a URLDataSource internally, |
|
40 |
* when it is constructed with a URL.</i> |
|
41 |
* |
|
42 |
* @see javax.activation.DataSource |
|
43 |
* @see javax.activation.DataHandler |
|
44 |
* |
|
45 |
* @since 1.6 |
|
46 |
*/ |
|
47 |
public class URLDataSource implements DataSource { |
|
48 |
private URL url = null; |
|
49 |
private URLConnection 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 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 getContentType() { |
|
74 |
String type = null; |
|
75 |
||
76 |
try { |
|
77 |
if (url_conn == null) |
|
78 |
url_conn = url.openConnection(); |
|
79 |
} catch (IOException 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 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 getInputStream() throws IOException { |
|
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 getOutputStream() throws IOException { |
|
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 getURL() { |
|
135 |
return url; |
|
136 |
} |
|
137 |
} |