/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * 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.
 */

package org.springframework.core.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
 * Resource implementation for java.net.URL locators.
 * Obviously supports resolution as URL, and also as File
 * in case of the "file:" protocol.
 *
 * @author Juergen Hoeller
 * @since 28.12.2003
 * @see java.net.URL
 */
public class UrlResource extends AbstractResource {

	/**
	 * Original URL, used for actual access.
	 */
	private final URL url;

	/**
	 * Cleaned URL (with normalized path), used for comparisons.
	 */
	private final URL cleanedUrl;


	/**
	 * Create a new UrlResource.
	 * @param url a URL
	 */
	public UrlResource(URL url) {
		Assert.notNull(url, "url is required");
		this.url = url;
		this.cleanedUrl = getCleanedUrl(this.url, url.toString());
	}

	/**
	 * Create a new UrlResource.
	 * @param path a URL path
	 */
	public UrlResource(String path) throws MalformedURLException {
		Assert.notNull(path, "path is required");
		this.url = new URL(path);
		this.cleanedUrl = getCleanedUrl(this.url, path);
	}

	/**
	 * Determine a cleaned URL for the given original URL.
	 * @param originalUrl the original URL
	 * @param originalPath the original URL path
	 * @return the cleaned URL
	 * @see org.springframework.util.StringUtils#cleanPath
	 */
	private URL getCleanedUrl(URL originalUrl, String originalPath) {
		try {
			return new URL(StringUtils.cleanPath(originalPath));
		}
		catch (MalformedURLException ex) {
			// Cleaned URL path cannot be converted to URL
			// -> take original URL.
			return originalUrl;
		}
	}


	/**
	 * This implementation opens an InputStream for the given URL.
	 * @see java.net.URL#openStream()
	 */
	public InputStream getInputStream() throws IOException {
		return this.url.openStream();
	}

	/**
	 * This implementation returns the underlying URL reference.
	 */
	public URL getURL() throws IOException {
		return this.url;
	}

	/**
	 * This implementation returns a File reference for the underlying URL,
	 * provided that it refers to a file in the file system.
	 * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
	 */
	public File getFile() throws IOException {
		return ResourceUtils.getFile(this.url, getDescription());
	}

	/**
	 * This implementation creates a UrlResource, applying the given path
	 * relative to the path of the underlying URL of this resource descriptor.
	 * @see java.net.URL#URL(java.net.URL, String)
	 */
	public Resource createRelative(String relativePath) throws MalformedURLException {
		if (relativePath.startsWith("/")) {
			relativePath = relativePath.substring(1);
		}
		return new UrlResource(new URL(this.url, relativePath));
	}

	/**
	 * This implementation returns the name of the file that this URL refers to.
	 * @see java.net.URL#getFile()
	 * @see java.io.File#getName()
	 */
	public String getFilename() {
		return new File(this.url.getFile()).getName();
	}

	/**
	 * This implementation returns a description that includes the URL.
	 */
	public String getDescription() {
		return "URL [" + this.url + "]";
	}


	/**
	 * This implementation compares the underlying URL references.
	 */
	public boolean equals(Object obj) {
		return (obj == this ||
		    (obj instanceof UrlResource && this.cleanedUrl.equals(((UrlResource) obj).cleanedUrl)));
	}

	/**
	 * This implementation returns the hash code of the underlying URL reference.
	 */
	public int hashCode() {
		return this.cleanedUrl.hashCode();
	}

}

@@ -792,16 +792,47 @@
 net.URL;
+%0Aimport java.net.URLConnection;
 %0A%0Aimport
@@ -2491,124 +2491,411 @@
 %09 * 
-@see java.net.URL#openStream()%0A%09 */%0A%09public InputStream getInputStream() throws IOException %7B%0A%09%09return this.url.open
+It sets the %22UseCaches%22 flag to %3Ccode%3Efalse%3C/code%3E,%0A%09 * mainly to avoid jar file locking on Windows.%0A%09 * @see java.net.URL#openConnection()%0A%09 * @see java.net.URLConnection#setUseCaches(boolean)%0A%09 * @see java.net.URLConnection#getInputStream()%0A%09 */%0A%09public InputStream getInputStream() throws IOException %7B%0A%09%09URLConnection con = this.url.openConnection();%0A%09%09con.setUseCaches(false);%0A%09%09return con.getInput
 Stre

set the "UseCaches" flag to "false" for every URLConnection, mainly to avoid jar file locking on Windows