Java URL Connection getContainerUrl(URL url, String resourceInThatDir)

Here you can find the source of getContainerUrl(URL url, String resourceInThatDir)

Description

get Container Url

License

Apache License

Declaration

public static URL getContainerUrl(URL url, String resourceInThatDir) 

Method Source Code

//package com.java2s;
/*//from www  .ja v  a 2 s . c o  m
 * Copyright 2015 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static URL getContainerUrl(URL url, String resourceInThatDir) {
        //Switching from manual parsing of jar: and file: URLs to java provided functionality.
        //The old code was breaking on any Windows path and instead of fixing it, using
        //the provided Java APIs seemed like the better option since they are already tested
        //on multiple platforms.
        boolean isJar = "jar".equals(url.getProtocol());
        if (isJar) {
            try {
                //let java handle the parsing of jar URL, no network connection is established.
                //Strips the jar protocol:
                //  jar:file:/<path to jar>!<resourceInThatDir>
                //  becomes
                //  file:/<path to jar>
                JarURLConnection connection = (JarURLConnection) url
                        .openConnection();
                url = connection.getJarFileURL();
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        } else {
            //Remove the trailing resouceInThatDir path from the URL, thus getting the parent folder.
            String path = url.toString();
            int i = path.indexOf(resourceInThatDir);
            if (i == -1)
                throw new IllegalStateException("Resource path ("
                        + resourceInThatDir + ") not in url substring ("
                        + url + ")");
            String parent = path.substring(0, i);
            try {
                url = new URL(parent);
            } catch (MalformedURLException e) {
                throw new IllegalStateException("Resource ("
                        + resourceInThatDir
                        + ") found at invalid URL parent (" + parent + ")",
                        e);
            }
        }
        return url;
    }
}

Related

  1. getCharset(URLConnection connection)
  2. getCharsFromURL(URL url)
  3. getChildren(URL url)
  4. getConnection(URL url)
  5. getConnectionResponseHeaders(URLConnection c)
  6. getCookies(URLConnection conn, Map> store)
  7. getDataFromServer(URL url)
  8. getDefaultUrlConnection(URL url)
  9. getFeedReader(URL feedUrl)