Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e79a6f8b84efd5b7f6196b1be492dc2c79fb0af9 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.storage.url;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.eclipse.osgi.internal.messages.Msg;
import org.eclipse.osgi.storage.bundlefile.BundleEntry;
import org.eclipse.osgi.util.NLS;

/**
 * URLConnection for BundleClassLoader resources.
 */

public class BundleURLConnection extends URLConnection {
	/** BundleEntry that the URL is associated. */
	protected final BundleEntry bundleEntry;

	/** InputStream for this URLConnection. */
	protected InputStream in;

	/** content type for this URLConnection */
	protected String contentType;

	/**
	 * Constructor for a BundleClassLoader resource URLConnection.
	 *
	 * @param url  URL for this URLConnection.
	 * @param bundleEntry  BundleEntry that the URLConnection is associated.
	 */
	public BundleURLConnection(URL url, BundleEntry bundleEntry) {
		super(url);

		this.bundleEntry = bundleEntry;
		this.in = null;
		this.contentType = null;
	}

	public synchronized void connect() throws IOException {
		if (!connected) {
			if (bundleEntry != null) {
				in = bundleEntry.getInputStream();
				connected = true;
			} else {
				throw new IOException(NLS.bind(Msg.RESOURCE_NOT_FOUND_EXCEPTION, url));
			}
		}
	}

	public int getContentLength() {
		return ((int) bundleEntry.getSize());
	}

	public String getContentType() {
		if (contentType == null) {
			contentType = guessContentTypeFromName(bundleEntry.getName());

			if (contentType == null) {
				if (!connected) {
					try {
						connect();
					} catch (IOException e) {
						return (null);
					}
				}
				try {
					if (in.markSupported())
						contentType = guessContentTypeFromStream(in);
				} catch (IOException e) {
					// do nothing
				}
			}
		}

		return (contentType);
	}

	public boolean getDoInput() {
		return (true);
	}

	public boolean getDoOutput() {
		return (false);
	}

	public InputStream getInputStream() throws IOException {
		if (!connected) {
			connect();
		}

		return (in);
	}

	public long getLastModified() {
		long lastModified = bundleEntry.getTime();

		if (lastModified == -1) {
			return (0);
		}

		return (lastModified);
	}

	/**
	 * Converts the URL to a common local URL protocol (i.e file: or jar: protocol)
	 * @return the local URL using a common local protocol
	 */
	public URL getLocalURL() {
		return bundleEntry.getLocalURL();
	}

	/**
	 * Converts the URL to a URL that uses the file: protocol.  The content of this
	 * URL may be downloaded or extracted onto the local filesystem to create a file URL.
	 * @return the local URL that uses the file: protocol
	 */
	public URL getFileURL() {
		return bundleEntry.getFileURL();
	}
}

Back to the top