0N/A/*
4019N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.net.www.protocol.jar;
0N/A
4019N/Aimport java.io.IOException;
0N/Aimport java.net.*;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A/*
0N/A * Jar URL Handler
0N/A */
0N/Apublic class Handler extends java.net.URLStreamHandler {
0N/A
0N/A private static final String separator = "!/";
0N/A
0N/A protected java.net.URLConnection openConnection(URL u)
0N/A throws IOException {
0N/A return new JarURLConnection(u, this);
0N/A }
0N/A
4019N/A private static int indexOfBangSlash(String spec) {
0N/A int indexOfBang = spec.length();
0N/A while((indexOfBang = spec.lastIndexOf('!', indexOfBang)) != -1) {
0N/A if ((indexOfBang != (spec.length() - 1)) &&
0N/A (spec.charAt(indexOfBang + 1) == '/')) {
0N/A return indexOfBang + 1;
0N/A } else {
0N/A indexOfBang--;
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A
4019N/A /**
4019N/A * Compare two jar URLs
4019N/A */
4019N/A @Override
4019N/A protected boolean sameFile(URL u1, URL u2) {
4019N/A if (!u1.getProtocol().equals("jar") || !u2.getProtocol().equals("jar"))
4019N/A return false;
4019N/A
4019N/A String file1 = u1.getFile();
4019N/A String file2 = u2.getFile();
4019N/A int sep1 = file1.indexOf(separator);
4019N/A int sep2 = file2.indexOf(separator);
4019N/A
4019N/A if (sep1 == -1 || sep2 == -1) {
4019N/A return super.sameFile(u1, u2);
4019N/A }
4019N/A
4019N/A String entry1 = file1.substring(sep1 + 2);
4019N/A String entry2 = file2.substring(sep2 + 2);
4019N/A
4019N/A if (!entry1.equals(entry2))
4019N/A return false;
4019N/A
4019N/A URL enclosedURL1 = null, enclosedURL2 = null;
4019N/A try {
4019N/A enclosedURL1 = new URL(file1.substring(0, sep1));
4019N/A enclosedURL2 = new URL(file2.substring(0, sep2));
4019N/A } catch (MalformedURLException unused) {
4019N/A return super.sameFile(u1, u2);
4019N/A }
4019N/A
4019N/A if (!super.sameFile(enclosedURL1, enclosedURL2)) {
4019N/A return false;
4019N/A }
4019N/A
4019N/A return true;
4019N/A }
4019N/A
4019N/A @Override
4019N/A protected int hashCode(URL u) {
4019N/A int h = 0;
4019N/A
4019N/A String protocol = u.getProtocol();
4019N/A if (protocol != null)
4019N/A h += protocol.hashCode();
4019N/A
4019N/A String file = u.getFile();
4019N/A int sep = file.indexOf(separator);
4019N/A
4019N/A if (sep == -1)
4019N/A return h + file.hashCode();
4019N/A
4019N/A URL enclosedURL = null;
4019N/A String fileWithoutEntry = file.substring(0, sep);
4019N/A try {
4019N/A enclosedURL = new URL(fileWithoutEntry);
4019N/A h += enclosedURL.hashCode();
4019N/A } catch (MalformedURLException unused) {
4019N/A h += fileWithoutEntry.hashCode();
4019N/A }
4019N/A
4019N/A String entry = file.substring(sep + 2);
4019N/A h += entry.hashCode();
4019N/A
4019N/A return h;
4019N/A }
4019N/A
4019N/A
4019N/A @Override
0N/A protected void parseURL(URL url, String spec,
0N/A int start, int limit) {
0N/A String file = null;
0N/A String ref = null;
0N/A // first figure out if there is an anchor
0N/A int refPos = spec.indexOf('#', limit);
0N/A boolean refOnly = refPos == start;
0N/A if (refPos > -1) {
0N/A ref = spec.substring(refPos + 1, spec.length());
0N/A if (refOnly) {
0N/A file = url.getFile();
0N/A }
0N/A }
0N/A // then figure out if the spec is
0N/A // 1. absolute (jar:)
0N/A // 2. relative (i.e. url + foo/bar/baz.ext)
0N/A // 3. anchor-only (i.e. url + #foo), which we already did (refOnly)
0N/A boolean absoluteSpec = false;
0N/A if (spec.length() >= 4) {
0N/A absoluteSpec = spec.substring(0, 4).equalsIgnoreCase("jar:");
0N/A }
0N/A spec = spec.substring(start, limit);
0N/A
0N/A if (absoluteSpec) {
0N/A file = parseAbsoluteSpec(spec);
0N/A } else if (!refOnly) {
0N/A file = parseContextSpec(url, spec);
0N/A
0N/A // Canonize the result after the bangslash
0N/A int bangSlash = indexOfBangSlash(file);
0N/A String toBangSlash = file.substring(0, bangSlash);
0N/A String afterBangSlash = file.substring(bangSlash);
0N/A sun.net.www.ParseUtil canonizer = new ParseUtil();
0N/A afterBangSlash = canonizer.canonizeString(afterBangSlash);
0N/A file = toBangSlash + afterBangSlash;
0N/A }
0N/A setURL(url, "jar", "", -1, file, ref);
0N/A }
0N/A
0N/A private String parseAbsoluteSpec(String spec) {
0N/A URL url = null;
0N/A int index = -1;
0N/A // check for !/
0N/A if ((index = indexOfBangSlash(spec)) == -1) {
0N/A throw new NullPointerException("no !/ in spec");
0N/A }
0N/A // test the inner URL
0N/A try {
0N/A String innerSpec = spec.substring(0, index - 1);
0N/A url = new URL(innerSpec);
0N/A } catch (MalformedURLException e) {
0N/A throw new NullPointerException("invalid url: " +
0N/A spec + " (" + e + ")");
0N/A }
0N/A return spec;
0N/A }
0N/A
0N/A private String parseContextSpec(URL url, String spec) {
0N/A String ctxFile = url.getFile();
0N/A // if the spec begins with /, chop up the jar back !/
0N/A if (spec.startsWith("/")) {
0N/A int bangSlash = indexOfBangSlash(ctxFile);
0N/A if (bangSlash == -1) {
0N/A throw new NullPointerException("malformed " +
0N/A "context url:" +
0N/A url +
0N/A ": no !/");
0N/A }
0N/A ctxFile = ctxFile.substring(0, bangSlash);
0N/A }
0N/A if (!ctxFile.endsWith("/") && (!spec.startsWith("/"))){
0N/A // chop up the last component
0N/A int lastSlash = ctxFile.lastIndexOf('/');
0N/A if (lastSlash == -1) {
0N/A throw new NullPointerException("malformed " +
0N/A "context url:" +
0N/A url);
0N/A }
0N/A ctxFile = ctxFile.substring(0, lastSlash + 1);
0N/A }
0N/A return (ctxFile + spec);
0N/A }
0N/A}