1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package sun.net.www.protocol.jar;

import java.io.InputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import java.util.Enumeration;
import java.util.Map;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.security.Permission;

/**
 * @author Benjamin Renaud
 * @since 1.2
 */
public class JarURLConnection extends java.net.JarURLConnection {

    private static final boolean debug = false;

    /* the Jar file factory. It handles both retrieval and caching. 
     */
    private static JarFileFactory factory = new JarFileFactory();

    /* the url for the Jar file */
    private URL jarFileURL;

    /* the permission to get this JAR file. This is the actual, ultimate,
     * permission, returned by the jar file factory. 
     */
    private Permission permission;

    /* the url connection for the JAR file */
    private URLConnection jarFileURLConnection;

    /* the entry name, if any */
    private String entryName;

    /* the JarEntry */
    private JarEntry jarEntry;

    /* the jar file corresponding to this connection */
    private JarFile jarFile;

    /* the content type for this connection */
    private String contentType;

    public JarURLConnection(URL url, Handler handler) 
    throws MalformedURLException, IOException {
    super(url);

    jarFileURL = getJarFileURL();
    jarFileURLConnection = jarFileURL.openConnection();
    entryName = getEntryName();
    }

    public JarFile getJarFile() throws IOException {
    connect();
    return jarFile;
    }

    public JarEntry getJarEntry() throws IOException {
    connect();
    return jarEntry;
    }
    
    public Permission getPermission() throws IOException {
    return jarFileURLConnection.getPermission();
    }

    class JarURLInputStream extends java.io.FilterInputStream {
    JarURLInputStream (InputStream src) {
        super (src);
    }
    public void close () throws IOException {
        try {
            super.close();
        } finally {
        if (!getUseCaches()) {
            jarFile.close();
        }
        }
    }
    }
        
        
    
    public void connect() throws IOException {
    if (!connected) {
        /* the factory call will do the security checks */
        jarFile = factory.get(getJarFileURL(), getUseCaches());
        
        /* we also ask the factory the permission that was required
         * to get the jarFile, and set it as our permission.
         */
        if (getUseCaches()) {
            jarFileURLConnection = factory.getConnection(jarFile);
        } 
        
        if ((entryName != null)) {
            jarEntry = (JarEntry)jarFile.getEntry(entryName);
        if (jarEntry == null) {
                    try {
            if (!getUseCaches()) {
                            jarFile.close();
            }
                    } catch (Exception e) {
                    }
            throw new FileNotFoundException("JAR entry " + entryName + 
                            " not found in " + 
                            jarFile.getName());
        }
        }
        connected = true;
    }       
    }

    public InputStream getInputStream() throws IOException {
    connect();

    InputStream result = null;

    if (entryName == null) {
        throw new IOException("no entry name specified");
    } else {
        if (jarEntry == null) {
        throw new FileNotFoundException("JAR entry " + entryName + 
                        " not found in " + 
                        jarFile.getName());
        }
        result = new JarURLInputStream (jarFile.getInputStream(jarEntry));
    }
    return result;
    }

    public int getContentLength() {
    int result = -1;
    try {
        connect();
        if (jarEntry == null) {
        /* if the URL referes to an archive */
        result = jarFileURLConnection.getContentLength();
        } else {
        /* if the URL referes to an archive entry */
        result = (int)getJarEntry().getSize();
        }
    } catch (IOException e) {
    }
    return result;
    }

    public Object getContent() throws IOException {
    Object result = null;
    
    connect();
    if (entryName == null) {
        result = jarFile;
    } else {
        result = super.getContent();
    }
    return result;
    }

    public String getContentType() {
    if (contentType == null) {
        if (entryName == null) {
        contentType = "x-java/jar";
        } else {
        try {
            connect();
            InputStream in = jarFile.getInputStream(jarEntry);
            contentType = guessContentTypeFromStream(
                    new BufferedInputStream(in));
            in.close();
        } catch (IOException e) {
            // don't do anything
        }
        }
        if (contentType == null) {
        contentType = guessContentTypeFromName(entryName);
        }
        if (contentType == null) {
        contentType = "content/unknown";
        }
    }
    return contentType;
    }

    public String getHeaderField(String name) {
    return jarFileURLConnection.getHeaderField(name);
    }

