jtulach@1396
|
1 |
/*
|
jtulach@1396
|
2 |
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1396
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1396
|
4 |
*
|
jtulach@1396
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1396
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1396
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1396
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1396
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1396
|
10 |
*
|
jtulach@1396
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1396
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1396
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1396
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1396
|
15 |
* accompanied this code).
|
jtulach@1396
|
16 |
*
|
jtulach@1396
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1396
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1396
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1396
|
20 |
*
|
jtulach@1396
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1396
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1396
|
23 |
* questions.
|
jtulach@1396
|
24 |
*/
|
jtulach@1396
|
25 |
|
jtulach@1396
|
26 |
package java.net;
|
jtulach@1396
|
27 |
|
jtulach@1396
|
28 |
import java.io.IOException;
|
jtulach@1396
|
29 |
import java.io.InputStream;
|
jtulach@1396
|
30 |
import java.io.OutputStream;
|
jaroslav@1398
|
31 |
import java.io.PrintStream;
|
jaroslav@1398
|
32 |
import java.util.ArrayList;
|
jtulach@1396
|
33 |
import java.util.Date;
|
jtulach@1396
|
34 |
import java.util.StringTokenizer;
|
jtulach@1396
|
35 |
import java.util.Collections;
|
jaroslav@1398
|
36 |
import java.util.HashMap;
|
jaroslav@1398
|
37 |
import java.util.Hashtable;
|
jaroslav@1398
|
38 |
import java.util.Iterator;
|
jtulach@1396
|
39 |
import java.util.Map;
|
jtulach@1396
|
40 |
import java.util.List;
|
jaroslav@1398
|
41 |
import java.util.NoSuchElementException;
|
jtulach@1396
|
42 |
|
jtulach@1396
|
43 |
/**
|
jtulach@1396
|
44 |
* The abstract class <code>URLConnection</code> is the superclass
|
jtulach@1396
|
45 |
* of all classes that represent a communications link between the
|
jtulach@1396
|
46 |
* application and a URL. Instances of this class can be used both to
|
jtulach@1396
|
47 |
* read from and to write to the resource referenced by the URL. In
|
jtulach@1396
|
48 |
* general, creating a connection to a URL is a multistep process:
|
jtulach@1396
|
49 |
* <p>
|
jtulach@1396
|
50 |
* <center><table border=2 summary="Describes the process of creating a connection to a URL: openConnection() and connect() over time.">
|
jtulach@1396
|
51 |
* <tr><th><code>openConnection()</code></th>
|
jtulach@1396
|
52 |
* <th><code>connect()</code></th></tr>
|
jtulach@1396
|
53 |
* <tr><td>Manipulate parameters that affect the connection to the remote
|
jtulach@1396
|
54 |
* resource.</td>
|
jtulach@1396
|
55 |
* <td>Interact with the resource; query header fields and
|
jtulach@1396
|
56 |
* contents.</td></tr>
|
jtulach@1396
|
57 |
* </table>
|
jtulach@1396
|
58 |
* ---------------------------->
|
jtulach@1396
|
59 |
* <br>time</center>
|
jtulach@1396
|
60 |
*
|
jtulach@1396
|
61 |
* <ol>
|
jtulach@1396
|
62 |
* <li>The connection object is created by invoking the
|
jtulach@1396
|
63 |
* <code>openConnection</code> method on a URL.
|
jtulach@1396
|
64 |
* <li>The setup parameters and general request properties are manipulated.
|
jtulach@1396
|
65 |
* <li>The actual connection to the remote object is made, using the
|
jtulach@1396
|
66 |
* <code>connect</code> method.
|
jtulach@1396
|
67 |
* <li>The remote object becomes available. The header fields and the contents
|
jtulach@1396
|
68 |
* of the remote object can be accessed.
|
jtulach@1396
|
69 |
* </ol>
|
jtulach@1396
|
70 |
* <p>
|
jtulach@1396
|
71 |
* The setup parameters are modified using the following methods:
|
jtulach@1396
|
72 |
* <ul>
|
jtulach@1396
|
73 |
* <li><code>setAllowUserInteraction</code>
|
jtulach@1396
|
74 |
* <li><code>setDoInput</code>
|
jtulach@1396
|
75 |
* <li><code>setDoOutput</code>
|
jtulach@1396
|
76 |
* <li><code>setIfModifiedSince</code>
|
jtulach@1396
|
77 |
* <li><code>setUseCaches</code>
|
jtulach@1396
|
78 |
* </ul>
|
jtulach@1396
|
79 |
* <p>
|
jtulach@1396
|
80 |
* and the general request properties are modified using the method:
|
jtulach@1396
|
81 |
* <ul>
|
jtulach@1396
|
82 |
* <li><code>setRequestProperty</code>
|
jtulach@1396
|
83 |
* </ul>
|
jtulach@1396
|
84 |
* <p>
|
jtulach@1396
|
85 |
* Default values for the <code>AllowUserInteraction</code> and
|
jtulach@1396
|
86 |
* <code>UseCaches</code> parameters can be set using the methods
|
jtulach@1396
|
87 |
* <code>setDefaultAllowUserInteraction</code> and
|
jtulach@1396
|
88 |
* <code>setDefaultUseCaches</code>.
|
jtulach@1396
|
89 |
* <p>
|
jtulach@1396
|
90 |
* Each of the above <code>set</code> methods has a corresponding
|
jtulach@1396
|
91 |
* <code>get</code> method to retrieve the value of the parameter or
|
jtulach@1396
|
92 |
* general request property. The specific parameters and general
|
jtulach@1396
|
93 |
* request properties that are applicable are protocol specific.
|
jtulach@1396
|
94 |
* <p>
|
jtulach@1396
|
95 |
* The following methods are used to access the header fields and
|
jtulach@1396
|
96 |
* the contents after the connection is made to the remote object:
|
jtulach@1396
|
97 |
* <ul>
|
jtulach@1396
|
98 |
* <li><code>getContent</code>
|
jtulach@1396
|
99 |
* <li><code>getHeaderField</code>
|
jtulach@1396
|
100 |
* <li><code>getInputStream</code>
|
jtulach@1396
|
101 |
* <li><code>getOutputStream</code>
|
jtulach@1396
|
102 |
* </ul>
|
jtulach@1396
|
103 |
* <p>
|
jtulach@1396
|
104 |
* Certain header fields are accessed frequently. The methods:
|
jtulach@1396
|
105 |
* <ul>
|
jtulach@1396
|
106 |
* <li><code>getContentEncoding</code>
|
jtulach@1396
|
107 |
* <li><code>getContentLength</code>
|
jtulach@1396
|
108 |
* <li><code>getContentType</code>
|
jtulach@1396
|
109 |
* <li><code>getDate</code>
|
jtulach@1396
|
110 |
* <li><code>getExpiration</code>
|
jtulach@1396
|
111 |
* <li><code>getLastModifed</code>
|
jtulach@1396
|
112 |
* </ul>
|
jtulach@1396
|
113 |
* <p>
|
jtulach@1396
|
114 |
* provide convenient access to these fields. The
|
jtulach@1396
|
115 |
* <code>getContentType</code> method is used by the
|
jtulach@1396
|
116 |
* <code>getContent</code> method to determine the type of the remote
|
jtulach@1396
|
117 |
* object; subclasses may find it convenient to override the
|
jtulach@1396
|
118 |
* <code>getContentType</code> method.
|
jtulach@1396
|
119 |
* <p>
|
jtulach@1396
|
120 |
* In the common case, all of the pre-connection parameters and
|
jtulach@1396
|
121 |
* general request properties can be ignored: the pre-connection
|
jtulach@1396
|
122 |
* parameters and request properties default to sensible values. For
|
jtulach@1396
|
123 |
* most clients of this interface, there are only two interesting
|
jtulach@1396
|
124 |
* methods: <code>getInputStream</code> and <code>getContent</code>,
|
jtulach@1396
|
125 |
* which are mirrored in the <code>URL</code> class by convenience methods.
|
jtulach@1396
|
126 |
* <p>
|
jtulach@1396
|
127 |
* More information on the request properties and header fields of
|
jtulach@1396
|
128 |
* an <code>http</code> connection can be found at:
|
jtulach@1396
|
129 |
* <blockquote><pre>
|
jtulach@1396
|
130 |
* <a href="http://www.ietf.org/rfc/rfc2616.txt">http://www.ietf.org/rfc/rfc2616.txt</a>
|
jtulach@1396
|
131 |
* </pre></blockquote>
|
jtulach@1396
|
132 |
*
|
jtulach@1396
|
133 |
* Note about <code>fileNameMap</code>: In versions prior to JDK 1.1.6,
|
jtulach@1396
|
134 |
* field <code>fileNameMap</code> of <code>URLConnection</code> was public.
|
jtulach@1396
|
135 |
* In JDK 1.1.6 and later, <code>fileNameMap</code> is private; accessor
|
jtulach@1396
|
136 |
* and mutator methods {@link #getFileNameMap() getFileNameMap} and
|
jtulach@1396
|
137 |
* {@link #setFileNameMap(java.net.FileNameMap) setFileNameMap} are added
|
jtulach@1396
|
138 |
* to access it. This change is also described on the <a href=
|
jtulach@1396
|
139 |
* "http://java.sun.com/products/jdk/1.2/compatibility.html">
|
jtulach@1396
|
140 |
* Compatibility</a> page.
|
jtulach@1396
|
141 |
*
|
jtulach@1396
|
142 |
* Invoking the <tt>close()</tt> methods on the <tt>InputStream</tt> or <tt>OutputStream</tt> of an
|
jtulach@1396
|
143 |
* <tt>URLConnection</tt> after a request may free network resources associated with this
|
jtulach@1396
|
144 |
* instance, unless particular protocol specifications specify different behaviours
|
jtulach@1396
|
145 |
* for it.
|
jtulach@1396
|
146 |
*
|
jtulach@1396
|
147 |
* @author James Gosling
|
jtulach@1396
|
148 |
* @see java.net.URL#openConnection()
|
jtulach@1396
|
149 |
* @see java.net.URLConnection#connect()
|
jtulach@1396
|
150 |
* @see java.net.URLConnection#getContent()
|
jtulach@1396
|
151 |
* @see java.net.URLConnection#getContentEncoding()
|
jtulach@1396
|
152 |
* @see java.net.URLConnection#getContentLength()
|
jtulach@1396
|
153 |
* @see java.net.URLConnection#getContentType()
|
jtulach@1396
|
154 |
* @see java.net.URLConnection#getDate()
|
jtulach@1396
|
155 |
* @see java.net.URLConnection#getExpiration()
|
jtulach@1396
|
156 |
* @see java.net.URLConnection#getHeaderField(int)
|
jtulach@1396
|
157 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
158 |
* @see java.net.URLConnection#getInputStream()
|
jtulach@1396
|
159 |
* @see java.net.URLConnection#getLastModified()
|
jtulach@1396
|
160 |
* @see java.net.URLConnection#getOutputStream()
|
jtulach@1396
|
161 |
* @see java.net.URLConnection#setAllowUserInteraction(boolean)
|
jtulach@1396
|
162 |
* @see java.net.URLConnection#setDefaultUseCaches(boolean)
|
jtulach@1396
|
163 |
* @see java.net.URLConnection#setDoInput(boolean)
|
jtulach@1396
|
164 |
* @see java.net.URLConnection#setDoOutput(boolean)
|
jtulach@1396
|
165 |
* @see java.net.URLConnection#setIfModifiedSince(long)
|
jtulach@1396
|
166 |
* @see java.net.URLConnection#setRequestProperty(java.lang.String, java.lang.String)
|
jtulach@1396
|
167 |
* @see java.net.URLConnection#setUseCaches(boolean)
|
jtulach@1396
|
168 |
* @since JDK1.0
|
jtulach@1396
|
169 |
*/
|
jtulach@1396
|
170 |
public abstract class URLConnection {
|
jtulach@1396
|
171 |
|
jtulach@1396
|
172 |
/**
|
jtulach@1396
|
173 |
* The URL represents the remote object on the World Wide Web to
|
jtulach@1396
|
174 |
* which this connection is opened.
|
jtulach@1396
|
175 |
* <p>
|
jtulach@1396
|
176 |
* The value of this field can be accessed by the
|
jtulach@1396
|
177 |
* <code>getURL</code> method.
|
jtulach@1396
|
178 |
* <p>
|
jtulach@1396
|
179 |
* The default value of this variable is the value of the URL
|
jtulach@1396
|
180 |
* argument in the <code>URLConnection</code> constructor.
|
jtulach@1396
|
181 |
*
|
jtulach@1396
|
182 |
* @see java.net.URLConnection#getURL()
|
jtulach@1396
|
183 |
* @see java.net.URLConnection#url
|
jtulach@1396
|
184 |
*/
|
jtulach@1396
|
185 |
protected URL url;
|
jtulach@1396
|
186 |
|
jtulach@1396
|
187 |
/**
|
jtulach@1396
|
188 |
* This variable is set by the <code>setDoInput</code> method. Its
|
jtulach@1396
|
189 |
* value is returned by the <code>getDoInput</code> method.
|
jtulach@1396
|
190 |
* <p>
|
jtulach@1396
|
191 |
* A URL connection can be used for input and/or output. Setting the
|
jtulach@1396
|
192 |
* <code>doInput</code> flag to <code>true</code> indicates that
|
jtulach@1396
|
193 |
* the application intends to read data from the URL connection.
|
jtulach@1396
|
194 |
* <p>
|
jtulach@1396
|
195 |
* The default value of this field is <code>true</code>.
|
jtulach@1396
|
196 |
*
|
jtulach@1396
|
197 |
* @see java.net.URLConnection#getDoInput()
|
jtulach@1396
|
198 |
* @see java.net.URLConnection#setDoInput(boolean)
|
jtulach@1396
|
199 |
*/
|
jtulach@1396
|
200 |
protected boolean doInput = true;
|
jtulach@1396
|
201 |
|
jtulach@1396
|
202 |
/**
|
jtulach@1396
|
203 |
* This variable is set by the <code>setDoOutput</code> method. Its
|
jtulach@1396
|
204 |
* value is returned by the <code>getDoOutput</code> method.
|
jtulach@1396
|
205 |
* <p>
|
jtulach@1396
|
206 |
* A URL connection can be used for input and/or output. Setting the
|
jtulach@1396
|
207 |
* <code>doOutput</code> flag to <code>true</code> indicates
|
jtulach@1396
|
208 |
* that the application intends to write data to the URL connection.
|
jtulach@1396
|
209 |
* <p>
|
jtulach@1396
|
210 |
* The default value of this field is <code>false</code>.
|
jtulach@1396
|
211 |
*
|
jtulach@1396
|
212 |
* @see java.net.URLConnection#getDoOutput()
|
jtulach@1396
|
213 |
* @see java.net.URLConnection#setDoOutput(boolean)
|
jtulach@1396
|
214 |
*/
|
jtulach@1396
|
215 |
protected boolean doOutput = false;
|
jtulach@1396
|
216 |
|
jtulach@1396
|
217 |
private static boolean defaultAllowUserInteraction = false;
|
jtulach@1396
|
218 |
|
jtulach@1396
|
219 |
/**
|
jtulach@1396
|
220 |
* If <code>true</code>, this <code>URL</code> is being examined in
|
jtulach@1396
|
221 |
* a context in which it makes sense to allow user interactions such
|
jtulach@1396
|
222 |
* as popping up an authentication dialog. If <code>false</code>,
|
jtulach@1396
|
223 |
* then no user interaction is allowed.
|
jtulach@1396
|
224 |
* <p>
|
jtulach@1396
|
225 |
* The value of this field can be set by the
|
jtulach@1396
|
226 |
* <code>setAllowUserInteraction</code> method.
|
jtulach@1396
|
227 |
* Its value is returned by the
|
jtulach@1396
|
228 |
* <code>getAllowUserInteraction</code> method.
|
jtulach@1396
|
229 |
* Its default value is the value of the argument in the last invocation
|
jtulach@1396
|
230 |
* of the <code>setDefaultAllowUserInteraction</code> method.
|
jtulach@1396
|
231 |
*
|
jtulach@1396
|
232 |
* @see java.net.URLConnection#getAllowUserInteraction()
|
jtulach@1396
|
233 |
* @see java.net.URLConnection#setAllowUserInteraction(boolean)
|
jtulach@1396
|
234 |
* @see java.net.URLConnection#setDefaultAllowUserInteraction(boolean)
|
jtulach@1396
|
235 |
*/
|
jtulach@1396
|
236 |
protected boolean allowUserInteraction = defaultAllowUserInteraction;
|
jtulach@1396
|
237 |
|
jtulach@1396
|
238 |
private static boolean defaultUseCaches = true;
|
jtulach@1396
|
239 |
|
jtulach@1396
|
240 |
/**
|
jtulach@1396
|
241 |
* If <code>true</code>, the protocol is allowed to use caching
|
jtulach@1396
|
242 |
* whenever it can. If <code>false</code>, the protocol must always
|
jtulach@1396
|
243 |
* try to get a fresh copy of the object.
|
jtulach@1396
|
244 |
* <p>
|
jtulach@1396
|
245 |
* This field is set by the <code>setUseCaches</code> method. Its
|
jtulach@1396
|
246 |
* value is returned by the <code>getUseCaches</code> method.
|
jtulach@1396
|
247 |
* <p>
|
jtulach@1396
|
248 |
* Its default value is the value given in the last invocation of the
|
jtulach@1396
|
249 |
* <code>setDefaultUseCaches</code> method.
|
jtulach@1396
|
250 |
*
|
jtulach@1396
|
251 |
* @see java.net.URLConnection#setUseCaches(boolean)
|
jtulach@1396
|
252 |
* @see java.net.URLConnection#getUseCaches()
|
jtulach@1396
|
253 |
* @see java.net.URLConnection#setDefaultUseCaches(boolean)
|
jtulach@1396
|
254 |
*/
|
jtulach@1396
|
255 |
protected boolean useCaches = defaultUseCaches;
|
jtulach@1396
|
256 |
|
jtulach@1396
|
257 |
/**
|
jtulach@1396
|
258 |
* Some protocols support skipping the fetching of the object unless
|
jtulach@1396
|
259 |
* the object has been modified more recently than a certain time.
|
jtulach@1396
|
260 |
* <p>
|
jtulach@1396
|
261 |
* A nonzero value gives a time as the number of milliseconds since
|
jtulach@1396
|
262 |
* January 1, 1970, GMT. The object is fetched only if it has been
|
jtulach@1396
|
263 |
* modified more recently than that time.
|
jtulach@1396
|
264 |
* <p>
|
jtulach@1396
|
265 |
* This variable is set by the <code>setIfModifiedSince</code>
|
jtulach@1396
|
266 |
* method. Its value is returned by the
|
jtulach@1396
|
267 |
* <code>getIfModifiedSince</code> method.
|
jtulach@1396
|
268 |
* <p>
|
jtulach@1396
|
269 |
* The default value of this field is <code>0</code>, indicating
|
jtulach@1396
|
270 |
* that the fetching must always occur.
|
jtulach@1396
|
271 |
*
|
jtulach@1396
|
272 |
* @see java.net.URLConnection#getIfModifiedSince()
|
jtulach@1396
|
273 |
* @see java.net.URLConnection#setIfModifiedSince(long)
|
jtulach@1396
|
274 |
*/
|
jtulach@1396
|
275 |
protected long ifModifiedSince = 0;
|
jtulach@1396
|
276 |
|
jtulach@1396
|
277 |
/**
|
jtulach@1396
|
278 |
* If <code>false</code>, this connection object has not created a
|
jtulach@1396
|
279 |
* communications link to the specified URL. If <code>true</code>,
|
jtulach@1396
|
280 |
* the communications link has been established.
|
jtulach@1396
|
281 |
*/
|
jtulach@1396
|
282 |
protected boolean connected = false;
|
jtulach@1396
|
283 |
|
jtulach@1396
|
284 |
/**
|
jtulach@1396
|
285 |
* @since 1.5
|
jtulach@1396
|
286 |
*/
|
jtulach@1396
|
287 |
private int connectTimeout;
|
jtulach@1396
|
288 |
private int readTimeout;
|
jtulach@1396
|
289 |
|
jtulach@1396
|
290 |
/**
|
jtulach@1396
|
291 |
* @since 1.6
|
jtulach@1396
|
292 |
*/
|
jtulach@1396
|
293 |
private MessageHeader requests;
|
jtulach@1396
|
294 |
|
jtulach@1396
|
295 |
/**
|
jtulach@1396
|
296 |
* @since JDK1.1
|
jtulach@1396
|
297 |
*/
|
jtulach@1396
|
298 |
private static FileNameMap fileNameMap;
|
jtulach@1396
|
299 |
|
jtulach@1396
|
300 |
/**
|
jtulach@1396
|
301 |
* @since 1.2.2
|
jtulach@1396
|
302 |
*/
|
jtulach@1396
|
303 |
private static boolean fileNameMapLoaded = false;
|
jtulach@1396
|
304 |
|
jtulach@1396
|
305 |
/**
|
jtulach@1396
|
306 |
* Loads filename map (a mimetable) from a data file. It will
|
jtulach@1396
|
307 |
* first try to load the user-specific table, defined
|
jtulach@1396
|
308 |
* by "content.types.user.table" property. If that fails,
|
jtulach@1396
|
309 |
* it tries to load the default built-in table at
|
jtulach@1396
|
310 |
* lib/content-types.properties under java home.
|
jtulach@1396
|
311 |
*
|
jtulach@1396
|
312 |
* @return the FileNameMap
|
jtulach@1396
|
313 |
* @since 1.2
|
jtulach@1396
|
314 |
* @see #setFileNameMap(java.net.FileNameMap)
|
jtulach@1396
|
315 |
*/
|
jtulach@1396
|
316 |
public static synchronized FileNameMap getFileNameMap() {
|
jtulach@1396
|
317 |
if ((fileNameMap == null) && !fileNameMapLoaded) {
|
jaroslav@1398
|
318 |
fileNameMap = new FileNameMap() {
|
jaroslav@1398
|
319 |
@Override
|
jaroslav@1398
|
320 |
public String getContentTypeFor(String fileName) {
|
jaroslav@1398
|
321 |
return "text/plain";
|
jaroslav@1398
|
322 |
}
|
jaroslav@1398
|
323 |
};
|
jtulach@1396
|
324 |
fileNameMapLoaded = true;
|
jtulach@1396
|
325 |
}
|
jtulach@1396
|
326 |
|
jtulach@1396
|
327 |
return new FileNameMap() {
|
jtulach@1396
|
328 |
private FileNameMap map = fileNameMap;
|
jtulach@1396
|
329 |
public String getContentTypeFor(String fileName) {
|
jtulach@1396
|
330 |
return map.getContentTypeFor(fileName);
|
jtulach@1396
|
331 |
}
|
jtulach@1396
|
332 |
};
|
jtulach@1396
|
333 |
}
|
jtulach@1396
|
334 |
|
jtulach@1396
|
335 |
/**
|
jtulach@1396
|
336 |
* Sets the FileNameMap.
|
jtulach@1396
|
337 |
* <p>
|
jtulach@1396
|
338 |
* If there is a security manager, this method first calls
|
jtulach@1396
|
339 |
* the security manager's <code>checkSetFactory</code> method
|
jtulach@1396
|
340 |
* to ensure the operation is allowed.
|
jtulach@1396
|
341 |
* This could result in a SecurityException.
|
jtulach@1396
|
342 |
*
|
jtulach@1396
|
343 |
* @param map the FileNameMap to be set
|
jtulach@1396
|
344 |
* @exception SecurityException if a security manager exists and its
|
jtulach@1396
|
345 |
* <code>checkSetFactory</code> method doesn't allow the operation.
|
jtulach@1396
|
346 |
* @see SecurityManager#checkSetFactory
|
jtulach@1396
|
347 |
* @see #getFileNameMap()
|
jtulach@1396
|
348 |
* @since 1.2
|
jtulach@1396
|
349 |
*/
|
jtulach@1396
|
350 |
public static void setFileNameMap(FileNameMap map) {
|
jaroslav@1398
|
351 |
throw new SecurityException();
|
jtulach@1396
|
352 |
}
|
jtulach@1396
|
353 |
|
jtulach@1396
|
354 |
/**
|
jtulach@1396
|
355 |
* Opens a communications link to the resource referenced by this
|
jtulach@1396
|
356 |
* URL, if such a connection has not already been established.
|
jtulach@1396
|
357 |
* <p>
|
jtulach@1396
|
358 |
* If the <code>connect</code> method is called when the connection
|
jtulach@1396
|
359 |
* has already been opened (indicated by the <code>connected</code>
|
jtulach@1396
|
360 |
* field having the value <code>true</code>), the call is ignored.
|
jtulach@1396
|
361 |
* <p>
|
jtulach@1396
|
362 |
* URLConnection objects go through two phases: first they are
|
jtulach@1396
|
363 |
* created, then they are connected. After being created, and
|
jtulach@1396
|
364 |
* before being connected, various options can be specified
|
jtulach@1396
|
365 |
* (e.g., doInput and UseCaches). After connecting, it is an
|
jtulach@1396
|
366 |
* error to try to set them. Operations that depend on being
|
jtulach@1396
|
367 |
* connected, like getContentLength, will implicitly perform the
|
jtulach@1396
|
368 |
* connection, if necessary.
|
jtulach@1396
|
369 |
*
|
jtulach@1396
|
370 |
* @throws SocketTimeoutException if the timeout expires before
|
jtulach@1396
|
371 |
* the connection can be established
|
jtulach@1396
|
372 |
* @exception IOException if an I/O error occurs while opening the
|
jtulach@1396
|
373 |
* connection.
|
jtulach@1396
|
374 |
* @see java.net.URLConnection#connected
|
jtulach@1396
|
375 |
* @see #getConnectTimeout()
|
jtulach@1396
|
376 |
* @see #setConnectTimeout(int)
|
jtulach@1396
|
377 |
*/
|
jtulach@1396
|
378 |
abstract public void connect() throws IOException;
|
jtulach@1396
|
379 |
|
jtulach@1396
|
380 |
/**
|
jtulach@1396
|
381 |
* Sets a specified timeout value, in milliseconds, to be used
|
jtulach@1396
|
382 |
* when opening a communications link to the resource referenced
|
jtulach@1396
|
383 |
* by this URLConnection. If the timeout expires before the
|
jtulach@1396
|
384 |
* connection can be established, a
|
jtulach@1396
|
385 |
* java.net.SocketTimeoutException is raised. A timeout of zero is
|
jtulach@1396
|
386 |
* interpreted as an infinite timeout.
|
jtulach@1396
|
387 |
|
jtulach@1396
|
388 |
* <p> Some non-standard implmentation of this method may ignore
|
jtulach@1396
|
389 |
* the specified timeout. To see the connect timeout set, please
|
jtulach@1396
|
390 |
* call getConnectTimeout().
|
jtulach@1396
|
391 |
*
|
jtulach@1396
|
392 |
* @param timeout an <code>int</code> that specifies the connect
|
jtulach@1396
|
393 |
* timeout value in milliseconds
|
jtulach@1396
|
394 |
* @throws IllegalArgumentException if the timeout parameter is negative
|
jtulach@1396
|
395 |
*
|
jtulach@1396
|
396 |
* @see #getConnectTimeout()
|
jtulach@1396
|
397 |
* @see #connect()
|
jtulach@1396
|
398 |
* @since 1.5
|
jtulach@1396
|
399 |
*/
|
jtulach@1396
|
400 |
public void setConnectTimeout(int timeout) {
|
jtulach@1396
|
401 |
if (timeout < 0) {
|
jtulach@1396
|
402 |
throw new IllegalArgumentException("timeout can not be negative");
|
jtulach@1396
|
403 |
}
|
jtulach@1396
|
404 |
connectTimeout = timeout;
|
jtulach@1396
|
405 |
}
|
jtulach@1396
|
406 |
|
jtulach@1396
|
407 |
/**
|
jtulach@1396
|
408 |
* Returns setting for connect timeout.
|
jtulach@1396
|
409 |
* <p>
|
jtulach@1396
|
410 |
* 0 return implies that the option is disabled
|
jtulach@1396
|
411 |
* (i.e., timeout of infinity).
|
jtulach@1396
|
412 |
*
|
jtulach@1396
|
413 |
* @return an <code>int</code> that indicates the connect timeout
|
jtulach@1396
|
414 |
* value in milliseconds
|
jtulach@1396
|
415 |
* @see #setConnectTimeout(int)
|
jtulach@1396
|
416 |
* @see #connect()
|
jtulach@1396
|
417 |
* @since 1.5
|
jtulach@1396
|
418 |
*/
|
jtulach@1396
|
419 |
public int getConnectTimeout() {
|
jtulach@1396
|
420 |
return connectTimeout;
|
jtulach@1396
|
421 |
}
|
jtulach@1396
|
422 |
|
jtulach@1396
|
423 |
/**
|
jtulach@1396
|
424 |
* Sets the read timeout to a specified timeout, in
|
jtulach@1396
|
425 |
* milliseconds. A non-zero value specifies the timeout when
|
jtulach@1396
|
426 |
* reading from Input stream when a connection is established to a
|
jtulach@1396
|
427 |
* resource. If the timeout expires before there is data available
|
jtulach@1396
|
428 |
* for read, a java.net.SocketTimeoutException is raised. A
|
jtulach@1396
|
429 |
* timeout of zero is interpreted as an infinite timeout.
|
jtulach@1396
|
430 |
*
|
jtulach@1396
|
431 |
*<p> Some non-standard implementation of this method ignores the
|
jtulach@1396
|
432 |
* specified timeout. To see the read timeout set, please call
|
jtulach@1396
|
433 |
* getReadTimeout().
|
jtulach@1396
|
434 |
*
|
jtulach@1396
|
435 |
* @param timeout an <code>int</code> that specifies the timeout
|
jtulach@1396
|
436 |
* value to be used in milliseconds
|
jtulach@1396
|
437 |
* @throws IllegalArgumentException if the timeout parameter is negative
|
jtulach@1396
|
438 |
*
|
jtulach@1396
|
439 |
* @see #getReadTimeout()
|
jtulach@1396
|
440 |
* @see InputStream#read()
|
jtulach@1396
|
441 |
* @since 1.5
|
jtulach@1396
|
442 |
*/
|
jtulach@1396
|
443 |
public void setReadTimeout(int timeout) {
|
jtulach@1396
|
444 |
if (timeout < 0) {
|
jtulach@1396
|
445 |
throw new IllegalArgumentException("timeout can not be negative");
|
jtulach@1396
|
446 |
}
|
jtulach@1396
|
447 |
readTimeout = timeout;
|
jtulach@1396
|
448 |
}
|
jtulach@1396
|
449 |
|
jtulach@1396
|
450 |
/**
|
jtulach@1396
|
451 |
* Returns setting for read timeout. 0 return implies that the
|
jtulach@1396
|
452 |
* option is disabled (i.e., timeout of infinity).
|
jtulach@1396
|
453 |
*
|
jtulach@1396
|
454 |
* @return an <code>int</code> that indicates the read timeout
|
jtulach@1396
|
455 |
* value in milliseconds
|
jtulach@1396
|
456 |
*
|
jtulach@1396
|
457 |
* @see #setReadTimeout(int)
|
jtulach@1396
|
458 |
* @see InputStream#read()
|
jtulach@1396
|
459 |
* @since 1.5
|
jtulach@1396
|
460 |
*/
|
jtulach@1396
|
461 |
public int getReadTimeout() {
|
jtulach@1396
|
462 |
return readTimeout;
|
jtulach@1396
|
463 |
}
|
jtulach@1396
|
464 |
|
jtulach@1396
|
465 |
/**
|
jtulach@1396
|
466 |
* Constructs a URL connection to the specified URL. A connection to
|
jtulach@1396
|
467 |
* the object referenced by the URL is not created.
|
jtulach@1396
|
468 |
*
|
jtulach@1396
|
469 |
* @param url the specified URL.
|
jtulach@1396
|
470 |
*/
|
jtulach@1396
|
471 |
protected URLConnection(URL url) {
|
jtulach@1396
|
472 |
this.url = url;
|
jtulach@1396
|
473 |
}
|
jtulach@1396
|
474 |
|
jtulach@1396
|
475 |
/**
|
jtulach@1396
|
476 |
* Returns the value of this <code>URLConnection</code>'s <code>URL</code>
|
jtulach@1396
|
477 |
* field.
|
jtulach@1396
|
478 |
*
|
jtulach@1396
|
479 |
* @return the value of this <code>URLConnection</code>'s <code>URL</code>
|
jtulach@1396
|
480 |
* field.
|
jtulach@1396
|
481 |
* @see java.net.URLConnection#url
|
jtulach@1396
|
482 |
*/
|
jtulach@1396
|
483 |
public URL getURL() {
|
jtulach@1396
|
484 |
return url;
|
jtulach@1396
|
485 |
}
|
jtulach@1396
|
486 |
|
jtulach@1396
|
487 |
/**
|
jtulach@1396
|
488 |
* Returns the value of the <code>content-length</code> header field.
|
jtulach@1396
|
489 |
* <P>
|
jtulach@1396
|
490 |
* <B>Note</B>: {@link #getContentLengthLong() getContentLengthLong()}
|
jtulach@1396
|
491 |
* should be preferred over this method, since it returns a {@code long}
|
jtulach@1396
|
492 |
* instead and is therefore more portable.</P>
|
jtulach@1396
|
493 |
*
|
jtulach@1396
|
494 |
* @return the content length of the resource that this connection's URL
|
jtulach@1396
|
495 |
* references, {@code -1} if the content length is not known,
|
jtulach@1396
|
496 |
* or if the content length is greater than Integer.MAX_VALUE.
|
jtulach@1396
|
497 |
*/
|
jtulach@1396
|
498 |
public int getContentLength() {
|
jtulach@1396
|
499 |
long l = getContentLengthLong();
|
jtulach@1396
|
500 |
if (l > Integer.MAX_VALUE)
|
jtulach@1396
|
501 |
return -1;
|
jtulach@1396
|
502 |
return (int) l;
|
jtulach@1396
|
503 |
}
|
jtulach@1396
|
504 |
|
jtulach@1396
|
505 |
/**
|
jtulach@1396
|
506 |
* Returns the value of the <code>content-length</code> header field as a
|
jtulach@1396
|
507 |
* long.
|
jtulach@1396
|
508 |
*
|
jtulach@1396
|
509 |
* @return the content length of the resource that this connection's URL
|
jtulach@1396
|
510 |
* references, or <code>-1</code> if the content length is
|
jtulach@1396
|
511 |
* not known.
|
jtulach@1396
|
512 |
* @since 7.0
|
jtulach@1396
|
513 |
*/
|
jtulach@1396
|
514 |
public long getContentLengthLong() {
|
jtulach@1396
|
515 |
return getHeaderFieldLong("content-length", -1);
|
jtulach@1396
|
516 |
}
|
jtulach@1396
|
517 |
|
jtulach@1396
|
518 |
/**
|
jtulach@1396
|
519 |
* Returns the value of the <code>content-type</code> header field.
|
jtulach@1396
|
520 |
*
|
jtulach@1396
|
521 |
* @return the content type of the resource that the URL references,
|
jtulach@1396
|
522 |
* or <code>null</code> if not known.
|
jtulach@1396
|
523 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
524 |
*/
|
jtulach@1396
|
525 |
public String getContentType() {
|
jtulach@1396
|
526 |
return getHeaderField("content-type");
|
jtulach@1396
|
527 |
}
|
jtulach@1396
|
528 |
|
jtulach@1396
|
529 |
/**
|
jtulach@1396
|
530 |
* Returns the value of the <code>content-encoding</code> header field.
|
jtulach@1396
|
531 |
*
|
jtulach@1396
|
532 |
* @return the content encoding of the resource that the URL references,
|
jtulach@1396
|
533 |
* or <code>null</code> if not known.
|
jtulach@1396
|
534 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
535 |
*/
|
jtulach@1396
|
536 |
public String getContentEncoding() {
|
jtulach@1396
|
537 |
return getHeaderField("content-encoding");
|
jtulach@1396
|
538 |
}
|
jtulach@1396
|
539 |
|
jtulach@1396
|
540 |
/**
|
jtulach@1396
|
541 |
* Returns the value of the <code>expires</code> header field.
|
jtulach@1396
|
542 |
*
|
jtulach@1396
|
543 |
* @return the expiration date of the resource that this URL references,
|
jtulach@1396
|
544 |
* or 0 if not known. The value is the number of milliseconds since
|
jtulach@1396
|
545 |
* January 1, 1970 GMT.
|
jtulach@1396
|
546 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
547 |
*/
|
jtulach@1396
|
548 |
public long getExpiration() {
|
jtulach@1396
|
549 |
return getHeaderFieldDate("expires", 0);
|
jtulach@1396
|
550 |
}
|
jtulach@1396
|
551 |
|
jtulach@1396
|
552 |
/**
|
jtulach@1396
|
553 |
* Returns the value of the <code>date</code> header field.
|
jtulach@1396
|
554 |
*
|
jtulach@1396
|
555 |
* @return the sending date of the resource that the URL references,
|
jtulach@1396
|
556 |
* or <code>0</code> if not known. The value returned is the
|
jtulach@1396
|
557 |
* number of milliseconds since January 1, 1970 GMT.
|
jtulach@1396
|
558 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
559 |
*/
|
jtulach@1396
|
560 |
public long getDate() {
|
jtulach@1396
|
561 |
return getHeaderFieldDate("date", 0);
|
jtulach@1396
|
562 |
}
|
jtulach@1396
|
563 |
|
jtulach@1396
|
564 |
/**
|
jtulach@1396
|
565 |
* Returns the value of the <code>last-modified</code> header field.
|
jtulach@1396
|
566 |
* The result is the number of milliseconds since January 1, 1970 GMT.
|
jtulach@1396
|
567 |
*
|
jtulach@1396
|
568 |
* @return the date the resource referenced by this
|
jtulach@1396
|
569 |
* <code>URLConnection</code> was last modified, or 0 if not known.
|
jtulach@1396
|
570 |
* @see java.net.URLConnection#getHeaderField(java.lang.String)
|
jtulach@1396
|
571 |
*/
|
jtulach@1396
|
572 |
public long getLastModified() {
|
jtulach@1396
|
573 |
return getHeaderFieldDate("last-modified", 0);
|
jtulach@1396
|
574 |
}
|
jtulach@1396
|
575 |
|
jtulach@1396
|
576 |
/**
|
jtulach@1396
|
577 |
* Returns the value of the named header field.
|
jtulach@1396
|
578 |
* <p>
|
jtulach@1396
|
579 |
* If called on a connection that sets the same header multiple times
|
jtulach@1396
|
580 |
* with possibly different values, only the last value is returned.
|
jtulach@1396
|
581 |
*
|
jtulach@1396
|
582 |
*
|
jtulach@1396
|
583 |
* @param name the name of a header field.
|
jtulach@1396
|
584 |
* @return the value of the named header field, or <code>null</code>
|
jtulach@1396
|
585 |
* if there is no such field in the header.
|
jtulach@1396
|
586 |
*/
|
jtulach@1396
|
587 |
public String getHeaderField(String name) {
|
jtulach@1396
|
588 |
return null;
|
jtulach@1396
|
589 |
}
|
jtulach@1396
|
590 |
|
jtulach@1396
|
591 |
/**
|
jtulach@1396
|
592 |
* Returns an unmodifiable Map of the header fields.
|
jtulach@1396
|
593 |
* The Map keys are Strings that represent the
|
jtulach@1396
|
594 |
* response-header field names. Each Map value is an
|
jtulach@1396
|
595 |
* unmodifiable List of Strings that represents
|
jtulach@1396
|
596 |
* the corresponding field values.
|
jtulach@1396
|
597 |
*
|
jtulach@1396
|
598 |
* @return a Map of header fields
|
jtulach@1396
|
599 |
* @since 1.4
|
jtulach@1396
|
600 |
*/
|
jtulach@1396
|
601 |
public Map<String,List<String>> getHeaderFields() {
|
jtulach@1396
|
602 |
return Collections.EMPTY_MAP;
|
jtulach@1396
|
603 |
}
|
jtulach@1396
|
604 |
|
jtulach@1396
|
605 |
/**
|
jtulach@1396
|
606 |
* Returns the value of the named field parsed as a number.
|
jtulach@1396
|
607 |
* <p>
|
jtulach@1396
|
608 |
* This form of <code>getHeaderField</code> exists because some
|
jtulach@1396
|
609 |
* connection types (e.g., <code>http-ng</code>) have pre-parsed
|
jtulach@1396
|
610 |
* headers. Classes for that connection type can override this method
|
jtulach@1396
|
611 |
* and short-circuit the parsing.
|
jtulach@1396
|
612 |
*
|
jtulach@1396
|
613 |
* @param name the name of the header field.
|
jtulach@1396
|
614 |
* @param Default the default value.
|
jtulach@1396
|
615 |
* @return the value of the named field, parsed as an integer. The
|
jtulach@1396
|
616 |
* <code>Default</code> value is returned if the field is
|
jtulach@1396
|
617 |
* missing or malformed.
|
jtulach@1396
|
618 |
*/
|
jtulach@1396
|
619 |
public int getHeaderFieldInt(String name, int Default) {
|
jtulach@1396
|
620 |
String value = getHeaderField(name);
|
jtulach@1396
|
621 |
try {
|
jtulach@1396
|
622 |
return Integer.parseInt(value);
|
jtulach@1396
|
623 |
} catch (Exception e) { }
|
jtulach@1396
|
624 |
return Default;
|
jtulach@1396
|
625 |
}
|
jtulach@1396
|
626 |
|
jtulach@1396
|
627 |
/**
|
jtulach@1396
|
628 |
* Returns the value of the named field parsed as a number.
|
jtulach@1396
|
629 |
* <p>
|
jtulach@1396
|
630 |
* This form of <code>getHeaderField</code> exists because some
|
jtulach@1396
|
631 |
* connection types (e.g., <code>http-ng</code>) have pre-parsed
|
jtulach@1396
|
632 |
* headers. Classes for that connection type can override this method
|
jtulach@1396
|
633 |
* and short-circuit the parsing.
|
jtulach@1396
|
634 |
*
|
jtulach@1396
|
635 |
* @param name the name of the header field.
|
jtulach@1396
|
636 |
* @param Default the default value.
|
jtulach@1396
|
637 |
* @return the value of the named field, parsed as a long. The
|
jtulach@1396
|
638 |
* <code>Default</code> value is returned if the field is
|
jtulach@1396
|
639 |
* missing or malformed.
|
jtulach@1396
|
640 |
* @since 7.0
|
jtulach@1396
|
641 |
*/
|
jtulach@1396
|
642 |
public long getHeaderFieldLong(String name, long Default) {
|
jtulach@1396
|
643 |
String value = getHeaderField(name);
|
jtulach@1396
|
644 |
try {
|
jtulach@1396
|
645 |
return Long.parseLong(value);
|
jtulach@1396
|
646 |
} catch (Exception e) { }
|
jtulach@1396
|
647 |
return Default;
|
jtulach@1396
|
648 |
}
|
jtulach@1396
|
649 |
|
jtulach@1396
|
650 |
/**
|
jtulach@1396
|
651 |
* Returns the value of the named field parsed as date.
|
jtulach@1396
|
652 |
* The result is the number of milliseconds since January 1, 1970 GMT
|
jtulach@1396
|
653 |
* represented by the named field.
|
jtulach@1396
|
654 |
* <p>
|
jtulach@1396
|
655 |
* This form of <code>getHeaderField</code> exists because some
|
jtulach@1396
|
656 |
* connection types (e.g., <code>http-ng</code>) have pre-parsed
|
jtulach@1396
|
657 |
* headers. Classes for that connection type can override this method
|
jtulach@1396
|
658 |
* and short-circuit the parsing.
|
jtulach@1396
|
659 |
*
|
jtulach@1396
|
660 |
* @param name the name of the header field.
|
jtulach@1396
|
661 |
* @param Default a default value.
|
jtulach@1396
|
662 |
* @return the value of the field, parsed as a date. The value of the
|
jtulach@1396
|
663 |
* <code>Default</code> argument is returned if the field is
|
jtulach@1396
|
664 |
* missing or malformed.
|
jtulach@1396
|
665 |
*/
|
jtulach@1396
|
666 |
public long getHeaderFieldDate(String name, long Default) {
|
jtulach@1396
|
667 |
String value = getHeaderField(name);
|
jtulach@1396
|
668 |
try {
|
jtulach@1396
|
669 |
return Date.parse(value);
|
jtulach@1396
|
670 |
} catch (Exception e) { }
|
jtulach@1396
|
671 |
return Default;
|
jtulach@1396
|
672 |
}
|
jtulach@1396
|
673 |
|
jtulach@1396
|
674 |
/**
|
jtulach@1396
|
675 |
* Returns the key for the <code>n</code><sup>th</sup> header field.
|
jtulach@1396
|
676 |
* It returns <code>null</code> if there are fewer than <code>n+1</code> fields.
|
jtulach@1396
|
677 |
*
|
jtulach@1396
|
678 |
* @param n an index, where n>=0
|
jtulach@1396
|
679 |
* @return the key for the <code>n</code><sup>th</sup> header field,
|
jtulach@1396
|
680 |
* or <code>null</code> if there are fewer than <code>n+1</code>
|
jtulach@1396
|
681 |
* fields.
|
jtulach@1396
|
682 |
*/
|
jtulach@1396
|
683 |
public String getHeaderFieldKey(int n) {
|
jtulach@1396
|
684 |
return null;
|
jtulach@1396
|
685 |
}
|
jtulach@1396
|
686 |
|
jtulach@1396
|
687 |
/**
|
jtulach@1396
|
688 |
* Returns the value for the <code>n</code><sup>th</sup> header field.
|
jtulach@1396
|
689 |
* It returns <code>null</code> if there are fewer than
|
jtulach@1396
|
690 |
* <code>n+1</code>fields.
|
jtulach@1396
|
691 |
* <p>
|
jtulach@1396
|
692 |
* This method can be used in conjunction with the
|
jtulach@1396
|
693 |
* {@link #getHeaderFieldKey(int) getHeaderFieldKey} method to iterate through all
|
jtulach@1396
|
694 |
* the headers in the message.
|
jtulach@1396
|
695 |
*
|
jtulach@1396
|
696 |
* @param n an index, where n>=0
|
jtulach@1396
|
697 |
* @return the value of the <code>n</code><sup>th</sup> header field
|
jtulach@1396
|
698 |
* or <code>null</code> if there are fewer than <code>n+1</code> fields
|
jtulach@1396
|
699 |
* @see java.net.URLConnection#getHeaderFieldKey(int)
|
jtulach@1396
|
700 |
*/
|
jtulach@1396
|
701 |
public String getHeaderField(int n) {
|
jtulach@1396
|
702 |
return null;
|
jtulach@1396
|
703 |
}
|
jtulach@1396
|
704 |
|
jtulach@1396
|
705 |
/**
|
jtulach@1396
|
706 |
* Retrieves the contents of this URL connection.
|
jtulach@1396
|
707 |
* <p>
|
jtulach@1396
|
708 |
* This method first determines the content type of the object by
|
jtulach@1396
|
709 |
* calling the <code>getContentType</code> method. If this is
|
jtulach@1396
|
710 |
* the first time that the application has seen that specific content
|
jtulach@1396
|
711 |
* type, a content handler for that content type is created:
|
jtulach@1396
|
712 |
* <ol>
|
jtulach@1396
|
713 |
* <li>If the application has set up a content handler factory instance
|
jtulach@1396
|
714 |
* using the <code>setContentHandlerFactory</code> method, the
|
jtulach@1396
|
715 |
* <code>createContentHandler</code> method of that instance is called
|
jtulach@1396
|
716 |
* with the content type as an argument; the result is a content
|
jtulach@1396
|
717 |
* handler for that content type.
|
jtulach@1396
|
718 |
* <li>If no content handler factory has yet been set up, or if the
|
jtulach@1396
|
719 |
* factory's <code>createContentHandler</code> method returns
|
jtulach@1396
|
720 |
* <code>null</code>, then the application loads the class named:
|
jtulach@1396
|
721 |
* <blockquote><pre>
|
jtulach@1396
|
722 |
* sun.net.www.content.<<i>contentType</i>>
|
jtulach@1396
|
723 |
* </pre></blockquote>
|
jtulach@1396
|
724 |
* where <<i>contentType</i>> is formed by taking the
|
jtulach@1396
|
725 |
* content-type string, replacing all slash characters with a
|
jtulach@1396
|
726 |
* <code>period</code> ('.'), and all other non-alphanumeric characters
|
jtulach@1396
|
727 |
* with the underscore character '<code>_</code>'. The alphanumeric
|
jtulach@1396
|
728 |
* characters are specifically the 26 uppercase ASCII letters
|
jtulach@1396
|
729 |
* '<code>A</code>' through '<code>Z</code>', the 26 lowercase ASCII
|
jtulach@1396
|
730 |
* letters '<code>a</code>' through '<code>z</code>', and the 10 ASCII
|
jtulach@1396
|
731 |
* digits '<code>0</code>' through '<code>9</code>'. If the specified
|
jtulach@1396
|
732 |
* class does not exist, or is not a subclass of
|
jtulach@1396
|
733 |
* <code>ContentHandler</code>, then an
|
jtulach@1396
|
734 |
* <code>UnknownServiceException</code> is thrown.
|
jtulach@1396
|
735 |
* </ol>
|
jtulach@1396
|
736 |
*
|
jtulach@1396
|
737 |
* @return the object fetched. The <code>instanceof</code> operator
|
jtulach@1396
|
738 |
* should be used to determine the specific kind of object
|
jtulach@1396
|
739 |
* returned.
|
jtulach@1396
|
740 |
* @exception IOException if an I/O error occurs while
|
jtulach@1396
|
741 |
* getting the content.
|
jtulach@1396
|
742 |
* @exception UnknownServiceException if the protocol does not support
|
jtulach@1396
|
743 |
* the content type.
|
jtulach@1396
|
744 |
* @see java.net.ContentHandlerFactory#createContentHandler(java.lang.String)
|
jtulach@1396
|
745 |
* @see java.net.URLConnection#getContentType()
|
jtulach@1396
|
746 |
* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
|
jtulach@1396
|
747 |
*/
|
jtulach@1396
|
748 |
public Object getContent() throws IOException {
|
jtulach@1396
|
749 |
// Must call getInputStream before GetHeaderField gets called
|
jtulach@1396
|
750 |
// so that FileNotFoundException has a chance to be thrown up
|
jtulach@1396
|
751 |
// from here without being caught.
|
jtulach@1396
|
752 |
getInputStream();
|
jtulach@1396
|
753 |
return getContentHandler().getContent(this);
|
jtulach@1396
|
754 |
}
|
jtulach@1396
|
755 |
|
jtulach@1396
|
756 |
/**
|
jtulach@1396
|
757 |
* Retrieves the contents of this URL connection.
|
jtulach@1396
|
758 |
*
|
jtulach@1396
|
759 |
* @param classes the <code>Class</code> array
|
jtulach@1396
|
760 |
* indicating the requested types
|
jtulach@1396
|
761 |
* @return the object fetched that is the first match of the type
|
jtulach@1396
|
762 |
* specified in the classes array. null if none of
|
jtulach@1396
|
763 |
* the requested types are supported.
|
jtulach@1396
|
764 |
* The <code>instanceof</code> operator should be used to
|
jtulach@1396
|
765 |
* determine the specific kind of object returned.
|
jtulach@1396
|
766 |
* @exception IOException if an I/O error occurs while
|
jtulach@1396
|
767 |
* getting the content.
|
jtulach@1396
|
768 |
* @exception UnknownServiceException if the protocol does not support
|
jtulach@1396
|
769 |
* the content type.
|
jtulach@1396
|
770 |
* @see java.net.URLConnection#getContent()
|
jtulach@1396
|
771 |
* @see java.net.ContentHandlerFactory#createContentHandler(java.lang.String)
|
jtulach@1396
|
772 |
* @see java.net.URLConnection#getContent(java.lang.Class[])
|
jtulach@1396
|
773 |
* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
|
jtulach@1396
|
774 |
* @since 1.3
|
jtulach@1396
|
775 |
*/
|
jtulach@1396
|
776 |
public Object getContent(Class[] classes) throws IOException {
|
jtulach@1396
|
777 |
// Must call getInputStream before GetHeaderField gets called
|
jtulach@1396
|
778 |
// so that FileNotFoundException has a chance to be thrown up
|
jtulach@1396
|
779 |
// from here without being caught.
|
jtulach@1396
|
780 |
getInputStream();
|
jtulach@1396
|
781 |
return getContentHandler().getContent(this, classes);
|
jtulach@1396
|
782 |
}
|
jtulach@1396
|
783 |
|
jtulach@1396
|
784 |
/**
|
jtulach@1396
|
785 |
* Returns a permission object representing the permission
|
jtulach@1396
|
786 |
* necessary to make the connection represented by this
|
jtulach@1396
|
787 |
* object. This method returns null if no permission is
|
jtulach@1396
|
788 |
* required to make the connection. By default, this method
|
jtulach@1396
|
789 |
* returns <code>java.security.AllPermission</code>. Subclasses
|
jtulach@1396
|
790 |
* should override this method and return the permission
|
jtulach@1396
|
791 |
* that best represents the permission required to make a
|
jtulach@1396
|
792 |
* a connection to the URL. For example, a <code>URLConnection</code>
|
jtulach@1396
|
793 |
* representing a <code>file:</code> URL would return a
|
jtulach@1396
|
794 |
* <code>java.io.FilePermission</code> object.
|
jtulach@1396
|
795 |
*
|
jtulach@1396
|
796 |
* <p>The permission returned may dependent upon the state of the
|
jtulach@1396
|
797 |
* connection. For example, the permission before connecting may be
|
jtulach@1396
|
798 |
* different from that after connecting. For example, an HTTP
|
jtulach@1396
|
799 |
* sever, say foo.com, may redirect the connection to a different
|
jtulach@1396
|
800 |
* host, say bar.com. Before connecting the permission returned by
|
jtulach@1396
|
801 |
* the connection will represent the permission needed to connect
|
jtulach@1396
|
802 |
* to foo.com, while the permission returned after connecting will
|
jtulach@1396
|
803 |
* be to bar.com.
|
jtulach@1396
|
804 |
*
|
jtulach@1396
|
805 |
* <p>Permissions are generally used for two purposes: to protect
|
jtulach@1396
|
806 |
* caches of objects obtained through URLConnections, and to check
|
jtulach@1396
|
807 |
* the right of a recipient to learn about a particular URL. In
|
jtulach@1396
|
808 |
* the first case, the permission should be obtained
|
jtulach@1396
|
809 |
* <em>after</em> the object has been obtained. For example, in an
|
jtulach@1396
|
810 |
* HTTP connection, this will represent the permission to connect
|
jtulach@1396
|
811 |
* to the host from which the data was ultimately fetched. In the
|
jtulach@1396
|
812 |
* second case, the permission should be obtained and tested
|
jtulach@1396
|
813 |
* <em>before</em> connecting.
|
jtulach@1396
|
814 |
*
|
jtulach@1396
|
815 |
* @return the permission object representing the permission
|
jtulach@1396
|
816 |
* necessary to make the connection represented by this
|
jtulach@1396
|
817 |
* URLConnection.
|
jtulach@1396
|
818 |
*
|
jtulach@1396
|
819 |
* @exception IOException if the computation of the permission
|
jtulach@1396
|
820 |
* requires network or file I/O and an exception occurs while
|
jtulach@1396
|
821 |
* computing it.
|
jtulach@1396
|
822 |
*/
|
jaroslav@1398
|
823 |
// public Permission getPermission() throws IOException {
|
jaroslav@1398
|
824 |
// return SecurityConstants.ALL_PERMISSION;
|
jaroslav@1398
|
825 |
// }
|
jtulach@1396
|
826 |
|
jtulach@1396
|
827 |
/**
|
jtulach@1396
|
828 |
* Returns an input stream that reads from this open connection.
|
jtulach@1396
|
829 |
*
|
jtulach@1396
|
830 |
* A SocketTimeoutException can be thrown when reading from the
|
jtulach@1396
|
831 |
* returned input stream if the read timeout expires before data
|
jtulach@1396
|
832 |
* is available for read.
|
jtulach@1396
|
833 |
*
|
jtulach@1396
|
834 |
* @return an input stream that reads from this open connection.
|
jtulach@1396
|
835 |
* @exception IOException if an I/O error occurs while
|
jtulach@1396
|
836 |
* creating the input stream.
|
jtulach@1396
|
837 |
* @exception UnknownServiceException if the protocol does not support
|
jtulach@1396
|
838 |
* input.
|
jtulach@1396
|
839 |
* @see #setReadTimeout(int)
|
jtulach@1396
|
840 |
* @see #getReadTimeout()
|
jtulach@1396
|
841 |
*/
|
jtulach@1396
|
842 |
public InputStream getInputStream() throws IOException {
|
jtulach@1396
|
843 |
throw new UnknownServiceException("protocol doesn't support input");
|
jtulach@1396
|
844 |
}
|
jtulach@1396
|
845 |
|
jtulach@1396
|
846 |
/**
|
jtulach@1396
|
847 |
* Returns an output stream that writes to this connection.
|
jtulach@1396
|
848 |
*
|
jtulach@1396
|
849 |
* @return an output stream that writes to this connection.
|
jtulach@1396
|
850 |
* @exception IOException if an I/O error occurs while
|
jtulach@1396
|
851 |
* creating the output stream.
|
jtulach@1396
|
852 |
* @exception UnknownServiceException if the protocol does not support
|
jtulach@1396
|
853 |
* output.
|
jtulach@1396
|
854 |
*/
|
jtulach@1396
|
855 |
public OutputStream getOutputStream() throws IOException {
|
jtulach@1396
|
856 |
throw new UnknownServiceException("protocol doesn't support output");
|
jtulach@1396
|
857 |
}
|
jtulach@1396
|
858 |
|
jtulach@1396
|
859 |
/**
|
jtulach@1396
|
860 |
* Returns a <code>String</code> representation of this URL connection.
|
jtulach@1396
|
861 |
*
|
jtulach@1396
|
862 |
* @return a string representation of this <code>URLConnection</code>.
|
jtulach@1396
|
863 |
*/
|
jtulach@1396
|
864 |
public String toString() {
|
jtulach@1396
|
865 |
return this.getClass().getName() + ":" + url;
|
jtulach@1396
|
866 |
}
|
jtulach@1396
|
867 |
|
jtulach@1396
|
868 |
/**
|
jtulach@1396
|
869 |
* Sets the value of the <code>doInput</code> field for this
|
jtulach@1396
|
870 |
* <code>URLConnection</code> to the specified value.
|
jtulach@1396
|
871 |
* <p>
|
jtulach@1396
|
872 |
* A URL connection can be used for input and/or output. Set the DoInput
|
jtulach@1396
|
873 |
* flag to true if you intend to use the URL connection for input,
|
jtulach@1396
|
874 |
* false if not. The default is true.
|
jtulach@1396
|
875 |
*
|
jtulach@1396
|
876 |
* @param doinput the new value.
|
jtulach@1396
|
877 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
878 |
* @see java.net.URLConnection#doInput
|
jtulach@1396
|
879 |
* @see #getDoInput()
|
jtulach@1396
|
880 |
*/
|
jtulach@1396
|
881 |
public void setDoInput(boolean doinput) {
|
jtulach@1396
|
882 |
if (connected)
|
jtulach@1396
|
883 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
884 |
doInput = doinput;
|
jtulach@1396
|
885 |
}
|
jtulach@1396
|
886 |
|
jtulach@1396
|
887 |
/**
|
jtulach@1396
|
888 |
* Returns the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
889 |
* <code>doInput</code> flag.
|
jtulach@1396
|
890 |
*
|
jtulach@1396
|
891 |
* @return the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
892 |
* <code>doInput</code> flag.
|
jtulach@1396
|
893 |
* @see #setDoInput(boolean)
|
jtulach@1396
|
894 |
*/
|
jtulach@1396
|
895 |
public boolean getDoInput() {
|
jtulach@1396
|
896 |
return doInput;
|
jtulach@1396
|
897 |
}
|
jtulach@1396
|
898 |
|
jtulach@1396
|
899 |
/**
|
jtulach@1396
|
900 |
* Sets the value of the <code>doOutput</code> field for this
|
jtulach@1396
|
901 |
* <code>URLConnection</code> to the specified value.
|
jtulach@1396
|
902 |
* <p>
|
jtulach@1396
|
903 |
* A URL connection can be used for input and/or output. Set the DoOutput
|
jtulach@1396
|
904 |
* flag to true if you intend to use the URL connection for output,
|
jtulach@1396
|
905 |
* false if not. The default is false.
|
jtulach@1396
|
906 |
*
|
jtulach@1396
|
907 |
* @param dooutput the new value.
|
jtulach@1396
|
908 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
909 |
* @see #getDoOutput()
|
jtulach@1396
|
910 |
*/
|
jtulach@1396
|
911 |
public void setDoOutput(boolean dooutput) {
|
jtulach@1396
|
912 |
if (connected)
|
jtulach@1396
|
913 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
914 |
doOutput = dooutput;
|
jtulach@1396
|
915 |
}
|
jtulach@1396
|
916 |
|
jtulach@1396
|
917 |
/**
|
jtulach@1396
|
918 |
* Returns the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
919 |
* <code>doOutput</code> flag.
|
jtulach@1396
|
920 |
*
|
jtulach@1396
|
921 |
* @return the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
922 |
* <code>doOutput</code> flag.
|
jtulach@1396
|
923 |
* @see #setDoOutput(boolean)
|
jtulach@1396
|
924 |
*/
|
jtulach@1396
|
925 |
public boolean getDoOutput() {
|
jtulach@1396
|
926 |
return doOutput;
|
jtulach@1396
|
927 |
}
|
jtulach@1396
|
928 |
|
jtulach@1396
|
929 |
/**
|
jtulach@1396
|
930 |
* Set the value of the <code>allowUserInteraction</code> field of
|
jtulach@1396
|
931 |
* this <code>URLConnection</code>.
|
jtulach@1396
|
932 |
*
|
jtulach@1396
|
933 |
* @param allowuserinteraction the new value.
|
jtulach@1396
|
934 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
935 |
* @see #getAllowUserInteraction()
|
jtulach@1396
|
936 |
*/
|
jtulach@1396
|
937 |
public void setAllowUserInteraction(boolean allowuserinteraction) {
|
jtulach@1396
|
938 |
if (connected)
|
jtulach@1396
|
939 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
940 |
allowUserInteraction = allowuserinteraction;
|
jtulach@1396
|
941 |
}
|
jtulach@1396
|
942 |
|
jtulach@1396
|
943 |
/**
|
jtulach@1396
|
944 |
* Returns the value of the <code>allowUserInteraction</code> field for
|
jtulach@1396
|
945 |
* this object.
|
jtulach@1396
|
946 |
*
|
jtulach@1396
|
947 |
* @return the value of the <code>allowUserInteraction</code> field for
|
jtulach@1396
|
948 |
* this object.
|
jtulach@1396
|
949 |
* @see #setAllowUserInteraction(boolean)
|
jtulach@1396
|
950 |
*/
|
jtulach@1396
|
951 |
public boolean getAllowUserInteraction() {
|
jtulach@1396
|
952 |
return allowUserInteraction;
|
jtulach@1396
|
953 |
}
|
jtulach@1396
|
954 |
|
jtulach@1396
|
955 |
/**
|
jtulach@1396
|
956 |
* Sets the default value of the
|
jtulach@1396
|
957 |
* <code>allowUserInteraction</code> field for all future
|
jtulach@1396
|
958 |
* <code>URLConnection</code> objects to the specified value.
|
jtulach@1396
|
959 |
*
|
jtulach@1396
|
960 |
* @param defaultallowuserinteraction the new value.
|
jtulach@1396
|
961 |
* @see #getDefaultAllowUserInteraction()
|
jtulach@1396
|
962 |
*/
|
jtulach@1396
|
963 |
public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) {
|
jtulach@1396
|
964 |
defaultAllowUserInteraction = defaultallowuserinteraction;
|
jtulach@1396
|
965 |
}
|
jtulach@1396
|
966 |
|
jtulach@1396
|
967 |
/**
|
jtulach@1396
|
968 |
* Returns the default value of the <code>allowUserInteraction</code>
|
jtulach@1396
|
969 |
* field.
|
jtulach@1396
|
970 |
* <p>
|
jtulach@1396
|
971 |
* Ths default is "sticky", being a part of the static state of all
|
jtulach@1396
|
972 |
* URLConnections. This flag applies to the next, and all following
|
jtulach@1396
|
973 |
* URLConnections that are created.
|
jtulach@1396
|
974 |
*
|
jtulach@1396
|
975 |
* @return the default value of the <code>allowUserInteraction</code>
|
jtulach@1396
|
976 |
* field.
|
jtulach@1396
|
977 |
* @see #setDefaultAllowUserInteraction(boolean)
|
jtulach@1396
|
978 |
*/
|
jtulach@1396
|
979 |
public static boolean getDefaultAllowUserInteraction() {
|
jtulach@1396
|
980 |
return defaultAllowUserInteraction;
|
jtulach@1396
|
981 |
}
|
jtulach@1396
|
982 |
|
jtulach@1396
|
983 |
/**
|
jtulach@1396
|
984 |
* Sets the value of the <code>useCaches</code> field of this
|
jtulach@1396
|
985 |
* <code>URLConnection</code> to the specified value.
|
jtulach@1396
|
986 |
* <p>
|
jtulach@1396
|
987 |
* Some protocols do caching of documents. Occasionally, it is important
|
jtulach@1396
|
988 |
* to be able to "tunnel through" and ignore the caches (e.g., the
|
jtulach@1396
|
989 |
* "reload" button in a browser). If the UseCaches flag on a connection
|
jtulach@1396
|
990 |
* is true, the connection is allowed to use whatever caches it can.
|
jtulach@1396
|
991 |
* If false, caches are to be ignored.
|
jtulach@1396
|
992 |
* The default value comes from DefaultUseCaches, which defaults to
|
jtulach@1396
|
993 |
* true.
|
jtulach@1396
|
994 |
*
|
jtulach@1396
|
995 |
* @param usecaches a <code>boolean</code> indicating whether
|
jtulach@1396
|
996 |
* or not to allow caching
|
jtulach@1396
|
997 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
998 |
* @see #getUseCaches()
|
jtulach@1396
|
999 |
*/
|
jtulach@1396
|
1000 |
public void setUseCaches(boolean usecaches) {
|
jtulach@1396
|
1001 |
if (connected)
|
jtulach@1396
|
1002 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1003 |
useCaches = usecaches;
|
jtulach@1396
|
1004 |
}
|
jtulach@1396
|
1005 |
|
jtulach@1396
|
1006 |
/**
|
jtulach@1396
|
1007 |
* Returns the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
1008 |
* <code>useCaches</code> field.
|
jtulach@1396
|
1009 |
*
|
jtulach@1396
|
1010 |
* @return the value of this <code>URLConnection</code>'s
|
jtulach@1396
|
1011 |
* <code>useCaches</code> field.
|
jtulach@1396
|
1012 |
* @see #setUseCaches(boolean)
|
jtulach@1396
|
1013 |
*/
|
jtulach@1396
|
1014 |
public boolean getUseCaches() {
|
jtulach@1396
|
1015 |
return useCaches;
|
jtulach@1396
|
1016 |
}
|
jtulach@1396
|
1017 |
|
jtulach@1396
|
1018 |
/**
|
jtulach@1396
|
1019 |
* Sets the value of the <code>ifModifiedSince</code> field of
|
jtulach@1396
|
1020 |
* this <code>URLConnection</code> to the specified value.
|
jtulach@1396
|
1021 |
*
|
jtulach@1396
|
1022 |
* @param ifmodifiedsince the new value.
|
jtulach@1396
|
1023 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
1024 |
* @see #getIfModifiedSince()
|
jtulach@1396
|
1025 |
*/
|
jtulach@1396
|
1026 |
public void setIfModifiedSince(long ifmodifiedsince) {
|
jtulach@1396
|
1027 |
if (connected)
|
jtulach@1396
|
1028 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1029 |
ifModifiedSince = ifmodifiedsince;
|
jtulach@1396
|
1030 |
}
|
jtulach@1396
|
1031 |
|
jtulach@1396
|
1032 |
/**
|
jtulach@1396
|
1033 |
* Returns the value of this object's <code>ifModifiedSince</code> field.
|
jtulach@1396
|
1034 |
*
|
jtulach@1396
|
1035 |
* @return the value of this object's <code>ifModifiedSince</code> field.
|
jtulach@1396
|
1036 |
* @see #setIfModifiedSince(long)
|
jtulach@1396
|
1037 |
*/
|
jtulach@1396
|
1038 |
public long getIfModifiedSince() {
|
jtulach@1396
|
1039 |
return ifModifiedSince;
|
jtulach@1396
|
1040 |
}
|
jtulach@1396
|
1041 |
|
jtulach@1396
|
1042 |
/**
|
jtulach@1396
|
1043 |
* Returns the default value of a <code>URLConnection</code>'s
|
jtulach@1396
|
1044 |
* <code>useCaches</code> flag.
|
jtulach@1396
|
1045 |
* <p>
|
jtulach@1396
|
1046 |
* Ths default is "sticky", being a part of the static state of all
|
jtulach@1396
|
1047 |
* URLConnections. This flag applies to the next, and all following
|
jtulach@1396
|
1048 |
* URLConnections that are created.
|
jtulach@1396
|
1049 |
*
|
jtulach@1396
|
1050 |
* @return the default value of a <code>URLConnection</code>'s
|
jtulach@1396
|
1051 |
* <code>useCaches</code> flag.
|
jtulach@1396
|
1052 |
* @see #setDefaultUseCaches(boolean)
|
jtulach@1396
|
1053 |
*/
|
jtulach@1396
|
1054 |
public boolean getDefaultUseCaches() {
|
jtulach@1396
|
1055 |
return defaultUseCaches;
|
jtulach@1396
|
1056 |
}
|
jtulach@1396
|
1057 |
|
jtulach@1396
|
1058 |
/**
|
jtulach@1396
|
1059 |
* Sets the default value of the <code>useCaches</code> field to the
|
jtulach@1396
|
1060 |
* specified value.
|
jtulach@1396
|
1061 |
*
|
jtulach@1396
|
1062 |
* @param defaultusecaches the new value.
|
jtulach@1396
|
1063 |
* @see #getDefaultUseCaches()
|
jtulach@1396
|
1064 |
*/
|
jtulach@1396
|
1065 |
public void setDefaultUseCaches(boolean defaultusecaches) {
|
jtulach@1396
|
1066 |
defaultUseCaches = defaultusecaches;
|
jtulach@1396
|
1067 |
}
|
jtulach@1396
|
1068 |
|
jtulach@1396
|
1069 |
/**
|
jtulach@1396
|
1070 |
* Sets the general request property. If a property with the key already
|
jtulach@1396
|
1071 |
* exists, overwrite its value with the new value.
|
jtulach@1396
|
1072 |
*
|
jtulach@1396
|
1073 |
* <p> NOTE: HTTP requires all request properties which can
|
jtulach@1396
|
1074 |
* legally have multiple instances with the same key
|
jtulach@1396
|
1075 |
* to use a comma-seperated list syntax which enables multiple
|
jtulach@1396
|
1076 |
* properties to be appended into a single property.
|
jtulach@1396
|
1077 |
*
|
jtulach@1396
|
1078 |
* @param key the keyword by which the request is known
|
jtulach@1396
|
1079 |
* (e.g., "<code>Accept</code>").
|
jtulach@1396
|
1080 |
* @param value the value associated with it.
|
jtulach@1396
|
1081 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
1082 |
* @throws NullPointerException if key is <CODE>null</CODE>
|
jtulach@1396
|
1083 |
* @see #getRequestProperty(java.lang.String)
|
jtulach@1396
|
1084 |
*/
|
jtulach@1396
|
1085 |
public void setRequestProperty(String key, String value) {
|
jtulach@1396
|
1086 |
if (connected)
|
jtulach@1396
|
1087 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1088 |
if (key == null)
|
jtulach@1396
|
1089 |
throw new NullPointerException ("key is null");
|
jtulach@1396
|
1090 |
|
jtulach@1396
|
1091 |
if (requests == null)
|
jtulach@1396
|
1092 |
requests = new MessageHeader();
|
jtulach@1396
|
1093 |
|
jtulach@1396
|
1094 |
requests.set(key, value);
|
jtulach@1396
|
1095 |
}
|
jtulach@1396
|
1096 |
|
jtulach@1396
|
1097 |
/**
|
jtulach@1396
|
1098 |
* Adds a general request property specified by a
|
jtulach@1396
|
1099 |
* key-value pair. This method will not overwrite
|
jtulach@1396
|
1100 |
* existing values associated with the same key.
|
jtulach@1396
|
1101 |
*
|
jtulach@1396
|
1102 |
* @param key the keyword by which the request is known
|
jtulach@1396
|
1103 |
* (e.g., "<code>Accept</code>").
|
jtulach@1396
|
1104 |
* @param value the value associated with it.
|
jtulach@1396
|
1105 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
1106 |
* @throws NullPointerException if key is null
|
jtulach@1396
|
1107 |
* @see #getRequestProperties()
|
jtulach@1396
|
1108 |
* @since 1.4
|
jtulach@1396
|
1109 |
*/
|
jtulach@1396
|
1110 |
public void addRequestProperty(String key, String value) {
|
jtulach@1396
|
1111 |
if (connected)
|
jtulach@1396
|
1112 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1113 |
if (key == null)
|
jtulach@1396
|
1114 |
throw new NullPointerException ("key is null");
|
jtulach@1396
|
1115 |
|
jtulach@1396
|
1116 |
if (requests == null)
|
jtulach@1396
|
1117 |
requests = new MessageHeader();
|
jtulach@1396
|
1118 |
|
jtulach@1396
|
1119 |
requests.add(key, value);
|
jtulach@1396
|
1120 |
}
|
jtulach@1396
|
1121 |
|
jtulach@1396
|
1122 |
|
jtulach@1396
|
1123 |
/**
|
jtulach@1396
|
1124 |
* Returns the value of the named general request property for this
|
jtulach@1396
|
1125 |
* connection.
|
jtulach@1396
|
1126 |
*
|
jtulach@1396
|
1127 |
* @param key the keyword by which the request is known (e.g., "Accept").
|
jtulach@1396
|
1128 |
* @return the value of the named general request property for this
|
jtulach@1396
|
1129 |
* connection. If key is null, then null is returned.
|
jtulach@1396
|
1130 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
1131 |
* @see #setRequestProperty(java.lang.String, java.lang.String)
|
jtulach@1396
|
1132 |
*/
|
jtulach@1396
|
1133 |
public String getRequestProperty(String key) {
|
jtulach@1396
|
1134 |
if (connected)
|
jtulach@1396
|
1135 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1136 |
|
jtulach@1396
|
1137 |
if (requests == null)
|
jtulach@1396
|
1138 |
return null;
|
jtulach@1396
|
1139 |
|
jtulach@1396
|
1140 |
return requests.findValue(key);
|
jtulach@1396
|
1141 |
}
|
jtulach@1396
|
1142 |
|
jtulach@1396
|
1143 |
/**
|
jtulach@1396
|
1144 |
* Returns an unmodifiable Map of general request
|
jtulach@1396
|
1145 |
* properties for this connection. The Map keys
|
jtulach@1396
|
1146 |
* are Strings that represent the request-header
|
jtulach@1396
|
1147 |
* field names. Each Map value is a unmodifiable List
|
jtulach@1396
|
1148 |
* of Strings that represents the corresponding
|
jtulach@1396
|
1149 |
* field values.
|
jtulach@1396
|
1150 |
*
|
jtulach@1396
|
1151 |
* @return a Map of the general request properties for this connection.
|
jtulach@1396
|
1152 |
* @throws IllegalStateException if already connected
|
jtulach@1396
|
1153 |
* @since 1.4
|
jtulach@1396
|
1154 |
*/
|
jtulach@1396
|
1155 |
public Map<String,List<String>> getRequestProperties() {
|
jtulach@1396
|
1156 |
if (connected)
|
jtulach@1396
|
1157 |
throw new IllegalStateException("Already connected");
|
jtulach@1396
|
1158 |
|
jtulach@1396
|
1159 |
if (requests == null)
|
jtulach@1396
|
1160 |
return Collections.EMPTY_MAP;
|
jtulach@1396
|
1161 |
|
jtulach@1396
|
1162 |
return requests.getHeaders(null);
|
jtulach@1396
|
1163 |
}
|
jtulach@1396
|
1164 |
|
jtulach@1396
|
1165 |
/**
|
jtulach@1396
|
1166 |
* Sets the default value of a general request property. When a
|
jtulach@1396
|
1167 |
* <code>URLConnection</code> is created, it is initialized with
|
jtulach@1396
|
1168 |
* these properties.
|
jtulach@1396
|
1169 |
*
|
jtulach@1396
|
1170 |
* @param key the keyword by which the request is known
|
jtulach@1396
|
1171 |
* (e.g., "<code>Accept</code>").
|
jtulach@1396
|
1172 |
* @param value the value associated with the key.
|
jtulach@1396
|
1173 |
*
|
jtulach@1396
|
1174 |
* @see java.net.URLConnection#setRequestProperty(java.lang.String,java.lang.String)
|
jtulach@1396
|
1175 |
*
|
jtulach@1396
|
1176 |
* @deprecated The instance specific setRequestProperty method
|
jtulach@1396
|
1177 |
* should be used after an appropriate instance of URLConnection
|
jtulach@1396
|
1178 |
* is obtained. Invoking this method will have no effect.
|
jtulach@1396
|
1179 |
*
|
jtulach@1396
|
1180 |
* @see #getDefaultRequestProperty(java.lang.String)
|
jtulach@1396
|
1181 |
*/
|
jtulach@1396
|
1182 |
@Deprecated
|
jtulach@1396
|
1183 |
public static void setDefaultRequestProperty(String key, String value) {
|
jtulach@1396
|
1184 |
}
|
jtulach@1396
|
1185 |
|
jtulach@1396
|
1186 |
/**
|
jtulach@1396
|
1187 |
* Returns the value of the default request property. Default request
|
jtulach@1396
|
1188 |
* properties are set for every connection.
|
jtulach@1396
|
1189 |
*
|
jtulach@1396
|
1190 |
* @param key the keyword by which the request is known (e.g., "Accept").
|
jtulach@1396
|
1191 |
* @return the value of the default request property
|
jtulach@1396
|
1192 |
* for the specified key.
|
jtulach@1396
|
1193 |
*
|
jtulach@1396
|
1194 |
* @see java.net.URLConnection#getRequestProperty(java.lang.String)
|
jtulach@1396
|
1195 |
*
|
jtulach@1396
|
1196 |
* @deprecated The instance specific getRequestProperty method
|
jtulach@1396
|
1197 |
* should be used after an appropriate instance of URLConnection
|
jtulach@1396
|
1198 |
* is obtained.
|
jtulach@1396
|
1199 |
*
|
jtulach@1396
|
1200 |
* @see #setDefaultRequestProperty(java.lang.String, java.lang.String)
|
jtulach@1396
|
1201 |
*/
|
jtulach@1396
|
1202 |
@Deprecated
|
jtulach@1396
|
1203 |
public static String getDefaultRequestProperty(String key) {
|
jtulach@1396
|
1204 |
return null;
|
jtulach@1396
|
1205 |
}
|
jtulach@1396
|
1206 |
|
jtulach@1396
|
1207 |
/**
|
jtulach@1396
|
1208 |
* The ContentHandler factory.
|
jtulach@1396
|
1209 |
*/
|
jtulach@1396
|
1210 |
static ContentHandlerFactory factory;
|
jtulach@1396
|
1211 |
|
jtulach@1396
|
1212 |
/**
|
jtulach@1396
|
1213 |
* Sets the <code>ContentHandlerFactory</code> of an
|
jtulach@1396
|
1214 |
* application. It can be called at most once by an application.
|
jtulach@1396
|
1215 |
* <p>
|
jtulach@1396
|
1216 |
* The <code>ContentHandlerFactory</code> instance is used to
|
jtulach@1396
|
1217 |
* construct a content handler from a content type
|
jtulach@1396
|
1218 |
* <p>
|
jtulach@1396
|
1219 |
* If there is a security manager, this method first calls
|
jtulach@1396
|
1220 |
* the security manager's <code>checkSetFactory</code> method
|
jtulach@1396
|
1221 |
* to ensure the operation is allowed.
|
jtulach@1396
|
1222 |
* This could result in a SecurityException.
|
jtulach@1396
|
1223 |
*
|
jtulach@1396
|
1224 |
* @param fac the desired factory.
|
jtulach@1396
|
1225 |
* @exception Error if the factory has already been defined.
|
jtulach@1396
|
1226 |
* @exception SecurityException if a security manager exists and its
|
jtulach@1396
|
1227 |
* <code>checkSetFactory</code> method doesn't allow the operation.
|
jtulach@1396
|
1228 |
* @see java.net.ContentHandlerFactory
|
jtulach@1396
|
1229 |
* @see java.net.URLConnection#getContent()
|
jtulach@1396
|
1230 |
* @see SecurityManager#checkSetFactory
|
jtulach@1396
|
1231 |
*/
|
jtulach@1396
|
1232 |
public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
|
jaroslav@1398
|
1233 |
throw new SecurityException();
|
jtulach@1396
|
1234 |
}
|
jtulach@1396
|
1235 |
|
jtulach@1396
|
1236 |
private static Hashtable handlers = new Hashtable();
|
jtulach@1396
|
1237 |
|
jtulach@1396
|
1238 |
/**
|
jtulach@1396
|
1239 |
* Gets the Content Handler appropriate for this connection.
|
jtulach@1396
|
1240 |
* @param connection the connection to use.
|
jtulach@1396
|
1241 |
*/
|
jtulach@1396
|
1242 |
synchronized ContentHandler getContentHandler()
|
jtulach@1396
|
1243 |
throws UnknownServiceException
|
jtulach@1396
|
1244 |
{
|
jtulach@1396
|
1245 |
String contentType = stripOffParameters(getContentType());
|
jtulach@1396
|
1246 |
ContentHandler handler = null;
|
jtulach@1396
|
1247 |
if (contentType == null)
|
jtulach@1396
|
1248 |
throw new UnknownServiceException("no content-type");
|
jtulach@1396
|
1249 |
try {
|
jtulach@1396
|
1250 |
handler = (ContentHandler) handlers.get(contentType);
|
jtulach@1396
|
1251 |
if (handler != null)
|
jtulach@1396
|
1252 |
return handler;
|
jtulach@1396
|
1253 |
} catch(Exception e) {
|
jtulach@1396
|
1254 |
}
|
jtulach@1396
|
1255 |
|
jtulach@1396
|
1256 |
if (factory != null)
|
jtulach@1396
|
1257 |
handler = factory.createContentHandler(contentType);
|
jtulach@1396
|
1258 |
if (handler == null) {
|
jtulach@1396
|
1259 |
try {
|
jtulach@1396
|
1260 |
handler = lookupContentHandlerClassFor(contentType);
|
jtulach@1396
|
1261 |
} catch(Exception e) {
|
jtulach@1396
|
1262 |
e.printStackTrace();
|
jtulach@1396
|
1263 |
handler = UnknownContentHandler.INSTANCE;
|
jtulach@1396
|
1264 |
}
|
jtulach@1396
|
1265 |
handlers.put(contentType, handler);
|
jtulach@1396
|
1266 |
}
|
jtulach@1396
|
1267 |
return handler;
|
jtulach@1396
|
1268 |
}
|
jtulach@1396
|
1269 |
|
jtulach@1396
|
1270 |
/*
|
jtulach@1396
|
1271 |
* Media types are in the format: type/subtype*(; parameter).
|
jtulach@1396
|
1272 |
* For looking up the content handler, we should ignore those
|
jtulach@1396
|
1273 |
* parameters.
|
jtulach@1396
|
1274 |
*/
|
jtulach@1396
|
1275 |
private String stripOffParameters(String contentType)
|
jtulach@1396
|
1276 |
{
|
jtulach@1396
|
1277 |
if (contentType == null)
|
jtulach@1396
|
1278 |
return null;
|
jtulach@1396
|
1279 |
int index = contentType.indexOf(';');
|
jtulach@1396
|
1280 |
|
jtulach@1396
|
1281 |
if (index > 0)
|
jtulach@1396
|
1282 |
return contentType.substring(0, index);
|
jtulach@1396
|
1283 |
else
|
jtulach@1396
|
1284 |
return contentType;
|
jtulach@1396
|
1285 |
}
|
jtulach@1396
|
1286 |
|
jtulach@1396
|
1287 |
private static final String contentClassPrefix = "sun.net.www.content";
|
jtulach@1396
|
1288 |
private static final String contentPathProp = "java.content.handler.pkgs";
|
jtulach@1396
|
1289 |
|
jtulach@1396
|
1290 |
/**
|
jtulach@1396
|
1291 |
* Looks for a content handler in a user-defineable set of places.
|
jtulach@1396
|
1292 |
* By default it looks in sun.net.www.content, but users can define a
|
jtulach@1396
|
1293 |
* vertical-bar delimited set of class prefixes to search through in
|
jtulach@1396
|
1294 |
* addition by defining the java.content.handler.pkgs property.
|
jtulach@1396
|
1295 |
* The class name must be of the form:
|
jtulach@1396
|
1296 |
* <pre>
|
jtulach@1396
|
1297 |
* {package-prefix}.{major}.{minor}
|
jtulach@1396
|
1298 |
* e.g.
|
jtulach@1396
|
1299 |
* YoyoDyne.experimental.text.plain
|
jtulach@1396
|
1300 |
* </pre>
|
jtulach@1396
|
1301 |
*/
|
jtulach@1396
|
1302 |
private ContentHandler lookupContentHandlerClassFor(String contentType)
|
jtulach@1396
|
1303 |
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
jtulach@1396
|
1304 |
String contentHandlerClassName = typeToPackageName(contentType);
|
jtulach@1396
|
1305 |
|
jtulach@1396
|
1306 |
String contentHandlerPkgPrefixes =getContentHandlerPkgPrefixes();
|
jtulach@1396
|
1307 |
|
jtulach@1396
|
1308 |
StringTokenizer packagePrefixIter =
|
jtulach@1396
|
1309 |
new StringTokenizer(contentHandlerPkgPrefixes, "|");
|
jtulach@1396
|
1310 |
|
jtulach@1396
|
1311 |
while (packagePrefixIter.hasMoreTokens()) {
|
jtulach@1396
|
1312 |
String packagePrefix = packagePrefixIter.nextToken().trim();
|
jtulach@1396
|
1313 |
|
jtulach@1396
|
1314 |
try {
|
jtulach@1396
|
1315 |
String clsName = packagePrefix + "." + contentHandlerClassName;
|
jtulach@1396
|
1316 |
Class cls = null;
|
jtulach@1396
|
1317 |
try {
|
jtulach@1396
|
1318 |
cls = Class.forName(clsName);
|
jtulach@1396
|
1319 |
} catch (ClassNotFoundException e) {
|
jtulach@1396
|
1320 |
ClassLoader cl = ClassLoader.getSystemClassLoader();
|
jtulach@1396
|
1321 |
if (cl != null) {
|
jtulach@1396
|
1322 |
cls = cl.loadClass(clsName);
|
jtulach@1396
|
1323 |
}
|
jtulach@1396
|
1324 |
}
|
jtulach@1396
|
1325 |
if (cls != null) {
|
jtulach@1396
|
1326 |
ContentHandler handler =
|
jtulach@1396
|
1327 |
(ContentHandler)cls.newInstance();
|
jtulach@1396
|
1328 |
return handler;
|
jtulach@1396
|
1329 |
}
|
jtulach@1396
|
1330 |
} catch(Exception e) {
|
jtulach@1396
|
1331 |
}
|
jtulach@1396
|
1332 |
}
|
jtulach@1396
|
1333 |
|
jtulach@1396
|
1334 |
return UnknownContentHandler.INSTANCE;
|
jtulach@1396
|
1335 |
}
|
jtulach@1396
|
1336 |
|
jtulach@1396
|
1337 |
/**
|
jtulach@1396
|
1338 |
* Utility function to map a MIME content type into an equivalent
|
jtulach@1396
|
1339 |
* pair of class name components. For example: "text/html" would
|
jtulach@1396
|
1340 |
* be returned as "text.html"
|
jtulach@1396
|
1341 |
*/
|
jtulach@1396
|
1342 |
private String typeToPackageName(String contentType) {
|
jtulach@1396
|
1343 |
// make sure we canonicalize the class name: all lower case
|
jtulach@1396
|
1344 |
contentType = contentType.toLowerCase();
|
jtulach@1396
|
1345 |
int len = contentType.length();
|
jtulach@1396
|
1346 |
char nm[] = new char[len];
|
jtulach@1396
|
1347 |
contentType.getChars(0, len, nm, 0);
|
jtulach@1396
|
1348 |
for (int i = 0; i < len; i++) {
|
jtulach@1396
|
1349 |
char c = nm[i];
|
jtulach@1396
|
1350 |
if (c == '/') {
|
jtulach@1396
|
1351 |
nm[i] = '.';
|
jtulach@1396
|
1352 |
} else if (!('A' <= c && c <= 'Z' ||
|
jtulach@1396
|
1353 |
'a' <= c && c <= 'z' ||
|
jtulach@1396
|
1354 |
'0' <= c && c <= '9')) {
|
jtulach@1396
|
1355 |
nm[i] = '_';
|
jtulach@1396
|
1356 |
}
|
jtulach@1396
|
1357 |
}
|
jtulach@1396
|
1358 |
return new String(nm);
|
jtulach@1396
|
1359 |
}
|
jtulach@1396
|
1360 |
|
jtulach@1396
|
1361 |
|
jtulach@1396
|
1362 |
/**
|
jtulach@1396
|
1363 |
* Returns a vertical bar separated list of package prefixes for potential
|
jtulach@1396
|
1364 |
* content handlers. Tries to get the java.content.handler.pkgs property
|
jtulach@1396
|
1365 |
* to use as a set of package prefixes to search. Whether or not
|
jtulach@1396
|
1366 |
* that property has been defined, the sun.net.www.content is always
|
jtulach@1396
|
1367 |
* the last one on the returned package list.
|
jtulach@1396
|
1368 |
*/
|
jtulach@1396
|
1369 |
private String getContentHandlerPkgPrefixes() {
|
jaroslav@1398
|
1370 |
String packagePrefixList = "";
|
jaroslav@1398
|
1371 |
|
jtulach@1396
|
1372 |
if (packagePrefixList != "") {
|
jtulach@1396
|
1373 |
packagePrefixList += "|";
|
jtulach@1396
|
1374 |
}
|
jtulach@1396
|
1375 |
|
jtulach@1396
|
1376 |
return packagePrefixList + contentClassPrefix;
|
jtulach@1396
|
1377 |
}
|
jtulach@1396
|
1378 |
|
jtulach@1396
|
1379 |
/**
|
jtulach@1396
|
1380 |
* Tries to determine the content type of an object, based
|
jtulach@1396
|
1381 |
* on the specified "file" component of a URL.
|
jtulach@1396
|
1382 |
* This is a convenience method that can be used by
|
jtulach@1396
|
1383 |
* subclasses that override the <code>getContentType</code> method.
|
jtulach@1396
|
1384 |
*
|
jtulach@1396
|
1385 |
* @param fname a filename.
|
jtulach@1396
|
1386 |
* @return a guess as to what the content type of the object is,
|
jtulach@1396
|
1387 |
* based upon its file name.
|
jtulach@1396
|
1388 |
* @see java.net.URLConnection#getContentType()
|
jtulach@1396
|
1389 |
*/
|
jtulach@1396
|
1390 |
public static String guessContentTypeFromName(String fname) {
|
jtulach@1396
|
1391 |
return getFileNameMap().getContentTypeFor(fname);
|
jtulach@1396
|
1392 |
}
|
jtulach@1396
|
1393 |
|
jtulach@1396
|
1394 |
/**
|
jtulach@1396
|
1395 |
* Tries to determine the type of an input stream based on the
|
jtulach@1396
|
1396 |
* characters at the beginning of the input stream. This method can
|
jtulach@1396
|
1397 |
* be used by subclasses that override the
|
jtulach@1396
|
1398 |
* <code>getContentType</code> method.
|
jtulach@1396
|
1399 |
* <p>
|
jtulach@1396
|
1400 |
* Ideally, this routine would not be needed. But many
|
jtulach@1396
|
1401 |
* <code>http</code> servers return the incorrect content type; in
|
jtulach@1396
|
1402 |
* addition, there are many nonstandard extensions. Direct inspection
|
jtulach@1396
|
1403 |
* of the bytes to determine the content type is often more accurate
|
jtulach@1396
|
1404 |
* than believing the content type claimed by the <code>http</code> server.
|
jtulach@1396
|
1405 |
*
|
jtulach@1396
|
1406 |
* @param is an input stream that supports marks.
|
jtulach@1396
|
1407 |
* @return a guess at the content type, or <code>null</code> if none
|
jtulach@1396
|
1408 |
* can be determined.
|
jtulach@1396
|
1409 |
* @exception IOException if an I/O error occurs while reading the
|
jtulach@1396
|
1410 |
* input stream.
|
jtulach@1396
|
1411 |
* @see java.io.InputStream#mark(int)
|
jtulach@1396
|
1412 |
* @see java.io.InputStream#markSupported()
|
jtulach@1396
|
1413 |
* @see java.net.URLConnection#getContentType()
|
jtulach@1396
|
1414 |
*/
|
jtulach@1396
|
1415 |
static public String guessContentTypeFromStream(InputStream is)
|
jtulach@1396
|
1416 |
throws IOException {
|
jtulach@1396
|
1417 |
// If we can't read ahead safely, just give up on guessing
|
jtulach@1396
|
1418 |
if (!is.markSupported())
|
jtulach@1396
|
1419 |
return null;
|
jtulach@1396
|
1420 |
|
jtulach@1396
|
1421 |
is.mark(16);
|
jtulach@1396
|
1422 |
int c1 = is.read();
|
jtulach@1396
|
1423 |
int c2 = is.read();
|
jtulach@1396
|
1424 |
int c3 = is.read();
|
jtulach@1396
|
1425 |
int c4 = is.read();
|
jtulach@1396
|
1426 |
int c5 = is.read();
|
jtulach@1396
|
1427 |
int c6 = is.read();
|
jtulach@1396
|
1428 |
int c7 = is.read();
|
jtulach@1396
|
1429 |
int c8 = is.read();
|
jtulach@1396
|
1430 |
int c9 = is.read();
|
jtulach@1396
|
1431 |
int c10 = is.read();
|
jtulach@1396
|
1432 |
int c11 = is.read();
|
jtulach@1396
|
1433 |
int c12 = is.read();
|
jtulach@1396
|
1434 |
int c13 = is.read();
|
jtulach@1396
|
1435 |
int c14 = is.read();
|
jtulach@1396
|
1436 |
int c15 = is.read();
|
jtulach@1396
|
1437 |
int c16 = is.read();
|
jtulach@1396
|
1438 |
is.reset();
|
jtulach@1396
|
1439 |
|
jtulach@1396
|
1440 |
if (c1 == 0xCA && c2 == 0xFE && c3 == 0xBA && c4 == 0xBE) {
|
jtulach@1396
|
1441 |
return "application/java-vm";
|
jtulach@1396
|
1442 |
}
|
jtulach@1396
|
1443 |
|
jtulach@1396
|
1444 |
if (c1 == 0xAC && c2 == 0xED) {
|
jtulach@1396
|
1445 |
// next two bytes are version number, currently 0x00 0x05
|
jtulach@1396
|
1446 |
return "application/x-java-serialized-object";
|
jtulach@1396
|
1447 |
}
|
jtulach@1396
|
1448 |
|
jtulach@1396
|
1449 |
if (c1 == '<') {
|
jtulach@1396
|
1450 |
if (c2 == '!'
|
jtulach@1396
|
1451 |
|| ((c2 == 'h' && (c3 == 't' && c4 == 'm' && c5 == 'l' ||
|
jtulach@1396
|
1452 |
c3 == 'e' && c4 == 'a' && c5 == 'd') ||
|
jtulach@1396
|
1453 |
(c2 == 'b' && c3 == 'o' && c4 == 'd' && c5 == 'y'))) ||
|
jtulach@1396
|
1454 |
((c2 == 'H' && (c3 == 'T' && c4 == 'M' && c5 == 'L' ||
|
jtulach@1396
|
1455 |
c3 == 'E' && c4 == 'A' && c5 == 'D') ||
|
jtulach@1396
|
1456 |
(c2 == 'B' && c3 == 'O' && c4 == 'D' && c5 == 'Y')))) {
|
jtulach@1396
|
1457 |
return "text/html";
|
jtulach@1396
|
1458 |
}
|
jtulach@1396
|
1459 |
|
jtulach@1396
|
1460 |
if (c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l' && c6 == ' ') {
|
jtulach@1396
|
1461 |
return "application/xml";
|
jtulach@1396
|
1462 |
}
|
jtulach@1396
|
1463 |
}
|
jtulach@1396
|
1464 |
|
jtulach@1396
|
1465 |
// big and little (identical) endian UTF-8 encodings, with BOM
|
jtulach@1396
|
1466 |
if (c1 == 0xef && c2 == 0xbb && c3 == 0xbf) {
|
jtulach@1396
|
1467 |
if (c4 == '<' && c5 == '?' && c6 == 'x') {
|
jtulach@1396
|
1468 |
return "application/xml";
|
jtulach@1396
|
1469 |
}
|
jtulach@1396
|
1470 |
}
|
jtulach@1396
|
1471 |
|
jtulach@1396
|
1472 |
// big and little endian UTF-16 encodings, with byte order mark
|
jtulach@1396
|
1473 |
if (c1 == 0xfe && c2 == 0xff) {
|
jtulach@1396
|
1474 |
if (c3 == 0 && c4 == '<' && c5 == 0 && c6 == '?' &&
|
jtulach@1396
|
1475 |
c7 == 0 && c8 == 'x') {
|
jtulach@1396
|
1476 |
return "application/xml";
|
jtulach@1396
|
1477 |
}
|
jtulach@1396
|
1478 |
}
|
jtulach@1396
|
1479 |
|
jtulach@1396
|
1480 |
if (c1 == 0xff && c2 == 0xfe) {
|
jtulach@1396
|
1481 |
if (c3 == '<' && c4 == 0 && c5 == '?' && c6 == 0 &&
|
jtulach@1396
|
1482 |
c7 == 'x' && c8 == 0) {
|
jtulach@1396
|
1483 |
return "application/xml";
|
jtulach@1396
|
1484 |
}
|
jtulach@1396
|
1485 |
}
|
jtulach@1396
|
1486 |
|
jtulach@1396
|
1487 |
// big and little endian UTF-32 encodings, with BOM
|
jtulach@1396
|
1488 |
if (c1 == 0x00 && c2 == 0x00 && c3 == 0xfe && c4 == 0xff) {
|
jtulach@1396
|
1489 |
if (c5 == 0 && c6 == 0 && c7 == 0 && c8 == '<' &&
|
jtulach@1396
|
1490 |
c9 == 0 && c10 == 0 && c11 == 0 && c12 == '?' &&
|
jtulach@1396
|
1491 |
c13 == 0 && c14 == 0 && c15 == 0 && c16 == 'x') {
|
jtulach@1396
|
1492 |
return "application/xml";
|
jtulach@1396
|
1493 |
}
|
jtulach@1396
|
1494 |
}
|
jtulach@1396
|
1495 |
|
jtulach@1396
|
1496 |
if (c1 == 0xff && c2 == 0xfe && c3 == 0x00 && c4 == 0x00) {
|
jtulach@1396
|
1497 |
if (c5 == '<' && c6 == 0 && c7 == 0 && c8 == 0 &&
|
jtulach@1396
|
1498 |
c9 == '?' && c10 == 0 && c11 == 0 && c12 == 0 &&
|
jtulach@1396
|
1499 |
c13 == 'x' && c14 == 0 && c15 == 0 && c16 == 0) {
|
jtulach@1396
|
1500 |
return "application/xml";
|
jtulach@1396
|
1501 |
}
|
jtulach@1396
|
1502 |
}
|
jtulach@1396
|
1503 |
|
jtulach@1396
|
1504 |
if (c1 == 'G' && c2 == 'I' && c3 == 'F' && c4 == '8') {
|
jtulach@1396
|
1505 |
return "image/gif";
|
jtulach@1396
|
1506 |
}
|
jtulach@1396
|
1507 |
|
jtulach@1396
|
1508 |
if (c1 == '#' && c2 == 'd' && c3 == 'e' && c4 == 'f') {
|
jtulach@1396
|
1509 |
return "image/x-bitmap";
|
jtulach@1396
|
1510 |
}
|
jtulach@1396
|
1511 |
|
jtulach@1396
|
1512 |
if (c1 == '!' && c2 == ' ' && c3 == 'X' && c4 == 'P' &&
|
jtulach@1396
|
1513 |
c5 == 'M' && c6 == '2') {
|
jtulach@1396
|
1514 |
return "image/x-pixmap";
|
jtulach@1396
|
1515 |
}
|
jtulach@1396
|
1516 |
|
jtulach@1396
|
1517 |
if (c1 == 137 && c2 == 80 && c3 == 78 &&
|
jtulach@1396
|
1518 |
c4 == 71 && c5 == 13 && c6 == 10 &&
|
jtulach@1396
|
1519 |
c7 == 26 && c8 == 10) {
|
jtulach@1396
|
1520 |
return "image/png";
|
jtulach@1396
|
1521 |
}
|
jtulach@1396
|
1522 |
|
jtulach@1396
|
1523 |
if (c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF) {
|
jtulach@1396
|
1524 |
if (c4 == 0xE0) {
|
jtulach@1396
|
1525 |
return "image/jpeg";
|
jtulach@1396
|
1526 |
}
|
jtulach@1396
|
1527 |
|
jtulach@1396
|
1528 |
/**
|
jtulach@1396
|
1529 |
* File format used by digital cameras to store images.
|
jtulach@1396
|
1530 |
* Exif Format can be read by any application supporting
|
jtulach@1396
|
1531 |
* JPEG. Exif Spec can be found at:
|
jtulach@1396
|
1532 |
* http://www.pima.net/standards/it10/PIMA15740/Exif_2-1.PDF
|
jtulach@1396
|
1533 |
*/
|
jtulach@1396
|
1534 |
if ((c4 == 0xE1) &&
|
jtulach@1396
|
1535 |
(c7 == 'E' && c8 == 'x' && c9 == 'i' && c10 =='f' &&
|
jtulach@1396
|
1536 |
c11 == 0)) {
|
jtulach@1396
|
1537 |
return "image/jpeg";
|
jtulach@1396
|
1538 |
}
|
jtulach@1396
|
1539 |
|
jtulach@1396
|
1540 |
if (c4 == 0xEE) {
|
jtulach@1396
|
1541 |
return "image/jpg";
|
jtulach@1396
|
1542 |
}
|
jtulach@1396
|
1543 |
}
|
jtulach@1396
|
1544 |
|
jtulach@1396
|
1545 |
if (c1 == 0xD0 && c2 == 0xCF && c3 == 0x11 && c4 == 0xE0 &&
|
jtulach@1396
|
1546 |
c5 == 0xA1 && c6 == 0xB1 && c7 == 0x1A && c8 == 0xE1) {
|
jtulach@1396
|
1547 |
|
jtulach@1396
|
1548 |
/* Above is signature of Microsoft Structured Storage.
|
jtulach@1396
|
1549 |
* Below this, could have tests for various SS entities.
|
jtulach@1396
|
1550 |
* For now, just test for FlashPix.
|
jtulach@1396
|
1551 |
*/
|
jtulach@1396
|
1552 |
if (checkfpx(is)) {
|
jtulach@1396
|
1553 |
return "image/vnd.fpx";
|
jtulach@1396
|
1554 |
}
|
jtulach@1396
|
1555 |
}
|
jtulach@1396
|
1556 |
|
jtulach@1396
|
1557 |
if (c1 == 0x2E && c2 == 0x73 && c3 == 0x6E && c4 == 0x64) {
|
jtulach@1396
|
1558 |
return "audio/basic"; // .au format, big endian
|
jtulach@1396
|
1559 |
}
|
jtulach@1396
|
1560 |
|
jtulach@1396
|
1561 |
if (c1 == 0x64 && c2 == 0x6E && c3 == 0x73 && c4 == 0x2E) {
|
jtulach@1396
|
1562 |
return "audio/basic"; // .au format, little endian
|
jtulach@1396
|
1563 |
}
|
jtulach@1396
|
1564 |
|
jtulach@1396
|
1565 |
if (c1 == 'R' && c2 == 'I' && c3 == 'F' && c4 == 'F') {
|
jtulach@1396
|
1566 |
/* I don't know if this is official but evidence
|
jtulach@1396
|
1567 |
* suggests that .wav files start with "RIFF" - brown
|
jtulach@1396
|
1568 |
*/
|
jtulach@1396
|
1569 |
return "audio/x-wav";
|
jtulach@1396
|
1570 |
}
|
jtulach@1396
|
1571 |
return null;
|
jtulach@1396
|
1572 |
}
|
jtulach@1396
|
1573 |
|
jtulach@1396
|
1574 |
/**
|
jtulach@1396
|
1575 |
* Check for FlashPix image data in InputStream is. Return true if
|
jtulach@1396
|
1576 |
* the stream has FlashPix data, false otherwise. Before calling this
|
jtulach@1396
|
1577 |
* method, the stream should have already been checked to be sure it
|
jtulach@1396
|
1578 |
* contains Microsoft Structured Storage data.
|
jtulach@1396
|
1579 |
*/
|
jtulach@1396
|
1580 |
static private boolean checkfpx(InputStream is) throws IOException {
|
jtulach@1396
|
1581 |
|
jtulach@1396
|
1582 |
/* Test for FlashPix image data in Microsoft Structured Storage format.
|
jtulach@1396
|
1583 |
* In general, should do this with calls to an SS implementation.
|
jtulach@1396
|
1584 |
* Lacking that, need to dig via offsets to get to the FlashPix
|
jtulach@1396
|
1585 |
* ClassID. Details:
|
jtulach@1396
|
1586 |
*
|
jtulach@1396
|
1587 |
* Offset to Fpx ClsID from beginning of stream should be:
|
jtulach@1396
|
1588 |
*
|
jtulach@1396
|
1589 |
* FpxClsidOffset = rootEntryOffset + clsidOffset
|
jtulach@1396
|
1590 |
*
|
jtulach@1396
|
1591 |
* where: clsidOffset = 0x50.
|
jtulach@1396
|
1592 |
* rootEntryOffset = headerSize + sectorSize*sectDirStart
|
jtulach@1396
|
1593 |
* + 128*rootEntryDirectory
|
jtulach@1396
|
1594 |
*
|
jtulach@1396
|
1595 |
* where: headerSize = 0x200 (always)
|
jtulach@1396
|
1596 |
* sectorSize = 2 raised to power of uSectorShift,
|
jtulach@1396
|
1597 |
* which is found in the header at
|
jtulach@1396
|
1598 |
* offset 0x1E.
|
jtulach@1396
|
1599 |
* sectDirStart = found in the header at offset 0x30.
|
jtulach@1396
|
1600 |
* rootEntryDirectory = in general, should search for
|
jtulach@1396
|
1601 |
* directory labelled as root.
|
jtulach@1396
|
1602 |
* We will assume value of 0 (i.e.,
|
jtulach@1396
|
1603 |
* rootEntry is in first directory)
|
jtulach@1396
|
1604 |
*/
|
jtulach@1396
|
1605 |
|
jtulach@1396
|
1606 |
// Mark the stream so we can reset it. 0x100 is enough for the first
|
jtulach@1396
|
1607 |
// few reads, but the mark will have to be reset and set again once
|
jtulach@1396
|
1608 |
// the offset to the root directory entry is computed. That offset
|
jtulach@1396
|
1609 |
// can be very large and isn't know until the stream has been read from
|
jtulach@1396
|
1610 |
is.mark(0x100);
|
jtulach@1396
|
1611 |
|
jtulach@1396
|
1612 |
// Get the byte ordering located at 0x1E. 0xFE is Intel,
|
jtulach@1396
|
1613 |
// 0xFF is other
|
jtulach@1396
|
1614 |
long toSkip = (long)0x1C;
|
jtulach@1396
|
1615 |
long posn;
|
jtulach@1396
|
1616 |
|
jtulach@1396
|
1617 |
if ((posn = skipForward(is, toSkip)) < toSkip) {
|
jtulach@1396
|
1618 |
is.reset();
|
jtulach@1396
|
1619 |
return false;
|
jtulach@1396
|
1620 |
}
|
jtulach@1396
|
1621 |
|
jtulach@1396
|
1622 |
int c[] = new int[16];
|
jtulach@1396
|
1623 |
if (readBytes(c, 2, is) < 0) {
|
jtulach@1396
|
1624 |
is.reset();
|
jtulach@1396
|
1625 |
return false;
|
jtulach@1396
|
1626 |
}
|
jtulach@1396
|
1627 |
|
jtulach@1396
|
1628 |
int byteOrder = c[0];
|
jtulach@1396
|
1629 |
|
jtulach@1396
|
1630 |
posn+=2;
|
jtulach@1396
|
1631 |
int uSectorShift;
|
jtulach@1396
|
1632 |
if (readBytes(c, 2, is) < 0) {
|
jtulach@1396
|
1633 |
is.reset();
|
jtulach@1396
|
1634 |
return false;
|
jtulach@1396
|
1635 |
}
|
jtulach@1396
|
1636 |
|
jtulach@1396
|
1637 |
if(byteOrder == 0xFE) {
|
jtulach@1396
|
1638 |
uSectorShift = c[0];
|
jtulach@1396
|
1639 |
uSectorShift += c[1] << 8;
|
jtulach@1396
|
1640 |
}
|
jtulach@1396
|
1641 |
else {
|
jtulach@1396
|
1642 |
uSectorShift = c[0] << 8;
|
jtulach@1396
|
1643 |
uSectorShift += c[1];
|
jtulach@1396
|
1644 |
}
|
jtulach@1396
|
1645 |
|
jtulach@1396
|
1646 |
posn += 2;
|
jtulach@1396
|
1647 |
toSkip = (long)0x30 - posn;
|
jtulach@1396
|
1648 |
long skipped = 0;
|
jtulach@1396
|
1649 |
if ((skipped = skipForward(is, toSkip)) < toSkip) {
|
jtulach@1396
|
1650 |
is.reset();
|
jtulach@1396
|
1651 |
return false;
|
jtulach@1396
|
1652 |
}
|
jtulach@1396
|
1653 |
posn += skipped;
|
jtulach@1396
|
1654 |
|
jtulach@1396
|
1655 |
if (readBytes(c, 4, is) < 0) {
|
jtulach@1396
|
1656 |
is.reset();
|
jtulach@1396
|
1657 |
return false;
|
jtulach@1396
|
1658 |
}
|
jtulach@1396
|
1659 |
|
jtulach@1396
|
1660 |
int sectDirStart;
|
jtulach@1396
|
1661 |
if(byteOrder == 0xFE) {
|
jtulach@1396
|
1662 |
sectDirStart = c[0];
|
jtulach@1396
|
1663 |
sectDirStart += c[1] << 8;
|
jtulach@1396
|
1664 |
sectDirStart += c[2] << 16;
|
jtulach@1396
|
1665 |
sectDirStart += c[3] << 24;
|
jtulach@1396
|
1666 |
} else {
|
jtulach@1396
|
1667 |
sectDirStart = c[0] << 24;
|
jtulach@1396
|
1668 |
sectDirStart += c[1] << 16;
|
jtulach@1396
|
1669 |
sectDirStart += c[2] << 8;
|
jtulach@1396
|
1670 |
sectDirStart += c[3];
|
jtulach@1396
|
1671 |
}
|
jtulach@1396
|
1672 |
posn += 4;
|
jtulach@1396
|
1673 |
is.reset(); // Reset back to the beginning
|
jtulach@1396
|
1674 |
|
jtulach@1396
|
1675 |
toSkip = 0x200L + (long)(1<<uSectorShift)*sectDirStart + 0x50L;
|
jtulach@1396
|
1676 |
|
jtulach@1396
|
1677 |
// Sanity check!
|
jtulach@1396
|
1678 |
if (toSkip < 0) {
|
jtulach@1396
|
1679 |
return false;
|
jtulach@1396
|
1680 |
}
|
jtulach@1396
|
1681 |
|
jtulach@1396
|
1682 |
/*
|
jtulach@1396
|
1683 |
* How far can we skip? Is there any performance problem here?
|
jtulach@1396
|
1684 |
* This skip can be fairly long, at least 0x4c650 in at least
|
jtulach@1396
|
1685 |
* one case. Have to assume that the skip will fit in an int.
|
jtulach@1396
|
1686 |
* Leave room to read whole root dir
|
jtulach@1396
|
1687 |
*/
|
jtulach@1396
|
1688 |
is.mark((int)toSkip+0x30);
|
jtulach@1396
|
1689 |
|
jtulach@1396
|
1690 |
if ((skipForward(is, toSkip)) < toSkip) {
|
jtulach@1396
|
1691 |
is.reset();
|
jtulach@1396
|
1692 |
return false;
|
jtulach@1396
|
1693 |
}
|
jtulach@1396
|
1694 |
|
jtulach@1396
|
1695 |
/* should be at beginning of ClassID, which is as follows
|
jtulach@1396
|
1696 |
* (in Intel byte order):
|
jtulach@1396
|
1697 |
* 00 67 61 56 54 C1 CE 11 85 53 00 AA 00 A1 F9 5B
|
jtulach@1396
|
1698 |
*
|
jtulach@1396
|
1699 |
* This is stored from Windows as long,short,short,char[8]
|
jtulach@1396
|
1700 |
* so for byte order changes, the order only changes for
|
jtulach@1396
|
1701 |
* the first 8 bytes in the ClassID.
|
jtulach@1396
|
1702 |
*
|
jtulach@1396
|
1703 |
* Test against this, ignoring second byte (Intel) since
|
jtulach@1396
|
1704 |
* this could change depending on part of Fpx file we have.
|
jtulach@1396
|
1705 |
*/
|
jtulach@1396
|
1706 |
|
jtulach@1396
|
1707 |
if (readBytes(c, 16, is) < 0) {
|
jtulach@1396
|
1708 |
is.reset();
|
jtulach@1396
|
1709 |
return false;
|
jtulach@1396
|
1710 |
}
|
jtulach@1396
|
1711 |
|
jtulach@1396
|
1712 |
// intel byte order
|
jtulach@1396
|
1713 |
if (byteOrder == 0xFE &&
|
jtulach@1396
|
1714 |
c[0] == 0x00 && c[2] == 0x61 && c[3] == 0x56 &&
|
jtulach@1396
|
1715 |
c[4] == 0x54 && c[5] == 0xC1 && c[6] == 0xCE &&
|
jtulach@1396
|
1716 |
c[7] == 0x11 && c[8] == 0x85 && c[9] == 0x53 &&
|
jtulach@1396
|
1717 |
c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 &&
|
jtulach@1396
|
1718 |
c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) {
|
jtulach@1396
|
1719 |
is.reset();
|
jtulach@1396
|
1720 |
return true;
|
jtulach@1396
|
1721 |
}
|
jtulach@1396
|
1722 |
|
jtulach@1396
|
1723 |
// non-intel byte order
|
jtulach@1396
|
1724 |
else if (c[3] == 0x00 && c[1] == 0x61 && c[0] == 0x56 &&
|
jtulach@1396
|
1725 |
c[5] == 0x54 && c[4] == 0xC1 && c[7] == 0xCE &&
|
jtulach@1396
|
1726 |
c[6] == 0x11 && c[8] == 0x85 && c[9] == 0x53 &&
|
jtulach@1396
|
1727 |
c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 &&
|
jtulach@1396
|
1728 |
c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) {
|
jtulach@1396
|
1729 |
is.reset();
|
jtulach@1396
|
1730 |
return true;
|
jtulach@1396
|
1731 |
}
|
jtulach@1396
|
1732 |
is.reset();
|
jtulach@1396
|
1733 |
return false;
|
jtulach@1396
|
1734 |
}
|
jtulach@1396
|
1735 |
|
jtulach@1396
|
1736 |
/**
|
jtulach@1396
|
1737 |
* Tries to read the specified number of bytes from the stream
|
jtulach@1396
|
1738 |
* Returns -1, If EOF is reached before len bytes are read, returns 0
|
jtulach@1396
|
1739 |
* otherwise
|
jtulach@1396
|
1740 |
*/
|
jtulach@1396
|
1741 |
static private int readBytes(int c[], int len, InputStream is)
|
jtulach@1396
|
1742 |
throws IOException {
|
jtulach@1396
|
1743 |
|
jtulach@1396
|
1744 |
byte buf[] = new byte[len];
|
jtulach@1396
|
1745 |
if (is.read(buf, 0, len) < len) {
|
jtulach@1396
|
1746 |
return -1;
|
jtulach@1396
|
1747 |
}
|
jtulach@1396
|
1748 |
|
jtulach@1396
|
1749 |
// fill the passed in int array
|
jtulach@1396
|
1750 |
for (int i = 0; i < len; i++) {
|
jtulach@1396
|
1751 |
c[i] = buf[i] & 0xff;
|
jtulach@1396
|
1752 |
}
|
jtulach@1396
|
1753 |
return 0;
|
jtulach@1396
|
1754 |
}
|
jtulach@1396
|
1755 |
|
jtulach@1396
|
1756 |
|
jtulach@1396
|
1757 |
/**
|
jtulach@1396
|
1758 |
* Skips through the specified number of bytes from the stream
|
jtulach@1396
|
1759 |
* until either EOF is reached, or the specified
|
jtulach@1396
|
1760 |
* number of bytes have been skipped
|
jtulach@1396
|
1761 |
*/
|
jtulach@1396
|
1762 |
static private long skipForward(InputStream is, long toSkip)
|
jtulach@1396
|
1763 |
throws IOException {
|
jtulach@1396
|
1764 |
|
jtulach@1396
|
1765 |
long eachSkip = 0;
|
jtulach@1396
|
1766 |
long skipped = 0;
|
jtulach@1396
|
1767 |
|
jtulach@1396
|
1768 |
while (skipped != toSkip) {
|
jtulach@1396
|
1769 |
eachSkip = is.skip(toSkip - skipped);
|
jtulach@1396
|
1770 |
|
jtulach@1396
|
1771 |
// check if EOF is reached
|
jtulach@1396
|
1772 |
if (eachSkip <= 0) {
|
jtulach@1396
|
1773 |
if (is.read() == -1) {
|
jtulach@1396
|
1774 |
return skipped ;
|
jtulach@1396
|
1775 |
} else {
|
jtulach@1396
|
1776 |
skipped++;
|
jtulach@1396
|
1777 |
}
|
jtulach@1396
|
1778 |
}
|
jtulach@1396
|
1779 |
skipped += eachSkip;
|
jtulach@1396
|
1780 |
}
|
jtulach@1396
|
1781 |
return skipped;
|
jtulach@1396
|
1782 |
}
|
jtulach@1396
|
1783 |
|
jtulach@1396
|
1784 |
}
|
jtulach@1396
|
1785 |
|
jtulach@1396
|
1786 |
|
jtulach@1396
|
1787 |
class UnknownContentHandler extends ContentHandler {
|
jtulach@1396
|
1788 |
static final ContentHandler INSTANCE = new UnknownContentHandler();
|
jtulach@1396
|
1789 |
|
jtulach@1396
|
1790 |
public Object getContent(URLConnection uc) throws IOException {
|
jtulach@1396
|
1791 |
return uc.getInputStream();
|
jtulach@1396
|
1792 |
}
|
jtulach@1396
|
1793 |
}
|
jaroslav@1398
|
1794 |
|
jaroslav@1398
|
1795 |
/** An RFC 844 or MIME message header. Includes methods
|
jaroslav@1398
|
1796 |
for parsing headers from incoming streams, fetching
|
jaroslav@1398
|
1797 |
values, setting values, and printing headers.
|
jaroslav@1398
|
1798 |
Key values of null are legal: they indicate lines in
|
jaroslav@1398
|
1799 |
the header that don't have a valid key, but do have
|
jaroslav@1398
|
1800 |
a value (this isn't legal according to the standard,
|
jaroslav@1398
|
1801 |
but lines like this are everywhere). */
|
jaroslav@1398
|
1802 |
class MessageHeader {
|
jaroslav@1398
|
1803 |
private String keys[];
|
jaroslav@1398
|
1804 |
private String values[];
|
jaroslav@1398
|
1805 |
private int nkeys;
|
jaroslav@1398
|
1806 |
|
jaroslav@1398
|
1807 |
public MessageHeader () {
|
jaroslav@1398
|
1808 |
grow();
|
jaroslav@1398
|
1809 |
}
|
jaroslav@1398
|
1810 |
|
jaroslav@1398
|
1811 |
public MessageHeader (InputStream is) throws java.io.IOException {
|
jaroslav@1398
|
1812 |
parseHeader(is);
|
jaroslav@1398
|
1813 |
}
|
jaroslav@1398
|
1814 |
|
jaroslav@1398
|
1815 |
/**
|
jaroslav@1398
|
1816 |
* Reset a message header (all key/values removed)
|
jaroslav@1398
|
1817 |
*/
|
jaroslav@1398
|
1818 |
public synchronized void reset() {
|
jaroslav@1398
|
1819 |
keys = null;
|
jaroslav@1398
|
1820 |
values = null;
|
jaroslav@1398
|
1821 |
nkeys = 0;
|
jaroslav@1398
|
1822 |
grow();
|
jaroslav@1398
|
1823 |
}
|
jaroslav@1398
|
1824 |
|
jaroslav@1398
|
1825 |
/**
|
jaroslav@1398
|
1826 |
* Find the value that corresponds to this key.
|
jaroslav@1398
|
1827 |
* It finds only the first occurrence of the key.
|
jaroslav@1398
|
1828 |
* @param k the key to find.
|
jaroslav@1398
|
1829 |
* @return null if not found.
|
jaroslav@1398
|
1830 |
*/
|
jaroslav@1398
|
1831 |
public synchronized String findValue(String k) {
|
jaroslav@1398
|
1832 |
if (k == null) {
|
jaroslav@1398
|
1833 |
for (int i = nkeys; --i >= 0;)
|
jaroslav@1398
|
1834 |
if (keys[i] == null)
|
jaroslav@1398
|
1835 |
return values[i];
|
jaroslav@1398
|
1836 |
} else
|
jaroslav@1398
|
1837 |
for (int i = nkeys; --i >= 0;) {
|
jaroslav@1398
|
1838 |
if (k.equalsIgnoreCase(keys[i]))
|
jaroslav@1398
|
1839 |
return values[i];
|
jaroslav@1398
|
1840 |
}
|
jaroslav@1398
|
1841 |
return null;
|
jaroslav@1398
|
1842 |
}
|
jaroslav@1398
|
1843 |
|
jaroslav@1398
|
1844 |
// return the location of the key
|
jaroslav@1398
|
1845 |
public synchronized int getKey(String k) {
|
jaroslav@1398
|
1846 |
for (int i = nkeys; --i >= 0;)
|
jaroslav@1398
|
1847 |
if ((keys[i] == k) ||
|
jaroslav@1398
|
1848 |
(k != null && k.equalsIgnoreCase(keys[i])))
|
jaroslav@1398
|
1849 |
return i;
|
jaroslav@1398
|
1850 |
return -1;
|
jaroslav@1398
|
1851 |
}
|
jaroslav@1398
|
1852 |
|
jaroslav@1398
|
1853 |
public synchronized String getKey(int n) {
|
jaroslav@1398
|
1854 |
if (n < 0 || n >= nkeys) return null;
|
jaroslav@1398
|
1855 |
return keys[n];
|
jaroslav@1398
|
1856 |
}
|
jaroslav@1398
|
1857 |
|
jaroslav@1398
|
1858 |
public synchronized String getValue(int n) {
|
jaroslav@1398
|
1859 |
if (n < 0 || n >= nkeys) return null;
|
jaroslav@1398
|
1860 |
return values[n];
|
jaroslav@1398
|
1861 |
}
|
jaroslav@1398
|
1862 |
|
jaroslav@1398
|
1863 |
/** Deprecated: Use multiValueIterator() instead.
|
jaroslav@1398
|
1864 |
*
|
jaroslav@1398
|
1865 |
* Find the next value that corresponds to this key.
|
jaroslav@1398
|
1866 |
* It finds the first value that follows v. To iterate
|
jaroslav@1398
|
1867 |
* over all the values of a key use:
|
jaroslav@1398
|
1868 |
* <pre>
|
jaroslav@1398
|
1869 |
* for(String v=h.findValue(k); v!=null; v=h.findNextValue(k, v)) {
|
jaroslav@1398
|
1870 |
* ...
|
jaroslav@1398
|
1871 |
* }
|
jaroslav@1398
|
1872 |
* </pre>
|
jaroslav@1398
|
1873 |
*/
|
jaroslav@1398
|
1874 |
public synchronized String findNextValue(String k, String v) {
|
jaroslav@1398
|
1875 |
boolean foundV = false;
|
jaroslav@1398
|
1876 |
if (k == null) {
|
jaroslav@1398
|
1877 |
for (int i = nkeys; --i >= 0;)
|
jaroslav@1398
|
1878 |
if (keys[i] == null)
|
jaroslav@1398
|
1879 |
if (foundV)
|
jaroslav@1398
|
1880 |
return values[i];
|
jaroslav@1398
|
1881 |
else if (values[i] == v)
|
jaroslav@1398
|
1882 |
foundV = true;
|
jaroslav@1398
|
1883 |
} else
|
jaroslav@1398
|
1884 |
for (int i = nkeys; --i >= 0;)
|
jaroslav@1398
|
1885 |
if (k.equalsIgnoreCase(keys[i]))
|
jaroslav@1398
|
1886 |
if (foundV)
|
jaroslav@1398
|
1887 |
return values[i];
|
jaroslav@1398
|
1888 |
else if (values[i] == v)
|
jaroslav@1398
|
1889 |
foundV = true;
|
jaroslav@1398
|
1890 |
return null;
|
jaroslav@1398
|
1891 |
}
|
jaroslav@1398
|
1892 |
|
jaroslav@1398
|
1893 |
class HeaderIterator implements Iterator<String> {
|
jaroslav@1398
|
1894 |
int index = 0;
|
jaroslav@1398
|
1895 |
int next = -1;
|
jaroslav@1398
|
1896 |
String key;
|
jaroslav@1398
|
1897 |
boolean haveNext = false;
|
jaroslav@1398
|
1898 |
Object lock;
|
jaroslav@1398
|
1899 |
|
jaroslav@1398
|
1900 |
public HeaderIterator (String k, Object lock) {
|
jaroslav@1398
|
1901 |
key = k;
|
jaroslav@1398
|
1902 |
this.lock = lock;
|
jaroslav@1398
|
1903 |
}
|
jaroslav@1398
|
1904 |
public boolean hasNext () {
|
jaroslav@1398
|
1905 |
synchronized (lock) {
|
jaroslav@1398
|
1906 |
if (haveNext) {
|
jaroslav@1398
|
1907 |
return true;
|
jaroslav@1398
|
1908 |
}
|
jaroslav@1398
|
1909 |
while (index < nkeys) {
|
jaroslav@1398
|
1910 |
if (key.equalsIgnoreCase (keys[index])) {
|
jaroslav@1398
|
1911 |
haveNext = true;
|
jaroslav@1398
|
1912 |
next = index++;
|
jaroslav@1398
|
1913 |
return true;
|
jaroslav@1398
|
1914 |
}
|
jaroslav@1398
|
1915 |
index ++;
|
jaroslav@1398
|
1916 |
}
|
jaroslav@1398
|
1917 |
return false;
|
jaroslav@1398
|
1918 |
}
|
jaroslav@1398
|
1919 |
}
|
jaroslav@1398
|
1920 |
public String next() {
|
jaroslav@1398
|
1921 |
synchronized (lock) {
|
jaroslav@1398
|
1922 |
if (haveNext) {
|
jaroslav@1398
|
1923 |
haveNext = false;
|
jaroslav@1398
|
1924 |
return values [next];
|
jaroslav@1398
|
1925 |
}
|
jaroslav@1398
|
1926 |
if (hasNext()) {
|
jaroslav@1398
|
1927 |
return next();
|
jaroslav@1398
|
1928 |
} else {
|
jaroslav@1398
|
1929 |
throw new NoSuchElementException ("No more elements");
|
jaroslav@1398
|
1930 |
}
|
jaroslav@1398
|
1931 |
}
|
jaroslav@1398
|
1932 |
}
|
jaroslav@1398
|
1933 |
public void remove () {
|
jaroslav@1398
|
1934 |
throw new UnsupportedOperationException ("remove not allowed");
|
jaroslav@1398
|
1935 |
}
|
jaroslav@1398
|
1936 |
}
|
jaroslav@1398
|
1937 |
|
jaroslav@1398
|
1938 |
/**
|
jaroslav@1398
|
1939 |
* return an Iterator that returns all values of a particular
|
jaroslav@1398
|
1940 |
* key in sequence
|
jaroslav@1398
|
1941 |
*/
|
jaroslav@1398
|
1942 |
public Iterator<String> multiValueIterator (String k) {
|
jaroslav@1398
|
1943 |
return new HeaderIterator (k, this);
|
jaroslav@1398
|
1944 |
}
|
jaroslav@1398
|
1945 |
|
jaroslav@1398
|
1946 |
public synchronized Map<String, List<String>> getHeaders() {
|
jaroslav@1398
|
1947 |
return getHeaders(null);
|
jaroslav@1398
|
1948 |
}
|
jaroslav@1398
|
1949 |
|
jaroslav@1398
|
1950 |
public synchronized Map<String, List<String>> getHeaders(String[] excludeList) {
|
jaroslav@1398
|
1951 |
return filterAndAddHeaders(excludeList, null);
|
jaroslav@1398
|
1952 |
}
|
jaroslav@1398
|
1953 |
|
jaroslav@1398
|
1954 |
public synchronized Map<String, List<String>> filterAndAddHeaders(String[] excludeList, Map<String, List<String>> include) {
|
jaroslav@1398
|
1955 |
boolean skipIt = false;
|
jaroslav@1398
|
1956 |
Map<String, List<String>> m = new HashMap<String, List<String>>();
|
jaroslav@1398
|
1957 |
for (int i = nkeys; --i >= 0;) {
|
jaroslav@1398
|
1958 |
if (excludeList != null) {
|
jaroslav@1398
|
1959 |
// check if the key is in the excludeList.
|
jaroslav@1398
|
1960 |
// if so, don't include it in the Map.
|
jaroslav@1398
|
1961 |
for (int j = 0; j < excludeList.length; j++) {
|
jaroslav@1398
|
1962 |
if ((excludeList[j] != null) &&
|
jaroslav@1398
|
1963 |
(excludeList[j].equalsIgnoreCase(keys[i]))) {
|
jaroslav@1398
|
1964 |
skipIt = true;
|
jaroslav@1398
|
1965 |
break;
|
jaroslav@1398
|
1966 |
}
|
jaroslav@1398
|
1967 |
}
|
jaroslav@1398
|
1968 |
}
|
jaroslav@1398
|
1969 |
if (!skipIt) {
|
jaroslav@1398
|
1970 |
List<String> l = m.get(keys[i]);
|
jaroslav@1398
|
1971 |
if (l == null) {
|
jaroslav@1398
|
1972 |
l = new ArrayList<String>();
|
jaroslav@1398
|
1973 |
m.put(keys[i], l);
|
jaroslav@1398
|
1974 |
}
|
jaroslav@1398
|
1975 |
l.add(values[i]);
|
jaroslav@1398
|
1976 |
} else {
|
jaroslav@1398
|
1977 |
// reset the flag
|
jaroslav@1398
|
1978 |
skipIt = false;
|
jaroslav@1398
|
1979 |
}
|
jaroslav@1398
|
1980 |
}
|
jaroslav@1398
|
1981 |
|
jaroslav@1398
|
1982 |
if (include != null) {
|
jaroslav@1398
|
1983 |
Iterator entries = include.entrySet().iterator();
|
jaroslav@1398
|
1984 |
while (entries.hasNext()) {
|
jaroslav@1398
|
1985 |
Map.Entry entry = (Map.Entry)entries.next();
|
jaroslav@1398
|
1986 |
List l = (List)m.get(entry.getKey());
|
jaroslav@1398
|
1987 |
if (l == null) {
|
jaroslav@1398
|
1988 |
l = new ArrayList();
|
jaroslav@1398
|
1989 |
m.put((String)entry.getKey(), l);
|
jaroslav@1398
|
1990 |
}
|
jaroslav@1398
|
1991 |
l.add(entry.getValue());
|
jaroslav@1398
|
1992 |
}
|
jaroslav@1398
|
1993 |
}
|
jaroslav@1398
|
1994 |
|
jaroslav@1398
|
1995 |
for (String key : m.keySet()) {
|
jaroslav@1398
|
1996 |
m.put(key, Collections.unmodifiableList(m.get(key)));
|
jaroslav@1398
|
1997 |
}
|
jaroslav@1398
|
1998 |
|
jaroslav@1398
|
1999 |
return Collections.unmodifiableMap(m);
|
jaroslav@1398
|
2000 |
}
|
jaroslav@1398
|
2001 |
|
jaroslav@1398
|
2002 |
/** Prints the key-value pairs represented by this
|
jaroslav@1398
|
2003 |
header. Also prints the RFC required blank line
|
jaroslav@1398
|
2004 |
at the end. Omits pairs with a null key. */
|
jaroslav@1398
|
2005 |
public synchronized void print(PrintStream p) {
|
jaroslav@1398
|
2006 |
for (int i = 0; i < nkeys; i++)
|
jaroslav@1398
|
2007 |
if (keys[i] != null) {
|
jaroslav@1398
|
2008 |
p.print(keys[i] +
|
jaroslav@1398
|
2009 |
(values[i] != null ? ": "+values[i]: "") + "\r\n");
|
jaroslav@1398
|
2010 |
}
|
jaroslav@1398
|
2011 |
p.print("\r\n");
|
jaroslav@1398
|
2012 |
p.flush();
|
jaroslav@1398
|
2013 |
}
|
jaroslav@1398
|
2014 |
|
jaroslav@1398
|
2015 |
/** Adds a key value pair to the end of the
|
jaroslav@1398
|
2016 |
header. Duplicates are allowed */
|
jaroslav@1398
|
2017 |
public synchronized void add(String k, String v) {
|
jaroslav@1398
|
2018 |
grow();
|
jaroslav@1398
|
2019 |
keys[nkeys] = k;
|
jaroslav@1398
|
2020 |
values[nkeys] = v;
|
jaroslav@1398
|
2021 |
nkeys++;
|
jaroslav@1398
|
2022 |
}
|
jaroslav@1398
|
2023 |
|
jaroslav@1398
|
2024 |
/** Prepends a key value pair to the beginning of the
|
jaroslav@1398
|
2025 |
header. Duplicates are allowed */
|
jaroslav@1398
|
2026 |
public synchronized void prepend(String k, String v) {
|
jaroslav@1398
|
2027 |
grow();
|
jaroslav@1398
|
2028 |
for (int i = nkeys; i > 0; i--) {
|
jaroslav@1398
|
2029 |
keys[i] = keys[i-1];
|
jaroslav@1398
|
2030 |
values[i] = values[i-1];
|
jaroslav@1398
|
2031 |
}
|
jaroslav@1398
|
2032 |
keys[0] = k;
|
jaroslav@1398
|
2033 |
values[0] = v;
|
jaroslav@1398
|
2034 |
nkeys++;
|
jaroslav@1398
|
2035 |
}
|
jaroslav@1398
|
2036 |
|
jaroslav@1398
|
2037 |
/** Overwrite the previous key/val pair at location 'i'
|
jaroslav@1398
|
2038 |
* with the new k/v. If the index didn't exist before
|
jaroslav@1398
|
2039 |
* the key/val is simply tacked onto the end.
|
jaroslav@1398
|
2040 |
*/
|
jaroslav@1398
|
2041 |
|
jaroslav@1398
|
2042 |
public synchronized void set(int i, String k, String v) {
|
jaroslav@1398
|
2043 |
grow();
|
jaroslav@1398
|
2044 |
if (i < 0) {
|
jaroslav@1398
|
2045 |
return;
|
jaroslav@1398
|
2046 |
} else if (i >= nkeys) {
|
jaroslav@1398
|
2047 |
add(k, v);
|
jaroslav@1398
|
2048 |
} else {
|
jaroslav@1398
|
2049 |
keys[i] = k;
|
jaroslav@1398
|
2050 |
values[i] = v;
|
jaroslav@1398
|
2051 |
}
|
jaroslav@1398
|
2052 |
}
|
jaroslav@1398
|
2053 |
|
jaroslav@1398
|
2054 |
|
jaroslav@1398
|
2055 |
/** grow the key/value arrays as needed */
|
jaroslav@1398
|
2056 |
|
jaroslav@1398
|
2057 |
private void grow() {
|
jaroslav@1398
|
2058 |
if (keys == null || nkeys >= keys.length) {
|
jaroslav@1398
|
2059 |
String[] nk = new String[nkeys + 4];
|
jaroslav@1398
|
2060 |
String[] nv = new String[nkeys + 4];
|
jaroslav@1398
|
2061 |
if (keys != null)
|
jaroslav@1398
|
2062 |
System.arraycopy(keys, 0, nk, 0, nkeys);
|
jaroslav@1398
|
2063 |
if (values != null)
|
jaroslav@1398
|
2064 |
System.arraycopy(values, 0, nv, 0, nkeys);
|
jaroslav@1398
|
2065 |
keys = nk;
|
jaroslav@1398
|
2066 |
values = nv;
|
jaroslav@1398
|
2067 |
}
|
jaroslav@1398
|
2068 |
}
|
jaroslav@1398
|
2069 |
|
jaroslav@1398
|
2070 |
/**
|
jaroslav@1398
|
2071 |
* Remove the key from the header. If there are multiple values under
|
jaroslav@1398
|
2072 |
* the same key, they are all removed.
|
jaroslav@1398
|
2073 |
* Nothing is done if the key doesn't exist.
|
jaroslav@1398
|
2074 |
* After a remove, the other pairs' order are not changed.
|
jaroslav@1398
|
2075 |
* @param k the key to remove
|
jaroslav@1398
|
2076 |
*/
|
jaroslav@1398
|
2077 |
public synchronized void remove(String k) {
|
jaroslav@1398
|
2078 |
if(k == null) {
|
jaroslav@1398
|
2079 |
for (int i = 0; i < nkeys; i++) {
|
jaroslav@1398
|
2080 |
while (keys[i] == null && i < nkeys) {
|
jaroslav@1398
|
2081 |
for(int j=i; j<nkeys-1; j++) {
|
jaroslav@1398
|
2082 |
keys[j] = keys[j+1];
|
jaroslav@1398
|
2083 |
values[j] = values[j+1];
|
jaroslav@1398
|
2084 |
}
|
jaroslav@1398
|
2085 |
nkeys--;
|
jaroslav@1398
|
2086 |
}
|
jaroslav@1398
|
2087 |
}
|
jaroslav@1398
|
2088 |
} else {
|
jaroslav@1398
|
2089 |
for (int i = 0; i < nkeys; i++) {
|
jaroslav@1398
|
2090 |
while (k.equalsIgnoreCase(keys[i]) && i < nkeys) {
|
jaroslav@1398
|
2091 |
for(int j=i; j<nkeys-1; j++) {
|
jaroslav@1398
|
2092 |
keys[j] = keys[j+1];
|
jaroslav@1398
|
2093 |
values[j] = values[j+1];
|
jaroslav@1398
|
2094 |
}
|
jaroslav@1398
|
2095 |
nkeys--;
|
jaroslav@1398
|
2096 |
}
|
jaroslav@1398
|
2097 |
}
|
jaroslav@1398
|
2098 |
}
|
jaroslav@1398
|
2099 |
}
|
jaroslav@1398
|
2100 |
|
jaroslav@1398
|
2101 |
/** Sets the value of a key. If the key already
|
jaroslav@1398
|
2102 |
exists in the header, it's value will be
|
jaroslav@1398
|
2103 |
changed. Otherwise a new key/value pair will
|
jaroslav@1398
|
2104 |
be added to the end of the header. */
|
jaroslav@1398
|
2105 |
public synchronized void set(String k, String v) {
|
jaroslav@1398
|
2106 |
for (int i = nkeys; --i >= 0;)
|
jaroslav@1398
|
2107 |
if (k.equalsIgnoreCase(keys[i])) {
|
jaroslav@1398
|
2108 |
values[i] = v;
|
jaroslav@1398
|
2109 |
return;
|
jaroslav@1398
|
2110 |
}
|
jaroslav@1398
|
2111 |
add(k, v);
|
jaroslav@1398
|
2112 |
}
|
jaroslav@1398
|
2113 |
|
jaroslav@1398
|
2114 |
/** Set's the value of a key only if there is no
|
jaroslav@1398
|
2115 |
* key with that value already.
|
jaroslav@1398
|
2116 |
*/
|
jaroslav@1398
|
2117 |
|
jaroslav@1398
|
2118 |
public synchronized void setIfNotSet(String k, String v) {
|
jaroslav@1398
|
2119 |
if (findValue(k) == null) {
|
jaroslav@1398
|
2120 |
add(k, v);
|
jaroslav@1398
|
2121 |
}
|
jaroslav@1398
|
2122 |
}
|
jaroslav@1398
|
2123 |
|
jaroslav@1398
|
2124 |
/** Convert a message-id string to canonical form (strips off
|
jaroslav@1398
|
2125 |
leading and trailing <>s) */
|
jaroslav@1398
|
2126 |
public static String canonicalID(String id) {
|
jaroslav@1398
|
2127 |
if (id == null)
|
jaroslav@1398
|
2128 |
return "";
|
jaroslav@1398
|
2129 |
int st = 0;
|
jaroslav@1398
|
2130 |
int len = id.length();
|
jaroslav@1398
|
2131 |
boolean substr = false;
|
jaroslav@1398
|
2132 |
int c;
|
jaroslav@1398
|
2133 |
while (st < len && ((c = id.charAt(st)) == '<' ||
|
jaroslav@1398
|
2134 |
c <= ' ')) {
|
jaroslav@1398
|
2135 |
st++;
|
jaroslav@1398
|
2136 |
substr = true;
|
jaroslav@1398
|
2137 |
}
|
jaroslav@1398
|
2138 |
while (st < len && ((c = id.charAt(len - 1)) == '>' ||
|
jaroslav@1398
|
2139 |
c <= ' ')) {
|
jaroslav@1398
|
2140 |
len--;
|
jaroslav@1398
|
2141 |
substr = true;
|
jaroslav@1398
|
2142 |
}
|
jaroslav@1398
|
2143 |
return substr ? id.substring(st, len) : id;
|
jaroslav@1398
|
2144 |
}
|
jaroslav@1398
|
2145 |
|
jaroslav@1398
|
2146 |
/** Parse a MIME header from an input stream. */
|
jaroslav@1398
|
2147 |
public void parseHeader(InputStream is) throws java.io.IOException {
|
jaroslav@1398
|
2148 |
synchronized (this) {
|
jaroslav@1398
|
2149 |
nkeys = 0;
|
jaroslav@1398
|
2150 |
}
|
jaroslav@1398
|
2151 |
mergeHeader(is);
|
jaroslav@1398
|
2152 |
}
|
jaroslav@1398
|
2153 |
|
jaroslav@1398
|
2154 |
/** Parse and merge a MIME header from an input stream. */
|
jaroslav@1398
|
2155 |
public void mergeHeader(InputStream is) throws java.io.IOException {
|
jaroslav@1398
|
2156 |
if (is == null)
|
jaroslav@1398
|
2157 |
return;
|
jaroslav@1398
|
2158 |
char s[] = new char[10];
|
jaroslav@1398
|
2159 |
int firstc = is.read();
|
jaroslav@1398
|
2160 |
while (firstc != '\n' && firstc != '\r' && firstc >= 0) {
|
jaroslav@1398
|
2161 |
int len = 0;
|
jaroslav@1398
|
2162 |
int keyend = -1;
|
jaroslav@1398
|
2163 |
int c;
|
jaroslav@1398
|
2164 |
boolean inKey = firstc > ' ';
|
jaroslav@1398
|
2165 |
s[len++] = (char) firstc;
|
jaroslav@1398
|
2166 |
parseloop:{
|
jaroslav@1398
|
2167 |
while ((c = is.read()) >= 0) {
|
jaroslav@1398
|
2168 |
switch (c) {
|
jaroslav@1398
|
2169 |
case ':':
|
jaroslav@1398
|
2170 |
if (inKey && len > 0)
|
jaroslav@1398
|
2171 |
keyend = len;
|
jaroslav@1398
|
2172 |
inKey = false;
|
jaroslav@1398
|
2173 |
break;
|
jaroslav@1398
|
2174 |
case '\t':
|
jaroslav@1398
|
2175 |
c = ' ';
|
jaroslav@1398
|
2176 |
case ' ':
|
jaroslav@1398
|
2177 |
inKey = false;
|
jaroslav@1398
|
2178 |
break;
|
jaroslav@1398
|
2179 |
case '\r':
|
jaroslav@1398
|
2180 |
case '\n':
|
jaroslav@1398
|
2181 |
firstc = is.read();
|
jaroslav@1398
|
2182 |
if (c == '\r' && firstc == '\n') {
|
jaroslav@1398
|
2183 |
firstc = is.read();
|
jaroslav@1398
|
2184 |
if (firstc == '\r')
|
jaroslav@1398
|
2185 |
firstc = is.read();
|
jaroslav@1398
|
2186 |
}
|
jaroslav@1398
|
2187 |
if (firstc == '\n' || firstc == '\r' || firstc > ' ')
|
jaroslav@1398
|
2188 |
break parseloop;
|
jaroslav@1398
|
2189 |
/* continuation */
|
jaroslav@1398
|
2190 |
c = ' ';
|
jaroslav@1398
|
2191 |
break;
|
jaroslav@1398
|
2192 |
}
|
jaroslav@1398
|
2193 |
if (len >= s.length) {
|
jaroslav@1398
|
2194 |
char ns[] = new char[s.length * 2];
|
jaroslav@1398
|
2195 |
System.arraycopy(s, 0, ns, 0, len);
|
jaroslav@1398
|
2196 |
s = ns;
|
jaroslav@1398
|
2197 |
}
|
jaroslav@1398
|
2198 |
s[len++] = (char) c;
|
jaroslav@1398
|
2199 |
}
|
jaroslav@1398
|
2200 |
firstc = -1;
|
jaroslav@1398
|
2201 |
}
|
jaroslav@1398
|
2202 |
while (len > 0 && s[len - 1] <= ' ')
|
jaroslav@1398
|
2203 |
len--;
|
jaroslav@1398
|
2204 |
String k;
|
jaroslav@1398
|
2205 |
if (keyend <= 0) {
|
jaroslav@1398
|
2206 |
k = null;
|
jaroslav@1398
|
2207 |
keyend = 0;
|
jaroslav@1398
|
2208 |
} else {
|
jaroslav@1398
|
2209 |
k = String.copyValueOf(s, 0, keyend);
|
jaroslav@1398
|
2210 |
if (keyend < len && s[keyend] == ':')
|
jaroslav@1398
|
2211 |
keyend++;
|
jaroslav@1398
|
2212 |
while (keyend < len && s[keyend] <= ' ')
|
jaroslav@1398
|
2213 |
keyend++;
|
jaroslav@1398
|
2214 |
}
|
jaroslav@1398
|
2215 |
String v;
|
jaroslav@1398
|
2216 |
if (keyend >= len)
|
jaroslav@1398
|
2217 |
v = new String();
|
jaroslav@1398
|
2218 |
else
|
jaroslav@1398
|
2219 |
v = String.copyValueOf(s, keyend, len - keyend);
|
jaroslav@1398
|
2220 |
add(k, v);
|
jaroslav@1398
|
2221 |
}
|
jaroslav@1398
|
2222 |
}
|
jaroslav@1398
|
2223 |
|
jaroslav@1398
|
2224 |
public synchronized String toString() {
|
jaroslav@1398
|
2225 |
String result = super.toString() + nkeys + " pairs: ";
|
jaroslav@1398
|
2226 |
for (int i = 0; i < keys.length && i < nkeys; i++) {
|
jaroslav@1398
|
2227 |
result += "{"+keys[i]+": "+values[i]+"}";
|
jaroslav@1398
|
2228 |
}
|
jaroslav@1398
|
2229 |
return result;
|
jaroslav@1398
|
2230 |
}
|
jaroslav@1398
|
2231 |
}
|
jaroslav@1398
|
2232 |
|
jaroslav@1398
|
2233 |
interface ContentHandlerFactory {
|
jaroslav@1398
|
2234 |
|
jaroslav@1398
|
2235 |
public ContentHandler createContentHandler(String contentType);
|
jaroslav@1398
|
2236 |
}
|
jaroslav@1398
|
2237 |
|
jaroslav@1398
|
2238 |
abstract class ContentHandler {
|
jaroslav@1398
|
2239 |
/**
|
jaroslav@1398
|
2240 |
* Given a URL connect stream positioned at the beginning of the
|
jaroslav@1398
|
2241 |
* representation of an object, this method reads that stream and
|
jaroslav@1398
|
2242 |
* creates an object from it.
|
jaroslav@1398
|
2243 |
*
|
jaroslav@1398
|
2244 |
* @param urlc a URL connection.
|
jaroslav@1398
|
2245 |
* @return the object read by the <code>ContentHandler</code>.
|
jaroslav@1398
|
2246 |
* @exception IOException if an I/O error occurs while reading the object.
|
jaroslav@1398
|
2247 |
*/
|
jaroslav@1398
|
2248 |
abstract public Object getContent(URLConnection urlc) throws IOException;
|
jaroslav@1398
|
2249 |
|
jaroslav@1398
|
2250 |
/**
|
jaroslav@1398
|
2251 |
* Given a URL connect stream positioned at the beginning of the
|
jaroslav@1398
|
2252 |
* representation of an object, this method reads that stream and
|
jaroslav@1398
|
2253 |
* creates an object that matches one of the types specified.
|
jaroslav@1398
|
2254 |
*
|
jaroslav@1398
|
2255 |
* The default implementation of this method should call getContent()
|
jaroslav@1398
|
2256 |
* and screen the return type for a match of the suggested types.
|
jaroslav@1398
|
2257 |
*
|
jaroslav@1398
|
2258 |
* @param urlc a URL connection.
|
jaroslav@1398
|
2259 |
* @param classes an array of types requested
|
jaroslav@1398
|
2260 |
* @return the object read by the <code>ContentHandler</code> that is
|
jaroslav@1398
|
2261 |
* the first match of the suggested types.
|
jaroslav@1398
|
2262 |
* null if none of the requested are supported.
|
jaroslav@1398
|
2263 |
* @exception IOException if an I/O error occurs while reading the object.
|
jaroslav@1398
|
2264 |
* @since 1.3
|
jaroslav@1398
|
2265 |
*/
|
jaroslav@1398
|
2266 |
public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
|
jaroslav@1398
|
2267 |
Object obj = getContent(urlc);
|
jaroslav@1398
|
2268 |
|
jaroslav@1398
|
2269 |
for (int i = 0; i < classes.length; i++) {
|
jaroslav@1398
|
2270 |
if (classes[i].isInstance(obj)) {
|
jaroslav@1398
|
2271 |
return obj;
|
jaroslav@1398
|
2272 |
}
|
jaroslav@1398
|
2273 |
}
|
jaroslav@1398
|
2274 |
return null;
|
jaroslav@1398
|
2275 |
}
|
jaroslav@1398
|
2276 |
|
jaroslav@1398
|
2277 |
}
|
jaroslav@1398
|
2278 |
class UnknownServiceException extends IOException {
|
jaroslav@1398
|
2279 |
private static final long serialVersionUID = -4169033248853639508L;
|
jaroslav@1398
|
2280 |
|
jaroslav@1398
|
2281 |
/**
|
jaroslav@1398
|
2282 |
* Constructs a new <code>UnknownServiceException</code> with no
|
jaroslav@1398
|
2283 |
* detail message.
|
jaroslav@1398
|
2284 |
*/
|
jaroslav@1398
|
2285 |
public UnknownServiceException() {
|
jaroslav@1398
|
2286 |
}
|
jaroslav@1398
|
2287 |
|
jaroslav@1398
|
2288 |
/**
|
jaroslav@1398
|
2289 |
* Constructs a new <code>UnknownServiceException</code> with the
|
jaroslav@1398
|
2290 |
* specified detail message.
|
jaroslav@1398
|
2291 |
*
|
jaroslav@1398
|
2292 |
* @param msg the detail message.
|
jaroslav@1398
|
2293 |
*/
|
jaroslav@1398
|
2294 |
public UnknownServiceException(String msg) {
|
jaroslav@1398
|
2295 |
super(msg);
|
jaroslav@1398
|
2296 |
}
|
jaroslav@1398
|
2297 |
}
|
jaroslav@1398
|
2298 |
/**
|
jaroslav@1398
|
2299 |
* A simple interface which provides a mechanism to map
|
jaroslav@1398
|
2300 |
* between a file name and a MIME type string.
|
jaroslav@1398
|
2301 |
*
|
jaroslav@1398
|
2302 |
* @author Steven B. Byrne
|
jaroslav@1398
|
2303 |
* @since JDK1.1
|
jaroslav@1398
|
2304 |
*/
|
jaroslav@1398
|
2305 |
interface FileNameMap {
|
jaroslav@1398
|
2306 |
|
jaroslav@1398
|
2307 |
/**
|
jaroslav@1398
|
2308 |
* Gets the MIME type for the specified file name.
|
jaroslav@1398
|
2309 |
* @param fileName the specified file name
|
jaroslav@1398
|
2310 |
* @return a <code>String</code> indicating the MIME
|
jaroslav@1398
|
2311 |
* type for the specified file name.
|
jaroslav@1398
|
2312 |
*/
|
jaroslav@1398
|
2313 |
public String getContentTypeFor(String fileName);
|
jaroslav@1398
|
2314 |
}
|