1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www.protocol.jrt;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FilePermission;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.MalformedURLException;
34 import java.net.URL;
35 import java.security.AccessController;
36 import java.security.Permission;
37 import java.security.PrivilegedAction;
38
39 import jdk.internal.jimage.ImageLocation;
40 import jdk.internal.jimage.ImageReader;
41 import jdk.internal.jimage.ImageReaderFactory;
42
43 import jdk.internal.loader.URLClassPath;
44 import jdk.internal.loader.Resource;
45 import sun.net.www.ParseUtil;
46 import sun.net.www.URLConnection;
47 import sun.security.action.GetPropertyAction;
48
49 /**
50 * URLConnection implementation that can be used to connect to resources
51 * contained in the runtime image.
52 */
53 public class JavaRuntimeURLConnection extends URLConnection {
54
55 // ImageReader to access resources in jimage
56 private static final ImageReader reader;
57 static {
58 PrivilegedAction<ImageReader> pa = ImageReaderFactory::getImageReader;
59 reader = AccessController.doPrivileged(pa);
60 }
61
62 // the module and resource name in the URL
63 private final String module;
64 private final String name;
65
66 // the Resource when connected
67 private volatile Resource resource;
68
69 // the permission to access resources in the runtime image, created lazily
70 private static volatile Permission permission;
71
72 JavaRuntimeURLConnection(URL url) throws IOException {
73 super(url);
74 String path = url.getPath();
75 if (path.length() == 0 || path.charAt(0) != '/')
76 throw new MalformedURLException(url + " missing path or /");
77 if (path.length() == 1) {
78 this.module = null;
79 this.name = null;
80 } else {
81 int pos = path.indexOf('/', 1);
82 if (pos == -1) {
83 this.module = path.substring(1);
84 this.name = null;
85 } else {
86 this.module = path.substring(1, pos);
87 this.name = ParseUtil.decode(path.substring(pos+1));
88 }
89 }
90 }
91
147 return resource.getInputStream();
148 }
149
150 @Override
151 public long getContentLengthLong() {
152 try {
153 connect();
154 return resource.getContentLength();
155 } catch (IOException ioe) {
156 return -1L;
157 }
158 }
159
160 @Override
161 public int getContentLength() {
162 long len = getContentLengthLong();
163 return len > Integer.MAX_VALUE ? -1 : (int)len;
164 }
165
166 @Override
167 public Permission getPermission() throws IOException {
168 Permission p = permission;
169 if (p == null) {
170 String home = GetPropertyAction.privilegedGetProperty("java.home");
171 p = new FilePermission(home + File.separator + "-", "read");
172 permission = p;
173 }
174 return p;
175 }
176
177 /**
178 * Returns a jrt URL for the given module and resource name.
179 */
180 private static URL toJrtURL(String module, String name) {
181 try {
182 return new URL("jrt:/" + module + "/" + name);
183 } catch (MalformedURLException e) {
184 throw new InternalError(e);
185 }
186 }
187
188 /**
189 * Returns a jrt URL for the given module.
190 */
191 private static URL toJrtURL(String module) {
192 try {
193 return new URL("jrt:/" + module);
194 } catch (MalformedURLException e) {
|
1 /*
2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www.protocol.jrt;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.security.AccessController;
34 import java.security.Permission;
35 import java.security.PrivilegedAction;
36
37 import jdk.internal.jimage.ImageLocation;
38 import jdk.internal.jimage.ImageReader;
39 import jdk.internal.jimage.ImageReaderFactory;
40
41 import jdk.internal.loader.URLClassPath;
42 import jdk.internal.loader.Resource;
43 import sun.net.www.ParseUtil;
44 import sun.net.www.URLConnection;
45
46 /**
47 * URLConnection implementation that can be used to connect to resources
48 * contained in the runtime image.
49 */
50 public class JavaRuntimeURLConnection extends URLConnection {
51
52 // ImageReader to access resources in jimage
53 private static final ImageReader reader;
54 static {
55 PrivilegedAction<ImageReader> pa = ImageReaderFactory::getImageReader;
56 reader = AccessController.doPrivileged(pa);
57 }
58
59 // the module and resource name in the URL
60 private final String module;
61 private final String name;
62
63 // the Resource when connected
64 private volatile Resource resource;
65
66 JavaRuntimeURLConnection(URL url) throws IOException {
67 super(url);
68 String path = url.getPath();
69 if (path.length() == 0 || path.charAt(0) != '/')
70 throw new MalformedURLException(url + " missing path or /");
71 if (path.length() == 1) {
72 this.module = null;
73 this.name = null;
74 } else {
75 int pos = path.indexOf('/', 1);
76 if (pos == -1) {
77 this.module = path.substring(1);
78 this.name = null;
79 } else {
80 this.module = path.substring(1, pos);
81 this.name = ParseUtil.decode(path.substring(pos+1));
82 }
83 }
84 }
85
141 return resource.getInputStream();
142 }
143
144 @Override
145 public long getContentLengthLong() {
146 try {
147 connect();
148 return resource.getContentLength();
149 } catch (IOException ioe) {
150 return -1L;
151 }
152 }
153
154 @Override
155 public int getContentLength() {
156 long len = getContentLengthLong();
157 return len > Integer.MAX_VALUE ? -1 : (int)len;
158 }
159
160 @Override
161 public Permission getPermission() {
162 return new RuntimePermission("accessSystemModules");
163 }
164
165 /**
166 * Returns a jrt URL for the given module and resource name.
167 */
168 private static URL toJrtURL(String module, String name) {
169 try {
170 return new URL("jrt:/" + module + "/" + name);
171 } catch (MalformedURLException e) {
172 throw new InternalError(e);
173 }
174 }
175
176 /**
177 * Returns a jrt URL for the given module.
178 */
179 private static URL toJrtURL(String module) {
180 try {
181 return new URL("jrt:/" + module);
182 } catch (MalformedURLException e) {
|