OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.protocol.jarjar;
15
 
16
/*******************************************************************************
17
 * Copyright (c) 2009 Pavel Savara as part of Robocode project All rights reserved. This program and
18
 * the accompanying materials are made available under the terms of the Common Public License v1.0
19
 * which accompanies this distribution, and is available at
20
 * http://robocode.sourceforge.net/license/cpl-v10.html
21
 *
22
 * Contributors: Pavel Savara - JarJarURLStreamHandler is just tweaked version of jar handler from
23
 * OpenJDK, license below
24
 *******************************************************************************/
25
 
26
/*
27
 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. DO NOT ALTER OR REMOVE COPYRIGHT
28
 * NOTICES OR THIS FILE HEADER.
29
 *
30
 * This code is free software; you can redistribute it and/or modify it under the terms of the GNU
31
 * General Public License version 2 only, as published by the Free Software Foundation. Sun
32
 * designates this particular file as subject to the "Classpath" exception as provided by Sun in the
33
 * LICENSE file that accompanied this code.
34
 *
35
 * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
36
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37
 * General Public License version 2 for more details (a copy is included in the LICENSE file that
38
 * accompanied this code).
39
 *
40
 * You should have received a copy of the GNU General Public License version 2 along with this work;
41
 * if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
42
 * 02110-1301 USA.
43
 *
44
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, CA 95054 USA or visit
45
 * www.sun.com if you need additional information or have any questions.
46
 */
47
 
48
import java.io.IOException;
49
import java.io.InputStream;
50
import java.net.MalformedURLException;
51
import java.net.URL;
52
import java.net.URLConnection;
53
import java.net.URLStreamHandler;
54
 
55
public class Handler extends URLStreamHandler {
56
 
57
    protected URLConnection openConnection(URL u) throws IOException {
58
        return new JarJarURLConnection(u);
59
    }
60
 
61
    private int indexOfBangSlash(String spec) {
62
        int indexOfBang = spec.length();
63
 
64
        while ((indexOfBang = spec.lastIndexOf(JarJarURLConnection.SEPARATOR_CHAR, indexOfBang)) != -1) {
65
            if ((indexOfBang != (spec.length() - 1)) && (spec.charAt(indexOfBang + 1) == '/')) {
66
                return indexOfBang + 1;
67
            } else {
68
                indexOfBang--;
69
            }
70
        }
71
        return -1;
72
    }
73
 
180 ilm 74
    @SuppressWarnings({ "deprecation" })
17 ilm 75
    protected void parseURL(URL url, String spec, int start, int limit) {
76
        String file = null;
77
        String ref = null;
78
        // first figure out if there is an anchor
79
        int refPos = spec.indexOf('#', limit);
80
        boolean refOnly = refPos == start;
81
 
82
        if (refPos > -1) {
83
            ref = spec.substring(refPos + 1, spec.length());
84
            if (refOnly) {
85
                file = url.getFile();
86
            }
87
        }
88
        // then figure out if the spec is
89
        // 1. absolute (jarjar:)
90
        // 2. relative (i.e. url + foo/bar/baz.ext)
91
        // 3. anchor-only (i.e. url + #foo), which we already did (refOnly)
92
        boolean absoluteSpec = false;
93
 
94
        if (spec.length() >= 7) {
95
            absoluteSpec = spec.substring(0, 7).equalsIgnoreCase("jarjar:");
96
        }
97
        spec = spec.substring(start, limit);
98
 
99
        if (absoluteSpec) {
100
            file = parseAbsoluteSpec(spec);
101
        } else if (!refOnly) {
102
            file = parseContextSpec(url, spec);
103
 
104
            // Canonize the result after the bangslash
105
            int bangSlash = indexOfBangSlash(file);
106
            String toBangSlash = file.substring(0, bangSlash);
107
            String afterBangSlash = file.substring(bangSlash);
108
 
109
            file = toBangSlash + afterBangSlash;
110
        }
111
        file = file != null ? "jar:" + file.replaceFirst("\\" + JarJarURLConnection.SEPARATOR, "!/") : null;
112
        setURL(url, "jarjar", "", -1, file, ref);
113
    }
114
 
180 ilm 115
    @SuppressWarnings({ "UnusedAssignment", "UnusedDeclaration" })
17 ilm 116
    private String parseAbsoluteSpec(String spec) {
117
        @SuppressWarnings("unused")
118
        URL url = null;
119
        int index = -1;
120
 
121
        // check for !/
122
        if ((index = indexOfBangSlash(spec)) == -1) {
180 ilm 123
            throw new IllegalArgumentException("no " + JarJarURLConnection.SEPARATOR + " in spec: " + spec);
17 ilm 124
        }
125
        // test the inner URL
126
        try {
127
            String innerSpec = spec.substring(0, index - 1);
128
 
129
            url = new URL(innerSpec);
130
        } catch (MalformedURLException e) {
131
            throw new IllegalArgumentException("invalid url: " + spec + " (" + e + ")");
132
        }
133
        return spec;
134
    }
135
 
136
    private String parseContextSpec(URL url, String spec) {
137
        String ctxFile = url.getFile();
138
 
139
        // if the spec begins with /, chop up the jar back !/
140
        if (spec.startsWith("/")) {
141
            int bangSlash = indexOfBangSlash(ctxFile);
142
 
143
            if (bangSlash == -1) {
144
                throw new NullPointerException("malformed " + "context url:" + url + ": no " + JarJarURLConnection.SEPARATOR);
145
            }
146
            ctxFile = ctxFile.substring(0, bangSlash);
147
        }
148
        if (!ctxFile.endsWith("/") && (!spec.startsWith("/"))) {
149
            // chop up the last component
150
            int lastSlash = ctxFile.lastIndexOf('/');
151
 
152
            if (lastSlash == -1) {
153
                throw new NullPointerException("malformed " + "context url:" + url);
154
            }
155
            ctxFile = ctxFile.substring(0, lastSlash + 1);
156
        }
157
        return (ctxFile + spec);
158
    }
159
 
160
    /**
161
     * @author Pavel Savara
162
     */
163
    static public class JarJarURLConnection extends URLConnection {
164
        private URLConnection connection;
165
        public final static char SEPARATOR_CHAR = '^';
166
        public final static String SEPARATOR = SEPARATOR_CHAR + "/";
167
 
168
        public JarJarURLConnection(URL url) throws IOException {
169
            super(url);
170
            final String file = url.getFile();
171
            URL inner = new URL(file);
172
 
173
            connection = inner.openConnection();
174
        }
175
 
176
        public void connect() throws IOException {
177
            if (!connected) {
178
                connection.connect();
179
                connected = true;
180
            }
181
        }
182
 
183
        public InputStream getInputStream() throws IOException {
184
            connect();
185
            return connection.getInputStream();
186
        }
187
    }
188
}