    /**
     * Sets the general request property. 
     *
     * @param   key     the keyword by which the request is known
     *                  (e.g., "<code>accept</code>").
     * @param   value   the value associated with it.
     */
    public void setRequestProperty(String key, String value) {
    jarFileURLConnection.setRequestProperty(key, value);
    }

    /**
     * Returns the value of the named general request property for this
     * connection.
     *
     * @return  the value of the named general request property for this
     *           connection.
     */
    public String getRequestProperty(String key) {
    return jarFileURLConnection.getRequestProperty(key);
    }

    /**
     * Adds a general request property specified by a
     * key-value pair.  This method will not overwrite
     * existing values associated with the same key.
     *
     * @param   key     the keyword by which the request is known
     *                  (e.g., "<code>accept</code>").
     * @param   value   the value associated with it.
     */
    public void addRequestProperty(String key, String value) {
        jarFileURLConnection.addRequestProperty(key, value);
    }

    /**
     * Returns an unmodifiable Map of general request
     * properties for this connection. The Map keys
     * are Strings that represent the request-header
     * field names. Each Map value is a unmodifiable List
     * of Strings that represents the corresponding
     * field values.
     *
     * @return  a Map of the general request properties for this connection.
     */
    public Map<String,List<String>> getRequestProperties() {
    return jarFileURLConnection.getRequestProperties();
    }

    /**
     * Set the value of the <code>allowUserInteraction</code> field of 
     * this <code>URLConnection</code>. 
     *
     * @param   allowuserinteraction   the new value.
     * @see     java.net.URLConnection#allowUserInteraction
     */
    public void setAllowUserInteraction(boolean allowuserinteraction) {
    jarFileURLConnection.setAllowUserInteraction(allowuserinteraction);
    }

    /**
     * Returns the value of the <code>allowUserInteraction</code> field for
     * this object.
     *
     * @return  the value of the <code>allowUserInteraction</code> field for
     *          this object.
     * @see     java.net.URLConnection#allowUserInteraction
     */
    public boolean getAllowUserInteraction() {
    return jarFileURLConnection.getAllowUserInteraction();
    }
    
    /* 
     * cache control 
     */

    /**
     * Sets the value of the <code>useCaches</code> field of this 
     * <code>URLConnection</code> to the specified value. 
     * <p>
     * Some protocols do caching of documents.  Occasionally, it is important
     * to be able to "tunnel through" and ignore the caches (e.g., the
     * "reload" button in a browser).  If the UseCaches flag on a connection
     * is true, the connection is allowed to use whatever caches it can.
     *  If false, caches are to be ignored.
     *  The default value comes from DefaultUseCaches, which defaults to
     * true.
     *
     * @see     java.net.URLConnection#useCaches
     */
    public void setUseCaches(boolean usecaches) {
    jarFileURLConnection.setUseCaches(usecaches);
    }

    /**
     * Returns the value of this <code>URLConnection</code>'s
     * <code>useCaches</code> field.
     *
     * @return  the value of this <code>URLConnection</code>'s
     *          <code>useCaches</code> field.
     * @see     java.net.URLConnection#useCaches
     */
    public boolean getUseCaches() {
    return jarFileURLConnection.getUseCaches();
    }

    /**
     * Sets the value of the <code>ifModifiedSince</code> field of 
     * this <code>URLConnection</code> to the specified value.
     *
     * @param   value   the new value.
     * @see     java.net.URLConnection#ifModifiedSince
     */
    public void setIfModifiedSince(long ifmodifiedsince) {
    jarFileURLConnection.setIfModifiedSince(ifmodifiedsince);
    }

   /**
     * Sets the default value of the <code>useCaches</code> field to the 
     * specified value. 
     *
     * @param   defaultusecaches   the new value.
     * @see     java.net.URLConnection#useCaches
     */
    public void setDefaultUseCaches(boolean defaultusecaches) {
    jarFileURLConnection.setDefaultUseCaches(defaultusecaches);
    }

   /**
     * Returns the default value of a <code>URLConnection</code>'s
     * <code>useCaches</code> flag.
     * <p>
     * Ths default is "sticky", being a part of the static state of all
     * URLConnections.  This flag applies to the next, and all following
     * URLConnections that are created.
     *
     * @return  the default value of a <code>URLConnection</code>'s
     *          <code>useCaches</code> flag.
     * @see     java.net.URLConnection#useCaches
     */
    public boolean getDefaultUseCaches() {
    return jarFileURLConnection.getDefaultUseCaches();
    }
}
			
			

Browsed Source: [clear]